]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewproperties.cpp
It was a bad idea to store the enabled additional-information as bit-values: With...
[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 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
44 const bool useGlobalViewProps = settings->globalViewProps();
45
46 // We try and save it to the file .directory in the directory being viewed.
47 // If the directory is not writable by the user or the directory is not local,
48 // we store the properties information in a local file.
49 const bool isSearchUrl = url.protocol().contains("search");
50 if (isSearchUrl) {
51 m_filePath = destinationDir("search");
52 } else if (useGlobalViewProps) {
53 m_filePath = destinationDir("global");
54 } else if (url.isLocalFile()) {
55 m_filePath = url.toLocalFile();
56 const QFileInfo info(m_filePath);
57 if (!info.isWritable()) {
58 m_filePath = destinationDir("local") + m_filePath;
59 }
60 } else {
61 m_filePath = destinationDir("remote") + m_filePath;
62 }
63
64 const QString file = m_filePath + QDir::separator() + QLatin1String(".directory");
65 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
66
67 // If the .directory file does not exist or the timestamp is too old,
68 // use default values instead.
69 const bool useDefaultProps = (!useGlobalViewProps || isSearchUrl) &&
70 (!QFileInfo(file).exists() ||
71 (m_node->timestamp() < settings->viewPropsTimestamp()));
72 if (useDefaultProps) {
73 if (isSearchUrl) {
74 setViewMode(DolphinView::DetailsView);
75 setAdditionalInfo(KFileItemDelegate::InformationList() << KFileItemDelegate::LocalPathOrUrl);
76 } else {
77 // The global view-properties act as default for directories without
78 // any view-property configuration
79 settings->setGlobalViewProps(true);
80
81 ViewProperties defaultProps(url);
82 setDirProperties(defaultProps);
83
84 settings->setGlobalViewProps(false);
85 m_changedProps = false;
86 }
87 }
88 }
89
90 ViewProperties::~ViewProperties()
91 {
92 if (m_changedProps && m_autoSave) {
93 save();
94 }
95
96 delete m_node;
97 m_node = 0;
98 }
99
100 void ViewProperties::setViewMode(DolphinView::Mode mode)
101 {
102 if (m_node->viewMode() != mode) {
103 m_node->setViewMode(mode);
104 update();
105 }
106 }
107
108 DolphinView::Mode ViewProperties::viewMode() const
109 {
110 return static_cast<DolphinView::Mode>(m_node->viewMode());
111 }
112
113 void ViewProperties::setShowPreview(bool show)
114 {
115 if (m_node->showPreview() != show) {
116 m_node->setShowPreview(show);
117 update();
118 }
119 }
120
121 bool ViewProperties::showPreview() const
122 {
123 return m_node->showPreview();
124 }
125
126 void ViewProperties::setShowHiddenFiles(bool show)
127 {
128 if (m_node->showHiddenFiles() != show) {
129 m_node->setShowHiddenFiles(show);
130 update();
131 }
132 }
133
134 void ViewProperties::setCategorizedSorting(bool categorized)
135 {
136 if (m_node->categorizedSorting() != categorized) {
137 m_node->setCategorizedSorting(categorized);
138 update();
139 }
140 }
141
142 bool ViewProperties::categorizedSorting() const
143 {
144 return m_node->categorizedSorting();
145 }
146
147 bool ViewProperties::showHiddenFiles() const
148 {
149 return m_node->showHiddenFiles();
150 }
151
152 void ViewProperties::setSorting(DolphinView::Sorting sorting)
153 {
154 if (m_node->sorting() != sorting) {
155 m_node->setSorting(sorting);
156 update();
157 }
158 }
159
160 DolphinView::Sorting ViewProperties::sorting() const
161 {
162 return static_cast<DolphinView::Sorting>(m_node->sorting());
163 }
164
165 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
166 {
167 if (m_node->sortOrder() != sortOrder) {
168 m_node->setSortOrder(sortOrder);
169 update();
170 }
171 }
172
173 Qt::SortOrder ViewProperties::sortOrder() const
174 {
175 return static_cast<Qt::SortOrder>(m_node->sortOrder());
176 }
177
178 void ViewProperties::setSortFoldersFirst(bool foldersFirst)
179 {
180 if (m_node->sortFoldersFirst() != foldersFirst) {
181 m_node->setSortFoldersFirst(foldersFirst);
182 update();
183 }
184 }
185
186 bool ViewProperties::sortFoldersFirst() const
187 {
188 return m_node->sortFoldersFirst();
189 }
190
191 void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList& list)
192 {
193 // See ViewProperties::additionalInfoV2() for the storage format
194 // of the additional information.
195
196 // Remove the old values stored for the current view-mode
197 const QStringList oldInfoStringList = m_node->additionalInfoV2();
198 const QString prefix = viewModePrefix();
199 QStringList newInfoStringList = oldInfoStringList;
200 for (int i = newInfoStringList.count() - 1; i >= 0; --i) {
201 if (newInfoStringList.at(i).startsWith(prefix)) {
202 newInfoStringList.removeAt(i);
203 }
204 }
205
206 // Add the updated values for the current view-mode
207 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
208 foreach (KFileItemDelegate::Information info, list) {
209 newInfoStringList.append(prefix + infoAccessor.value(info));
210 }
211
212 // Only update the information if it has been changed
213 bool changed = oldInfoStringList.count() != newInfoStringList.count();
214 if (!changed) {
215 foreach (const QString& oldInfoString, oldInfoStringList) {
216 if (!newInfoStringList.contains(oldInfoString)) {
217 changed = true;
218 break;
219 }
220 }
221 }
222
223 if (changed) {
224 if (m_node->version() < 2) {
225 m_node->setVersion(2);
226 }
227 m_node->setAdditionalInfoV2(newInfoStringList);
228 update();
229 }
230 }
231
232 KFileItemDelegate::InformationList ViewProperties::additionalInfo() const
233 {
234 KFileItemDelegate::InformationList usedInfos;
235
236 switch (m_node->version()) {
237 case 1: usedInfos = additionalInfoV1(); break;
238 case 2: usedInfos = additionalInfoV2(); break;
239 default: kWarning() << "Unknown version of the view properties";
240 }
241
242 return usedInfos;
243 }
244
245
246 void ViewProperties::setDirProperties(const ViewProperties& props)
247 {
248 setViewMode(props.viewMode());
249 setShowPreview(props.showPreview());
250 setShowHiddenFiles(props.showHiddenFiles());
251 setCategorizedSorting(props.categorizedSorting());
252 setSorting(props.sorting());
253 setSortOrder(props.sortOrder());
254 setSortFoldersFirst(props.sortFoldersFirst());
255 setAdditionalInfo(props.additionalInfo());
256 }
257
258 void ViewProperties::setAutoSaveEnabled(bool autoSave)
259 {
260 m_autoSave = autoSave;
261 }
262
263 bool ViewProperties::isAutoSaveEnabled() const
264 {
265 return m_autoSave;
266 }
267
268 void ViewProperties::update()
269 {
270 m_changedProps = true;
271 m_node->setTimestamp(QDateTime::currentDateTime());
272
273 // If the view-properties are stored in an older format, take
274 // care to update them to the current format.
275 switch (m_node->version()) {
276 case 1: {
277 const KFileItemDelegate::InformationList infoList = additionalInfoV1();
278 m_node->setVersion(2);
279 setAdditionalInfo(infoList);
280 break;
281 }
282 case 2:
283 // Current version. Nothing needs to get converted.
284 break;
285 default:
286 kWarning() << "Unknown version of the view properties";
287 }
288 }
289
290 void ViewProperties::save()
291 {
292 KStandardDirs::makeDir(m_filePath);
293 m_node->writeConfig();
294 m_changedProps = false;
295 }
296
297 KUrl ViewProperties::mirroredDirectory()
298 {
299 QString basePath = KGlobal::mainComponent().componentName();
300 basePath.append("/view_properties/");
301 return KUrl(KStandardDirs::locateLocal("data", basePath));
302 }
303
304 QString ViewProperties::destinationDir(const QString& subDir) const
305 {
306 QString basePath = KGlobal::mainComponent().componentName();
307 basePath.append("/view_properties/").append(subDir);
308 return KStandardDirs::locateLocal("data", basePath);
309 }
310
311 KFileItemDelegate::InformationList ViewProperties::additionalInfoV1() const
312 {
313 KFileItemDelegate::InformationList usedInfos;
314
315 int decodedInfo = m_node->additionalInfo();
316
317 switch (viewMode()) {
318 case DolphinView::DetailsView:
319 decodedInfo = decodedInfo & 0xFF;
320 if (decodedInfo == 0) {
321 // A details view without any additional info makes no sense, hence
322 // provide at least a size-info and date-info as fallback
323 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
324 decodedInfo = infoAccessor.bitValue(KFileItemDelegate::Size) |
325 infoAccessor.bitValue(KFileItemDelegate::ModificationTime);
326 }
327 break;
328 case DolphinView::IconsView:
329 decodedInfo = (decodedInfo >> 8) & 0xFF;
330 break;
331 case DolphinView::ColumnView:
332 decodedInfo = (decodedInfo >> 16) & 0xFF;
333 break;
334 default: break;
335 }
336
337 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
338 const KFileItemDelegate::InformationList infoKeys = infoAccessor.keys();
339
340 foreach (const KFileItemDelegate::Information info, infoKeys) {
341 if (decodedInfo & infoAccessor.bitValue(info)) {
342 usedInfos.append(info);
343 }
344 }
345
346 return usedInfos;
347 }
348
349 KFileItemDelegate::InformationList ViewProperties::additionalInfoV2() const
350 {
351 // The shown additional information is stored for each view-mode separately as
352 // string with the view-mode as prefix. Example:
353 //
354 // AdditionalInfoV2=Details_Size,Details_Date,Details_Owner,Icon_Size
355 //
356 // To get the representation as KFileItemDelegate::InformationList, the current
357 // view-mode must be checked and the values of this mode added to the list.
358
359 KFileItemDelegate::InformationList usedInfos;
360
361 // infoHash allows to get the mapped KFileItemDelegate::Information value
362 // for a stored string-value in a fast way
363 static QHash<QString, KFileItemDelegate::Information> infoHash;
364 if (infoHash.isEmpty()) {
365 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
366 const KFileItemDelegate::InformationList keys = infoAccessor.keys();
367 foreach (const KFileItemDelegate::Information key, keys) {
368 infoHash.insert(infoAccessor.value(key), key);
369 }
370 }
371
372 // Iterate through all stored keys stored as strings and map them to
373 // the corresponding KFileItemDelegate::Information values.
374 const QString prefix = viewModePrefix();
375 const int prefixLength = prefix.length();
376 const QStringList infoStringList = m_node->additionalInfoV2();
377 foreach (const QString& infoString, infoStringList) {
378 if (infoString.startsWith(prefix)) {
379 const QString key = infoString.right(infoString.length() - prefixLength);
380 Q_ASSERT(infoHash.contains(key));
381 usedInfos.append(infoHash.value(key));
382 }
383 }
384
385 return usedInfos;
386 }
387
388 QString ViewProperties::viewModePrefix() const
389 {
390 QString prefix;
391
392 switch (m_node->viewMode()) {
393 case DolphinView::DetailsView: prefix = "Details_"; break;
394 case DolphinView::IconsView: prefix = "Icons_"; break;
395 case DolphinView::ColumnView: prefix = "Column_"; break;
396 default: kWarning() << "Unknown view-mode of the view properties";
397 }
398
399 return prefix;
400 }