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"
30 #include <KComponentData>
31 #include <QCryptographicHash>
35 #include <QStandardPaths>
38 const int AdditionalInfoViewPropertiesVersion
= 1;
39 const int NameRolePropertiesVersion
= 2;
40 const int CurrentViewPropertiesVersion
= 3;
42 // String representation to mark the additional properties of
43 // the details view as customized by the user. See
44 // ViewProperties::visibleRoles() for more information.
45 const char CustomizedDetailsString
[] = "CustomizedDetails";
47 // Filename that is used for storing the properties
48 const char ViewPropertiesFileName
[] = ".directory";
51 ViewProperties::ViewProperties(const QUrl
& url
) :
52 m_changedProps(false),
56 GeneralSettings
* settings
= GeneralSettings::self();
57 const bool useGlobalViewProps
= settings
->globalViewProps() || url
.isEmpty();
58 bool useDetailsViewWithPath
= false;
60 // We try and save it to the file .directory in the directory being viewed.
61 // If the directory is not writable by the user or the directory is not local,
62 // we store the properties information in a local file.
63 if (useGlobalViewProps
) {
64 m_filePath
= destinationDir("global");
65 } else if (url
.scheme().contains("search")) {
66 m_filePath
= destinationDir("search/") + directoryHashForUrl(url
);
67 useDetailsViewWithPath
= true;
68 } else if (url
.scheme() == QLatin1String("trash")) {
69 m_filePath
= destinationDir("trash");
70 useDetailsViewWithPath
= true;
71 } else if (url
.isLocalFile()) {
72 m_filePath
= url
.toLocalFile();
73 const QFileInfo
dirInfo(m_filePath
);
74 const QFileInfo
fileInfo(m_filePath
+ QDir::separator() + ViewPropertiesFileName
);
75 // Check if the directory is writable and check if the ".directory" file exists and
76 // is read- and writable.
77 if (!dirInfo
.isWritable()
78 || (fileInfo
.exists() && !(fileInfo
.isReadable() && fileInfo
.isWritable()))
79 || !isPartOfHome(m_filePath
)) {
81 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
82 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
84 m_filePath
= destinationDir("local") + m_filePath
;
87 m_filePath
= destinationDir("remote") + m_filePath
;
90 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
91 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
93 // If the .directory file does not exist or the timestamp is too old,
94 // use default values instead.
95 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
96 (!QFile::exists(file
) ||
97 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
98 if (useDefaultProps
) {
99 if (useDetailsViewWithPath
) {
100 setViewMode(DolphinView::DetailsView
);
101 setVisibleRoles(QList
<QByteArray
>() << "path");
103 // The global view-properties act as default for directories without
104 // any view-property configuration. Constructing a ViewProperties
105 // instance for an empty QUrl ensures that the global view-properties
108 ViewProperties
defaultProps(emptyUrl
);
109 setDirProperties(defaultProps
);
111 m_changedProps
= false;
115 if (m_node
->version() < CurrentViewPropertiesVersion
) {
116 // The view-properties have an outdated version. Convert the properties
117 // to the changes of the current version.
118 if (m_node
->version() < AdditionalInfoViewPropertiesVersion
) {
119 convertAdditionalInfo();
120 Q_ASSERT(m_node
->version() == AdditionalInfoViewPropertiesVersion
);
123 if (m_node
->version() < NameRolePropertiesVersion
) {
124 convertNameRoleToTextRole();
125 Q_ASSERT(m_node
->version() == NameRolePropertiesVersion
);
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 foreach (const QByteArray
& role
, roles
) {
255 newVisibleRoles
.append(prefix
+ role
);
258 if (oldVisibleRoles
!= newVisibleRoles
) {
259 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
260 && !newVisibleRoles
.contains(CustomizedDetailsString
);
261 if (markCustomizedDetails
) {
262 // The additional information of the details-view has been modified. Set a marker,
263 // so that it is allowed to also show no additional information without doing the
264 // fallback to show the size and date per default.
265 newVisibleRoles
.append(CustomizedDetailsString
);
268 m_node
->setVisibleRoles(newVisibleRoles
);
273 QList
<QByteArray
> ViewProperties::visibleRoles() const
275 // The shown additional information is stored for each view-mode separately as
276 // string with the view-mode as prefix. Example:
278 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
280 // To get the representation as QList<QByteArray>, the current
281 // view-mode must be checked and the values of this mode added to the list.
283 // For the details-view a special case must be respected: Per default the size
284 // and date should be shown without creating a .directory file. Only if
285 // the user explictly has modified the properties of the details view (marked
286 // by "CustomizedDetails"), also a details-view with no additional information
289 QList
<QByteArray
> roles
;
290 roles
.append("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("date");
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 kDebug() << "Saving view-properties to" << m_filePath
;
367 dir
.mkpath(m_filePath
);
368 m_node
->setVersion(CurrentViewPropertiesVersion
);
369 m_node
->writeConfig();
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(KGlobal::mainComponent().componentName());
383 path
.append("/view_properties/").append(subDir
);
387 QString
ViewProperties::viewModePrefix() const
391 switch (m_node
->viewMode()) {
392 case DolphinView::IconsView
: prefix
= "Icons_"; break;
393 case DolphinView::CompactView
: prefix
= "Compact_"; break;
394 case DolphinView::DetailsView
: prefix
= "Details_"; break;
395 default: kWarning() << "Unknown view-mode of the view properties";
401 void ViewProperties::convertAdditionalInfo()
403 QStringList visibleRoles
;
405 const QStringList additionalInfo
= m_node
->additionalInfo();
406 if (!additionalInfo
.isEmpty()) {
407 // Convert the obsolete values like Icons_Size, Details_Date, ...
408 // to Icons_size, Details_date, ... where the suffix just represents
409 // the internal role. One special-case must be handled: "LinkDestination"
410 // has been used for "destination".
411 visibleRoles
.reserve(additionalInfo
.count());
412 foreach (const QString
& info
, additionalInfo
) {
413 QString visibleRole
= info
;
414 int index
= visibleRole
.indexOf('_');
415 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
417 if (visibleRole
[index
] == QLatin1Char('L')) {
418 visibleRole
.replace("LinkDestination", "destination");
420 visibleRole
[index
] = visibleRole
[index
].toLower();
423 visibleRoles
.append(visibleRole
);
427 m_node
->setAdditionalInfo(QStringList());
428 m_node
->setVisibleRoles(visibleRoles
);
429 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
433 void ViewProperties::convertNameRoleToTextRole()
435 QStringList visibleRoles
= m_node
->visibleRoles();
436 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
437 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
438 const int leftLength
= visibleRoles
[i
].length() - 5;
439 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
443 QString sortRole
= m_node
->sortRole();
444 if (sortRole
== QLatin1String("name")) {
445 sortRole
= QLatin1String("text");
448 m_node
->setVisibleRoles(visibleRoles
);
449 m_node
->setSortRole(sortRole
);
450 m_node
->setVersion(NameRolePropertiesVersion
);
454 bool ViewProperties::isPartOfHome(const QString
& filePath
)
456 // For performance reasons cache the path in a static QString
457 // (see QDir::homePath() for more details)
458 static QString homePath
;
459 if (homePath
.isEmpty()) {
460 homePath
= QDir::homePath();
461 Q_ASSERT(!homePath
.isEmpty());
464 return filePath
.startsWith(homePath
);
467 QString
ViewProperties::directoryHashForUrl(const QUrl
& url
)
469 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
470 QString hashString
= hashValue
.toBase64();
471 hashString
.replace('/', '-');