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";
47 ViewProperties::ViewProperties(const KUrl
& url
) :
48 m_changedProps(false),
52 GeneralSettings
* settings
= GeneralSettings::self();
53 const bool useGlobalViewProps
= settings
->globalViewProps();
54 bool useDetailsViewWithPath
= false;
56 // We try and save it to the file .directory in the directory being viewed.
57 // If the directory is not writable by the user or the directory is not local,
58 // we store the properties information in a local file.
59 if (useGlobalViewProps
) {
60 m_filePath
= destinationDir("global");
61 } else if (url
.protocol().contains("search")) {
62 m_filePath
= destinationDir("search/") + directoryHashForUrl(url
);
63 useDetailsViewWithPath
= true;
64 } else if (url
.protocol() == QLatin1String("trash")) {
65 m_filePath
= destinationDir("trash");
66 useDetailsViewWithPath
= true;
67 } else if (url
.isLocalFile()) {
68 m_filePath
= url
.toLocalFile();
69 const QFileInfo
info(m_filePath
);
70 if (!info
.isWritable() || !isPartOfHome(m_filePath
)) {
72 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
73 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
75 m_filePath
= destinationDir("local") + m_filePath
;
78 m_filePath
= destinationDir("remote") + m_filePath
;
81 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
82 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
84 // If the .directory file does not exist or the timestamp is too old,
85 // use default values instead.
86 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
87 (!QFileInfo(file
).exists() ||
88 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
89 if (useDefaultProps
) {
90 if (useDetailsViewWithPath
) {
91 setViewMode(DolphinView::DetailsView
);
92 setVisibleRoles(QList
<QByteArray
>() << "path");
94 // The global view-properties act as default for directories without
95 // any view-property configuration
96 settings
->setGlobalViewProps(true);
98 ViewProperties
defaultProps(url
);
99 setDirProperties(defaultProps
);
101 settings
->setGlobalViewProps(false);
102 m_changedProps
= false;
107 ViewProperties::~ViewProperties()
109 if (m_changedProps
&& m_autoSave
) {
117 void ViewProperties::setViewMode(DolphinView::Mode mode
)
119 if (m_node
->viewMode() != mode
) {
120 m_node
->setViewMode(mode
);
125 DolphinView::Mode
ViewProperties::viewMode() const
127 const int mode
= qBound(0, m_node
->viewMode(), 2);
128 return static_cast<DolphinView::Mode
>(mode
);
131 void ViewProperties::setPreviewsShown(bool show
)
133 if (m_node
->previewsShown() != show
) {
134 m_node
->setPreviewsShown(show
);
139 bool ViewProperties::previewsShown() const
141 return m_node
->previewsShown();
144 void ViewProperties::setHiddenFilesShown(bool show
)
146 if (m_node
->hiddenFilesShown() != show
) {
147 m_node
->setHiddenFilesShown(show
);
152 void ViewProperties::setGroupedSorting(bool grouped
)
154 if (m_node
->groupedSorting() != grouped
) {
155 m_node
->setGroupedSorting(grouped
);
160 bool ViewProperties::groupedSorting() const
162 return m_node
->groupedSorting();
165 bool ViewProperties::hiddenFilesShown() const
167 return m_node
->hiddenFilesShown();
170 void ViewProperties::setSortRole(const QByteArray
& role
)
172 if (m_node
->sortRole() != role
) {
173 m_node
->setSortRole(role
);
178 QByteArray
ViewProperties::sortRole() const
180 if (m_node
->version() <= NameRolePropertiesVersion
) {
181 const_cast<ViewProperties
*>(this)->convertNameRoleToTextRole();
184 return m_node
->sortRole().toLatin1();
187 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
189 if (m_node
->sortOrder() != sortOrder
) {
190 m_node
->setSortOrder(sortOrder
);
195 Qt::SortOrder
ViewProperties::sortOrder() const
197 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
200 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
202 if (m_node
->sortFoldersFirst() != foldersFirst
) {
203 m_node
->setSortFoldersFirst(foldersFirst
);
208 bool ViewProperties::sortFoldersFirst() const
210 return m_node
->sortFoldersFirst();
213 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
215 // See ViewProperties::visibleRoles() for the storage format
216 // of the additional information.
218 // Remove the old values stored for the current view-mode
219 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
220 const QString prefix
= viewModePrefix();
221 QStringList newVisibleRoles
= oldVisibleRoles
;
222 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
223 if (newVisibleRoles
[i
].startsWith(prefix
)) {
224 newVisibleRoles
.removeAt(i
);
228 // Add the updated values for the current view-mode
229 foreach (const QByteArray
& role
, roles
) {
230 newVisibleRoles
.append(prefix
+ role
);
233 if (oldVisibleRoles
!= newVisibleRoles
) {
234 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
235 && !newVisibleRoles
.contains(CustomizedDetailsString
);
236 if (markCustomizedDetails
) {
237 // The additional information of the details-view has been modified. Set a marker,
238 // so that it is allowed to also show no additional information without doing the
239 // fallback to show the size and date per default.
240 newVisibleRoles
.append(CustomizedDetailsString
);
243 m_node
->setVisibleRoles(newVisibleRoles
);
248 QList
<QByteArray
> ViewProperties::visibleRoles() const
250 // The shown additional information is stored for each view-mode separately as
251 // string with the view-mode as prefix. Example:
253 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
255 // To get the representation as QList<QByteArray>, the current
256 // view-mode must be checked and the values of this mode added to the list.
258 // For the details-view a special case must be respected: Per default the size
259 // and date should be shown without creating a .directory file. Only if
260 // the user explictly has modified the properties of the details view (marked
261 // by "CustomizedDetails"), also a details-view with no additional information
264 QList
<QByteArray
> roles
;
265 roles
.append("text");
267 // Iterate through all stored keys and append all roles that match to
268 // the curren view mode.
269 const QString prefix
= viewModePrefix();
270 const int prefixLength
= prefix
.length();
272 QStringList visibleRoles
= m_node
->visibleRoles();
273 const int version
= m_node
->version();
274 if (visibleRoles
.isEmpty() && version
<= AdditionalInfoViewPropertiesVersion
) {
275 // Convert the obsolete additionalInfo-property from older versions into the
276 // visibleRoles-property
277 const_cast<ViewProperties
*>(this)->convertAdditionalInfo();
278 visibleRoles
= m_node
->visibleRoles();
279 } else if (version
<= NameRolePropertiesVersion
) {
280 const_cast<ViewProperties
*>(this)->convertNameRoleToTextRole();
281 visibleRoles
= m_node
->visibleRoles();
284 foreach (const QString
& visibleRole
, visibleRoles
) {
285 if (visibleRole
.startsWith(prefix
)) {
286 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
287 if (role
!= "text") {
293 // For the details view the size and date should be shown per default
294 // until the additional information has been explicitly changed by the user
295 const bool useDefaultValues
= roles
.count() == 1 // "text"
296 && (m_node
->viewMode() == DolphinView::DetailsView
)
297 && !visibleRoles
.contains(CustomizedDetailsString
);
298 if (useDefaultValues
) {
299 roles
.append("size");
300 roles
.append("date");
306 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
308 if (m_node
->headerColumnWidths() != widths
) {
309 m_node
->setHeaderColumnWidths(widths
);
314 QList
<int> ViewProperties::headerColumnWidths() const
316 return m_node
->headerColumnWidths();
319 void ViewProperties::setDirProperties(const ViewProperties
& props
)
321 setViewMode(props
.viewMode());
322 setPreviewsShown(props
.previewsShown());
323 setHiddenFilesShown(props
.hiddenFilesShown());
324 setGroupedSorting(props
.groupedSorting());
325 setSortRole(props
.sortRole());
326 setSortOrder(props
.sortOrder());
327 setSortFoldersFirst(props
.sortFoldersFirst());
328 setVisibleRoles(props
.visibleRoles());
329 setHeaderColumnWidths(props
.headerColumnWidths());
332 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
334 m_autoSave
= autoSave
;
337 bool ViewProperties::isAutoSaveEnabled() const
342 void ViewProperties::update()
344 m_changedProps
= true;
345 m_node
->setTimestamp(QDateTime::currentDateTime());
348 void ViewProperties::save()
350 KStandardDirs::makeDir(m_filePath
);
351 m_node
->setVersion(CurrentViewPropertiesVersion
);
352 m_node
->writeConfig();
353 m_changedProps
= false;
356 KUrl
ViewProperties::mirroredDirectory()
358 QString basePath
= KGlobal::mainComponent().componentName();
359 basePath
.append("/view_properties/");
360 return KUrl(KStandardDirs::locateLocal("data", basePath
));
363 QString
ViewProperties::destinationDir(const QString
& subDir
) const
365 QString basePath
= KGlobal::mainComponent().componentName();
366 basePath
.append("/view_properties/").append(subDir
);
367 return KStandardDirs::locateLocal("data", basePath
);
370 QString
ViewProperties::viewModePrefix() const
374 switch (m_node
->viewMode()) {
375 case DolphinView::IconsView
: prefix
= "Icons_"; break;
376 case DolphinView::CompactView
: prefix
= "Compact_"; break;
377 case DolphinView::DetailsView
: prefix
= "Details_"; break;
378 default: kWarning() << "Unknown view-mode of the view properties";
384 void ViewProperties::convertAdditionalInfo()
386 QStringList visibleRoles
;
388 const QStringList additionalInfo
= m_node
->additionalInfo();
389 if (!additionalInfo
.isEmpty()) {
390 // Convert the obsolete values like Icons_Size, Details_Date, ...
391 // to Icons_size, Details_date, ... where the suffix just represents
392 // the internal role. One special-case must be handled: "LinkDestination"
393 // has been used for "destination".
394 visibleRoles
.reserve(additionalInfo
.count());
395 foreach (const QString
& info
, additionalInfo
) {
396 QString visibleRole
= info
;
397 int index
= visibleRole
.indexOf('_');
398 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
400 if (visibleRole
[index
] == QLatin1Char('L')) {
401 visibleRole
.replace("LinkDestination", "destination");
403 visibleRole
[index
] = visibleRole
[index
].toLower();
406 visibleRoles
.append(visibleRole
);
410 m_node
->setAdditionalInfo(QStringList());
411 m_node
->setVisibleRoles(visibleRoles
);
415 void ViewProperties::convertNameRoleToTextRole()
417 QStringList visibleRoles
= m_node
->visibleRoles();
418 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
419 if (visibleRoles
[i
].endsWith("_name")) {
420 const int leftLength
= visibleRoles
[i
].length() - 5;
421 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
425 QString sortRole
= m_node
->sortRole();
426 if (sortRole
== QLatin1String("name")) {
427 sortRole
= QLatin1String("text");
430 m_node
->setVisibleRoles(visibleRoles
);
431 m_node
->setSortRole(sortRole
);
435 bool ViewProperties::isPartOfHome(const QString
& filePath
)
437 // For performance reasons cache the path in a static QString
438 // (see QDir::homePath() for more details)
439 static QString homePath
;
440 if (homePath
.isEmpty()) {
441 homePath
= QDir::homePath();
442 Q_ASSERT(!homePath
.isEmpty());
445 return filePath
.startsWith(homePath
);
448 QString
ViewProperties::directoryHashForUrl(const KUrl
& url
)
450 const QByteArray hashValue
= QCryptographicHash::hash(url
.prettyUrl().toLatin1(),
451 QCryptographicHash::Sha1
);
452 QString hashString
= hashValue
.toBase64();
453 hashString
.replace('/', '-');