1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Aaron J. Seigo <aseigo@kde.org> *
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. *
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. *
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 ***************************************************************************/
21 #include "viewproperties.h"
23 #include "dolphin_directoryviewpropertysettings.h"
24 #include "dolphin_generalsettings.h"
25 #include "dolphindebug.h"
27 #include <QCryptographicHash>
32 const int AdditionalInfoViewPropertiesVersion
= 1;
33 const int NameRolePropertiesVersion
= 2;
34 const int DateRolePropertiesVersion
= 4;
35 const int CurrentViewPropertiesVersion
= 4;
37 // String representation to mark the additional properties of
38 // the details view as customized by the user. See
39 // ViewProperties::visibleRoles() for more information.
40 const char CustomizedDetailsString
[] = "CustomizedDetails";
42 // Filename that is used for storing the properties
43 const char ViewPropertiesFileName
[] = ".directory";
46 ViewProperties::ViewProperties(const QUrl
& url
) :
47 m_changedProps(false),
51 GeneralSettings
* settings
= GeneralSettings::self();
52 const bool useGlobalViewProps
= settings
->globalViewProps() || url
.isEmpty();
53 bool useDetailsViewWithPath
= false;
54 bool useRecentDocumentsView
= false;
55 bool useDownloadsView
= false;
57 // We try and save it to the file .directory in the directory being viewed.
58 // If the directory is not writable by the user or the directory is not local,
59 // we store the properties information in a local file.
60 if (useGlobalViewProps
) {
61 m_filePath
= destinationDir(QStringLiteral("global"));
62 } else if (url
.scheme().contains(QLatin1String("search"))) {
63 m_filePath
= destinationDir(QStringLiteral("search/")) + directoryHashForUrl(url
);
64 useDetailsViewWithPath
= true;
65 } else if (url
.scheme() == QLatin1String("trash")) {
66 m_filePath
= destinationDir(QStringLiteral("trash"));
67 useDetailsViewWithPath
= true;
68 } else if (url
.scheme() == QLatin1String("recentdocuments")) {
69 m_filePath
= destinationDir(QStringLiteral("recentdocuments"));
70 useRecentDocumentsView
= true;
71 } else if (url
.isLocalFile()) {
72 m_filePath
= url
.toLocalFile();
74 bool useDestinationDir
= !isPartOfHome(m_filePath
);
75 if (!useDestinationDir
) {
76 const KFileItem
fileItem(url
);
77 useDestinationDir
= fileItem
.isSlow();
80 if (!useDestinationDir
) {
81 const QFileInfo
dirInfo(m_filePath
);
82 const QFileInfo
fileInfo(m_filePath
+ QDir::separator() + ViewPropertiesFileName
);
83 useDestinationDir
= !dirInfo
.isWritable() || (dirInfo
.size() > 0 && fileInfo
.exists() && !(fileInfo
.isReadable() && fileInfo
.isWritable()));
86 if (useDestinationDir
) {
88 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
89 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
91 m_filePath
= destinationDir(QStringLiteral("local")) + m_filePath
;
94 if (m_filePath
== QStandardPaths::writableLocation(QStandardPaths::DownloadLocation
)) {
95 useDownloadsView
= true;
98 m_filePath
= destinationDir(QStringLiteral("remote")) + m_filePath
;
101 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
102 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
104 // If the .directory file does not exist or the timestamp is too old,
105 // use default values instead.
106 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
107 (!QFile::exists(file
) ||
108 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
109 if (useDefaultProps
) {
110 if (useDetailsViewWithPath
) {
111 setViewMode(DolphinView::DetailsView
);
112 setVisibleRoles({"path"});
113 } else if (useRecentDocumentsView
|| useDownloadsView
) {
114 setSortRole(QByteArrayLiteral("modificationtime"));
115 setSortOrder(Qt::DescendingOrder
);
117 if (useRecentDocumentsView
) {
118 setViewMode(DolphinView::DetailsView
);
119 setVisibleRoles({QByteArrayLiteral("path")});
120 } else if (useDownloadsView
) {
121 setSortFoldersFirst(false);
122 setGroupedSorting(true);
125 // The global view-properties act as default for directories without
126 // any view-property configuration. Constructing a ViewProperties
127 // instance for an empty QUrl ensures that the global view-properties
130 ViewProperties
defaultProps(emptyUrl
);
131 setDirProperties(defaultProps
);
133 m_changedProps
= false;
137 if (m_node
->version() < CurrentViewPropertiesVersion
) {
138 // The view-properties have an outdated version. Convert the properties
139 // to the changes of the current version.
140 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
141 convertAdditionalInfo();
142 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
145 if (m_node
->version() < NameRolePropertiesVersion
) {
146 convertNameRoleToTextRole();
147 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
150 if (m_node
->version() < DateRolePropertiesVersion
) {
151 convertDateRoleToModificationTimeRole();
152 Q_ASSERT(m_node
->version() == DateRolePropertiesVersion
);
155 m_node
->setVersion(CurrentViewPropertiesVersion
);
159 ViewProperties::~ViewProperties()
161 if (m_changedProps
&& m_autoSave
) {
169 void ViewProperties::setViewMode(DolphinView::Mode mode
)
171 if (m_node
->viewMode() != mode
) {
172 m_node
->setViewMode(mode
);
177 DolphinView::Mode
ViewProperties::viewMode() const
179 const int mode
= qBound(0, m_node
->viewMode(), 2);
180 return static_cast<DolphinView::Mode
>(mode
);
183 void ViewProperties::setPreviewsShown(bool show
)
185 if (m_node
->previewsShown() != show
) {
186 m_node
->setPreviewsShown(show
);
191 bool ViewProperties::previewsShown() const
193 return m_node
->previewsShown();
196 void ViewProperties::setHiddenFilesShown(bool show
)
198 if (m_node
->hiddenFilesShown() != show
) {
199 m_node
->setHiddenFilesShown(show
);
204 void ViewProperties::setGroupedSorting(bool grouped
)
206 if (m_node
->groupedSorting() != grouped
) {
207 m_node
->setGroupedSorting(grouped
);
212 bool ViewProperties::groupedSorting() const
214 return m_node
->groupedSorting();
217 bool ViewProperties::hiddenFilesShown() const
219 return m_node
->hiddenFilesShown();
222 void ViewProperties::setSortRole(const QByteArray
& role
)
224 if (m_node
->sortRole() != role
) {
225 m_node
->setSortRole(role
);
230 QByteArray
ViewProperties::sortRole() const
232 return m_node
->sortRole().toLatin1();
235 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
237 if (m_node
->sortOrder() != sortOrder
) {
238 m_node
->setSortOrder(sortOrder
);
243 Qt::SortOrder
ViewProperties::sortOrder() const
245 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
248 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
250 if (m_node
->sortFoldersFirst() != foldersFirst
) {
251 m_node
->setSortFoldersFirst(foldersFirst
);
256 bool ViewProperties::sortFoldersFirst() const
258 return m_node
->sortFoldersFirst();
261 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
263 if (roles
== visibleRoles()) {
267 // See ViewProperties::visibleRoles() for the storage format
268 // of the additional information.
270 // Remove the old values stored for the current view-mode
271 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
272 const QString prefix
= viewModePrefix();
273 QStringList newVisibleRoles
= oldVisibleRoles
;
274 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
275 if (newVisibleRoles
[i
].startsWith(prefix
)) {
276 newVisibleRoles
.removeAt(i
);
280 // Add the updated values for the current view-mode
281 newVisibleRoles
.reserve(roles
.count());
282 foreach (const QByteArray
& role
, roles
) {
283 newVisibleRoles
.append(prefix
+ role
);
286 if (oldVisibleRoles
!= newVisibleRoles
) {
287 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
288 && !newVisibleRoles
.contains(CustomizedDetailsString
);
289 if (markCustomizedDetails
) {
290 // The additional information of the details-view has been modified. Set a marker,
291 // so that it is allowed to also show no additional information without doing the
292 // fallback to show the size and date per default.
293 newVisibleRoles
.append(CustomizedDetailsString
);
296 m_node
->setVisibleRoles(newVisibleRoles
);
301 QList
<QByteArray
> ViewProperties::visibleRoles() const
303 // The shown additional information is stored for each view-mode separately as
304 // string with the view-mode as prefix. Example:
306 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
308 // To get the representation as QList<QByteArray>, the current
309 // view-mode must be checked and the values of this mode added to the list.
311 // For the details-view a special case must be respected: Per default the size
312 // and date should be shown without creating a .directory file. Only if
313 // the user explicitly has modified the properties of the details view (marked
314 // by "CustomizedDetails"), also a details-view with no additional information
317 QList
<QByteArray
> roles
{"text"};
319 // Iterate through all stored keys and append all roles that match to
320 // the current view mode.
321 const QString prefix
= viewModePrefix();
322 const int prefixLength
= prefix
.length();
324 const QStringList visibleRoles
= m_node
->visibleRoles();
325 foreach (const QString
& visibleRole
, visibleRoles
) {
326 if (visibleRole
.startsWith(prefix
)) {
327 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
328 if (role
!= "text") {
334 // For the details view the size and date should be shown per default
335 // until the additional information has been explicitly changed by the user
336 const bool useDefaultValues
= roles
.count() == 1 // "text"
337 && (m_node
->viewMode() == DolphinView::DetailsView
)
338 && !visibleRoles
.contains(CustomizedDetailsString
);
339 if (useDefaultValues
) {
340 roles
.append("size");
341 roles
.append("modificationtime");
347 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
349 if (m_node
->headerColumnWidths() != widths
) {
350 m_node
->setHeaderColumnWidths(widths
);
355 QList
<int> ViewProperties::headerColumnWidths() const
357 return m_node
->headerColumnWidths();
360 void ViewProperties::setDirProperties(const ViewProperties
& props
)
362 setViewMode(props
.viewMode());
363 setPreviewsShown(props
.previewsShown());
364 setHiddenFilesShown(props
.hiddenFilesShown());
365 setGroupedSorting(props
.groupedSorting());
366 setSortRole(props
.sortRole());
367 setSortOrder(props
.sortOrder());
368 setSortFoldersFirst(props
.sortFoldersFirst());
369 setVisibleRoles(props
.visibleRoles());
370 setHeaderColumnWidths(props
.headerColumnWidths());
371 m_node
->setVersion(props
.m_node
->version());
374 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
376 m_autoSave
= autoSave
;
379 bool ViewProperties::isAutoSaveEnabled() const
384 void ViewProperties::update()
386 m_changedProps
= true;
387 m_node
->setTimestamp(QDateTime::currentDateTime());
390 void ViewProperties::save()
392 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
394 dir
.mkpath(m_filePath
);
395 m_node
->setVersion(CurrentViewPropertiesVersion
);
397 m_changedProps
= false;
400 bool ViewProperties::exist() const
402 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
403 return QFile::exists(file
);
406 QString
ViewProperties::destinationDir(const QString
& subDir
) const
408 QString path
= QStandardPaths::writableLocation(QStandardPaths::DataLocation
);
409 path
.append("/view_properties/").append(subDir
);
413 QString
ViewProperties::viewModePrefix() const
417 switch (m_node
->viewMode()) {
418 case DolphinView::IconsView
: prefix
= QStringLiteral("Icons_"); break;
419 case DolphinView::CompactView
: prefix
= QStringLiteral("Compact_"); break;
420 case DolphinView::DetailsView
: prefix
= QStringLiteral("Details_"); break;
421 default: qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
427 void ViewProperties::convertAdditionalInfo()
429 QStringList visibleRoles
;
431 const QStringList additionalInfo
= m_node
->additionalInfo();
432 if (!additionalInfo
.isEmpty()) {
433 // Convert the obsolete values like Icons_Size, Details_Date, ...
434 // to Icons_size, Details_date, ... where the suffix just represents
435 // the internal role. One special-case must be handled: "LinkDestination"
436 // has been used for "destination".
437 visibleRoles
.reserve(additionalInfo
.count());
438 foreach (const QString
& info
, additionalInfo
) {
439 QString visibleRole
= info
;
440 int index
= visibleRole
.indexOf('_');
441 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
443 if (visibleRole
[index
] == QLatin1Char('L')) {
444 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
446 visibleRole
[index
] = visibleRole
[index
].toLower();
449 visibleRoles
.append(visibleRole
);
453 m_node
->setAdditionalInfo(QStringList());
454 m_node
->setVisibleRoles(visibleRoles
);
455 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
459 void ViewProperties::convertNameRoleToTextRole()
461 QStringList visibleRoles
= m_node
->visibleRoles();
462 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
463 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
464 const int leftLength
= visibleRoles
[i
].length() - 5;
465 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
469 QString sortRole
= m_node
->sortRole();
470 if (sortRole
== QLatin1String("name")) {
471 sortRole
= QStringLiteral("text");
474 m_node
->setVisibleRoles(visibleRoles
);
475 m_node
->setSortRole(sortRole
);
476 m_node
->setVersion(NameRolePropertiesVersion
);
480 void ViewProperties::convertDateRoleToModificationTimeRole()
482 QStringList visibleRoles
= m_node
->visibleRoles();
483 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
484 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
485 const int leftLength
= visibleRoles
[i
].length() - 5;
486 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
490 QString sortRole
= m_node
->sortRole();
491 if (sortRole
== QLatin1String("date")) {
492 sortRole
= QStringLiteral("modificationtime");
495 m_node
->setVisibleRoles(visibleRoles
);
496 m_node
->setSortRole(sortRole
);
497 m_node
->setVersion(DateRolePropertiesVersion
);
501 bool ViewProperties::isPartOfHome(const QString
& filePath
)
503 // For performance reasons cache the path in a static QString
504 // (see QDir::homePath() for more details)
505 static QString homePath
;
506 if (homePath
.isEmpty()) {
507 homePath
= QDir::homePath();
508 Q_ASSERT(!homePath
.isEmpty());
511 return filePath
.startsWith(homePath
);
514 QString
ViewProperties::directoryHashForUrl(const QUrl
& url
)
516 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
517 QString hashString
= hashValue
.toBase64();
518 hashString
.replace('/', '-');