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>
30 const int AdditionalInfoViewPropertiesVersion
= 1;
31 const int NameRolePropertiesVersion
= 2;
32 const int DateRolePropertiesVersion
= 4;
33 const int CurrentViewPropertiesVersion
= 4;
35 // String representation to mark the additional properties of
36 // the details view as customized by the user. See
37 // ViewProperties::visibleRoles() for more information.
38 const char CustomizedDetailsString
[] = "CustomizedDetails";
40 // Filename that is used for storing the properties
41 const char ViewPropertiesFileName
[] = ".directory";
44 ViewProperties::ViewProperties(const QUrl
& url
) :
45 m_changedProps(false),
49 GeneralSettings
* settings
= GeneralSettings::self();
50 const bool useGlobalViewProps
= settings
->globalViewProps() || url
.isEmpty();
51 bool useDetailsViewWithPath
= false;
52 bool useRecentDocumentsView
= false;
53 bool useDownloadsView
= false;
55 // We try and save it to the file .directory in the directory being viewed.
56 // If the directory is not writable by the user or the directory is not local,
57 // we store the properties information in a local file.
58 if (useGlobalViewProps
) {
59 m_filePath
= destinationDir(QStringLiteral("global"));
60 } else if (url
.scheme().contains(QStringLiteral("search"))) {
61 m_filePath
= destinationDir(QStringLiteral("search/")) + directoryHashForUrl(url
);
62 useDetailsViewWithPath
= true;
63 } else if (url
.scheme() == QLatin1String("trash")) {
64 m_filePath
= destinationDir(QStringLiteral("trash"));
65 useDetailsViewWithPath
= true;
66 } else if (url
.scheme() == QLatin1String("recentdocuments")) {
67 m_filePath
= destinationDir(QStringLiteral("recentdocuments"));
68 useRecentDocumentsView
= true;
69 } else if (url
.isLocalFile()) {
70 m_filePath
= url
.toLocalFile();
72 bool useDestinationDir
= !isPartOfHome(m_filePath
);
73 if (!useDestinationDir
) {
74 const QFileInfo
dirInfo(m_filePath
);
75 const QFileInfo
fileInfo(m_filePath
+ QDir::separator() + ViewPropertiesFileName
);
76 useDestinationDir
= !dirInfo
.isWritable() || (dirInfo
.size() > 0 && fileInfo
.exists() && !(fileInfo
.isReadable() && fileInfo
.isWritable()));
79 if (useDestinationDir
) {
81 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
82 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
84 m_filePath
= destinationDir(QStringLiteral("local")) + m_filePath
;
87 if (m_filePath
== QStandardPaths::writableLocation(QStandardPaths::DownloadLocation
)) {
88 useDownloadsView
= true;
91 m_filePath
= destinationDir(QStringLiteral("remote")) + m_filePath
;
94 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
95 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
97 // If the .directory file does not exist or the timestamp is too old,
98 // use default values instead.
99 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
100 (!QFile::exists(file
) ||
101 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
102 if (useDefaultProps
) {
103 if (useDetailsViewWithPath
) {
104 setViewMode(DolphinView::DetailsView
);
105 setVisibleRoles({"path"});
106 } else if (useRecentDocumentsView
|| useDownloadsView
) {
107 setSortRole(QByteArrayLiteral("modificationtime"));
108 setSortOrder(Qt::DescendingOrder
);
110 if (useRecentDocumentsView
) {
111 setViewMode(DolphinView::DetailsView
);
112 setVisibleRoles({QByteArrayLiteral("path")});
113 } else if (useDownloadsView
) {
114 setSortFoldersFirst(false);
115 setGroupedSorting(true);
118 // The global view-properties act as default for directories without
119 // any view-property configuration. Constructing a ViewProperties
120 // instance for an empty QUrl ensures that the global view-properties
123 ViewProperties
defaultProps(emptyUrl
);
124 setDirProperties(defaultProps
);
126 m_changedProps
= false;
130 if (m_node
->version() < CurrentViewPropertiesVersion
) {
131 // The view-properties have an outdated version. Convert the properties
132 // to the changes of the current version.
133 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
134 convertAdditionalInfo();
135 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
138 if (m_node
->version() < NameRolePropertiesVersion
) {
139 convertNameRoleToTextRole();
140 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
143 if (m_node
->version() < DateRolePropertiesVersion
) {
144 convertDateRoleToModificationTimeRole();
145 Q_ASSERT(m_node
->version() == DateRolePropertiesVersion
);
148 m_node
->setVersion(CurrentViewPropertiesVersion
);
152 ViewProperties::~ViewProperties()
154 if (m_changedProps
&& m_autoSave
) {
162 void ViewProperties::setViewMode(DolphinView::Mode mode
)
164 if (m_node
->viewMode() != mode
) {
165 m_node
->setViewMode(mode
);
170 DolphinView::Mode
ViewProperties::viewMode() const
172 const int mode
= qBound(0, m_node
->viewMode(), 2);
173 return static_cast<DolphinView::Mode
>(mode
);
176 void ViewProperties::setPreviewsShown(bool show
)
178 if (m_node
->previewsShown() != show
) {
179 m_node
->setPreviewsShown(show
);
184 bool ViewProperties::previewsShown() const
186 return m_node
->previewsShown();
189 void ViewProperties::setHiddenFilesShown(bool show
)
191 if (m_node
->hiddenFilesShown() != show
) {
192 m_node
->setHiddenFilesShown(show
);
197 void ViewProperties::setGroupedSorting(bool grouped
)
199 if (m_node
->groupedSorting() != grouped
) {
200 m_node
->setGroupedSorting(grouped
);
205 bool ViewProperties::groupedSorting() const
207 return m_node
->groupedSorting();
210 bool ViewProperties::hiddenFilesShown() const
212 return m_node
->hiddenFilesShown();
215 void ViewProperties::setSortRole(const QByteArray
& role
)
217 if (m_node
->sortRole() != role
) {
218 m_node
->setSortRole(role
);
223 QByteArray
ViewProperties::sortRole() const
225 return m_node
->sortRole().toLatin1();
228 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
230 if (m_node
->sortOrder() != sortOrder
) {
231 m_node
->setSortOrder(sortOrder
);
236 Qt::SortOrder
ViewProperties::sortOrder() const
238 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
241 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
243 if (m_node
->sortFoldersFirst() != foldersFirst
) {
244 m_node
->setSortFoldersFirst(foldersFirst
);
249 bool ViewProperties::sortFoldersFirst() const
251 return m_node
->sortFoldersFirst();
254 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
256 if (roles
== visibleRoles()) {
260 // See ViewProperties::visibleRoles() for the storage format
261 // of the additional information.
263 // Remove the old values stored for the current view-mode
264 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
265 const QString prefix
= viewModePrefix();
266 QStringList newVisibleRoles
= oldVisibleRoles
;
267 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
268 if (newVisibleRoles
[i
].startsWith(prefix
)) {
269 newVisibleRoles
.removeAt(i
);
273 // Add the updated values for the current view-mode
274 newVisibleRoles
.reserve(roles
.count());
275 foreach (const QByteArray
& role
, roles
) {
276 newVisibleRoles
.append(prefix
+ role
);
279 if (oldVisibleRoles
!= newVisibleRoles
) {
280 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
281 && !newVisibleRoles
.contains(CustomizedDetailsString
);
282 if (markCustomizedDetails
) {
283 // The additional information of the details-view has been modified. Set a marker,
284 // so that it is allowed to also show no additional information without doing the
285 // fallback to show the size and date per default.
286 newVisibleRoles
.append(CustomizedDetailsString
);
289 m_node
->setVisibleRoles(newVisibleRoles
);
294 QList
<QByteArray
> ViewProperties::visibleRoles() const
296 // The shown additional information is stored for each view-mode separately as
297 // string with the view-mode as prefix. Example:
299 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
301 // To get the representation as QList<QByteArray>, the current
302 // view-mode must be checked and the values of this mode added to the list.
304 // For the details-view a special case must be respected: Per default the size
305 // and date should be shown without creating a .directory file. Only if
306 // the user explicitly has modified the properties of the details view (marked
307 // by "CustomizedDetails"), also a details-view with no additional information
310 QList
<QByteArray
> roles
{"text"};
312 // Iterate through all stored keys and append all roles that match to
313 // the current view mode.
314 const QString prefix
= viewModePrefix();
315 const int prefixLength
= prefix
.length();
317 const QStringList visibleRoles
= m_node
->visibleRoles();
318 foreach (const QString
& visibleRole
, visibleRoles
) {
319 if (visibleRole
.startsWith(prefix
)) {
320 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
321 if (role
!= "text") {
327 // For the details view the size and date should be shown per default
328 // until the additional information has been explicitly changed by the user
329 const bool useDefaultValues
= roles
.count() == 1 // "text"
330 && (m_node
->viewMode() == DolphinView::DetailsView
)
331 && !visibleRoles
.contains(CustomizedDetailsString
);
332 if (useDefaultValues
) {
333 roles
.append("size");
334 roles
.append("modificationtime");
340 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
342 if (m_node
->headerColumnWidths() != widths
) {
343 m_node
->setHeaderColumnWidths(widths
);
348 QList
<int> ViewProperties::headerColumnWidths() const
350 return m_node
->headerColumnWidths();
353 void ViewProperties::setDirProperties(const ViewProperties
& props
)
355 setViewMode(props
.viewMode());
356 setPreviewsShown(props
.previewsShown());
357 setHiddenFilesShown(props
.hiddenFilesShown());
358 setGroupedSorting(props
.groupedSorting());
359 setSortRole(props
.sortRole());
360 setSortOrder(props
.sortOrder());
361 setSortFoldersFirst(props
.sortFoldersFirst());
362 setVisibleRoles(props
.visibleRoles());
363 setHeaderColumnWidths(props
.headerColumnWidths());
364 m_node
->setVersion(props
.m_node
->version());
367 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
369 m_autoSave
= autoSave
;
372 bool ViewProperties::isAutoSaveEnabled() const
377 void ViewProperties::update()
379 m_changedProps
= true;
380 m_node
->setTimestamp(QDateTime::currentDateTime());
383 void ViewProperties::save()
385 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
387 dir
.mkpath(m_filePath
);
388 m_node
->setVersion(CurrentViewPropertiesVersion
);
390 m_changedProps
= false;
393 bool ViewProperties::exist() const
395 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
396 return QFile::exists(file
);
399 QString
ViewProperties::destinationDir(const QString
& subDir
) const
401 QString path
= QStandardPaths::writableLocation(QStandardPaths::DataLocation
);
402 path
.append("/view_properties/").append(subDir
);
406 QString
ViewProperties::viewModePrefix() const
410 switch (m_node
->viewMode()) {
411 case DolphinView::IconsView
: prefix
= QStringLiteral("Icons_"); break;
412 case DolphinView::CompactView
: prefix
= QStringLiteral("Compact_"); break;
413 case DolphinView::DetailsView
: prefix
= QStringLiteral("Details_"); break;
414 default: qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
420 void ViewProperties::convertAdditionalInfo()
422 QStringList visibleRoles
;
424 const QStringList additionalInfo
= m_node
->additionalInfo();
425 if (!additionalInfo
.isEmpty()) {
426 // Convert the obsolete values like Icons_Size, Details_Date, ...
427 // to Icons_size, Details_date, ... where the suffix just represents
428 // the internal role. One special-case must be handled: "LinkDestination"
429 // has been used for "destination".
430 visibleRoles
.reserve(additionalInfo
.count());
431 foreach (const QString
& info
, additionalInfo
) {
432 QString visibleRole
= info
;
433 int index
= visibleRole
.indexOf('_');
434 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
436 if (visibleRole
[index
] == QLatin1Char('L')) {
437 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
439 visibleRole
[index
] = visibleRole
[index
].toLower();
442 visibleRoles
.append(visibleRole
);
446 m_node
->setAdditionalInfo(QStringList());
447 m_node
->setVisibleRoles(visibleRoles
);
448 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
452 void ViewProperties::convertNameRoleToTextRole()
454 QStringList visibleRoles
= m_node
->visibleRoles();
455 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
456 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
457 const int leftLength
= visibleRoles
[i
].length() - 5;
458 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
462 QString sortRole
= m_node
->sortRole();
463 if (sortRole
== QLatin1String("name")) {
464 sortRole
= QStringLiteral("text");
467 m_node
->setVisibleRoles(visibleRoles
);
468 m_node
->setSortRole(sortRole
);
469 m_node
->setVersion(NameRolePropertiesVersion
);
473 void ViewProperties::convertDateRoleToModificationTimeRole()
475 QStringList visibleRoles
= m_node
->visibleRoles();
476 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
477 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
478 const int leftLength
= visibleRoles
[i
].length() - 5;
479 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
483 QString sortRole
= m_node
->sortRole();
484 if (sortRole
== QLatin1String("date")) {
485 sortRole
= QStringLiteral("modificationtime");
488 m_node
->setVisibleRoles(visibleRoles
);
489 m_node
->setSortRole(sortRole
);
490 m_node
->setVersion(DateRolePropertiesVersion
);
494 bool ViewProperties::isPartOfHome(const QString
& filePath
)
496 // For performance reasons cache the path in a static QString
497 // (see QDir::homePath() for more details)
498 static QString homePath
;
499 if (homePath
.isEmpty()) {
500 homePath
= QDir::homePath();
501 Q_ASSERT(!homePath
.isEmpty());
504 return filePath
.startsWith(homePath
);
507 QString
ViewProperties::directoryHashForUrl(const QUrl
& url
)
509 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
510 QString hashString
= hashValue
.toBase64();
511 hashString
.replace('/', '-');