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({"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
{"text"};
291 // Iterate through all stored keys and append all roles that match to
292 // the current view mode.
293 const QString prefix
= viewModePrefix();
294 const int prefixLength
= prefix
.length();
296 const QStringList visibleRoles
= m_node
->visibleRoles();
297 foreach (const QString
& visibleRole
, visibleRoles
) {
298 if (visibleRole
.startsWith(prefix
)) {
299 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
300 if (role
!= "text") {
306 // For the details view the size and date should be shown per default
307 // until the additional information has been explicitly changed by the user
308 const bool useDefaultValues
= roles
.count() == 1 // "text"
309 && (m_node
->viewMode() == DolphinView::DetailsView
)
310 && !visibleRoles
.contains(CustomizedDetailsString
);
311 if (useDefaultValues
) {
312 roles
.append("size");
313 roles
.append("date");
319 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
321 if (m_node
->headerColumnWidths() != widths
) {
322 m_node
->setHeaderColumnWidths(widths
);
327 QList
<int> ViewProperties::headerColumnWidths() const
329 return m_node
->headerColumnWidths();
332 void ViewProperties::setDirProperties(const ViewProperties
& props
)
334 setViewMode(props
.viewMode());
335 setPreviewsShown(props
.previewsShown());
336 setHiddenFilesShown(props
.hiddenFilesShown());
337 setGroupedSorting(props
.groupedSorting());
338 setSortRole(props
.sortRole());
339 setSortOrder(props
.sortOrder());
340 setSortFoldersFirst(props
.sortFoldersFirst());
341 setVisibleRoles(props
.visibleRoles());
342 setHeaderColumnWidths(props
.headerColumnWidths());
343 m_node
->setVersion(props
.m_node
->version());
346 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
348 m_autoSave
= autoSave
;
351 bool ViewProperties::isAutoSaveEnabled() const
356 void ViewProperties::update()
358 m_changedProps
= true;
359 m_node
->setTimestamp(QDateTime::currentDateTime());
362 void ViewProperties::save()
364 kDebug() << "Saving view-properties to" << m_filePath
;
366 dir
.mkpath(m_filePath
);
367 m_node
->setVersion(CurrentViewPropertiesVersion
);
368 m_node
->writeConfig();
369 m_changedProps
= false;
372 bool ViewProperties::exist() const
374 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
375 return QFile::exists(file
);
378 QString
ViewProperties::destinationDir(const QString
& subDir
) const
380 QString path
= QStandardPaths::writableLocation(QStandardPaths::DataLocation
);
381 path
.append("/view_properties/").append(subDir
);
385 QString
ViewProperties::viewModePrefix() const
389 switch (m_node
->viewMode()) {
390 case DolphinView::IconsView
: prefix
= "Icons_"; break;
391 case DolphinView::CompactView
: prefix
= "Compact_"; break;
392 case DolphinView::DetailsView
: prefix
= "Details_"; break;
393 default: kWarning() << "Unknown view-mode of the view properties";
399 void ViewProperties::convertAdditionalInfo()
401 QStringList visibleRoles
;
403 const QStringList additionalInfo
= m_node
->additionalInfo();
404 if (!additionalInfo
.isEmpty()) {
405 // Convert the obsolete values like Icons_Size, Details_Date, ...
406 // to Icons_size, Details_date, ... where the suffix just represents
407 // the internal role. One special-case must be handled: "LinkDestination"
408 // has been used for "destination".
409 visibleRoles
.reserve(additionalInfo
.count());
410 foreach (const QString
& info
, additionalInfo
) {
411 QString visibleRole
= info
;
412 int index
= visibleRole
.indexOf('_');
413 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
415 if (visibleRole
[index
] == QLatin1Char('L')) {
416 visibleRole
.replace("LinkDestination", "destination");
418 visibleRole
[index
] = visibleRole
[index
].toLower();
421 visibleRoles
.append(visibleRole
);
425 m_node
->setAdditionalInfo(QStringList());
426 m_node
->setVisibleRoles(visibleRoles
);
427 m_node
->setVersion(AdditionalInfoViewPropertiesVersion
);
431 void ViewProperties::convertNameRoleToTextRole()
433 QStringList visibleRoles
= m_node
->visibleRoles();
434 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
435 if (visibleRoles
[i
].endsWith(QLatin1String("_name"))) {
436 const int leftLength
= visibleRoles
[i
].length() - 5;
437 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
441 QString sortRole
= m_node
->sortRole();
442 if (sortRole
== QLatin1String("name")) {
443 sortRole
= QLatin1String("text");
446 m_node
->setVisibleRoles(visibleRoles
);
447 m_node
->setSortRole(sortRole
);
448 m_node
->setVersion(NameRolePropertiesVersion
);
452 bool ViewProperties::isPartOfHome(const QString
& filePath
)
454 // For performance reasons cache the path in a static QString
455 // (see QDir::homePath() for more details)
456 static QString homePath
;
457 if (homePath
.isEmpty()) {
458 homePath
= QDir::homePath();
459 Q_ASSERT(!homePath
.isEmpty());
462 return filePath
.startsWith(homePath
);
465 QString
ViewProperties::directoryHashForUrl(const QUrl
& url
)
467 const QByteArray hashValue
= QCryptographicHash::hash(url
.toEncoded(), QCryptographicHash::Sha1
);
468 QString hashString
= hashValue
.toBase64();
469 hashString
.replace('/', '-');