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>
19 const int AdditionalInfoViewPropertiesVersion
= 1;
20 const int NameRolePropertiesVersion
= 2;
21 const int DateRolePropertiesVersion
= 4;
22 const int CurrentViewPropertiesVersion
= 4;
24 // String representation to mark the additional properties of
25 // the details view as customized by the user. See
26 // ViewProperties::visibleRoles() for more information.
27 const char CustomizedDetailsString
[] = "CustomizedDetails";
29 // Filename that is used for storing the properties
30 const char ViewPropertiesFileName
[] = ".directory";
33 ViewProperties::ViewProperties(const QUrl
& url
) :
34 m_changedProps(false),
38 GeneralSettings
* settings
= GeneralSettings::self();
39 const bool useGlobalViewProps
= settings
->globalViewProps() || url
.isEmpty();
40 bool useDetailsViewWithPath
= false;
41 bool useRecentDocumentsView
= false;
42 bool useDownloadsView
= false;
44 // We try and save it to the file .directory in the directory being viewed.
45 // If the directory is not writable by the user or the directory is not local,
46 // we store the properties information in a local file.
47 if (useGlobalViewProps
) {
48 m_filePath
= destinationDir(QStringLiteral("global"));
49 } else if (url
.scheme().contains(QLatin1String("search"))) {
50 m_filePath
= destinationDir(QStringLiteral("search/")) + directoryHashForUrl(url
);
51 useDetailsViewWithPath
= true;
52 } else if (url
.scheme() == QLatin1String("trash")) {
53 m_filePath
= destinationDir(QStringLiteral("trash"));
54 useDetailsViewWithPath
= true;
55 } else if (url
.scheme() == QLatin1String("recentdocuments")) {
56 m_filePath
= destinationDir(QStringLiteral("recentdocuments"));
57 useRecentDocumentsView
= true;
58 } else if (url
.isLocalFile()) {
59 m_filePath
= url
.toLocalFile();
61 bool useDestinationDir
= !isPartOfHome(m_filePath
);
62 if (!useDestinationDir
) {
63 const KFileItem
fileItem(url
);
64 useDestinationDir
= fileItem
.isSlow();
67 if (!useDestinationDir
) {
68 const QFileInfo
dirInfo(m_filePath
);
69 const QFileInfo
fileInfo(m_filePath
+ QDir::separator() + ViewPropertiesFileName
);
70 useDestinationDir
= !dirInfo
.isWritable() || (dirInfo
.size() > 0 && fileInfo
.exists() && !(fileInfo
.isReadable() && fileInfo
.isWritable()));
73 if (useDestinationDir
) {
75 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
76 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
78 m_filePath
= destinationDir(QStringLiteral("local")) + m_filePath
;
81 if (m_filePath
== QStandardPaths::writableLocation(QStandardPaths::DownloadLocation
)) {
82 useDownloadsView
= true;
85 m_filePath
= destinationDir(QStringLiteral("remote")) + m_filePath
;
88 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
89 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
91 // If the .directory file does not exist or the timestamp is too old,
92 // use default values instead.
93 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
94 (!QFile::exists(file
) ||
95 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
96 if (useDefaultProps
) {
97 if (useDetailsViewWithPath
) {
98 setViewMode(DolphinView::DetailsView
);
99 setVisibleRoles({"path"});
100 } else if (useRecentDocumentsView
|| useDownloadsView
) {
101 setSortRole(QByteArrayLiteral("modificationtime"));
102 setSortOrder(Qt::DescendingOrder
);
104 if (useRecentDocumentsView
) {
105 setViewMode(DolphinView::DetailsView
);
106 setVisibleRoles({QByteArrayLiteral("path")});
107 } else if (useDownloadsView
) {
108 setSortFoldersFirst(false);
109 setGroupedSorting(true);
112 // The global view-properties act as default for directories without
113 // any view-property configuration. Constructing a ViewProperties
114 // instance for an empty QUrl ensures that the global view-properties
117 ViewProperties
defaultProps(emptyUrl
);
118 setDirProperties(defaultProps
);
120 m_changedProps
= false;
124 if (m_node
->version() < CurrentViewPropertiesVersion
) {
125 // The view-properties have an outdated version. Convert the properties
126 // to the changes of the current version.
127 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
128 convertAdditionalInfo();
129 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
132 if (m_node
->version() < NameRolePropertiesVersion
) {
133 convertNameRoleToTextRole();
134 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
137 if (m_node
->version() < DateRolePropertiesVersion
) {
138 convertDateRoleToModificationTimeRole();
139 Q_ASSERT(m_node
->version() == DateRolePropertiesVersion
);
142 m_node
->setVersion(CurrentViewPropertiesVersion
);
146 ViewProperties::~ViewProperties()
148 if (m_changedProps
&& m_autoSave
) {
156 void ViewProperties::setViewMode(DolphinView::Mode mode
)
158 if (m_node
->viewMode() != mode
) {
159 m_node
->setViewMode(mode
);
164 DolphinView::Mode
ViewProperties::viewMode() const
166 const int mode
= qBound(0, m_node
->viewMode(), 2);
167 return static_cast<DolphinView::Mode
>(mode
);
170 void ViewProperties::setPreviewsShown(bool show
)
172 if (m_node
->previewsShown() != show
) {
173 m_node
->setPreviewsShown(show
);
178 bool ViewProperties::previewsShown() const
180 return m_node
->previewsShown();
183 void ViewProperties::setHiddenFilesShown(bool show
)
185 if (m_node
->hiddenFilesShown() != show
) {
186 m_node
->setHiddenFilesShown(show
);
191 void ViewProperties::setGroupedSorting(bool grouped
)
193 if (m_node
->groupedSorting() != grouped
) {
194 m_node
->setGroupedSorting(grouped
);
199 bool ViewProperties::groupedSorting() const
201 return m_node
->groupedSorting();
204 bool ViewProperties::hiddenFilesShown() const
206 return m_node
->hiddenFilesShown();
209 void ViewProperties::setSortRole(const QByteArray
& role
)
211 if (m_node
->sortRole() != role
) {
212 m_node
->setSortRole(role
);
217 QByteArray
ViewProperties::sortRole() const
219 return m_node
->sortRole().toLatin1();
222 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
224 if (m_node
->sortOrder() != sortOrder
) {
225 m_node
->setSortOrder(sortOrder
);
230 Qt::SortOrder
ViewProperties::sortOrder() const
232 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
235 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
237 if (m_node
->sortFoldersFirst() != foldersFirst
) {
238 m_node
->setSortFoldersFirst(foldersFirst
);
243 bool ViewProperties::sortFoldersFirst() const
245 return m_node
->sortFoldersFirst();
248 void ViewProperties::setSortHiddenLast(bool hiddenLast
)
250 if (m_node
->sortHiddenLast() != hiddenLast
) {
251 m_node
->setSortHiddenLast(hiddenLast
);
256 bool ViewProperties::sortHiddenLast() const
258 return m_node
->sortHiddenLast();
261 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
263 if (roles
== visibleRoles()) {
267 // See ViewProperties::visibleRoles() for the storage format
268 // of the additional information.
270 // Remove the old values stored for the current view-mode
271 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
272 const QString prefix
= viewModePrefix();
273 QStringList newVisibleRoles
= oldVisibleRoles
;
274 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
275 if (newVisibleRoles
[i
].startsWith(prefix
)) {
276 newVisibleRoles
.removeAt(i
);
280 // Add the updated values for the current view-mode
281 newVisibleRoles
.reserve(roles
.count());
282 for (const QByteArray
& role
: roles
) {
283 newVisibleRoles
.append(prefix
+ role
);
286 if (oldVisibleRoles
!= newVisibleRoles
) {
287 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
288 && !newVisibleRoles
.contains(CustomizedDetailsString
);
289 if (markCustomizedDetails
) {
290 // The additional information of the details-view has been modified. Set a marker,
291 // so that it is allowed to also show no additional information without doing the
292 // fallback to show the size and date per default.
293 newVisibleRoles
.append(CustomizedDetailsString
);
296 m_node
->setVisibleRoles(newVisibleRoles
);
301 QList
<QByteArray
> ViewProperties::visibleRoles() const
303 // The shown additional information is stored for each view-mode separately as
304 // string with the view-mode as prefix. Example:
306 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
308 // To get the representation as QList<QByteArray>, the current
309 // view-mode must be checked and the values of this mode added to the list.
311 // For the details-view a special case must be respected: Per default the size
312 // and date should be shown without creating a .directory file. Only if
313 // the user explicitly has modified the properties of the details view (marked
314 // by "CustomizedDetails"), also a details-view with no additional information
317 QList
<QByteArray
> roles
{"text"};
319 // Iterate through all stored keys and append all roles that match to
320 // the current view mode.
321 const QString prefix
= viewModePrefix();
322 const int prefixLength
= prefix
.length();
324 const QStringList visibleRoles
= m_node
->visibleRoles();
325 for (const QString
& visibleRole
: visibleRoles
) {
326 if (visibleRole
.startsWith(prefix
)) {
327 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
328 if (role
!= "text") {
334 // For the details view the size and date should be shown per default
335 // until the additional information has been explicitly changed by the user
336 const bool useDefaultValues
= roles
.count() == 1 // "text"
337 && (m_node
->viewMode() == DolphinView::DetailsView
)
338 && !visibleRoles
.contains(CustomizedDetailsString
);
339 if (useDefaultValues
) {
340 roles
.append("size");
341 roles
.append("modificationtime");
347 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
349 if (m_node
->headerColumnWidths() != widths
) {
350 m_node
->setHeaderColumnWidths(widths
);
355 QList
<int> ViewProperties::headerColumnWidths() const
357 return m_node
->headerColumnWidths();
360 void ViewProperties::setDirProperties(const ViewProperties
& props
)
362 setViewMode(props
.viewMode());
363 setPreviewsShown(props
.previewsShown());
364 setHiddenFilesShown(props
.hiddenFilesShown());
365 setGroupedSorting(props
.groupedSorting());
366 setSortRole(props
.sortRole());
367 setSortOrder(props
.sortOrder());
368 setSortFoldersFirst(props
.sortFoldersFirst());
369 setSortHiddenLast(props
.sortHiddenLast());
370 setVisibleRoles(props
.visibleRoles());
371 setHeaderColumnWidths(props
.headerColumnWidths());
372 m_node
->setVersion(props
.m_node
->version());
375 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
377 m_autoSave
= autoSave
;
380 bool ViewProperties::isAutoSaveEnabled() const
385 void ViewProperties::update()
387 m_changedProps
= true;
388 m_node
->setTimestamp(QDateTime::currentDateTime());
391 void ViewProperties::save()
393 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
395 dir
.mkpath(m_filePath
);
396 m_node
->setVersion(CurrentViewPropertiesVersion
);
398 m_changedProps
= false;
401 bool ViewProperties::exist() const
403 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
404 return QFile::exists(file
);
407 QString
ViewProperties::destinationDir(const QString
& subDir
) const
409 QString path
= QStandardPaths::writableLocation(QStandardPaths::AppDataLocation
);
410 path
.append("/view_properties/").append(subDir
);
414 QString
ViewProperties::viewModePrefix() const
418 switch (m_node
->viewMode()) {
419 case DolphinView::IconsView
: prefix
= QStringLiteral("Icons_"); break;
420 case DolphinView::CompactView
: prefix
= QStringLiteral("Compact_"); break;
421 case DolphinView::DetailsView
: prefix
= QStringLiteral("Details_"); break;
422 default: qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
428 void ViewProperties::convertAdditionalInfo()
430 QStringList visibleRoles
;
432 const QStringList additionalInfo
= m_node
->additionalInfo();
433 if (!additionalInfo
.isEmpty()) {
434 // Convert the obsolete values like Icons_Size, Details_Date, ...
435 // to Icons_size, Details_date, ... where the suffix just represents
436 // the internal role. One special-case must be handled: "LinkDestination"
437 // has been used for "destination".
438 visibleRoles
.reserve(additionalInfo
.count());
439 for (const QString
& info
: additionalInfo
) {
440 QString visibleRole
= info
;
441 int index
= visibleRole
.indexOf('_');
442 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
444 if (visibleRole
[index
] == QLatin1Char('L')) {
445 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
447 visibleRole
[index
] = visibleRole
[index
].toLower();
450 visibleRoles
.append(visibleRole
);
454 m_node
->setAdditionalInfo(QStringList());
455 m_node
->setVisibleRoles(visibleRoles
);
456 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
460 void ViewProperties::convertNameRoleToTextRole()
462 QStringList visibleRoles
= m_node
->visibleRoles();
463 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
464 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
465 const int leftLength
= visibleRoles
[i
].length() - 5;
466 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
470 QString sortRole
= m_node
->sortRole();
471 if (sortRole
== QLatin1String("name")) {
472 sortRole
= QStringLiteral("text");
475 m_node
->setVisibleRoles(visibleRoles
);
476 m_node
->setSortRole(sortRole
);
477 m_node
->setVersion(NameRolePropertiesVersion
);
481 void ViewProperties::convertDateRoleToModificationTimeRole()
483 QStringList visibleRoles
= m_node
->visibleRoles();
484 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
485 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
486 const int leftLength
= visibleRoles
[i
].length() - 5;
487 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
491 QString sortRole
= m_node
->sortRole();
492 if (sortRole
== QLatin1String("date")) {
493 sortRole
= QStringLiteral("modificationtime");
496 m_node
->setVisibleRoles(visibleRoles
);
497 m_node
->setSortRole(sortRole
);
498 m_node
->setVersion(DateRolePropertiesVersion
);
502 bool ViewProperties::isPartOfHome(const QString
& filePath
)
504 // For performance reasons cache the path in a static QString
505 // (see QDir::homePath() for more details)
506 static QString homePath
;
507 if (homePath
.isEmpty()) {
508 homePath
= QDir::homePath();
509 Q_ASSERT(!homePath
.isEmpty());
512 return filePath
.startsWith(homePath
);
515 QString
ViewProperties::directoryHashForUrl(const QUrl
& url
)
517 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
518 QString hashString
= hashValue
.toBase64();
519 hashString
.replace('/', '-');