]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewproperties.cpp
8e09009e58a2fa8ab9860ef1d9353bc3e19055f0
[dolphin.git] / src / views / viewproperties.cpp
1 /*
2 * SPDX-FileCopyrightText: 2006-2010 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2006 Aaron J. Seigo <aseigo@kde.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "viewproperties.h"
9
10 #include "dolphin_directoryviewpropertysettings.h"
11 #include "dolphin_generalsettings.h"
12 #include "dolphindebug.h"
13
14 #include <QCryptographicHash>
15 #include <QTemporaryFile>
16
17 #include <KFileItem>
18 #include <KFileMetaData/UserMetaData>
19
20 namespace
21 {
22 const int AdditionalInfoViewPropertiesVersion = 1;
23 const int NameRolePropertiesVersion = 2;
24 const int DateRolePropertiesVersion = 4;
25 const int CurrentViewPropertiesVersion = 4;
26
27 // String representation to mark the additional properties of
28 // the details view as customized by the user. See
29 // ViewProperties::visibleRoles() for more information.
30 const char CustomizedDetailsString[] = "CustomizedDetails";
31
32 // Filename that is used for storing the properties
33 const char ViewPropertiesFileName[] = ".directory";
34 }
35
36 ViewPropertySettings *ViewProperties::loadProperties(const QString &folderPath) const
37 {
38 const QString settingsFile = folderPath + QDir::separator() + ViewPropertiesFileName;
39
40 KFileMetaData::UserMetaData metadata(folderPath);
41 if (!metadata.isSupported()) {
42 return new ViewPropertySettings(KSharedConfig::openConfig(settingsFile, KConfig::SimpleConfig));
43 }
44
45 auto createTempFile = []() -> QTemporaryFile * {
46 QTemporaryFile *tempFile = new QTemporaryFile;
47 tempFile->setAutoRemove(false);
48 if (!tempFile->open()) {
49 qCWarning(DolphinDebug) << "Could not open temp file";
50 return nullptr;
51 }
52 return tempFile;
53 };
54
55 if (QFile::exists(settingsFile)) {
56 // copy settings to tempfile to load them separately
57 const QTemporaryFile *tempFile = createTempFile();
58 if (!tempFile) {
59 return nullptr;
60 }
61 QFile::remove(tempFile->fileName());
62 QFile::copy(settingsFile, tempFile->fileName());
63
64 auto config = KConfig(tempFile->fileName(), KConfig::SimpleConfig);
65 // ignore settings that are outside of dolphin scope
66 if (config.hasGroup("Dolphin") || config.hasGroup("Settings")) {
67 const auto groupList = config.groupList();
68 for (const auto &group : groupList) {
69 if (group != QStringLiteral("Dolphin") && group != QStringLiteral("Settings")) {
70 config.deleteGroup(group);
71 }
72 }
73 return new ViewPropertySettings(KSharedConfig::openConfig(tempFile->fileName(), KConfig::SimpleConfig));
74
75 } else if (!config.groupList().isEmpty()) {
76 // clear temp file content
77 QFile::remove(tempFile->fileName());
78 }
79 }
80
81 // load from metadata
82 const QString viewPropertiesString = metadata.attribute(QStringLiteral("kde.fm.viewproperties#1"));
83 if (viewPropertiesString.isEmpty()) {
84 return nullptr;
85 }
86 // load view properties from xattr to temp file then loads into ViewPropertySettings
87 // clear the temp file
88 const QTemporaryFile *tempFile = createTempFile();
89 if (!tempFile) {
90 return nullptr;
91 }
92 QFile outputFile(tempFile->fileName());
93 outputFile.open(QIODevice::WriteOnly);
94 outputFile.write(viewPropertiesString.toUtf8());
95 outputFile.close();
96 return new ViewPropertySettings(KSharedConfig::openConfig(tempFile->fileName(), KConfig::SimpleConfig));
97 }
98
99 ViewPropertySettings *ViewProperties::defaultProperties() const
100 {
101 auto props = loadProperties(destinationDir(QStringLiteral("global")));
102 if (props == nullptr) {
103 qCWarning(DolphinDebug) << "Could not load default global viewproperties";
104 QTemporaryFile tempFile;
105 tempFile.setAutoRemove(false);
106 if (!tempFile.open()) {
107 qCWarning(DolphinDebug) << "Could not open temp file";
108 props = new ViewPropertySettings;
109 } else {
110 props = new ViewPropertySettings(KSharedConfig::openConfig(tempFile.fileName(), KConfig::SimpleConfig));
111 }
112 }
113
114 return props;
115 }
116
117 ViewProperties::ViewProperties(const QUrl &url)
118 : m_changedProps(false)
119 , m_autoSave(true)
120 , m_node(nullptr)
121 {
122 GeneralSettings *settings = GeneralSettings::self();
123 const bool useGlobalViewProps = settings->globalViewProps() || url.isEmpty();
124 bool useSearchView = false;
125 bool useTrashView = false;
126 bool useRecentDocumentsView = false;
127 bool useDownloadsView = false;
128
129 // We try and save it to the file .directory in the directory being viewed.
130 // If the directory is not writable by the user or the directory is not local,
131 // we store the properties information in a local file.
132 if (url.scheme().contains(QLatin1String("search"))) {
133 m_filePath = destinationDir(QStringLiteral("search/")) + directoryHashForUrl(url);
134 useSearchView = true;
135 } else if (url.scheme() == QLatin1String("trash")) {
136 m_filePath = destinationDir(QStringLiteral("trash"));
137 useTrashView = true;
138 } else if (url.scheme() == QLatin1String("recentlyused")) {
139 m_filePath = destinationDir(QStringLiteral("recentlyused"));
140 useRecentDocumentsView = true;
141 } else if (url.scheme() == QLatin1String("timeline")) {
142 m_filePath = destinationDir(QStringLiteral("timeline"));
143 useRecentDocumentsView = true;
144 } else if (useGlobalViewProps) {
145 m_filePath = destinationDir(QStringLiteral("global"));
146 } else if (url.isLocalFile()) {
147 m_filePath = url.toLocalFile();
148
149 bool useDestinationDir = !isPartOfHome(m_filePath);
150 if (!useDestinationDir) {
151 const KFileItem fileItem(url);
152 useDestinationDir = fileItem.isSlow();
153 }
154
155 if (!useDestinationDir) {
156 const QFileInfo dirInfo(m_filePath);
157 const QFileInfo fileInfo(m_filePath + QDir::separator() + ViewPropertiesFileName);
158 useDestinationDir = !dirInfo.isWritable() || (dirInfo.size() > 0 && fileInfo.exists() && !(fileInfo.isReadable() && fileInfo.isWritable()));
159 }
160
161 if (useDestinationDir) {
162 #ifdef Q_OS_WIN
163 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
164 m_filePath = QDir::separator() + m_filePath.remove(QLatin1Char(':'));
165 #endif
166 m_filePath = destinationDir(QStringLiteral("local")) + m_filePath;
167 }
168
169 if (m_filePath == QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)) {
170 useDownloadsView = true;
171 }
172 } else {
173 m_filePath = destinationDir(QStringLiteral("remote")) + m_filePath;
174 }
175
176 m_node = loadProperties(m_filePath);
177
178 bool useDefaultSettings = useGlobalViewProps ||
179 // If the props timestamp is too old,
180 // use default values instead.
181 (m_node != nullptr && (!useGlobalViewProps || useSearchView || useTrashView || useRecentDocumentsView || useDownloadsView)
182 && m_node->timestamp() < settings->viewPropsTimestamp());
183
184 if (m_node == nullptr) {
185 // no settings found for m_filepath, load defaults
186 m_node = defaultProperties();
187 useDefaultSettings = true;
188 }
189
190 // default values for special directories
191 if (useDefaultSettings) {
192 if (useSearchView) {
193 const QString path = url.path();
194
195 if (path == QLatin1String("/images")) {
196 setViewMode(DolphinView::IconsView);
197 setPreviewsShown(true);
198 setVisibleRoles({"text", "dimensions", "imageDateTime"});
199 } else if (path == QLatin1String("/audio")) {
200 setViewMode(DolphinView::DetailsView);
201 setVisibleRoles({"text", "artist", "album", "duration"});
202 } else if (path == QLatin1String("/videos")) {
203 setViewMode(DolphinView::IconsView);
204 setPreviewsShown(true);
205 setVisibleRoles({"text"});
206 } else {
207 setViewMode(DolphinView::DetailsView);
208 setVisibleRoles({"text", "path", "modificationtime"});
209 }
210 } else if (useTrashView) {
211 setViewMode(DolphinView::DetailsView);
212 setVisibleRoles({"text", "path", "deletiontime"});
213 } else if (useRecentDocumentsView || useDownloadsView) {
214 setSortOrder(Qt::DescendingOrder);
215 setSortFoldersFirst(false);
216 setGroupedSorting(true);
217
218 if (useRecentDocumentsView) {
219 setSortRole(QByteArrayLiteral("accesstime"));
220 setViewMode(DolphinView::DetailsView);
221 setVisibleRoles({"text", "path", "accesstime"});
222 } else {
223 setSortRole(QByteArrayLiteral("modificationtime"));
224 }
225 } else {
226 m_changedProps = false;
227 }
228 }
229
230 if (m_node->version() < CurrentViewPropertiesVersion) {
231 // The view-properties have an outdated version. Convert the properties
232 // to the changes of the current version.
233 if (m_node->version() < AdditionalInfoViewPropertiesVersion) {
234 convertAdditionalInfo();
235 Q_ASSERT(m_node->version() == AdditionalInfoViewPropertiesVersion);
236 }
237
238 if (m_node->version() < NameRolePropertiesVersion) {
239 convertNameRoleToTextRole();
240 Q_ASSERT(m_node->version() == NameRolePropertiesVersion);
241 }
242
243 if (m_node->version() < DateRolePropertiesVersion) {
244 convertDateRoleToModificationTimeRole();
245 Q_ASSERT(m_node->version() == DateRolePropertiesVersion);
246 }
247
248 m_node->setVersion(CurrentViewPropertiesVersion);
249 }
250 }
251
252 ViewProperties::~ViewProperties()
253 {
254 if (m_changedProps && m_autoSave) {
255 save();
256 }
257
258 if (!m_node->config()->name().endsWith(ViewPropertiesFileName)) {
259 // remove temp file
260 QFile::remove(m_node->config()->name());
261 }
262
263 delete m_node;
264 m_node = nullptr;
265 }
266
267 void ViewProperties::setViewMode(DolphinView::Mode mode)
268 {
269 if (m_node->viewMode() != mode) {
270 m_node->setViewMode(mode);
271 update();
272 }
273 }
274
275 DolphinView::Mode ViewProperties::viewMode() const
276 {
277 const int mode = qBound(0, m_node->viewMode(), 2);
278 return static_cast<DolphinView::Mode>(mode);
279 }
280
281 void ViewProperties::setPreviewsShown(bool show)
282 {
283 if (m_node->previewsShown() != show) {
284 m_node->setPreviewsShown(show);
285 update();
286 }
287 }
288
289 bool ViewProperties::previewsShown() const
290 {
291 return m_node->previewsShown();
292 }
293
294 void ViewProperties::setHiddenFilesShown(bool show)
295 {
296 if (m_node->hiddenFilesShown() != show) {
297 m_node->setHiddenFilesShown(show);
298 update();
299 }
300 }
301
302 void ViewProperties::setGroupedSorting(bool grouped)
303 {
304 if (m_node->groupedSorting() != grouped) {
305 m_node->setGroupedSorting(grouped);
306 update();
307 }
308 }
309
310 bool ViewProperties::groupedSorting() const
311 {
312 return m_node->groupedSorting();
313 }
314
315 bool ViewProperties::hiddenFilesShown() const
316 {
317 return m_node->hiddenFilesShown();
318 }
319
320 void ViewProperties::setSortRole(const QByteArray &role)
321 {
322 if (m_node->sortRole() != role) {
323 m_node->setSortRole(role);
324 update();
325 }
326 }
327
328 QByteArray ViewProperties::sortRole() const
329 {
330 return m_node->sortRole().toLatin1();
331 }
332
333 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
334 {
335 if (m_node->sortOrder() != sortOrder) {
336 m_node->setSortOrder(sortOrder);
337 update();
338 }
339 }
340
341 Qt::SortOrder ViewProperties::sortOrder() const
342 {
343 return static_cast<Qt::SortOrder>(m_node->sortOrder());
344 }
345
346 void ViewProperties::setSortFoldersFirst(bool foldersFirst)
347 {
348 if (m_node->sortFoldersFirst() != foldersFirst) {
349 m_node->setSortFoldersFirst(foldersFirst);
350 update();
351 }
352 }
353
354 bool ViewProperties::sortFoldersFirst() const
355 {
356 return m_node->sortFoldersFirst();
357 }
358
359 void ViewProperties::setSortHiddenLast(bool hiddenLast)
360 {
361 if (m_node->sortHiddenLast() != hiddenLast) {
362 m_node->setSortHiddenLast(hiddenLast);
363 update();
364 }
365 }
366
367 bool ViewProperties::sortHiddenLast() const
368 {
369 return m_node->sortHiddenLast();
370 }
371
372 void ViewProperties::setDynamicViewPassed(bool dynamicViewPassed)
373 {
374 if (m_node->dynamicViewPassed() != dynamicViewPassed) {
375 m_node->setDynamicViewPassed(dynamicViewPassed);
376 update();
377 }
378 }
379
380 bool ViewProperties::dynamicViewPassed() const
381 {
382 return m_node->dynamicViewPassed();
383 }
384
385 void ViewProperties::setVisibleRoles(const QList<QByteArray> &roles)
386 {
387 if (roles == visibleRoles()) {
388 return;
389 }
390
391 // See ViewProperties::visibleRoles() for the storage format
392 // of the additional information.
393
394 // Remove the old values stored for the current view-mode
395 const QStringList oldVisibleRoles = m_node->visibleRoles();
396 const QString prefix = viewModePrefix();
397 QStringList newVisibleRoles = oldVisibleRoles;
398 for (int i = newVisibleRoles.count() - 1; i >= 0; --i) {
399 if (newVisibleRoles[i].startsWith(prefix)) {
400 newVisibleRoles.removeAt(i);
401 }
402 }
403
404 // Add the updated values for the current view-mode
405 newVisibleRoles.reserve(roles.count());
406 for (const QByteArray &role : roles) {
407 newVisibleRoles.append(prefix + role);
408 }
409
410 if (oldVisibleRoles != newVisibleRoles) {
411 const bool markCustomizedDetails = (m_node->viewMode() == DolphinView::DetailsView) && !newVisibleRoles.contains(CustomizedDetailsString);
412 if (markCustomizedDetails) {
413 // The additional information of the details-view has been modified. Set a marker,
414 // so that it is allowed to also show no additional information without doing the
415 // fallback to show the size and date per default.
416 newVisibleRoles.append(CustomizedDetailsString);
417 }
418
419 m_node->setVisibleRoles(newVisibleRoles);
420 update();
421 }
422 }
423
424 QList<QByteArray> ViewProperties::visibleRoles() const
425 {
426 // The shown additional information is stored for each view-mode separately as
427 // string with the view-mode as prefix. Example:
428 //
429 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
430 //
431 // To get the representation as QList<QByteArray>, the current
432 // view-mode must be checked and the values of this mode added to the list.
433 //
434 // For the details-view a special case must be respected: Per default the size
435 // and date should be shown without creating a .directory file. Only if
436 // the user explicitly has modified the properties of the details view (marked
437 // by "CustomizedDetails"), also a details-view with no additional information
438 // is accepted.
439
440 QList<QByteArray> roles{"text"};
441
442 // Iterate through all stored keys and append all roles that match to
443 // the current view mode.
444 const QString prefix = viewModePrefix();
445 const int prefixLength = prefix.length();
446
447 const QStringList visibleRoles = m_node->visibleRoles();
448 for (const QString &visibleRole : visibleRoles) {
449 if (visibleRole.startsWith(prefix)) {
450 const QByteArray role = visibleRole.right(visibleRole.length() - prefixLength).toLatin1();
451 if (role != "text") {
452 roles.append(role);
453 }
454 }
455 }
456
457 // For the details view the size and date should be shown per default
458 // until the additional information has been explicitly changed by the user
459 const bool useDefaultValues = roles.count() == 1 // "text"
460 && (m_node->viewMode() == DolphinView::DetailsView) && !visibleRoles.contains(CustomizedDetailsString);
461 if (useDefaultValues) {
462 roles.append("size");
463 roles.append("modificationtime");
464 }
465
466 return roles;
467 }
468
469 void ViewProperties::setHeaderColumnWidths(const QList<int> &widths)
470 {
471 if (m_node->headerColumnWidths() != widths) {
472 m_node->setHeaderColumnWidths(widths);
473 update();
474 }
475 }
476
477 QList<int> ViewProperties::headerColumnWidths() const
478 {
479 return m_node->headerColumnWidths();
480 }
481
482 void ViewProperties::setDirProperties(const ViewProperties &props)
483 {
484 setViewMode(props.viewMode());
485 setPreviewsShown(props.previewsShown());
486 setHiddenFilesShown(props.hiddenFilesShown());
487 setGroupedSorting(props.groupedSorting());
488 setSortRole(props.sortRole());
489 setSortOrder(props.sortOrder());
490 setSortFoldersFirst(props.sortFoldersFirst());
491 setSortHiddenLast(props.sortHiddenLast());
492 setVisibleRoles(props.visibleRoles());
493 setHeaderColumnWidths(props.headerColumnWidths());
494 m_node->setVersion(props.m_node->version());
495 }
496
497 void ViewProperties::setAutoSaveEnabled(bool autoSave)
498 {
499 m_autoSave = autoSave;
500 }
501
502 bool ViewProperties::isAutoSaveEnabled() const
503 {
504 return m_autoSave;
505 }
506
507 void ViewProperties::update()
508 {
509 m_changedProps = true;
510 m_node->setTimestamp(QDateTime::currentDateTime());
511 }
512
513 void ViewProperties::save()
514 {
515 qCDebug(DolphinDebug) << "Saving view-properties to" << m_filePath;
516
517 auto cleanDotDirectoryFile = [this]() {
518 const QString settingsFile = m_filePath + QDir::separator() + ViewPropertiesFileName;
519 if (QFile::exists(settingsFile)) {
520 qCDebug(DolphinDebug) << "cleaning .directory" << settingsFile;
521 KConfig cfg(settingsFile, KConfig::OpenFlag::SimpleConfig);
522 const auto groupList = cfg.groupList();
523 for (const auto &group : groupList) {
524 if (group == QStringLiteral("Dolphin") || group == QStringLiteral("Settings")) {
525 cfg.deleteGroup(group);
526 }
527 }
528 if (cfg.groupList().isEmpty()) {
529 QFile::remove(settingsFile);
530 } else if (cfg.isDirty()) {
531 cfg.sync();
532 }
533 }
534 };
535
536 // ensures the destination dir exists, in case we don't write metadata directly on the folder
537 QDir destinationDir(m_filePath);
538 if (!destinationDir.exists() && !destinationDir.mkpath(m_filePath)) {
539 qCWarning(DolphinDebug) << "Could not create fake directory to store metadata";
540 }
541
542 KFileMetaData::UserMetaData metaData(m_filePath);
543 if (metaData.isSupported()) {
544 const auto metaDataKey = QStringLiteral("kde.fm.viewproperties#1");
545
546 const auto items = m_node->items();
547 const auto defaultConfig = defaultProperties();
548 bool allDefault = true;
549 for (const auto item : items) {
550 if (item->name() == "Timestamp") {
551 continue;
552 }
553 if (item->name() == "Version") {
554 if (m_node->version() != CurrentViewPropertiesVersion) {
555 allDefault = false;
556 break;
557 } else {
558 continue;
559 }
560 }
561 auto defaultItem = defaultConfig->findItem(item->name());
562 if (!defaultItem || defaultItem->property() != item->property()) {
563 allDefault = false;
564 break;
565 }
566 }
567
568 if (allDefault) {
569 if (metaData.hasAttribute(metaDataKey)) {
570 qCDebug(DolphinDebug) << "clearing extended attributes for " << m_filePath;
571 const auto result = metaData.setAttribute(metaDataKey, QString());
572 if (result != KFileMetaData::UserMetaData::NoError) {
573 qCWarning(DolphinDebug) << "could not clear extended attributes for " << m_filePath << "error:" << result;
574 }
575 }
576 cleanDotDirectoryFile();
577 return;
578 }
579
580 // save config to disk
581 if (!m_node->save()) {
582 qCWarning(DolphinDebug) << "could not save viewproperties" << m_node->config()->name();
583 return;
584 }
585
586 QFile configFile(m_node->config()->name());
587 if (!configFile.open(QIODevice::ReadOnly)) {
588 qCWarning(DolphinDebug) << "Could not open readonly config file" << m_node->config()->name();
589 } else {
590 // load config from disk
591 const QString viewPropertiesString = configFile.readAll();
592
593 // save to xattr
594 const auto result = metaData.setAttribute(metaDataKey, viewPropertiesString);
595 if (result != KFileMetaData::UserMetaData::NoError) {
596 if (result == KFileMetaData::UserMetaData::NoSpace) {
597 // copy settings to dotDirectory file as fallback
598 if (!configFile.copy(m_filePath + QDir::separator() + ViewPropertiesFileName)) {
599 qCWarning(DolphinDebug) << "could not write viewproperties to .directory for dir " << m_filePath;
600 }
601 // free the space used by viewproperties from the file metadata
602 metaData.setAttribute(metaDataKey, "");
603 } else {
604 qCWarning(DolphinDebug) << "could not save viewproperties to extended attributes for dir " << m_filePath << "error:" << result;
605 }
606 // keep .directory file
607 return;
608 }
609 cleanDotDirectoryFile();
610 }
611
612 m_changedProps = false;
613 return;
614 }
615
616 QDir dir;
617 dir.mkpath(m_filePath);
618 m_node->setVersion(CurrentViewPropertiesVersion);
619 m_node->save();
620
621 m_changedProps = false;
622 }
623
624 QString ViewProperties::destinationDir(const QString &subDir) const
625 {
626 QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
627 path.append("/view_properties/").append(subDir);
628 return path;
629 }
630
631 QString ViewProperties::viewModePrefix() const
632 {
633 QString prefix;
634
635 switch (m_node->viewMode()) {
636 case DolphinView::IconsView:
637 prefix = QStringLiteral("Icons_");
638 break;
639 case DolphinView::CompactView:
640 prefix = QStringLiteral("Compact_");
641 break;
642 case DolphinView::DetailsView:
643 prefix = QStringLiteral("Details_");
644 break;
645 default:
646 qCWarning(DolphinDebug) << "Unknown view-mode of the view properties";
647 }
648
649 return prefix;
650 }
651
652 void ViewProperties::convertAdditionalInfo()
653 {
654 QStringList visibleRoles = m_node->visibleRoles();
655
656 const QStringList additionalInfo = m_node->additionalInfo();
657 if (!additionalInfo.isEmpty()) {
658 // Convert the obsolete values like Icons_Size, Details_Date, ...
659 // to Icons_size, Details_date, ... where the suffix just represents
660 // the internal role. One special-case must be handled: "LinkDestination"
661 // has been used for "destination".
662 visibleRoles.reserve(visibleRoles.count() + additionalInfo.count());
663 for (const QString &info : additionalInfo) {
664 QString visibleRole = info;
665 int index = visibleRole.indexOf('_');
666 if (index >= 0 && index + 1 < visibleRole.length()) {
667 ++index;
668 if (visibleRole[index] == QLatin1Char('L')) {
669 visibleRole.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
670 } else {
671 visibleRole[index] = visibleRole[index].toLower();
672 }
673 }
674 if (!visibleRoles.contains(visibleRole)) {
675 visibleRoles.append(visibleRole);
676 }
677 }
678 }
679
680 m_node->setAdditionalInfo(QStringList());
681 m_node->setVisibleRoles(visibleRoles);
682 m_node->setVersion(AdditionalInfoViewPropertiesVersion);
683 update();
684 }
685
686 void ViewProperties::convertNameRoleToTextRole()
687 {
688 QStringList visibleRoles = m_node->visibleRoles();
689 for (int i = 0; i < visibleRoles.count(); ++i) {
690 if (visibleRoles[i].endsWith(QLatin1String("_name"))) {
691 const int leftLength = visibleRoles[i].length() - 5;
692 visibleRoles[i] = visibleRoles[i].left(leftLength) + "_text";
693 }
694 }
695
696 QString sortRole = m_node->sortRole();
697 if (sortRole == QLatin1String("name")) {
698 sortRole = QStringLiteral("text");
699 }
700
701 m_node->setVisibleRoles(visibleRoles);
702 m_node->setSortRole(sortRole);
703 m_node->setVersion(NameRolePropertiesVersion);
704 update();
705 }
706
707 void ViewProperties::convertDateRoleToModificationTimeRole()
708 {
709 QStringList visibleRoles = m_node->visibleRoles();
710 for (int i = 0; i < visibleRoles.count(); ++i) {
711 if (visibleRoles[i].endsWith(QLatin1String("_date"))) {
712 const int leftLength = visibleRoles[i].length() - 5;
713 visibleRoles[i] = visibleRoles[i].left(leftLength) + "_modificationtime";
714 }
715 }
716
717 QString sortRole = m_node->sortRole();
718 if (sortRole == QLatin1String("date")) {
719 sortRole = QStringLiteral("modificationtime");
720 }
721
722 m_node->setVisibleRoles(visibleRoles);
723 m_node->setSortRole(sortRole);
724 m_node->setVersion(DateRolePropertiesVersion);
725 update();
726 }
727
728 bool ViewProperties::isPartOfHome(const QString &filePath)
729 {
730 // For performance reasons cache the path in a static QString
731 // (see QDir::homePath() for more details)
732 static QString homePath;
733 if (homePath.isEmpty()) {
734 homePath = QDir::homePath();
735 Q_ASSERT(!homePath.isEmpty());
736 }
737
738 return filePath.startsWith(homePath);
739 }
740
741 QString ViewProperties::directoryHashForUrl(const QUrl &url)
742 {
743 const QByteArray hashValue = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Sha1);
744 QString hashString = hashValue.toBase64();
745 hashString.replace('/', '-');
746 return hashString;
747 }