]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewproperties.cpp
Fix spelling errors reported by crazy
[dolphin.git] / src / views / viewproperties.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Aaron J. Seigo <aseigo@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "viewproperties.h"
22
23 #include "additionalinfoaccessor.h"
24 #include "dolphin_directoryviewpropertysettings.h"
25 #include "dolphin_generalsettings.h"
26
27 #include <kcomponentdata.h>
28 #include <klocale.h>
29 #include <kstandarddirs.h>
30 #include <kurl.h>
31
32 #include <QDate>
33 #include <QFile>
34 #include <QFileInfo>
35
36 #include "settings/dolphinsettings.h"
37
38 ViewProperties::ViewProperties(const KUrl& url) :
39 m_changedProps(false),
40 m_autoSave(true),
41 m_node(0)
42 {
43 // We try and save it to the file .directory in the directory being viewed.
44 // If the directory is not writable by the user or the directory is not local,
45 // we store the properties information in a local file.
46 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
47 const bool useGlobalViewProps = settings->globalViewProps();
48 if (useGlobalViewProps) {
49 m_filePath = destinationDir("global");
50 } else if (url.isLocalFile()) {
51 m_filePath = url.toLocalFile();
52 const QFileInfo info(m_filePath);
53 if (!info.isWritable()) {
54 m_filePath = destinationDir("local") + m_filePath;
55 }
56 } else {
57 m_filePath = destinationDir("remote") + m_filePath;
58 }
59
60 const QString file = m_filePath + QDir::separator() + QLatin1String(".directory");
61 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
62
63 const bool useDefaultProps = !useGlobalViewProps &&
64 (!QFileInfo(file).exists() ||
65 (m_node->timestamp() < settings->viewPropsTimestamp()));
66 if (useDefaultProps) {
67 // If the .directory file does not exist or the timestamp is too old,
68 // use the values from the global .directory file instead, which acts
69 // as default view for new folders in this case.
70 settings->setGlobalViewProps(true);
71
72 ViewProperties defaultProps(url);
73 setDirProperties(defaultProps);
74
75 settings->setGlobalViewProps(false);
76 m_changedProps = false;
77 }
78 }
79
80 ViewProperties::~ViewProperties()
81 {
82 if (m_changedProps && m_autoSave) {
83 save();
84 }
85
86 delete m_node;
87 m_node = 0;
88 }
89
90 void ViewProperties::setViewMode(DolphinView::Mode mode)
91 {
92 if (m_node->viewMode() != mode) {
93 m_node->setViewMode(mode);
94 updateTimeStamp();
95 }
96 }
97
98 DolphinView::Mode ViewProperties::viewMode() const
99 {
100 return static_cast<DolphinView::Mode>(m_node->viewMode());
101 }
102
103 void ViewProperties::setShowPreview(bool show)
104 {
105 if (m_node->showPreview() != show) {
106 m_node->setShowPreview(show);
107 updateTimeStamp();
108 }
109 }
110
111 bool ViewProperties::showPreview() const
112 {
113 return m_node->showPreview();
114 }
115
116 void ViewProperties::setShowHiddenFiles(bool show)
117 {
118 if (m_node->showHiddenFiles() != show) {
119 m_node->setShowHiddenFiles(show);
120 updateTimeStamp();
121 }
122 }
123
124 void ViewProperties::setCategorizedSorting(bool categorized)
125 {
126 if (m_node->categorizedSorting() != categorized) {
127 m_node->setCategorizedSorting(categorized);
128 updateTimeStamp();
129 }
130 }
131
132 bool ViewProperties::categorizedSorting() const
133 {
134 return m_node->categorizedSorting();
135 }
136
137 bool ViewProperties::showHiddenFiles() const
138 {
139 return m_node->showHiddenFiles();
140 }
141
142 void ViewProperties::setSorting(DolphinView::Sorting sorting)
143 {
144 if (m_node->sorting() != sorting) {
145 m_node->setSorting(sorting);
146 updateTimeStamp();
147 }
148 }
149
150 DolphinView::Sorting ViewProperties::sorting() const
151 {
152 return static_cast<DolphinView::Sorting>(m_node->sorting());
153 }
154
155 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
156 {
157 if (m_node->sortOrder() != sortOrder) {
158 m_node->setSortOrder(sortOrder);
159 updateTimeStamp();
160 }
161 }
162
163 Qt::SortOrder ViewProperties::sortOrder() const
164 {
165 return static_cast<Qt::SortOrder>(m_node->sortOrder());
166 }
167
168 void ViewProperties::setSortFoldersFirst(bool foldersFirst)
169 {
170 if (m_node->sortFoldersFirst() != foldersFirst) {
171 m_node->setSortFoldersFirst(foldersFirst);
172 updateTimeStamp();
173 }
174 }
175
176 bool ViewProperties::sortFoldersFirst() const
177 {
178 return m_node->sortFoldersFirst();
179 }
180
181 void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList& list)
182 {
183 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
184
185 int infoMask = 0;
186 foreach (KFileItemDelegate::Information currentInfo, list) {
187 infoMask = infoMask | infoAccessor.bitValue(currentInfo);
188 }
189
190 const int encodedInfo = encodedAdditionalInfo(infoMask);
191 if (m_node->additionalInfo() != encodedInfo) {
192 m_node->setAdditionalInfo(encodedInfo);
193 updateTimeStamp();
194 }
195 }
196
197 KFileItemDelegate::InformationList ViewProperties::additionalInfo() const
198 {
199 KFileItemDelegate::InformationList usedInfos;
200
201 const int decodedInfo = decodedAdditionalInfo();
202
203 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
204 const KFileItemDelegate::InformationList infoKeys = infoAccessor.keys();
205
206 foreach (const KFileItemDelegate::Information info, infoKeys) {
207 if (decodedInfo & infoAccessor.bitValue(info)) {
208 usedInfos.append(info);
209 }
210 }
211
212 return usedInfos;
213 }
214
215
216 void ViewProperties::setDirProperties(const ViewProperties& props)
217 {
218 setViewMode(props.viewMode());
219 setShowPreview(props.showPreview());
220 setShowHiddenFiles(props.showHiddenFiles());
221 setCategorizedSorting(props.categorizedSorting());
222 setSorting(props.sorting());
223 setSortOrder(props.sortOrder());
224 setSortFoldersFirst(props.sortFoldersFirst());
225 setAdditionalInfo(props.additionalInfo());
226 }
227
228 void ViewProperties::setAutoSaveEnabled(bool autoSave)
229 {
230 m_autoSave = autoSave;
231 }
232
233 bool ViewProperties::isAutoSaveEnabled() const
234 {
235 return m_autoSave;
236 }
237
238 void ViewProperties::updateTimeStamp()
239 {
240 m_changedProps = true;
241 m_node->setTimestamp(QDateTime::currentDateTime());
242 }
243
244 void ViewProperties::save()
245 {
246 KStandardDirs::makeDir(m_filePath);
247 m_node->writeConfig();
248 m_changedProps = false;
249 }
250
251 KUrl ViewProperties::mirroredDirectory()
252 {
253 QString basePath = KGlobal::mainComponent().componentName();
254 basePath.append("/view_properties/");
255 return KUrl(KStandardDirs::locateLocal("data", basePath));
256 }
257
258 QString ViewProperties::destinationDir(const QString& subDir) const
259 {
260 QString basePath = KGlobal::mainComponent().componentName();
261 basePath.append("/view_properties/").append(subDir);
262 return KStandardDirs::locateLocal("data", basePath);
263 }
264
265 int ViewProperties::encodedAdditionalInfo(int info) const
266 {
267 int encodedInfo = m_node->additionalInfo();
268
269 switch (viewMode()) {
270 case DolphinView::DetailsView:
271 encodedInfo = (encodedInfo & 0xFFFF00) | info;
272 break;
273 case DolphinView::IconsView:
274 encodedInfo = (encodedInfo & 0xFF00FF) | (info << 8);
275 break;
276 case DolphinView::ColumnView:
277 encodedInfo = (encodedInfo & 0x00FFFF) | (info << 16);
278 break;
279 default: break;
280 }
281
282 return encodedInfo;
283 }
284
285 int ViewProperties::decodedAdditionalInfo() const
286 {
287 int decodedInfo = m_node->additionalInfo();
288
289 switch (viewMode()) {
290 case DolphinView::DetailsView:
291 decodedInfo = decodedInfo & 0xFF;
292 if (decodedInfo == 0) {
293 // A details view without any additional info makes no sense, hence
294 // provide at least a size-info and date-info as fallback
295 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
296 decodedInfo = infoAccessor.bitValue(KFileItemDelegate::Size) |
297 infoAccessor.bitValue(KFileItemDelegate::ModificationTime);
298 }
299 break;
300 case DolphinView::IconsView:
301 decodedInfo = (decodedInfo >> 8) & 0xFF;
302 break;
303 case DolphinView::ColumnView:
304 decodedInfo = (decodedInfo >> 16) & 0xFF;
305 break;
306 default: break;
307 }
308
309 return decodedInfo;
310 }