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