2 * SPDX-FileCopyrightText: 2006-2010 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2006 Aaron J. Seigo <aseigo@kde.org>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "viewproperties.h"
10 #include "dolphin_directoryviewpropertysettings.h"
11 #include "dolphin_generalsettings.h"
12 #include "dolphindebug.h"
14 #include <QCryptographicHash>
20 const int AdditionalInfoViewPropertiesVersion
= 1;
21 const int NameRolePropertiesVersion
= 2;
22 const int DateRolePropertiesVersion
= 4;
23 const int CurrentViewPropertiesVersion
= 4;
25 // String representation to mark the additional properties of
26 // the details view as customized by the user. See
27 // ViewProperties::visibleRoles() for more information.
28 const char CustomizedDetailsString
[] = "CustomizedDetails";
30 // Filename that is used for storing the properties
31 const char ViewPropertiesFileName
[] = ".directory";
34 ViewProperties::ViewProperties(const QUrl
&url
)
35 : m_changedProps(false)
39 GeneralSettings
*settings
= GeneralSettings::self();
40 const bool useGlobalViewProps
= settings
->globalViewProps() || url
.isEmpty();
41 bool useSearchView
= false;
42 bool useTrashView
= false;
43 bool useRecentDocumentsView
= false;
44 bool useDownloadsView
= false;
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 if (url
.scheme().contains(QLatin1String("search"))) {
50 m_filePath
= destinationDir(QStringLiteral("search/")) + directoryHashForUrl(url
);
52 } else if (url
.scheme() == QLatin1String("trash")) {
53 m_filePath
= destinationDir(QStringLiteral("trash"));
55 } else if (url
.scheme() == QLatin1String("recentlyused")) {
56 m_filePath
= destinationDir(QStringLiteral("recentlyused"));
57 useRecentDocumentsView
= true;
58 } else if (url
.scheme() == QLatin1String("timeline")) {
59 m_filePath
= destinationDir(QStringLiteral("timeline"));
60 useRecentDocumentsView
= true;
61 } else if (useGlobalViewProps
) {
62 m_filePath
= destinationDir(QStringLiteral("global"));
63 } else if (url
.isLocalFile()) {
64 m_filePath
= url
.toLocalFile();
66 bool useDestinationDir
= !isPartOfHome(m_filePath
);
67 if (!useDestinationDir
) {
68 const KFileItem
fileItem(url
);
69 useDestinationDir
= fileItem
.isSlow();
72 if (!useDestinationDir
) {
73 const QFileInfo
dirInfo(m_filePath
);
74 const QFileInfo
fileInfo(m_filePath
+ QDir::separator() + ViewPropertiesFileName
);
75 useDestinationDir
= !dirInfo
.isWritable() || (dirInfo
.size() > 0 && fileInfo
.exists() && !(fileInfo
.isReadable() && fileInfo
.isWritable()));
78 if (useDestinationDir
) {
80 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
81 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
83 m_filePath
= destinationDir(QStringLiteral("local")) + m_filePath
;
86 if (m_filePath
== QStandardPaths::writableLocation(QStandardPaths::DownloadLocation
)) {
87 useDownloadsView
= true;
90 m_filePath
= destinationDir(QStringLiteral("remote")) + m_filePath
;
93 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
94 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
96 // If the .directory file does not exist or the timestamp is too old,
97 // use default values instead.
98 const bool useDefaultProps
= (!useGlobalViewProps
|| useSearchView
|| useTrashView
|| useRecentDocumentsView
|| useDownloadsView
)
99 && (!QFile::exists(file
) || (m_node
->timestamp() < settings
->viewPropsTimestamp()));
100 if (useDefaultProps
) {
102 const QString path
= url
.path();
104 if (path
== QLatin1String("/images")) {
105 setViewMode(DolphinView::IconsView
);
106 setPreviewsShown(true);
107 setVisibleRoles({"text", "dimensions", "imageDateTime"});
108 } else if (path
== QLatin1String("/audio")) {
109 setViewMode(DolphinView::DetailsView
);
110 setVisibleRoles({"text", "artist", "album", "duration"});
111 } else if (path
== QLatin1String("/videos")) {
112 setViewMode(DolphinView::IconsView
);
113 setPreviewsShown(true);
114 setVisibleRoles({"text"});
116 setViewMode(DolphinView::DetailsView
);
117 setVisibleRoles({"text", "path", "modificationtime"});
119 } else if (useTrashView
) {
120 setViewMode(DolphinView::DetailsView
);
121 setVisibleRoles({"text", "path", "deletiontime"});
122 } else if (useRecentDocumentsView
|| useDownloadsView
) {
123 setSortOrder(Qt::DescendingOrder
);
124 setSortFoldersFirst(false);
125 setGroupedSorting(true);
127 if (useRecentDocumentsView
) {
128 setSortRole(QByteArrayLiteral("accesstime"));
129 setViewMode(DolphinView::DetailsView
);
130 setVisibleRoles({"text", "path", "accesstime"});
132 setSortRole(QByteArrayLiteral("modificationtime"));
135 // The global view-properties act as default for directories without
136 // any view-property configuration. Constructing a ViewProperties
137 // instance for an empty QUrl ensures that the global view-properties
140 ViewProperties
defaultProps(emptyUrl
);
141 setDirProperties(defaultProps
);
143 m_changedProps
= false;
147 if (m_node
->version() < CurrentViewPropertiesVersion
) {
148 // The view-properties have an outdated version. Convert the properties
149 // to the changes of the current version.
150 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
151 convertAdditionalInfo();
152 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
155 if (m_node
->version() < NameRolePropertiesVersion
) {
156 convertNameRoleToTextRole();
157 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
160 if (m_node
->version() < DateRolePropertiesVersion
) {
161 convertDateRoleToModificationTimeRole();
162 Q_ASSERT(m_node
->version() == DateRolePropertiesVersion
);
165 m_node
->setVersion(CurrentViewPropertiesVersion
);
169 ViewProperties::~ViewProperties()
171 if (m_changedProps
&& m_autoSave
) {
179 void ViewProperties::setViewMode(DolphinView::Mode mode
)
181 if (m_node
->viewMode() != mode
) {
182 m_node
->setViewMode(mode
);
187 DolphinView::Mode
ViewProperties::viewMode() const
189 const int mode
= qBound(0, m_node
->viewMode(), 2);
190 return static_cast<DolphinView::Mode
>(mode
);
193 void ViewProperties::setPreviewsShown(bool show
)
195 if (m_node
->previewsShown() != show
) {
196 m_node
->setPreviewsShown(show
);
201 bool ViewProperties::previewsShown() const
203 return m_node
->previewsShown();
206 void ViewProperties::setHiddenFilesShown(bool show
)
208 if (m_node
->hiddenFilesShown() != show
) {
209 m_node
->setHiddenFilesShown(show
);
214 void ViewProperties::setGroupedSorting(bool grouped
)
216 if (m_node
->groupedSorting() != grouped
) {
217 m_node
->setGroupedSorting(grouped
);
222 bool ViewProperties::groupedSorting() const
224 return m_node
->groupedSorting();
227 bool ViewProperties::hiddenFilesShown() const
229 return m_node
->hiddenFilesShown();
232 void ViewProperties::setSortRole(const QByteArray
&role
)
234 if (m_node
->sortRole() != role
) {
235 m_node
->setSortRole(role
);
240 QByteArray
ViewProperties::sortRole() const
242 return m_node
->sortRole().toLatin1();
245 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
247 if (m_node
->sortOrder() != sortOrder
) {
248 m_node
->setSortOrder(sortOrder
);
253 Qt::SortOrder
ViewProperties::sortOrder() const
255 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
258 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
260 if (m_node
->sortFoldersFirst() != foldersFirst
) {
261 m_node
->setSortFoldersFirst(foldersFirst
);
266 bool ViewProperties::sortFoldersFirst() const
268 return m_node
->sortFoldersFirst();
271 void ViewProperties::setSortHiddenLast(bool hiddenLast
)
273 if (m_node
->sortHiddenLast() != hiddenLast
) {
274 m_node
->setSortHiddenLast(hiddenLast
);
279 bool ViewProperties::sortHiddenLast() const
281 return m_node
->sortHiddenLast();
284 void ViewProperties::setVisibleRoles(const QList
<QByteArray
> &roles
)
286 if (roles
== visibleRoles()) {
290 // See ViewProperties::visibleRoles() for the storage format
291 // of the additional information.
293 // Remove the old values stored for the current view-mode
294 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
295 const QString prefix
= viewModePrefix();
296 QStringList newVisibleRoles
= oldVisibleRoles
;
297 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
298 if (newVisibleRoles
[i
].startsWith(prefix
)) {
299 newVisibleRoles
.removeAt(i
);
303 // Add the updated values for the current view-mode
304 newVisibleRoles
.reserve(roles
.count());
305 for (const QByteArray
&role
: roles
) {
306 newVisibleRoles
.append(prefix
+ role
);
309 if (oldVisibleRoles
!= newVisibleRoles
) {
310 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
) && !newVisibleRoles
.contains(CustomizedDetailsString
);
311 if (markCustomizedDetails
) {
312 // The additional information of the details-view has been modified. Set a marker,
313 // so that it is allowed to also show no additional information without doing the
314 // fallback to show the size and date per default.
315 newVisibleRoles
.append(CustomizedDetailsString
);
318 m_node
->setVisibleRoles(newVisibleRoles
);
323 QList
<QByteArray
> ViewProperties::visibleRoles() const
325 // The shown additional information is stored for each view-mode separately as
326 // string with the view-mode as prefix. Example:
328 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
330 // To get the representation as QList<QByteArray>, the current
331 // view-mode must be checked and the values of this mode added to the list.
333 // For the details-view a special case must be respected: Per default the size
334 // and date should be shown without creating a .directory file. Only if
335 // the user explicitly has modified the properties of the details view (marked
336 // by "CustomizedDetails"), also a details-view with no additional information
339 QList
<QByteArray
> roles
{"text"};
341 // Iterate through all stored keys and append all roles that match to
342 // the current view mode.
343 const QString prefix
= viewModePrefix();
344 const int prefixLength
= prefix
.length();
346 const QStringList visibleRoles
= m_node
->visibleRoles();
347 for (const QString
&visibleRole
: visibleRoles
) {
348 if (visibleRole
.startsWith(prefix
)) {
349 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
350 if (role
!= "text") {
356 // For the details view the size and date should be shown per default
357 // until the additional information has been explicitly changed by the user
358 const bool useDefaultValues
= roles
.count() == 1 // "text"
359 && (m_node
->viewMode() == DolphinView::DetailsView
) && !visibleRoles
.contains(CustomizedDetailsString
);
360 if (useDefaultValues
) {
361 roles
.append("size");
362 roles
.append("modificationtime");
368 void ViewProperties::setHeaderColumnWidths(const QList
<int> &widths
)
370 if (m_node
->headerColumnWidths() != widths
) {
371 m_node
->setHeaderColumnWidths(widths
);
376 QList
<int> ViewProperties::headerColumnWidths() const
378 return m_node
->headerColumnWidths();
381 void ViewProperties::setDirProperties(const ViewProperties
&props
)
383 setViewMode(props
.viewMode());
384 setPreviewsShown(props
.previewsShown());
385 setHiddenFilesShown(props
.hiddenFilesShown());
386 setGroupedSorting(props
.groupedSorting());
387 setSortRole(props
.sortRole());
388 setSortOrder(props
.sortOrder());
389 setSortFoldersFirst(props
.sortFoldersFirst());
390 setSortHiddenLast(props
.sortHiddenLast());
391 setVisibleRoles(props
.visibleRoles());
392 setHeaderColumnWidths(props
.headerColumnWidths());
393 m_node
->setVersion(props
.m_node
->version());
396 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
398 m_autoSave
= autoSave
;
401 bool ViewProperties::isAutoSaveEnabled() const
406 void ViewProperties::update()
408 m_changedProps
= true;
409 m_node
->setTimestamp(QDateTime::currentDateTime());
412 void ViewProperties::save()
414 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
416 dir
.mkpath(m_filePath
);
417 m_node
->setVersion(CurrentViewPropertiesVersion
);
419 m_changedProps
= false;
422 bool ViewProperties::exist() const
424 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
425 return QFile::exists(file
);
428 QString
ViewProperties::destinationDir(const QString
&subDir
) const
430 QString path
= QStandardPaths::writableLocation(QStandardPaths::AppDataLocation
);
431 path
.append("/view_properties/").append(subDir
);
435 QString
ViewProperties::viewModePrefix() const
439 switch (m_node
->viewMode()) {
440 case DolphinView::IconsView
:
441 prefix
= QStringLiteral("Icons_");
443 case DolphinView::CompactView
:
444 prefix
= QStringLiteral("Compact_");
446 case DolphinView::DetailsView
:
447 prefix
= QStringLiteral("Details_");
450 qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
456 void ViewProperties::convertAdditionalInfo()
458 QStringList visibleRoles
= m_node
->visibleRoles();
460 const QStringList additionalInfo
= m_node
->additionalInfo();
461 if (!additionalInfo
.isEmpty()) {
462 // Convert the obsolete values like Icons_Size, Details_Date, ...
463 // to Icons_size, Details_date, ... where the suffix just represents
464 // the internal role. One special-case must be handled: "LinkDestination"
465 // has been used for "destination".
466 visibleRoles
.reserve(visibleRoles
.count() + additionalInfo
.count());
467 for (const QString
&info
: additionalInfo
) {
468 QString visibleRole
= info
;
469 int index
= visibleRole
.indexOf('_');
470 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
472 if (visibleRole
[index
] == QLatin1Char('L')) {
473 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
475 visibleRole
[index
] = visibleRole
[index
].toLower();
478 if (!visibleRoles
.contains(visibleRole
)) {
479 visibleRoles
.append(visibleRole
);
484 m_node
->setAdditionalInfo(QStringList());
485 m_node
->setVisibleRoles(visibleRoles
);
486 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
490 void ViewProperties::convertNameRoleToTextRole()
492 QStringList visibleRoles
= m_node
->visibleRoles();
493 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
494 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
495 const int leftLength
= visibleRoles
[i
].length() - 5;
496 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
500 QString sortRole
= m_node
->sortRole();
501 if (sortRole
== QLatin1String("name")) {
502 sortRole
= QStringLiteral("text");
505 m_node
->setVisibleRoles(visibleRoles
);
506 m_node
->setSortRole(sortRole
);
507 m_node
->setVersion(NameRolePropertiesVersion
);
511 void ViewProperties::convertDateRoleToModificationTimeRole()
513 QStringList visibleRoles
= m_node
->visibleRoles();
514 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
515 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
516 const int leftLength
= visibleRoles
[i
].length() - 5;
517 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
521 QString sortRole
= m_node
->sortRole();
522 if (sortRole
== QLatin1String("date")) {
523 sortRole
= QStringLiteral("modificationtime");
526 m_node
->setVisibleRoles(visibleRoles
);
527 m_node
->setSortRole(sortRole
);
528 m_node
->setVersion(DateRolePropertiesVersion
);
532 bool ViewProperties::isPartOfHome(const QString
&filePath
)
534 // For performance reasons cache the path in a static QString
535 // (see QDir::homePath() for more details)
536 static QString homePath
;
537 if (homePath
.isEmpty()) {
538 homePath
= QDir::homePath();
539 Q_ASSERT(!homePath
.isEmpty());
542 return filePath
.startsWith(homePath
);
545 QString
ViewProperties::directoryHashForUrl(const QUrl
&url
)
547 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
548 QString hashString
= hashValue
.toBase64();
549 hashString
.replace('/', '-');