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;
110 ViewProperties::~ViewProperties()
112 if (m_changedProps
&& m_autoSave
) {
120 void ViewProperties::setViewMode(DolphinView::Mode mode
)
122 if (m_node
->viewMode() != mode
) {
123 m_node
->setViewMode(mode
);
128 DolphinView::Mode
ViewProperties::viewMode() const
130 const int mode
= qBound(0, m_node
->viewMode(), 2);
131 return static_cast<DolphinView::Mode
>(mode
);
134 void ViewProperties::setPreviewsShown(bool show
)
136 if (m_node
->previewsShown() != show
) {
137 m_node
->setPreviewsShown(show
);
142 bool ViewProperties::previewsShown() const
144 return m_node
->previewsShown();
147 void ViewProperties::setHiddenFilesShown(bool show
)
149 if (m_node
->hiddenFilesShown() != show
) {
150 m_node
->setHiddenFilesShown(show
);
155 void ViewProperties::setGroupedSorting(bool grouped
)
157 if (m_node
->groupedSorting() != grouped
) {
158 m_node
->setGroupedSorting(grouped
);
163 bool ViewProperties::groupedSorting() const
165 return m_node
->groupedSorting();
168 bool ViewProperties::hiddenFilesShown() const
170 return m_node
->hiddenFilesShown();
173 void ViewProperties::setSortRole(const QByteArray
& role
)
175 if (m_node
->sortRole() != role
) {
176 m_node
->setSortRole(role
);
181 QByteArray
ViewProperties::sortRole() const
183 if (m_node
->version() <= NameRolePropertiesVersion
) {
184 const_cast<ViewProperties
*>(this)->convertNameRoleToTextRole();
187 return m_node
->sortRole().toLatin1();
190 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
192 if (m_node
->sortOrder() != sortOrder
) {
193 m_node
->setSortOrder(sortOrder
);
198 Qt::SortOrder
ViewProperties::sortOrder() const
200 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
203 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
205 if (m_node
->sortFoldersFirst() != foldersFirst
) {
206 m_node
->setSortFoldersFirst(foldersFirst
);
211 bool ViewProperties::sortFoldersFirst() const
213 return m_node
->sortFoldersFirst();
216 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
218 // See ViewProperties::visibleRoles() for the storage format
219 // of the additional information.
221 // Remove the old values stored for the current view-mode
222 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
223 const QString prefix
= viewModePrefix();
224 QStringList newVisibleRoles
= oldVisibleRoles
;
225 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
226 if (newVisibleRoles
[i
].startsWith(prefix
)) {
227 newVisibleRoles
.removeAt(i
);
231 // Add the updated values for the current view-mode
232 foreach (const QByteArray
& role
, roles
) {
233 newVisibleRoles
.append(prefix
+ role
);
236 if (oldVisibleRoles
!= newVisibleRoles
) {
237 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
238 && !newVisibleRoles
.contains(CustomizedDetailsString
);
239 if (markCustomizedDetails
) {
240 // The additional information of the details-view has been modified. Set a marker,
241 // so that it is allowed to also show no additional information without doing the
242 // fallback to show the size and date per default.
243 newVisibleRoles
.append(CustomizedDetailsString
);
246 m_node
->setVisibleRoles(newVisibleRoles
);
251 QList
<QByteArray
> ViewProperties::visibleRoles() const
253 // The shown additional information is stored for each view-mode separately as
254 // string with the view-mode as prefix. Example:
256 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
258 // To get the representation as QList<QByteArray>, the current
259 // view-mode must be checked and the values of this mode added to the list.
261 // For the details-view a special case must be respected: Per default the size
262 // and date should be shown without creating a .directory file. Only if
263 // the user explictly has modified the properties of the details view (marked
264 // by "CustomizedDetails"), also a details-view with no additional information
267 QList
<QByteArray
> roles
;
268 roles
.append("text");
270 // Iterate through all stored keys and append all roles that match to
271 // the curren view mode.
272 const QString prefix
= viewModePrefix();
273 const int prefixLength
= prefix
.length();
275 QStringList visibleRoles
= m_node
->visibleRoles();
276 const int version
= m_node
->version();
277 if (visibleRoles
.isEmpty() && version
<= AdditionalInfoViewPropertiesVersion
) {
278 // Convert the obsolete additionalInfo-property from older versions into the
279 // visibleRoles-property
280 const_cast<ViewProperties
*>(this)->convertAdditionalInfo();
281 visibleRoles
= m_node
->visibleRoles();
282 } else if (version
<= NameRolePropertiesVersion
) {
283 const_cast<ViewProperties
*>(this)->convertNameRoleToTextRole();
284 visibleRoles
= m_node
->visibleRoles();
287 foreach (const QString
& visibleRole
, visibleRoles
) {
288 if (visibleRole
.startsWith(prefix
)) {
289 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
290 if (role
!= "text") {
296 // For the details view the size and date should be shown per default
297 // until the additional information has been explicitly changed by the user
298 const bool useDefaultValues
= roles
.count() == 1 // "text"
299 && (m_node
->viewMode() == DolphinView::DetailsView
)
300 && !visibleRoles
.contains(CustomizedDetailsString
);
301 if (useDefaultValues
) {
302 roles
.append("size");
303 roles
.append("date");
309 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
311 if (m_node
->headerColumnWidths() != widths
) {
312 m_node
->setHeaderColumnWidths(widths
);
317 QList
<int> ViewProperties::headerColumnWidths() const
319 return m_node
->headerColumnWidths();
322 void ViewProperties::setDirProperties(const ViewProperties
& props
)
324 setViewMode(props
.viewMode());
325 setPreviewsShown(props
.previewsShown());
326 setHiddenFilesShown(props
.hiddenFilesShown());
327 setGroupedSorting(props
.groupedSorting());
328 setSortRole(props
.sortRole());
329 setSortOrder(props
.sortOrder());
330 setSortFoldersFirst(props
.sortFoldersFirst());
331 setVisibleRoles(props
.visibleRoles());
332 setHeaderColumnWidths(props
.headerColumnWidths());
335 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
337 m_autoSave
= autoSave
;
340 bool ViewProperties::isAutoSaveEnabled() const
345 void ViewProperties::update()
347 m_changedProps
= true;
348 m_node
->setTimestamp(QDateTime::currentDateTime());
351 void ViewProperties::save()
353 KStandardDirs::makeDir(m_filePath
);
354 m_node
->setVersion(CurrentViewPropertiesVersion
);
355 m_node
->writeConfig();
356 m_changedProps
= false;
359 bool ViewProperties::exist() const
361 const QString file
= m_filePath
+ QDir::separator() + ViewPropertiesFileName
;
362 return QFile::exists(file
);
365 QString
ViewProperties::destinationDir(const QString
& subDir
) const
367 QString basePath
= KGlobal::mainComponent().componentName();
368 basePath
.append("/view_properties/").append(subDir
);
369 return KStandardDirs::locateLocal("data", basePath
);
372 QString
ViewProperties::viewModePrefix() const
376 switch (m_node
->viewMode()) {
377 case DolphinView::IconsView
: prefix
= "Icons_"; break;
378 case DolphinView::CompactView
: prefix
= "Compact_"; break;
379 case DolphinView::DetailsView
: prefix
= "Details_"; break;
380 default: kWarning() << "Unknown view-mode of the view properties";
386 void ViewProperties::convertAdditionalInfo()
388 QStringList visibleRoles
;
390 const QStringList additionalInfo
= m_node
->additionalInfo();
391 if (!additionalInfo
.isEmpty()) {
392 // Convert the obsolete values like Icons_Size, Details_Date, ...
393 // to Icons_size, Details_date, ... where the suffix just represents
394 // the internal role. One special-case must be handled: "LinkDestination"
395 // has been used for "destination".
396 visibleRoles
.reserve(additionalInfo
.count());
397 foreach (const QString
& info
, additionalInfo
) {
398 QString visibleRole
= info
;
399 int index
= visibleRole
.indexOf('_');
400 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
402 if (visibleRole
[index
] == QLatin1Char('L')) {
403 visibleRole
.replace("LinkDestination", "destination");
405 visibleRole
[index
] = visibleRole
[index
].toLower();
408 visibleRoles
.append(visibleRole
);
412 m_node
->setAdditionalInfo(QStringList());
413 m_node
->setVisibleRoles(visibleRoles
);
417 void ViewProperties::convertNameRoleToTextRole()
419 QStringList visibleRoles
= m_node
->visibleRoles();
420 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
421 if (visibleRoles
[i
].endsWith("_name")) {
422 const int leftLength
= visibleRoles
[i
].length() - 5;
423 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
427 QString sortRole
= m_node
->sortRole();
428 if (sortRole
== QLatin1String("name")) {
429 sortRole
= QLatin1String("text");
432 m_node
->setVisibleRoles(visibleRoles
);
433 m_node
->setSortRole(sortRole
);
437 bool ViewProperties::isPartOfHome(const QString
& filePath
)
439 // For performance reasons cache the path in a static QString
440 // (see QDir::homePath() for more details)
441 static QString homePath
;
442 if (homePath
.isEmpty()) {
443 homePath
= QDir::homePath();
444 Q_ASSERT(!homePath
.isEmpty());
447 return filePath
.startsWith(homePath
);
450 QString
ViewProperties::directoryHashForUrl(const KUrl
& url
)
452 const QByteArray hashValue
= QCryptographicHash::hash(url
.prettyUrl().toLatin1(),
453 QCryptographicHash::Sha1
);
454 QString hashString
= hashValue
.toBase64();
455 hashString
.replace('/', '-');
459 KUrl
ViewProperties::mirroredDirectory()
461 QString basePath
= KGlobal::mainComponent().componentName();
462 basePath
.append("/view_properties/");
463 return KUrl(KStandardDirs::locateLocal("data", basePath
));