1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Aaron J. Seigo <aseigo@kde.org> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "viewproperties.h"
23 #include "dolphin_directoryviewpropertysettings.h"
24 #include "dolphin_generalsettings.h"
25 #include "dolphindebug.h"
27 #include <QCryptographicHash>
30 const int AdditionalInfoViewPropertiesVersion
= 1;
31 const int NameRolePropertiesVersion
= 2;
32 const int DateRolePropertiesVersion
= 4;
33 const int CurrentViewPropertiesVersion
= 4;
35 // String representation to mark the additional properties of
36 // the details view as customized by the user. See
37 // ViewProperties::visibleRoles() for more information.
38 const char CustomizedDetailsString
[] = "CustomizedDetails";
40 // Filename that is used for storing the properties
41 const char ViewPropertiesFileName
[] = ".directory";
44 ViewProperties::ViewProperties(const QUrl
& url
) :
45 m_changedProps(false),
49 GeneralSettings
* settings
= GeneralSettings::self();
50 const bool useGlobalViewProps
= settings
->globalViewProps() || url
.isEmpty();
51 bool useDetailsViewWithPath
= false;
53 // We try and save it to the file .directory in the directory being viewed.
54 // If the directory is not writable by the user or the directory is not local,
55 // we store the properties information in a local file.
56 if (useGlobalViewProps
) {
57 m_filePath
= destinationDir(QStringLiteral("global"));
58 } else if (url
.scheme().contains(QStringLiteral("search"))) {
59 m_filePath
= destinationDir(QStringLiteral("search/")) + directoryHashForUrl(url
);
60 useDetailsViewWithPath
= true;
61 } else if (url
.scheme() == QLatin1String("trash")) {
62 m_filePath
= destinationDir(QStringLiteral("trash"));
63 useDetailsViewWithPath
= true;
64 } else if (url
.isLocalFile()) {
65 m_filePath
= url
.toLocalFile();
67 bool useDestinationDir
= !isPartOfHome(m_filePath
);
68 if (!useDestinationDir
) {
69 const QFileInfo
dirInfo(m_filePath
);
70 const QFileInfo
fileInfo(m_filePath
+ QDir::separator() + ViewPropertiesFileName
);
71 useDestinationDir
= !dirInfo
.isWritable() || (dirInfo
.size() > 0 && fileInfo
.exists() && !(fileInfo
.isReadable() && fileInfo
.isWritable()));
74 if (useDestinationDir
) {
76 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
77 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
79 m_filePath
= destinationDir(QStringLiteral("local")) + m_filePath
;
82 m_filePath
= destinationDir(QStringLiteral("remote")) + m_filePath
;
85 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
86 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
88 // If the .directory file does not exist or the timestamp is too old,
89 // use default values instead.
90 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
91 (!QFile::exists(file
) ||
92 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
93 if (useDefaultProps
) {
94 if (useDetailsViewWithPath
) {
95 setViewMode(DolphinView::DetailsView
);
96 setVisibleRoles({"path"});
98 // The global view-properties act as default for directories without
99 // any view-property configuration. Constructing a ViewProperties
100 // instance for an empty QUrl ensures that the global view-properties
103 ViewProperties
defaultProps(emptyUrl
);
104 setDirProperties(defaultProps
);
106 m_changedProps
= false;
110 if (m_node
->version() < CurrentViewPropertiesVersion
) {
111 // The view-properties have an outdated version. Convert the properties
112 // to the changes of the current version.
113 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
114 convertAdditionalInfo();
115 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
118 if (m_node
->version() < NameRolePropertiesVersion
) {
119 convertNameRoleToTextRole();
120 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
123 if (m_node
->version() < DateRolePropertiesVersion
) {
124 convertDateRoleToModificationTimeRole();
125 Q_ASSERT(m_node
->version() == DateRolePropertiesVersion
);
128 m_node
->setVersion(CurrentViewPropertiesVersion
);
132 ViewProperties::~ViewProperties()
134 if (m_changedProps
&& m_autoSave
) {
142 void ViewProperties::setViewMode(DolphinView::Mode mode
)
144 if (m_node
->viewMode() != mode
) {
145 m_node
->setViewMode(mode
);
150 DolphinView::Mode
ViewProperties::viewMode() const
152 const int mode
= qBound(0, m_node
->viewMode(), 2);
153 return static_cast<DolphinView::Mode
>(mode
);
156 void ViewProperties::setPreviewsShown(bool show
)
158 if (m_node
->previewsShown() != show
) {
159 m_node
->setPreviewsShown(show
);
164 bool ViewProperties::previewsShown() const
166 return m_node
->previewsShown();
169 void ViewProperties::setHiddenFilesShown(bool show
)
171 if (m_node
->hiddenFilesShown() != show
) {
172 m_node
->setHiddenFilesShown(show
);
177 void ViewProperties::setGroupedSorting(bool grouped
)
179 if (m_node
->groupedSorting() != grouped
) {
180 m_node
->setGroupedSorting(grouped
);
185 bool ViewProperties::groupedSorting() const
187 return m_node
->groupedSorting();
190 bool ViewProperties::hiddenFilesShown() const
192 return m_node
->hiddenFilesShown();
195 void ViewProperties::setSortRole(const QByteArray
& role
)
197 if (m_node
->sortRole() != role
) {
198 m_node
->setSortRole(role
);
203 QByteArray
ViewProperties::sortRole() const
205 return m_node
->sortRole().toLatin1();
208 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
210 if (m_node
->sortOrder() != sortOrder
) {
211 m_node
->setSortOrder(sortOrder
);
216 Qt::SortOrder
ViewProperties::sortOrder() const
218 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
221 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
223 if (m_node
->sortFoldersFirst() != foldersFirst
) {
224 m_node
->setSortFoldersFirst(foldersFirst
);
229 bool ViewProperties::sortFoldersFirst() const
231 return m_node
->sortFoldersFirst();
234 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
236 if (roles
== visibleRoles()) {
240 // See ViewProperties::visibleRoles() for the storage format
241 // of the additional information.
243 // Remove the old values stored for the current view-mode
244 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
245 const QString prefix
= viewModePrefix();
246 QStringList newVisibleRoles
= oldVisibleRoles
;
247 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
248 if (newVisibleRoles
[i
].startsWith(prefix
)) {
249 newVisibleRoles
.removeAt(i
);
253 // Add the updated values for the current view-mode
254 newVisibleRoles
.reserve(roles
.count());
255 foreach (const QByteArray
& role
, roles
) {
256 newVisibleRoles
.append(prefix
+ role
);
259 if (oldVisibleRoles
!= newVisibleRoles
) {
260 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
261 && !newVisibleRoles
.contains(CustomizedDetailsString
);
262 if (markCustomizedDetails
) {
263 // The additional information of the details-view has been modified. Set a marker,
264 // so that it is allowed to also show no additional information without doing the
265 // fallback to show the size and date per default.
266 newVisibleRoles
.append(CustomizedDetailsString
);
269 m_node
->setVisibleRoles(newVisibleRoles
);
274 QList
<QByteArray
> ViewProperties::visibleRoles() const
276 // The shown additional information is stored for each view-mode separately as
277 // string with the view-mode as prefix. Example:
279 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
281 // To get the representation as QList<QByteArray>, the current
282 // view-mode must be checked and the values of this mode added to the list.
284 // For the details-view a special case must be respected: Per default the size
285 // and date should be shown without creating a .directory file. Only if
286 // the user explictly has modified the properties of the details view (marked
287 // by "CustomizedDetails"), also a details-view with no additional information
290 QList
<QByteArray
> roles
{"text"};
292 // Iterate through all stored keys and append all roles that match to
293 // the current view mode.
294 const QString prefix
= viewModePrefix();
295 const int prefixLength
= prefix
.length();
297 const QStringList visibleRoles
= m_node
->visibleRoles();
298 foreach (const QString
& visibleRole
, visibleRoles
) {
299 if (visibleRole
.startsWith(prefix
)) {
300 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
301 if (role
!= "text") {
307 // For the details view the size and date should be shown per default
308 // until the additional information has been explicitly changed by the user
309 const bool useDefaultValues
= roles
.count() == 1 // "text"
310 && (m_node
->viewMode() == DolphinView::DetailsView
)
311 && !visibleRoles
.contains(CustomizedDetailsString
);
312 if (useDefaultValues
) {
313 roles
.append("size");
314 roles
.append("modificationtime");
320 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
322 if (m_node
->headerColumnWidths() != widths
) {
323 m_node
->setHeaderColumnWidths(widths
);
328 QList
<int> ViewProperties::headerColumnWidths() const
330 return m_node
->headerColumnWidths();
333 void ViewProperties::setDirProperties(const ViewProperties
& props
)
335 setViewMode(props
.viewMode());
336 setPreviewsShown(props
.previewsShown());
337 setHiddenFilesShown(props
.hiddenFilesShown());
338 setGroupedSorting(props
.groupedSorting());
339 setSortRole(props
.sortRole());
340 setSortOrder(props
.sortOrder());
341 setSortFoldersFirst(props
.sortFoldersFirst());
342 setVisibleRoles(props
.visibleRoles());
343 setHeaderColumnWidths(props
.headerColumnWidths());
344 m_node
->setVersion(props
.m_node
->version());
347 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
349 m_autoSave
= autoSave
;
352 bool ViewProperties::isAutoSaveEnabled() const
357 void ViewProperties::update()
359 m_changedProps
= true;
360 m_node
->setTimestamp(QDateTime::currentDateTime());
363 void ViewProperties::save()
365 qCDebug(DolphinDebug
) << "Saving view-properties to" << m_filePath
;
367 dir
.mkpath(m_filePath
);
368 m_node
->setVersion(CurrentViewPropertiesVersion
);
370 m_changedProps
= false;
373 bool ViewProperties::exist() const
375 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
376 return QFile::exists(file
);
379 QString
ViewProperties::destinationDir(const QString
& subDir
) const
381 QString path
= QStandardPaths::writableLocation(QStandardPaths::DataLocation
);
382 path
.append("/view_properties/").append(subDir
);
386 QString
ViewProperties::viewModePrefix() const
390 switch (m_node
->viewMode()) {
391 case DolphinView::IconsView
: prefix
= QStringLiteral("Icons_"); break;
392 case DolphinView::CompactView
: prefix
= QStringLiteral("Compact_"); break;
393 case DolphinView::DetailsView
: prefix
= QStringLiteral("Details_"); break;
394 default: qCWarning(DolphinDebug
) << "Unknown view-mode of the view properties";
400 void ViewProperties::convertAdditionalInfo()
402 QStringList visibleRoles
;
404 const QStringList additionalInfo
= m_node
->additionalInfo();
405 if (!additionalInfo
.isEmpty()) {
406 // Convert the obsolete values like Icons_Size, Details_Date, ...
407 // to Icons_size, Details_date, ... where the suffix just represents
408 // the internal role. One special-case must be handled: "LinkDestination"
409 // has been used for "destination".
410 visibleRoles
.reserve(additionalInfo
.count());
411 foreach (const QString
& info
, additionalInfo
) {
412 QString visibleRole
= info
;
413 int index
= visibleRole
.indexOf('_');
414 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
416 if (visibleRole
[index
] == QLatin1Char('L')) {
417 visibleRole
.replace(QLatin1String("LinkDestination"), QLatin1String("destination"));
419 visibleRole
[index
] = visibleRole
[index
].toLower();
422 visibleRoles
.append(visibleRole
);
426 m_node
->setAdditionalInfo(QStringList());
427 m_node
->setVisibleRoles(visibleRoles
);
428 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
432 void ViewProperties::convertNameRoleToTextRole()
434 QStringList visibleRoles
= m_node
->visibleRoles();
435 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
436 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
437 const int leftLength
= visibleRoles
[i
].length() - 5;
438 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
442 QString sortRole
= m_node
->sortRole();
443 if (sortRole
== QLatin1String("name")) {
444 sortRole
= QStringLiteral("text");
447 m_node
->setVisibleRoles(visibleRoles
);
448 m_node
->setSortRole(sortRole
);
449 m_node
->setVersion(NameRolePropertiesVersion
);
453 void ViewProperties::convertDateRoleToModificationTimeRole()
455 QStringList visibleRoles
= m_node
->visibleRoles();
456 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
457 if (visibleRoles
[i
].endsWith(QLatin1String("_date"))) {
458 const int leftLength
= visibleRoles
[i
].length() - 5;
459 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_modificationtime";
463 QString sortRole
= m_node
->sortRole();
464 if (sortRole
== QLatin1String("date")) {
465 sortRole
= QStringLiteral("modificationtime");
468 m_node
->setVisibleRoles(visibleRoles
);
469 m_node
->setSortRole(sortRole
);
470 m_node
->setVersion(DateRolePropertiesVersion
);
474 bool ViewProperties::isPartOfHome(const QString
& filePath
)
476 // For performance reasons cache the path in a static QString
477 // (see QDir::homePath() for more details)
478 static QString homePath
;
479 if (homePath
.isEmpty()) {
480 homePath
= QDir::homePath();
481 Q_ASSERT(!homePath
.isEmpty());
484 return filePath
.startsWith(homePath
);
487 QString
ViewProperties::directoryHashForUrl(const QUrl
& url
)
489 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
490 QString hashString
= hashValue
.toBase64();
491 hashString
.replace('/', '-');