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 (useGlobalViewProps
) {
50 m_filePath
= destinationDir(QStringLiteral("global"));
51 } else if (url
.scheme().contains(QLatin1String("search"))) {
52 m_filePath
= destinationDir(QStringLiteral("search/")) + directoryHashForUrl(url
);
54 } else if (url
.scheme() == QLatin1String("trash")) {
55 m_filePath
= destinationDir(QStringLiteral("trash"));
57 } else if (url
.scheme() == QLatin1String("recentdocuments")) {
58 m_filePath
= destinationDir(QStringLiteral("recentdocuments"));
59 useRecentDocumentsView
= true;
60 } else if (url
.scheme() == QLatin1String("recentlyused")) {
61 m_filePath
= destinationDir(QStringLiteral("recentlyused"));
62 useRecentDocumentsView
= true;
63 } else if (url
.scheme() == QLatin1String("timeline")) {
64 m_filePath
= destinationDir(QStringLiteral("timeline"));
65 useRecentDocumentsView
= true;
66 } else if (url
.isLocalFile()) {
67 m_filePath
= url
.toLocalFile();
69 bool useDestinationDir
= !isPartOfHome(m_filePath
);
70 if (!useDestinationDir
) {
71 const KFileItem
fileItem(url
);
72 useDestinationDir
= fileItem
.isSlow();
75 if (!useDestinationDir
) {
76 const QFileInfo
dirInfo(m_filePath
);
77 const QFileInfo
fileInfo(m_filePath
+ QDir::separator() + ViewPropertiesFileName
);
78 useDestinationDir
= !dirInfo
.isWritable() || (dirInfo
.size() > 0 && fileInfo
.exists() && !(fileInfo
.isReadable() && fileInfo
.isWritable()));
81 if (useDestinationDir
) {
83 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
84 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
86 m_filePath
= destinationDir(QStringLiteral("local")) + m_filePath
;
89 if (m_filePath
== QStandardPaths::writableLocation(QStandardPaths::DownloadLocation
)) {
90 useDownloadsView
= true;
93 m_filePath
= destinationDir(QStringLiteral("remote")) + m_filePath
;
96 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
97 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
99 // If the .directory file does not exist or the timestamp is too old,
100 // use default values instead.
101 const bool useDefaultProps
= (!useGlobalViewProps
|| useSearchView
|| useTrashView
|| useRecentDocumentsView
|| useDownloadsView
)
102 && (!QFile::exists(file
) || (m_node
->timestamp() < settings
->viewPropsTimestamp()));
103 if (useDefaultProps
) {
105 const QString path
= url
.path();
107 if (path
== QLatin1String("/images")) {
108 setViewMode(DolphinView::IconsView
);
109 setPreviewsShown(true);
110 setVisibleRoles({"text", "dimensions", "imageDateTime"});
111 } else if (path
== QLatin1String("/audio")) {
112 setViewMode(DolphinView::DetailsView
);
113 setVisibleRoles({"text", "artist", "album", "duration"});
114 } else if (path
== QLatin1String("/videos")) {
115 setViewMode(DolphinView::IconsView
);
116 setPreviewsShown(true);
117 setVisibleRoles({"text"});
119 setViewMode(DolphinView::DetailsView
);
120 setVisibleRoles({"text", "path", "modificationtime"});
122 } else if (useTrashView
) {
123 setViewMode(DolphinView::DetailsView
);
124 setVisibleRoles({"text", "path", "deletiontime"});
125 } else if (useRecentDocumentsView
|| useDownloadsView
) {
126 setSortRole(QByteArrayLiteral("modificationtime"));
127 setSortOrder(Qt::DescendingOrder
);
128 setSortFoldersFirst(false);
129 setGroupedSorting(true);
131 if (useRecentDocumentsView
) {
132 setViewMode(DolphinView::DetailsView
);
133 setVisibleRoles({"text", "path", "modificationtime"});
136 // The global view-properties act as default for directories without
137 // any view-property configuration. Constructing a ViewProperties
138 // instance for an empty QUrl ensures that the global view-properties
141 ViewProperties
defaultProps(emptyUrl
);
142 setDirProperties(defaultProps
);
144 m_changedProps
= false;
148 if (m_node
->version() < CurrentViewPropertiesVersion
) {
149 // The view-properties have an outdated version. Convert the properties
150 // to the changes of the current version.
151 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
152 convertAdditionalInfo();
153 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
156 if (m_node
->version() < NameRolePropertiesVersion
) {
157 convertNameRoleToTextRole();
158 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
161 if (m_node
->version() < DateRolePropertiesVersion
) {
162 convertDateRoleToModificationTimeRole();
163 Q_ASSERT(m_node
->version() == DateRolePropertiesVersion
);
166 m_node
->setVersion(CurrentViewPropertiesVersion
);
170 ViewProperties::~ViewProperties()
172 if (m_changedProps
&& m_autoSave
) {
180 void ViewProperties::setViewMode(DolphinView::Mode mode
)
182 if (m_node
->viewMode() != mode
) {
183 m_node
->setViewMode(mode
);
188 DolphinView::Mode
ViewProperties::viewMode() const
190 const int mode
= qBound(0, m_node
->viewMode(), 2);
191 return static_cast<DolphinView::Mode
>(mode
);
194 void ViewProperties::setPreviewsShown(bool show
)
196 if (m_node
->previewsShown() != show
) {
197 m_node
->setPreviewsShown(show
);
202 bool ViewProperties::previewsShown() const
204 return m_node
->previewsShown();
207 void ViewProperties::setHiddenFilesShown(bool show
)
209 if (m_node
->hiddenFilesShown() != show
) {
210 m_node
->setHiddenFilesShown(show
);
215 void ViewProperties::setGroupedSorting(bool grouped
)
217 if (m_node
->groupedSorting() != grouped
) {
218 m_node
->setGroupedSorting(grouped
);
223 bool ViewProperties::groupedSorting() const
225 return m_node
->groupedSorting();
228 bool ViewProperties::hiddenFilesShown() const
230 return m_node
->hiddenFilesShown();
233 void ViewProperties::setSortRole(const QByteArray
&role
)
235 if (m_node
->sortRole() != role
) {
236 m_node
->setSortRole(role
);
241 QByteArray
ViewProperties::sortRole() const
243 return m_node
->sortRole().toLatin1();
246 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
248 if (m_node
->sortOrder() != sortOrder
) {
249 m_node
->setSortOrder(sortOrder
);
254 Qt::SortOrder
ViewProperties::sortOrder() const
256 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
259 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
261 if (m_node
->sortFoldersFirst() != foldersFirst
) {
262 m_node
->setSortFoldersFirst(foldersFirst
);
267 bool ViewProperties::sortFoldersFirst() const
269 return m_node
->sortFoldersFirst();
272 void ViewProperties::setSortHiddenLast(bool hiddenLast
)
274 if (m_node
->sortHiddenLast() != hiddenLast
) {
275 m_node
->setSortHiddenLast(hiddenLast
);
280 bool ViewProperties::sortHiddenLast() const
282 return m_node
->sortHiddenLast();
285 void ViewProperties::setVisibleRoles(const QList
<QByteArray
> &roles
)
287 if (roles
== visibleRoles()) {
291 // See ViewProperties::visibleRoles() for the storage format
292 // of the additional information.
294 // Remove the old values stored for the current view-mode
295 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
296 const QString prefix
= viewModePrefix();
297 QStringList newVisibleRoles
= oldVisibleRoles
;
298 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
299 if (newVisibleRoles
[i
].startsWith(prefix
)) {
300 newVisibleRoles
.removeAt(i
);
304 // Add the updated values for the current view-mode
305 newVisibleRoles
.reserve(roles
.count());
306 for (const QByteArray
&role
: roles
) {
307 newVisibleRoles
.append(prefix
+ role
);
310 if (oldVisibleRoles
!= newVisibleRoles
) {
311 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
) && !newVisibleRoles
.contains(CustomizedDetailsString
);
312 if (markCustomizedDetails
) {
313 // The additional information of the details-view has been modified. Set a marker,
314 // so that it is allowed to also show no additional information without doing the
315 // fallback to show the size and date per default.
316 newVisibleRoles
.append(CustomizedDetailsString
);
319 m_node
->setVisibleRoles(newVisibleRoles
);
324 QList
<QByteArray
> ViewProperties::visibleRoles() const
326 // The shown additional information is stored for each view-mode separately as
327 // string with the view-mode as prefix. Example:
329 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
331 // To get the representation as QList<QByteArray>, the current
332 // view-mode must be checked and the values of this mode added to the list.
334 // For the details-view a special case must be respected: Per default the size
335 // and date should be shown without creating a .directory file. Only if
336 // the user explicitly has modified the properties of the details view (marked
337 // by "CustomizedDetails"), also a details-view with no additional information
340 QList
<QByteArray
> roles
{"text"};
342 // Iterate through all stored keys and append all roles that match to
343 // the current view mode.
344 const QString prefix
= viewModePrefix();
345 const int prefixLength
= prefix
.length();
347 const QStringList visibleRoles
= m_node
->visibleRoles();
348 for (const QString
&visibleRole
: visibleRoles
) {
349 if (visibleRole
.startsWith(prefix
)) {
350 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
351 if (role
!= "text") {
357 // For the details view the size and date should be shown per default
358 // until the additional information has been explicitly changed by the user
359 const bool useDefaultValues
= roles
.count() == 1 // "text"
360 && (m_node
->viewMode() == DolphinView::DetailsView
) && !visibleRoles
.contains(CustomizedDetailsString
);
361 if (useDefaultValues
) {
362 roles
.append("size");
363 roles
.append("modificationtime");
369 void ViewProperties::setHeaderColumnWidths(const QList
<int> &widths
)
371 if (m_node
->headerColumnWidths() != widths
) {
372 m_node
->setHeaderColumnWidths(widths
);
377 QList
<int> ViewProperties::headerColumnWidths() const
379 return m_node
->headerColumnWidths();
382 void ViewProperties::setDirProperties(const ViewProperties
&props
)
384 setViewMode(props
.viewMode());
385 setPreviewsShown(props
.previewsShown());
386 setHiddenFilesShown(props
.hiddenFilesShown());
387 setGroupedSorting(props
.groupedSorting());
388 setSortRole(props
.sortRole());
389 setSortOrder(props
.sortOrder());
390 setSortFoldersFirst(props
.sortFoldersFirst());
391 setSortHiddenLast(props
.sortHiddenLast());
392 setVisibleRoles(props
.visibleRoles());
393 setHeaderColumnWidths(props
.headerColumnWidths());
394 m_node
->setVersion(props
.m_node
->version());
397 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
399 m_autoSave
= autoSave
;
402 bool ViewProperties::isAutoSaveEnabled() const
407 void ViewProperties::update()
409 m_changedProps
= true;
410 m_node
->setTimestamp(QDateTime::currentDateTime());
413 void ViewProperties::save()
415 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
417 dir
.mkpath(m_filePath
);
418 m_node
->setVersion(CurrentViewPropertiesVersion
);
420 m_changedProps
= false;
423 bool ViewProperties::exist() const
425 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
426 return QFile::exists(file
);
429 QString
ViewProperties::destinationDir(const QString
&subDir
) const
431 QString path
= QStandardPaths::writableLocation(QStandardPaths::AppDataLocation
);
432 path
.append("/view_properties/").append(subDir
);
436 QString
ViewProperties::viewModePrefix() const
440 switch (m_node
->viewMode()) {
441 case DolphinView::IconsView
:
442 prefix
= QStringLiteral("Icons_");
444 case DolphinView::CompactView
:
445 prefix
= QStringLiteral("Compact_");
447 case DolphinView::DetailsView
:
448 prefix
= QStringLiteral("Details_");
451 qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
457 void ViewProperties::convertAdditionalInfo()
459 QStringList visibleRoles
= m_node
->visibleRoles();
461 const QStringList additionalInfo
= m_node
->additionalInfo();
462 if (!additionalInfo
.isEmpty()) {
463 // Convert the obsolete values like Icons_Size, Details_Date, ...
464 // to Icons_size, Details_date, ... where the suffix just represents
465 // the internal role. One special-case must be handled: "LinkDestination"
466 // has been used for "destination".
467 visibleRoles
.reserve(visibleRoles
.count() + additionalInfo
.count());
468 for (const QString
&info
: additionalInfo
) {
469 QString visibleRole
= info
;
470 int index
= visibleRole
.indexOf('_');
471 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
473 if (visibleRole
[index
] == QLatin1Char('L')) {
474 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
476 visibleRole
[index
] = visibleRole
[index
].toLower();
479 if (!visibleRoles
.contains(visibleRole
)) {
480 visibleRoles
.append(visibleRole
);
485 m_node
->setAdditionalInfo(QStringList());
486 m_node
->setVisibleRoles(visibleRoles
);
487 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
491 void ViewProperties::convertNameRoleToTextRole()
493 QStringList visibleRoles
= m_node
->visibleRoles();
494 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
495 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
496 const int leftLength
= visibleRoles
[i
].length() - 5;
497 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
501 QString sortRole
= m_node
->sortRole();
502 if (sortRole
== QLatin1String("name")) {
503 sortRole
= QStringLiteral("text");
506 m_node
->setVisibleRoles(visibleRoles
);
507 m_node
->setSortRole(sortRole
);
508 m_node
->setVersion(NameRolePropertiesVersion
);
512 void ViewProperties::convertDateRoleToModificationTimeRole()
514 QStringList visibleRoles
= m_node
->visibleRoles();
515 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
516 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
517 const int leftLength
= visibleRoles
[i
].length() - 5;
518 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
522 QString sortRole
= m_node
->sortRole();
523 if (sortRole
== QLatin1String("date")) {
524 sortRole
= QStringLiteral("modificationtime");
527 m_node
->setVisibleRoles(visibleRoles
);
528 m_node
->setSortRole(sortRole
);
529 m_node
->setVersion(DateRolePropertiesVersion
);
533 bool ViewProperties::isPartOfHome(const QString
&filePath
)
535 // For performance reasons cache the path in a static QString
536 // (see QDir::homePath() for more details)
537 static QString homePath
;
538 if (homePath
.isEmpty()) {
539 homePath
= QDir::homePath();
540 Q_ASSERT(!homePath
.isEmpty());
543 return filePath
.startsWith(homePath
);
546 QString
ViewProperties::directoryHashForUrl(const QUrl
&url
)
548 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
549 QString hashString
= hashValue
.toBase64();
550 hashString
.replace('/', '-');