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>
36 const int AdditionalInfoViewPropertiesVersion
= 1;
37 const int NameRolePropertiesVersion
= 2;
38 const int CurrentViewPropertiesVersion
= 3;
40 // String representation to mark the additional properties of
41 // the details view as customized by the user. See
42 // ViewProperties::visibleRoles() for more information.
43 const char* CustomizedDetailsString
= "CustomizedDetails";
46 ViewProperties::ViewProperties(const KUrl
& url
) :
47 m_changedProps(false),
51 GeneralSettings
* settings
= GeneralSettings::self();
52 const bool useGlobalViewProps
= settings
->globalViewProps();
53 bool useDetailsViewWithPath
= false;
55 // We try and save it to the file .directory in the directory being viewed.
56 // If the directory is not writable by the user or the directory is not local,
57 // we store the properties information in a local file.
58 if (useGlobalViewProps
) {
59 m_filePath
= destinationDir("global");
60 } else if (url
.protocol().contains("search")) {
61 m_filePath
= destinationDir("search");
62 useDetailsViewWithPath
= true;
63 } else if (url
.protocol() == QLatin1String("trash")) {
64 m_filePath
= destinationDir("trash");
65 useDetailsViewWithPath
= true;
66 } else if (url
.isLocalFile()) {
67 m_filePath
= url
.toLocalFile();
68 const QFileInfo
info(m_filePath
);
69 if (!info
.isWritable() || !isPartOfHome(m_filePath
)) {
71 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
72 m_filePath
= QDir::separator() + m_filePath
.remove(QLatin1Char(':'));
74 m_filePath
= destinationDir("local") + m_filePath
;
77 m_filePath
= destinationDir("remote") + m_filePath
;
80 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
81 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
83 // If the .directory file does not exist or the timestamp is too old,
84 // use default values instead.
85 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
86 (!QFileInfo(file
).exists() ||
87 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
88 if (useDefaultProps
) {
89 if (useDetailsViewWithPath
) {
90 setViewMode(DolphinView::DetailsView
);
91 setVisibleRoles(QList
<QByteArray
>() << "path");
93 // The global view-properties act as default for directories without
94 // any view-property configuration
95 settings
->setGlobalViewProps(true);
97 ViewProperties
defaultProps(url
);
98 setDirProperties(defaultProps
);
100 settings
->setGlobalViewProps(false);
101 m_changedProps
= false;
106 ViewProperties::~ViewProperties()
108 if (m_changedProps
&& m_autoSave
) {
116 void ViewProperties::setViewMode(DolphinView::Mode mode
)
118 if (m_node
->viewMode() != mode
) {
119 m_node
->setViewMode(mode
);
124 DolphinView::Mode
ViewProperties::viewMode() const
126 const int mode
= qBound(0, m_node
->viewMode(), 2);
127 return static_cast<DolphinView::Mode
>(mode
);
130 void ViewProperties::setPreviewsShown(bool show
)
132 if (m_node
->previewsShown() != show
) {
133 m_node
->setPreviewsShown(show
);
138 bool ViewProperties::previewsShown() const
140 return m_node
->previewsShown();
143 void ViewProperties::setHiddenFilesShown(bool show
)
145 if (m_node
->hiddenFilesShown() != show
) {
146 m_node
->setHiddenFilesShown(show
);
151 void ViewProperties::setGroupedSorting(bool grouped
)
153 if (m_node
->groupedSorting() != grouped
) {
154 m_node
->setGroupedSorting(grouped
);
159 bool ViewProperties::groupedSorting() const
161 return m_node
->groupedSorting();
164 bool ViewProperties::hiddenFilesShown() const
166 return m_node
->hiddenFilesShown();
169 void ViewProperties::setSortRole(const QByteArray
& role
)
171 if (m_node
->sortRole() != role
) {
172 m_node
->setSortRole(role
);
177 QByteArray
ViewProperties::sortRole() const
179 return m_node
->sortRole().toLatin1();
182 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
184 if (m_node
->sortOrder() != sortOrder
) {
185 m_node
->setSortOrder(sortOrder
);
190 Qt::SortOrder
ViewProperties::sortOrder() const
192 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
195 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
197 if (m_node
->sortFoldersFirst() != foldersFirst
) {
198 m_node
->setSortFoldersFirst(foldersFirst
);
203 bool ViewProperties::sortFoldersFirst() const
205 return m_node
->sortFoldersFirst();
208 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
210 // See ViewProperties::visibleRoles() for the storage format
211 // of the additional information.
213 // Remove the old values stored for the current view-mode
214 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
215 const QString prefix
= viewModePrefix();
216 QStringList newVisibleRoles
= oldVisibleRoles
;
217 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
218 if (newVisibleRoles
[i
].startsWith(prefix
)) {
219 newVisibleRoles
.removeAt(i
);
223 // Add the updated values for the current view-mode
224 foreach (const QByteArray
& role
, roles
) {
225 newVisibleRoles
.append(prefix
+ role
);
228 if (oldVisibleRoles
!= newVisibleRoles
) {
229 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
230 && !newVisibleRoles
.contains(CustomizedDetailsString
);
231 if (markCustomizedDetails
) {
232 // The additional information of the details-view has been modified. Set a marker,
233 // so that it is allowed to also show no additional information without doing the
234 // fallback to show the size and date per default.
235 newVisibleRoles
.append(CustomizedDetailsString
);
238 m_node
->setVisibleRoles(newVisibleRoles
);
243 QList
<QByteArray
> ViewProperties::visibleRoles() const
245 // The shown additional information is stored for each view-mode separately as
246 // string with the view-mode as prefix. Example:
248 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
250 // To get the representation as QList<QByteArray>, the current
251 // view-mode must be checked and the values of this mode added to the list.
253 // For the details-view a special case must be respected: Per default the size
254 // and date should be shown without creating a .directory file. Only if
255 // the user explictly has modified the properties of the details view (marked
256 // by "CustomizedDetails"), also a details-view with no additional information
259 QList
<QByteArray
> roles
;
260 roles
.append("text");
262 // Iterate through all stored keys and append all roles that match to
263 // the curren view mode.
264 const QString prefix
= viewModePrefix();
265 const int prefixLength
= prefix
.length();
267 QStringList visibleRoles
= m_node
->visibleRoles();
268 const int version
= m_node
->version();
269 if (visibleRoles
.isEmpty() && version
<= AdditionalInfoViewPropertiesVersion
) {
270 // Convert the obsolete additionalInfo-property from older versions into the
271 // visibleRoles-property
272 visibleRoles
= const_cast<ViewProperties
*>(this)->convertAdditionalInfo();
273 } else if (version
<= NameRolePropertiesVersion
) {
274 visibleRoles
= const_cast<ViewProperties
*>(this)->convertNameRole();
277 foreach (const QString
& visibleRole
, visibleRoles
) {
278 if (visibleRole
.startsWith(prefix
)) {
279 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
280 if (role
!= "text") {
286 // For the details view the size and date should be shown per default
287 // until the additional information has been explicitly changed by the user
288 const bool useDefaultValues
= roles
.count() == 1 // "text"
289 && (m_node
->viewMode() == DolphinView::DetailsView
)
290 && !visibleRoles
.contains(CustomizedDetailsString
);
291 if (useDefaultValues
) {
292 roles
.append("size");
293 roles
.append("date");
299 void ViewProperties::setHeaderColumnWidths(const QList
<int>& widths
)
301 if (m_node
->headerColumnWidths() != widths
) {
302 m_node
->setHeaderColumnWidths(widths
);
307 QList
<int> ViewProperties::headerColumnWidths() const
309 return m_node
->headerColumnWidths();
312 void ViewProperties::setDirProperties(const ViewProperties
& props
)
314 setViewMode(props
.viewMode());
315 setPreviewsShown(props
.previewsShown());
316 setHiddenFilesShown(props
.hiddenFilesShown());
317 setGroupedSorting(props
.groupedSorting());
318 setSortRole(props
.sortRole());
319 setSortOrder(props
.sortOrder());
320 setSortFoldersFirst(props
.sortFoldersFirst());
321 setVisibleRoles(props
.visibleRoles());
322 setHeaderColumnWidths(props
.headerColumnWidths());
325 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
327 m_autoSave
= autoSave
;
330 bool ViewProperties::isAutoSaveEnabled() const
335 void ViewProperties::update()
337 m_changedProps
= true;
338 m_node
->setTimestamp(QDateTime::currentDateTime());
341 void ViewProperties::save()
343 KStandardDirs::makeDir(m_filePath
);
344 m_node
->setVersion(CurrentViewPropertiesVersion
);
345 m_node
->writeConfig();
346 m_changedProps
= false;
349 KUrl
ViewProperties::mirroredDirectory()
351 QString basePath
= KGlobal::mainComponent().componentName();
352 basePath
.append("/view_properties/");
353 return KUrl(KStandardDirs::locateLocal("data", basePath
));
356 QString
ViewProperties::destinationDir(const QString
& subDir
) const
358 QString basePath
= KGlobal::mainComponent().componentName();
359 basePath
.append("/view_properties/").append(subDir
);
360 return KStandardDirs::locateLocal("data", basePath
);
363 QString
ViewProperties::viewModePrefix() const
367 switch (m_node
->viewMode()) {
368 case DolphinView::IconsView
: prefix
= "Icons_"; break;
369 case DolphinView::CompactView
: prefix
= "Compact_"; break;
370 case DolphinView::DetailsView
: prefix
= "Details_"; break;
371 default: kWarning() << "Unknown view-mode of the view properties";
377 QStringList
ViewProperties::convertAdditionalInfo()
379 QStringList visibleRoles
;
381 const QStringList additionalInfo
= m_node
->additionalInfo();
382 if (!additionalInfo
.isEmpty()) {
383 // Convert the obsolete values like Icons_Size, Details_Date, ...
384 // to Icons_size, Details_date, ... where the suffix just represents
385 // the internal role. One special-case must be handled: "LinkDestination"
386 // has been used for "destination".
387 visibleRoles
.reserve(additionalInfo
.count());
388 foreach (const QString
& info
, additionalInfo
) {
389 QString visibleRole
= info
;
390 int index
= visibleRole
.indexOf('_');
391 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
393 if (visibleRole
[index
] == QLatin1Char('L')) {
394 visibleRole
.replace("LinkDestination", "destination");
396 visibleRole
[index
] = visibleRole
[index
].toLower();
399 visibleRoles
.append(visibleRole
);
403 m_node
->setAdditionalInfo(QStringList());
404 m_node
->setVisibleRoles(visibleRoles
);
410 QStringList
ViewProperties::convertNameRole()
412 QStringList visibleRoles
= m_node
->visibleRoles();
413 for (int i
= 0; i
< visibleRoles
.count(); ++i
) {
414 if (visibleRoles
[i
].endsWith("_name")) {
415 const int leftLength
= visibleRoles
[i
].length() - 5;
416 visibleRoles
[i
] = visibleRoles
[i
].left(leftLength
) + "_text";
420 m_node
->setVisibleRoles(visibleRoles
);
427 bool ViewProperties::isPartOfHome(const QString
& filePath
)
429 // For performance reasons cache the path in a static QString
430 // (see QDir::homePath() for more details)
431 static QString homePath
;
432 if (homePath
.isEmpty()) {
433 homePath
= QDir::homePath();
434 Q_ASSERT(!homePath
.isEmpty());
437 return filePath
.startsWith(homePath
);