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"
26 #include <KComponentData>
28 #include <KStandardDirs>
31 #include <QCryptographicHash>
37 const int AdditionalInfoViewPropertiesVersion
= 1;
38 const int NameRolePropertiesVersion
= 2;
39 const int CurrentViewPropertiesVersion
= 3;
41 // String representation to mark the additional properties of
42 // the details view as customized by the user. See
43 // ViewProperties::visibleRoles() for more information.
44 const char* CustomizedDetailsString
= "CustomizedDetails";
46 // Filename that is used for storing the properties
47 const char* ViewPropertiesFileName
= ".directory";
50 ViewProperties::ViewProperties(const KUrl
& url
) :
51 m_changedProps(false),
55 GeneralSettings
* settings
= GeneralSettings::self();
56 const bool useGlobalViewProps
= settings
->globalViewProps();
57 bool useDetailsViewWithPath
= false;
59 // We try and save it to the file .directory in the directory being viewed.
60 // If the directory is not writable by the user or the directory is not local,
61 // we store the properties information in a local file.
62 if (useGlobalViewProps
) {
63 m_filePath
= destinationDir("global");
64 } else if (url
.protocol().contains("search")) {
65 m_filePath
= destinationDir("search/") + directoryHashForUrl(url
);
66 useDetailsViewWithPath
= true;
67 } else if (url
.protocol() == QLatin1String("trash")) {
68 m_filePath
= destinationDir("trash");
69 useDetailsViewWithPath
= true;
70 } else if (url
.isLocalFile()) {
71 m_filePath
= url
.toLocalFile();
72 const QFileInfo
info(m_filePath
);
73 if (!info
.isWritable() || !isPartOfHome(m_filePath
)) {
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("local") + m_filePath
;
81 m_filePath
= destinationDir("remote") + m_filePath
;
84 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
85 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
87 // If the .directory file does not exist or the timestamp is too old,
88 // use default values instead.
89 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
90 (!QFile::exists(file
) ||
91 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
92 if (useDefaultProps
) {
93 if (useDetailsViewWithPath
) {
94 setViewMode(DolphinView::DetailsView
);
95 setVisibleRoles(QList
<QByteArray
>() << "path");
97 // The global view-properties act as default for directories without
98 // any view-property configuration
99 settings
->setGlobalViewProps(true);
101 ViewProperties
defaultProps(url
);
102 setDirProperties(defaultProps
);
104 settings
->setGlobalViewProps(false);
105 m_changedProps
= false;
109 if (m_node
->version() < CurrentViewPropertiesVersion
) {
110 // The view-properties have an outdated version. Convert the properties
111 // to the changes of the current version.
112 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
113 convertAdditionalInfo();
114 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
117 if (m_node
->version() < NameRolePropertiesVersion
) {
118 convertNameRoleToTextRole();
119 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
122 m_node
->setVersion(CurrentViewPropertiesVersion
);
126 ViewProperties::~ViewProperties()
128 if (m_changedProps
&& m_autoSave
) {
136 void ViewProperties::setViewMode(DolphinView::Mode mode
)
138 if (m_node
->viewMode() != mode
) {
139 m_node
->setViewMode(mode
);
144 DolphinView::Mode
ViewProperties::viewMode() const
146 const int mode
= qBound(0, m_node
->viewMode(), 2);
147 return static_cast<DolphinView::Mode
>(mode
);
150 void ViewProperties::setPreviewsShown(bool show
)
152 if (m_node
->previewsShown() != show
) {
153 m_node
->setPreviewsShown(show
);
158 bool ViewProperties::previewsShown() const
160 return m_node
->previewsShown();
163 void ViewProperties::setHiddenFilesShown(bool show
)
165 if (m_node
->hiddenFilesShown() != show
) {
166 m_node
->setHiddenFilesShown(show
);
171 void ViewProperties::setGroupedSorting(bool grouped
)
173 if (m_node
->groupedSorting() != grouped
) {
174 m_node
->setGroupedSorting(grouped
);
179 bool ViewProperties::groupedSorting() const
181 return m_node
->groupedSorting();
184 bool ViewProperties::hiddenFilesShown() const
186 return m_node
->hiddenFilesShown();
189 void ViewProperties::setSortRole(const QByteArray
& role
)
191 if (m_node
->sortRole() != role
) {
192 m_node
->setSortRole(role
);
197 QByteArray
ViewProperties::sortRole() const
199 return m_node
->sortRole().toLatin1();
202 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
204 if (m_node
->sortOrder() != sortOrder
) {
205 m_node
->setSortOrder(sortOrder
);
210 Qt::SortOrder
ViewProperties::sortOrder() const
212 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
215 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
217 if (m_node
->sortFoldersFirst() != foldersFirst
) {
218 m_node
->setSortFoldersFirst(foldersFirst
);
223 bool ViewProperties::sortFoldersFirst() const
225 return m_node
->sortFoldersFirst();
228 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
230 if (roles
== visibleRoles()) {
234 // See ViewProperties::visibleRoles() for the storage format
235 // of the additional information.
237 // Remove the old values stored for the current view-mode
238 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
239 const QString prefix
= viewModePrefix();
240 QStringList newVisibleRoles
= oldVisibleRoles
;
241 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
242 if (newVisibleRoles
[i
].startsWith(prefix
)) {
243 newVisibleRoles
.removeAt(i
);
247 // Add the updated values for the current view-mode
248 foreach (const QByteArray
& role
, roles
) {
249 newVisibleRoles
.append(prefix
+ role
);
252 if (oldVisibleRoles
!= newVisibleRoles
) {
253 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
254 && !newVisibleRoles
.contains(CustomizedDetailsString
);
255 if (markCustomizedDetails
) {
256 // The additional information of the details-view has been modified. Set a marker,
257 // so that it is allowed to also show no additional information without doing the
258 // fallback to show the size and date per default.
259 newVisibleRoles
.append(CustomizedDetailsString
);
262 m_node
->setVisibleRoles(newVisibleRoles
);
267 QList
<QByteArray
> ViewProperties::visibleRoles() const
269 // The shown additional information is stored for each view-mode separately as
270 // string with the view-mode as prefix. Example:
272 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
274 // To get the representation as QList<QByteArray>, the current
275 // view-mode must be checked and the values of this mode added to the list.
277 // For the details-view a special case must be respected: Per default the size
278 // and date should be shown without creating a .directory file. Only if
279 // the user explictly has modified the properties of the details view (marked
280 // by "CustomizedDetails"), also a details-view with no additional information
283 QList
<QByteArray
> roles
;
284 roles
.append("text");
286 // Iterate through all stored keys and append all roles that match to
287 // the current view mode.
288 const QString prefix
= viewModePrefix();
289 const int prefixLength
= prefix
.length();
291 const QStringList visibleRoles
= m_node
->visibleRoles();
292 foreach (const QString
& visibleRole
, visibleRoles
) {
293 if (visibleRole
.startsWith(prefix
)) {
294 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
295 if (role
!= "text") {
301 // For the details view the size and date should be shown per default
302 // until the additional information has been explicitly changed by the user
303 const bool useDefaultValues
= roles
.count() == 1 // "text"
304 && (m_node
->viewMode() == DolphinView::DetailsView
)
305 && !visibleRoles
.contains(CustomizedDetailsString
);
306 if (useDefaultValues
) {
307 roles
.append("size");
308 roles
.append("date");
314 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
316 if (m_node
->headerColumnWidths() != widths
) {
317 m_node
->setHeaderColumnWidths(widths
);
322 QList
<int> ViewProperties::headerColumnWidths() const
324 return m_node
->headerColumnWidths();
327 void ViewProperties::setDirProperties(const ViewProperties
& props
)
329 setViewMode(props
.viewMode());
330 setPreviewsShown(props
.previewsShown());
331 setHiddenFilesShown(props
.hiddenFilesShown());
332 setGroupedSorting(props
.groupedSorting());
333 setSortRole(props
.sortRole());
334 setSortOrder(props
.sortOrder());
335 setSortFoldersFirst(props
.sortFoldersFirst());
336 setVisibleRoles(props
.visibleRoles());
337 setHeaderColumnWidths(props
.headerColumnWidths());
338 m_node
->setVersion(props
.m_node
->version());
341 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
343 m_autoSave
= autoSave
;
346 bool ViewProperties::isAutoSaveEnabled() const
351 void ViewProperties::update()
353 m_changedProps
= true;
354 m_node
->setTimestamp(QDateTime::currentDateTime());
357 void ViewProperties::save()
359 kDebug() << "Saving view-properties to" << m_filePath
;
360 KStandardDirs::makeDir(m_filePath
);
361 m_node
->setVersion(CurrentViewPropertiesVersion
);
362 m_node
->writeConfig();
363 m_changedProps
= false;
366 bool ViewProperties::exist() const
368 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
369 return QFile::exists(file
);
372 QString
ViewProperties::destinationDir(const QString
& subDir
) const
374 QString basePath
= KGlobal::mainComponent().componentName();
375 basePath
.append("/view_properties/").append(subDir
);
376 return KStandardDirs::locateLocal("data", basePath
);
379 QString
ViewProperties::viewModePrefix() const
383 switch (m_node
->viewMode()) {
384 case DolphinView::IconsView
: prefix
= "Icons_"; break;
385 case DolphinView::CompactView
: prefix
= "Compact_"; break;
386 case DolphinView::DetailsView
: prefix
= "Details_"; break;
387 default: kWarning() << "Unknown view-mode of the view properties";
393 void ViewProperties::convertAdditionalInfo()
395 QStringList visibleRoles
;
397 const QStringList additionalInfo
= m_node
->additionalInfo();
398 if (!additionalInfo
.isEmpty()) {
399 // Convert the obsolete values like Icons_Size, Details_Date, ...
400 // to Icons_size, Details_date, ... where the suffix just represents
401 // the internal role. One special-case must be handled: "LinkDestination"
402 // has been used for "destination".
403 visibleRoles
.reserve(additionalInfo
.count());
404 foreach (const QString
& info
, additionalInfo
) {
405 QString visibleRole
= info
;
406 int index
= visibleRole
.indexOf('_');
407 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
409 if (visibleRole
[index
] == QLatin1Char('L')) {
410 visibleRole
.replace("LinkDestination", "destination");
412 visibleRole
[index
] = visibleRole
[index
].toLower();
415 visibleRoles
.append(visibleRole
);
419 m_node
->setAdditionalInfo(QStringList());
420 m_node
->setVisibleRoles(visibleRoles
);
421 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
425 void ViewProperties::convertNameRoleToTextRole()
427 QStringList visibleRoles
= m_node
->visibleRoles();
428 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
429 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
430 const int leftLength
= visibleRoles
[i
].length() - 5;
431 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
435 QString sortRole
= m_node
->sortRole();
436 if (sortRole
== QLatin1String("name")) {
437 sortRole
= QLatin1String("text");
440 m_node
->setVisibleRoles(visibleRoles
);
441 m_node
->setSortRole(sortRole
);
442 m_node
->setVersion(NameRolePropertiesVersion
);
446 bool ViewProperties::isPartOfHome(const QString
& filePath
)
448 // For performance reasons cache the path in a static QString
449 // (see QDir::homePath() for more details)
450 static QString homePath
;
451 if (homePath
.isEmpty()) {
452 homePath
= QDir::homePath();
453 Q_ASSERT(!homePath
.isEmpty());
456 return filePath
.startsWith(homePath
);
459 QString
ViewProperties::directoryHashForUrl(const KUrl
& url
)
461 const QByteArray hashValue
= QCryptographicHash::hash(url
.prettyUrl().toLatin1(),
462 QCryptographicHash::Sha1
);
463 QString hashString
= hashValue
.toBase64();
464 hashString
.replace('/', '-');
468 KUrl
ViewProperties::mirroredDirectory()
470 QString basePath
= KGlobal::mainComponent().componentName();
471 basePath
.append("/view_properties/");
472 return KUrl(KStandardDirs::locateLocal("data", basePath
));