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 setSortRole(QByteArrayLiteral("modificationtime"));
124 setSortOrder(Qt::DescendingOrder
);
125 setSortFoldersFirst(false);
126 setGroupedSorting(true);
128 if (useRecentDocumentsView
) {
129 setViewMode(DolphinView::DetailsView
);
130 setVisibleRoles({"text", "path", "modificationtime"});
133 // The global view-properties act as default for directories without
134 // any view-property configuration. Constructing a ViewProperties
135 // instance for an empty QUrl ensures that the global view-properties
138 ViewProperties
defaultProps(emptyUrl
);
139 setDirProperties(defaultProps
);
141 m_changedProps
= false;
145 if (m_node
->version() < CurrentViewPropertiesVersion
) {
146 // The view-properties have an outdated version. Convert the properties
147 // to the changes of the current version.
148 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
149 convertAdditionalInfo();
150 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
153 if (m_node
->version() < NameRolePropertiesVersion
) {
154 convertNameRoleToTextRole();
155 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
158 if (m_node
->version() < DateRolePropertiesVersion
) {
159 convertDateRoleToModificationTimeRole();
160 Q_ASSERT(m_node
->version() == DateRolePropertiesVersion
);
163 m_node
->setVersion(CurrentViewPropertiesVersion
);
167 ViewProperties::~ViewProperties()
169 if (m_changedProps
&& m_autoSave
) {
177 void ViewProperties::setViewMode(DolphinView::Mode mode
)
179 if (m_node
->viewMode() != mode
) {
180 m_node
->setViewMode(mode
);
185 DolphinView::Mode
ViewProperties::viewMode() const
187 const int mode
= qBound(0, m_node
->viewMode(), 2);
188 return static_cast<DolphinView::Mode
>(mode
);
191 void ViewProperties::setPreviewsShown(bool show
)
193 if (m_node
->previewsShown() != show
) {
194 m_node
->setPreviewsShown(show
);
199 bool ViewProperties::previewsShown() const
201 return m_node
->previewsShown();
204 void ViewProperties::setHiddenFilesShown(bool show
)
206 if (m_node
->hiddenFilesShown() != show
) {
207 m_node
->setHiddenFilesShown(show
);
212 void ViewProperties::setGroupedSorting(bool grouped
)
214 if (m_node
->groupedSorting() != grouped
) {
215 m_node
->setGroupedSorting(grouped
);
220 bool ViewProperties::groupedSorting() const
222 return m_node
->groupedSorting();
225 bool ViewProperties::hiddenFilesShown() const
227 return m_node
->hiddenFilesShown();
230 void ViewProperties::setSortRole(const QByteArray
&role
)
232 if (m_node
->sortRole() != role
) {
233 m_node
->setSortRole(role
);
238 QByteArray
ViewProperties::sortRole() const
240 return m_node
->sortRole().toLatin1();
243 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
245 if (m_node
->sortOrder() != sortOrder
) {
246 m_node
->setSortOrder(sortOrder
);
251 Qt::SortOrder
ViewProperties::sortOrder() const
253 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
256 void ViewProperties::setGroupRole(const QByteArray
&role
)
258 if (m_node
->groupRole() != role
) {
259 m_node
->setGroupRole(role
);
264 QByteArray
ViewProperties::groupRole() const
266 return m_node
->groupRole().toLatin1();
269 void ViewProperties::setGroupOrder(Qt::SortOrder groupOrder
)
271 if (m_node
->groupOrder() != groupOrder
) {
272 m_node
->setGroupOrder(groupOrder
);
277 Qt::SortOrder
ViewProperties::groupOrder() const
279 return static_cast<Qt::SortOrder
>(m_node
->groupOrder());
282 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
284 if (m_node
->sortFoldersFirst() != foldersFirst
) {
285 m_node
->setSortFoldersFirst(foldersFirst
);
290 bool ViewProperties::sortFoldersFirst() const
292 return m_node
->sortFoldersFirst();
295 void ViewProperties::setSortHiddenLast(bool hiddenLast
)
297 if (m_node
->sortHiddenLast() != hiddenLast
) {
298 m_node
->setSortHiddenLast(hiddenLast
);
303 bool ViewProperties::sortHiddenLast() const
305 return m_node
->sortHiddenLast();
308 void ViewProperties::setVisibleRoles(const QList
<QByteArray
> &roles
)
310 if (roles
== visibleRoles()) {
314 // See ViewProperties::visibleRoles() for the storage format
315 // of the additional information.
317 // Remove the old values stored for the current view-mode
318 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
319 const QString prefix
= viewModePrefix();
320 QStringList newVisibleRoles
= oldVisibleRoles
;
321 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
322 if (newVisibleRoles
[i
].startsWith(prefix
)) {
323 newVisibleRoles
.removeAt(i
);
327 // Add the updated values for the current view-mode
328 newVisibleRoles
.reserve(roles
.count());
329 for (const QByteArray
&role
: roles
) {
330 newVisibleRoles
.append(prefix
+ role
);
333 if (oldVisibleRoles
!= newVisibleRoles
) {
334 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
) && !newVisibleRoles
.contains(CustomizedDetailsString
);
335 if (markCustomizedDetails
) {
336 // The additional information of the details-view has been modified. Set a marker,
337 // so that it is allowed to also show no additional information without doing the
338 // fallback to show the size and date per default.
339 newVisibleRoles
.append(CustomizedDetailsString
);
342 m_node
->setVisibleRoles(newVisibleRoles
);
347 QList
<QByteArray
> ViewProperties::visibleRoles() const
349 // The shown additional information is stored for each view-mode separately as
350 // string with the view-mode as prefix. Example:
352 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
354 // To get the representation as QList<QByteArray>, the current
355 // view-mode must be checked and the values of this mode added to the list.
357 // For the details-view a special case must be respected: Per default the size
358 // and date should be shown without creating a .directory file. Only if
359 // the user explicitly has modified the properties of the details view (marked
360 // by "CustomizedDetails"), also a details-view with no additional information
363 QList
<QByteArray
> roles
{"text"};
365 // Iterate through all stored keys and append all roles that match to
366 // the current view mode.
367 const QString prefix
= viewModePrefix();
368 const int prefixLength
= prefix
.length();
370 const QStringList visibleRoles
= m_node
->visibleRoles();
371 for (const QString
&visibleRole
: visibleRoles
) {
372 if (visibleRole
.startsWith(prefix
)) {
373 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
374 if (role
!= "text") {
380 // For the details view the size and date should be shown per default
381 // until the additional information has been explicitly changed by the user
382 const bool useDefaultValues
= roles
.count() == 1 // "text"
383 && (m_node
->viewMode() == DolphinView::DetailsView
) && !visibleRoles
.contains(CustomizedDetailsString
);
384 if (useDefaultValues
) {
385 roles
.append("size");
386 roles
.append("modificationtime");
392 void ViewProperties::setHeaderColumnWidths(const QList
<int> &widths
)
394 if (m_node
->headerColumnWidths() != widths
) {
395 m_node
->setHeaderColumnWidths(widths
);
400 QList
<int> ViewProperties::headerColumnWidths() const
402 return m_node
->headerColumnWidths();
405 void ViewProperties::setDirProperties(const ViewProperties
&props
)
407 setViewMode(props
.viewMode());
408 setPreviewsShown(props
.previewsShown());
409 setHiddenFilesShown(props
.hiddenFilesShown());
410 setGroupedSorting(props
.groupedSorting());
411 setSortRole(props
.sortRole());
412 setSortOrder(props
.sortOrder());
413 setGroupRole(props
.groupRole());
414 setGroupOrder(props
.groupOrder());
415 setSortFoldersFirst(props
.sortFoldersFirst());
416 setSortHiddenLast(props
.sortHiddenLast());
417 setVisibleRoles(props
.visibleRoles());
418 setHeaderColumnWidths(props
.headerColumnWidths());
419 m_node
->setVersion(props
.m_node
->version());
422 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
424 m_autoSave
= autoSave
;
427 bool ViewProperties::isAutoSaveEnabled() const
432 void ViewProperties::update()
434 m_changedProps
= true;
435 m_node
->setTimestamp(QDateTime::currentDateTime());
438 void ViewProperties::save()
440 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
442 dir
.mkpath(m_filePath
);
443 m_node
->setVersion(CurrentViewPropertiesVersion
);
445 m_changedProps
= false;
448 bool ViewProperties::exist() const
450 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
451 return QFile::exists(file
);
454 QString
ViewProperties::destinationDir(const QString
&subDir
) const
456 QString path
= QStandardPaths::writableLocation(QStandardPaths::AppDataLocation
);
457 path
.append("/view_properties/").append(subDir
);
461 QString
ViewProperties::viewModePrefix() const
465 switch (m_node
->viewMode()) {
466 case DolphinView::IconsView
:
467 prefix
= QStringLiteral("Icons_");
469 case DolphinView::CompactView
:
470 prefix
= QStringLiteral("Compact_");
472 case DolphinView::DetailsView
:
473 prefix
= QStringLiteral("Details_");
476 qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
482 void ViewProperties::convertAdditionalInfo()
484 QStringList visibleRoles
= m_node
->visibleRoles();
486 const QStringList additionalInfo
= m_node
->additionalInfo();
487 if (!additionalInfo
.isEmpty()) {
488 // Convert the obsolete values like Icons_Size, Details_Date, ...
489 // to Icons_size, Details_date, ... where the suffix just represents
490 // the internal role. One special-case must be handled: "LinkDestination"
491 // has been used for "destination".
492 visibleRoles
.reserve(visibleRoles
.count() + additionalInfo
.count());
493 for (const QString
&info
: additionalInfo
) {
494 QString visibleRole
= info
;
495 int index
= visibleRole
.indexOf('_');
496 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
498 if (visibleRole
[index
] == QLatin1Char('L')) {
499 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
501 visibleRole
[index
] = visibleRole
[index
].toLower();
504 if (!visibleRoles
.contains(visibleRole
)) {
505 visibleRoles
.append(visibleRole
);
510 m_node
->setAdditionalInfo(QStringList());
511 m_node
->setVisibleRoles(visibleRoles
);
512 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
516 void ViewProperties::convertNameRoleToTextRole()
518 QStringList visibleRoles
= m_node
->visibleRoles();
519 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
520 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
521 const int leftLength
= visibleRoles
[i
].length() - 5;
522 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
526 QString sortRole
= m_node
->sortRole();
527 if (sortRole
== QLatin1String("name")) {
528 sortRole
= QStringLiteral("text");
531 m_node
->setVisibleRoles(visibleRoles
);
532 m_node
->setSortRole(sortRole
);
533 m_node
->setVersion(NameRolePropertiesVersion
);
537 void ViewProperties::convertDateRoleToModificationTimeRole()
539 QStringList visibleRoles
= m_node
->visibleRoles();
540 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
541 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
542 const int leftLength
= visibleRoles
[i
].length() - 5;
543 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
547 QString sortRole
= m_node
->sortRole();
548 if (sortRole
== QLatin1String("date")) {
549 sortRole
= QStringLiteral("modificationtime");
552 m_node
->setVisibleRoles(visibleRoles
);
553 m_node
->setSortRole(sortRole
);
554 m_node
->setVersion(DateRolePropertiesVersion
);
558 bool ViewProperties::isPartOfHome(const QString
&filePath
)
560 // For performance reasons cache the path in a static QString
561 // (see QDir::homePath() for more details)
562 static QString homePath
;
563 if (homePath
.isEmpty()) {
564 homePath
= QDir::homePath();
565 Q_ASSERT(!homePath
.isEmpty());
568 return filePath
.startsWith(homePath
);
571 QString
ViewProperties::directoryHashForUrl(const QUrl
&url
)
573 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
574 QString hashString
= hashValue
.toBase64();
575 hashString
.replace('/', '-');