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::setGroupRole(const QByteArray
&role
)
260 if (m_node
->groupRole() != role
) {
261 m_node
->setGroupRole(role
);
266 QByteArray
ViewProperties::groupRole() const
268 return m_node
->groupRole().toLatin1();
271 void ViewProperties::setGroupOrder(Qt::SortOrder groupOrder
)
273 if (m_node
->groupOrder() != groupOrder
) {
274 m_node
->setGroupOrder(groupOrder
);
279 Qt::SortOrder
ViewProperties::groupOrder() const
281 return static_cast<Qt::SortOrder
>(m_node
->groupOrder());
284 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
286 if (m_node
->sortFoldersFirst() != foldersFirst
) {
287 m_node
->setSortFoldersFirst(foldersFirst
);
292 bool ViewProperties::sortFoldersFirst() const
294 return m_node
->sortFoldersFirst();
297 void ViewProperties::setSortHiddenLast(bool hiddenLast
)
299 if (m_node
->sortHiddenLast() != hiddenLast
) {
300 m_node
->setSortHiddenLast(hiddenLast
);
305 bool ViewProperties::sortHiddenLast() const
307 return m_node
->sortHiddenLast();
310 void ViewProperties::setVisibleRoles(const QList
<QByteArray
> &roles
)
312 if (roles
== visibleRoles()) {
316 // See ViewProperties::visibleRoles() for the storage format
317 // of the additional information.
319 // Remove the old values stored for the current view-mode
320 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
321 const QString prefix
= viewModePrefix();
322 QStringList newVisibleRoles
= oldVisibleRoles
;
323 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
324 if (newVisibleRoles
[i
].startsWith(prefix
)) {
325 newVisibleRoles
.removeAt(i
);
329 // Add the updated values for the current view-mode
330 newVisibleRoles
.reserve(roles
.count());
331 for (const QByteArray
&role
: roles
) {
332 newVisibleRoles
.append(prefix
+ role
);
335 if (oldVisibleRoles
!= newVisibleRoles
) {
336 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
) && !newVisibleRoles
.contains(CustomizedDetailsString
);
337 if (markCustomizedDetails
) {
338 // The additional information of the details-view has been modified. Set a marker,
339 // so that it is allowed to also show no additional information without doing the
340 // fallback to show the size and date per default.
341 newVisibleRoles
.append(CustomizedDetailsString
);
344 m_node
->setVisibleRoles(newVisibleRoles
);
349 QList
<QByteArray
> ViewProperties::visibleRoles() const
351 // The shown additional information is stored for each view-mode separately as
352 // string with the view-mode as prefix. Example:
354 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
356 // To get the representation as QList<QByteArray>, the current
357 // view-mode must be checked and the values of this mode added to the list.
359 // For the details-view a special case must be respected: Per default the size
360 // and date should be shown without creating a .directory file. Only if
361 // the user explicitly has modified the properties of the details view (marked
362 // by "CustomizedDetails"), also a details-view with no additional information
365 QList
<QByteArray
> roles
{"text"};
367 // Iterate through all stored keys and append all roles that match to
368 // the current view mode.
369 const QString prefix
= viewModePrefix();
370 const int prefixLength
= prefix
.length();
372 const QStringList visibleRoles
= m_node
->visibleRoles();
373 for (const QString
&visibleRole
: visibleRoles
) {
374 if (visibleRole
.startsWith(prefix
)) {
375 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
376 if (role
!= "text") {
382 // For the details view the size and date should be shown per default
383 // until the additional information has been explicitly changed by the user
384 const bool useDefaultValues
= roles
.count() == 1 // "text"
385 && (m_node
->viewMode() == DolphinView::DetailsView
) && !visibleRoles
.contains(CustomizedDetailsString
);
386 if (useDefaultValues
) {
387 roles
.append("size");
388 roles
.append("modificationtime");
394 void ViewProperties::setHeaderColumnWidths(const QList
<int> &widths
)
396 if (m_node
->headerColumnWidths() != widths
) {
397 m_node
->setHeaderColumnWidths(widths
);
402 QList
<int> ViewProperties::headerColumnWidths() const
404 return m_node
->headerColumnWidths();
407 void ViewProperties::setDirProperties(const ViewProperties
&props
)
409 setViewMode(props
.viewMode());
410 setPreviewsShown(props
.previewsShown());
411 setHiddenFilesShown(props
.hiddenFilesShown());
412 setGroupedSorting(props
.groupedSorting());
413 setSortRole(props
.sortRole());
414 setSortOrder(props
.sortOrder());
415 setGroupRole(props
.groupRole());
416 setGroupOrder(props
.groupOrder());
417 setSortFoldersFirst(props
.sortFoldersFirst());
418 setSortHiddenLast(props
.sortHiddenLast());
419 setVisibleRoles(props
.visibleRoles());
420 setHeaderColumnWidths(props
.headerColumnWidths());
421 m_node
->setVersion(props
.m_node
->version());
424 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
426 m_autoSave
= autoSave
;
429 bool ViewProperties::isAutoSaveEnabled() const
434 void ViewProperties::update()
436 m_changedProps
= true;
437 m_node
->setTimestamp(QDateTime::currentDateTime());
440 void ViewProperties::save()
442 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
444 dir
.mkpath(m_filePath
);
445 m_node
->setVersion(CurrentViewPropertiesVersion
);
447 m_changedProps
= false;
450 bool ViewProperties::exist() const
452 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
453 return QFile::exists(file
);
456 QString
ViewProperties::destinationDir(const QString
&subDir
) const
458 QString path
= QStandardPaths::writableLocation(QStandardPaths::AppDataLocation
);
459 path
.append("/view_properties/").append(subDir
);
463 QString
ViewProperties::viewModePrefix() const
467 switch (m_node
->viewMode()) {
468 case DolphinView::IconsView
:
469 prefix
= QStringLiteral("Icons_");
471 case DolphinView::CompactView
:
472 prefix
= QStringLiteral("Compact_");
474 case DolphinView::DetailsView
:
475 prefix
= QStringLiteral("Details_");
478 qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
484 void ViewProperties::convertAdditionalInfo()
486 QStringList visibleRoles
= m_node
->visibleRoles();
488 const QStringList additionalInfo
= m_node
->additionalInfo();
489 if (!additionalInfo
.isEmpty()) {
490 // Convert the obsolete values like Icons_Size, Details_Date, ...
491 // to Icons_size, Details_date, ... where the suffix just represents
492 // the internal role. One special-case must be handled: "LinkDestination"
493 // has been used for "destination".
494 visibleRoles
.reserve(visibleRoles
.count() + additionalInfo
.count());
495 for (const QString
&info
: additionalInfo
) {
496 QString visibleRole
= info
;
497 int index
= visibleRole
.indexOf('_');
498 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
500 if (visibleRole
[index
] == QLatin1Char('L')) {
501 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
503 visibleRole
[index
] = visibleRole
[index
].toLower();
506 if (!visibleRoles
.contains(visibleRole
)) {
507 visibleRoles
.append(visibleRole
);
512 m_node
->setAdditionalInfo(QStringList());
513 m_node
->setVisibleRoles(visibleRoles
);
514 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
518 void ViewProperties::convertNameRoleToTextRole()
520 QStringList visibleRoles
= m_node
->visibleRoles();
521 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
522 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
523 const int leftLength
= visibleRoles
[i
].length() - 5;
524 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
528 QString sortRole
= m_node
->sortRole();
529 if (sortRole
== QLatin1String("name")) {
530 sortRole
= QStringLiteral("text");
533 m_node
->setVisibleRoles(visibleRoles
);
534 m_node
->setSortRole(sortRole
);
535 m_node
->setVersion(NameRolePropertiesVersion
);
539 void ViewProperties::convertDateRoleToModificationTimeRole()
541 QStringList visibleRoles
= m_node
->visibleRoles();
542 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
543 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
544 const int leftLength
= visibleRoles
[i
].length() - 5;
545 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
549 QString sortRole
= m_node
->sortRole();
550 if (sortRole
== QLatin1String("date")) {
551 sortRole
= QStringLiteral("modificationtime");
554 m_node
->setVisibleRoles(visibleRoles
);
555 m_node
->setSortRole(sortRole
);
556 m_node
->setVersion(DateRolePropertiesVersion
);
560 bool ViewProperties::isPartOfHome(const QString
&filePath
)
562 // For performance reasons cache the path in a static QString
563 // (see QDir::homePath() for more details)
564 static QString homePath
;
565 if (homePath
.isEmpty()) {
566 homePath
= QDir::homePath();
567 Q_ASSERT(!homePath
.isEmpty());
570 return filePath
.startsWith(homePath
);
573 QString
ViewProperties::directoryHashForUrl(const QUrl
&url
)
575 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
576 QString hashString
= hashValue
.toBase64();
577 hashString
.replace('/', '-');