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 return m_node
->sortRole().toLatin1();
183 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
185 if (m_node
->sortOrder() != sortOrder
) {
186 m_node
->setSortOrder(sortOrder
);
191 Qt::SortOrder
ViewProperties::sortOrder() const
193 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
196 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
198 if (m_node
->sortFoldersFirst() != foldersFirst
) {
199 m_node
->setSortFoldersFirst(foldersFirst
);
204 bool ViewProperties::sortFoldersFirst() const
206 return m_node
->sortFoldersFirst();
209 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
211 // See ViewProperties::visibleRoles() for the storage format
212 // of the additional information.
214 // Remove the old values stored for the current view-mode
215 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
216 const QString prefix
= viewModePrefix();
217 QStringList newVisibleRoles
= oldVisibleRoles
;
218 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
219 if (newVisibleRoles
[i
].startsWith(prefix
)) {
220 newVisibleRoles
.removeAt(i
);
224 // Add the updated values for the current view-mode
225 foreach (const QByteArray
& role
, roles
) {
226 newVisibleRoles
.append(prefix
+ role
);
229 if (oldVisibleRoles
!= newVisibleRoles
) {
230 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
231 && !newVisibleRoles
.contains(CustomizedDetailsString
);
232 if (markCustomizedDetails
) {
233 // The additional information of the details-view has been modified. Set a marker,
234 // so that it is allowed to also show no additional information without doing the
235 // fallback to show the size and date per default.
236 newVisibleRoles
.append(CustomizedDetailsString
);
239 m_node
->setVisibleRoles(newVisibleRoles
);
244 QList
<QByteArray
> ViewProperties::visibleRoles() const
246 // The shown additional information is stored for each view-mode separately as
247 // string with the view-mode as prefix. Example:
249 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
251 // To get the representation as QList<QByteArray>, the current
252 // view-mode must be checked and the values of this mode added to the list.
254 // For the details-view a special case must be respected: Per default the size
255 // and date should be shown without creating a .directory file. Only if
256 // the user explictly has modified the properties of the details view (marked
257 // by "CustomizedDetails"), also a details-view with no additional information
260 QList
<QByteArray
> roles
;
261 roles
.append("text");
263 // Iterate through all stored keys and append all roles that match to
264 // the curren view mode.
265 const QString prefix
= viewModePrefix();
266 const int prefixLength
= prefix
.length();
268 QStringList visibleRoles
= m_node
->visibleRoles();
269 const int version
= m_node
->version();
270 if (visibleRoles
.isEmpty() && version
<= AdditionalInfoViewPropertiesVersion
) {
271 // Convert the obsolete additionalInfo-property from older versions into the
272 // visibleRoles-property
273 visibleRoles
= const_cast<ViewProperties
*>(this)->convertAdditionalInfo();
274 } else if (version
<= NameRolePropertiesVersion
) {
275 visibleRoles
= const_cast<ViewProperties
*>(this)->convertNameRole();
278 foreach (const QString
& visibleRole
, visibleRoles
) {
279 if (visibleRole
.startsWith(prefix
)) {
280 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
281 if (role
!= "text") {
287 // For the details view the size and date should be shown per default
288 // until the additional information has been explicitly changed by the user
289 const bool useDefaultValues
= roles
.count() == 1 // "text"
290 && (m_node
->viewMode() == DolphinView::DetailsView
)
291 && !visibleRoles
.contains(CustomizedDetailsString
);
292 if (useDefaultValues
) {
293 roles
.append("size");
294 roles
.append("date");
300 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
302 if (m_node
->headerColumnWidths() != widths
) {
303 m_node
->setHeaderColumnWidths(widths
);
308 QList
<int> ViewProperties::headerColumnWidths() const
310 return m_node
->headerColumnWidths();
313 void ViewProperties::setDirProperties(const ViewProperties
& props
)
315 setViewMode(props
.viewMode());
316 setPreviewsShown(props
.previewsShown());
317 setHiddenFilesShown(props
.hiddenFilesShown());
318 setGroupedSorting(props
.groupedSorting());
319 setSortRole(props
.sortRole());
320 setSortOrder(props
.sortOrder());
321 setSortFoldersFirst(props
.sortFoldersFirst());
322 setVisibleRoles(props
.visibleRoles());
323 setHeaderColumnWidths(props
.headerColumnWidths());
326 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
328 m_autoSave
= autoSave
;
331 bool ViewProperties::isAutoSaveEnabled() const
336 void ViewProperties::update()
338 m_changedProps
= true;
339 m_node
->setTimestamp(QDateTime::currentDateTime());
342 void ViewProperties::save()
344 KStandardDirs::makeDir(m_filePath
);
345 m_node
->setVersion(CurrentViewPropertiesVersion
);
346 m_node
->writeConfig();
347 m_changedProps
= false;
350 KUrl
ViewProperties::mirroredDirectory()
352 QString basePath
= KGlobal::mainComponent().componentName();
353 basePath
.append("/view_properties/");
354 return KUrl(KStandardDirs::locateLocal("data", basePath
));
357 QString
ViewProperties::destinationDir(const QString
& subDir
) const
359 QString basePath
= KGlobal::mainComponent().componentName();
360 basePath
.append("/view_properties/").append(subDir
);
361 return KStandardDirs::locateLocal("data", basePath
);
364 QString
ViewProperties::viewModePrefix() const
368 switch (m_node
->viewMode()) {
369 case DolphinView::IconsView
: prefix
= "Icons_"; break;
370 case DolphinView::CompactView
: prefix
= "Compact_"; break;
371 case DolphinView::DetailsView
: prefix
= "Details_"; break;
372 default: kWarning() << "Unknown view-mode of the view properties";
378 QStringList
ViewProperties::convertAdditionalInfo()
380 QStringList visibleRoles
;
382 const QStringList additionalInfo
= m_node
->additionalInfo();
383 if (!additionalInfo
.isEmpty()) {
384 // Convert the obsolete values like Icons_Size, Details_Date, ...
385 // to Icons_size, Details_date, ... where the suffix just represents
386 // the internal role. One special-case must be handled: "LinkDestination"
387 // has been used for "destination".
388 visibleRoles
.reserve(additionalInfo
.count());
389 foreach (const QString
& info
, additionalInfo
) {
390 QString visibleRole
= info
;
391 int index
= visibleRole
.indexOf('_');
392 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
394 if (visibleRole
[index
] == QLatin1Char('L')) {
395 visibleRole
.replace("LinkDestination", "destination");
397 visibleRole
[index
] = visibleRole
[index
].toLower();
400 visibleRoles
.append(visibleRole
);
404 m_node
->setAdditionalInfo(QStringList());
405 m_node
->setVisibleRoles(visibleRoles
);
411 QStringList
ViewProperties::convertNameRole()
413 QStringList visibleRoles
= m_node
->visibleRoles();
414 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
415 if (visibleRoles
[i
].endsWith("_name")) {
416 const int leftLength
= visibleRoles
[i
].length() - 5;
417 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
421 m_node
->setVisibleRoles(visibleRoles
);
428 bool ViewProperties::isPartOfHome(const QString
& filePath
)
430 // For performance reasons cache the path in a static QString
431 // (see QDir::homePath() for more details)
432 static QString homePath
;
433 if (homePath
.isEmpty()) {
434 homePath
= QDir::homePath();
435 Q_ASSERT(!homePath
.isEmpty());
438 return filePath
.startsWith(homePath
);
441 QString
ViewProperties::directoryHashForUrl(const KUrl
& url
)
443 const QByteArray hashValue
= QCryptographicHash::hash(url
.prettyUrl().toLatin1(),
444 QCryptographicHash::Sha1
);
445 QString hashString
= hashValue
.toBase64();
446 hashString
.replace('/', '-');