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::setVisibleRoles(const QList
<QByteArray
>& roles
)
250 if (roles
== visibleRoles()) {
254 // See ViewProperties::visibleRoles() for the storage format
255 // of the additional information.
257 // Remove the old values stored for the current view-mode
258 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
259 const QString prefix
= viewModePrefix();
260 QStringList newVisibleRoles
= oldVisibleRoles
;
261 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
262 if (newVisibleRoles
[i
].startsWith(prefix
)) {
263 newVisibleRoles
.removeAt(i
);
267 // Add the updated values for the current view-mode
268 newVisibleRoles
.reserve(roles
.count());
269 for (const QByteArray
& role
: roles
) {
270 newVisibleRoles
.append(prefix
+ role
);
273 if (oldVisibleRoles
!= newVisibleRoles
) {
274 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
275 && !newVisibleRoles
.contains(CustomizedDetailsString
);
276 if (markCustomizedDetails
) {
277 // The additional information of the details-view has been modified. Set a marker,
278 // so that it is allowed to also show no additional information without doing the
279 // fallback to show the size and date per default.
280 newVisibleRoles
.append(CustomizedDetailsString
);
283 m_node
->setVisibleRoles(newVisibleRoles
);
288 QList
<QByteArray
> ViewProperties::visibleRoles() const
290 // The shown additional information is stored for each view-mode separately as
291 // string with the view-mode as prefix. Example:
293 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
295 // To get the representation as QList<QByteArray>, the current
296 // view-mode must be checked and the values of this mode added to the list.
298 // For the details-view a special case must be respected: Per default the size
299 // and date should be shown without creating a .directory file. Only if
300 // the user explicitly has modified the properties of the details view (marked
301 // by "CustomizedDetails"), also a details-view with no additional information
304 QList
<QByteArray
> roles
{"text"};
306 // Iterate through all stored keys and append all roles that match to
307 // the current view mode.
308 const QString prefix
= viewModePrefix();
309 const int prefixLength
= prefix
.length();
311 const QStringList visibleRoles
= m_node
->visibleRoles();
312 for (const QString
& visibleRole
: visibleRoles
) {
313 if (visibleRole
.startsWith(prefix
)) {
314 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
315 if (role
!= "text") {
321 // For the details view the size and date should be shown per default
322 // until the additional information has been explicitly changed by the user
323 const bool useDefaultValues
= roles
.count() == 1 // "text"
324 && (m_node
->viewMode() == DolphinView::DetailsView
)
325 && !visibleRoles
.contains(CustomizedDetailsString
);
326 if (useDefaultValues
) {
327 roles
.append("size");
328 roles
.append("modificationtime");
334 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
336 if (m_node
->headerColumnWidths() != widths
) {
337 m_node
->setHeaderColumnWidths(widths
);
342 QList
<int> ViewProperties::headerColumnWidths() const
344 return m_node
->headerColumnWidths();
347 void ViewProperties::setDirProperties(const ViewProperties
& props
)
349 setViewMode(props
.viewMode());
350 setPreviewsShown(props
.previewsShown());
351 setHiddenFilesShown(props
.hiddenFilesShown());
352 setGroupedSorting(props
.groupedSorting());
353 setSortRole(props
.sortRole());
354 setSortOrder(props
.sortOrder());
355 setSortFoldersFirst(props
.sortFoldersFirst());
356 setVisibleRoles(props
.visibleRoles());
357 setHeaderColumnWidths(props
.headerColumnWidths());
358 m_node
->setVersion(props
.m_node
->version());
361 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
363 m_autoSave
= autoSave
;
366 bool ViewProperties::isAutoSaveEnabled() const
371 void ViewProperties::update()
373 m_changedProps
= true;
374 m_node
->setTimestamp(QDateTime::currentDateTime());
377 void ViewProperties::save()
379 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
381 dir
.mkpath(m_filePath
);
382 m_node
->setVersion(CurrentViewPropertiesVersion
);
384 m_changedProps
= false;
387 bool ViewProperties::exist() const
389 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
390 return QFile::exists(file
);
393 QString
ViewProperties::destinationDir(const QString
& subDir
) const
395 QString path
= QStandardPaths::writableLocation(QStandardPaths::AppDataLocation
);
396 path
.append("/view_properties/").append(subDir
);
400 QString
ViewProperties::viewModePrefix() const
404 switch (m_node
->viewMode()) {
405 case DolphinView::IconsView
: prefix
= QStringLiteral("Icons_"); break;
406 case DolphinView::CompactView
: prefix
= QStringLiteral("Compact_"); break;
407 case DolphinView::DetailsView
: prefix
= QStringLiteral("Details_"); break;
408 default: qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
414 void ViewProperties::convertAdditionalInfo()
416 QStringList visibleRoles
;
418 const QStringList additionalInfo
= m_node
->additionalInfo();
419 if (!additionalInfo
.isEmpty()) {
420 // Convert the obsolete values like Icons_Size, Details_Date, ...
421 // to Icons_size, Details_date, ... where the suffix just represents
422 // the internal role. One special-case must be handled: "LinkDestination"
423 // has been used for "destination".
424 visibleRoles
.reserve(additionalInfo
.count());
425 for (const QString
& info
: additionalInfo
) {
426 QString visibleRole
= info
;
427 int index
= visibleRole
.indexOf('_');
428 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
430 if (visibleRole
[index
] == QLatin1Char('L')) {
431 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
433 visibleRole
[index
] = visibleRole
[index
].toLower();
436 visibleRoles
.append(visibleRole
);
440 m_node
->setAdditionalInfo(QStringList());
441 m_node
->setVisibleRoles(visibleRoles
);
442 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
446 void ViewProperties::convertNameRoleToTextRole()
448 QStringList visibleRoles
= m_node
->visibleRoles();
449 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
450 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
451 const int leftLength
= visibleRoles
[i
].length() - 5;
452 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
456 QString sortRole
= m_node
->sortRole();
457 if (sortRole
== QLatin1String("name")) {
458 sortRole
= QStringLiteral("text");
461 m_node
->setVisibleRoles(visibleRoles
);
462 m_node
->setSortRole(sortRole
);
463 m_node
->setVersion(NameRolePropertiesVersion
);
467 void ViewProperties::convertDateRoleToModificationTimeRole()
469 QStringList visibleRoles
= m_node
->visibleRoles();
470 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
471 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
472 const int leftLength
= visibleRoles
[i
].length() - 5;
473 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
477 QString sortRole
= m_node
->sortRole();
478 if (sortRole
== QLatin1String("date")) {
479 sortRole
= QStringLiteral("modificationtime");
482 m_node
->setVisibleRoles(visibleRoles
);
483 m_node
->setSortRole(sortRole
);
484 m_node
->setVersion(DateRolePropertiesVersion
);
488 bool ViewProperties::isPartOfHome(const QString
& filePath
)
490 // For performance reasons cache the path in a static QString
491 // (see QDir::homePath() for more details)
492 static QString homePath
;
493 if (homePath
.isEmpty()) {
494 homePath
= QDir::homePath();
495 Q_ASSERT(!homePath
.isEmpty());
498 return filePath
.startsWith(homePath
);
501 QString
ViewProperties::directoryHashForUrl(const QUrl
& url
)
503 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
504 QString hashString
= hashValue
.toBase64();
505 hashString
.replace('/', '-');