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 "rolesaccessor.h"
24 #include "dolphin_directoryviewpropertysettings.h"
25 #include "dolphin_generalsettings.h"
27 #include <KComponentData>
29 #include <KStandardDirs>
37 const int CurrentViewPropertiesVersion
= 2;
38 const int AdditionalInfoViewPropertiesVersion
= 1;
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
)) {
70 m_filePath
= destinationDir("local") + m_filePath
;
73 m_filePath
= destinationDir("remote") + m_filePath
;
76 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
77 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
79 // If the .directory file does not exist or the timestamp is too old,
80 // use default values instead.
81 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
82 (!QFileInfo(file
).exists() ||
83 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
84 if (useDefaultProps
) {
85 if (useDetailsViewWithPath
) {
86 setViewMode(DolphinView::DetailsView
);
87 setVisibleRoles(QList
<QByteArray
>() << "path");
89 // The global view-properties act as default for directories without
90 // any view-property configuration
91 settings
->setGlobalViewProps(true);
93 ViewProperties
defaultProps(url
);
94 setDirProperties(defaultProps
);
96 settings
->setGlobalViewProps(false);
97 m_changedProps
= false;
102 ViewProperties::~ViewProperties()
104 if (m_changedProps
&& m_autoSave
) {
112 void ViewProperties::setViewMode(DolphinView::Mode mode
)
114 if (m_node
->viewMode() != mode
) {
115 m_node
->setViewMode(mode
);
120 DolphinView::Mode
ViewProperties::viewMode() const
122 const int mode
= qBound(0, m_node
->viewMode(), 2);
123 return static_cast<DolphinView::Mode
>(mode
);
126 void ViewProperties::setPreviewsShown(bool show
)
128 if (m_node
->previewsShown() != show
) {
129 m_node
->setPreviewsShown(show
);
134 bool ViewProperties::previewsShown() const
136 return m_node
->previewsShown();
139 void ViewProperties::setHiddenFilesShown(bool show
)
141 if (m_node
->hiddenFilesShown() != show
) {
142 m_node
->setHiddenFilesShown(show
);
147 void ViewProperties::setGroupedSorting(bool grouped
)
149 if (m_node
->groupedSorting() != grouped
) {
150 m_node
->setGroupedSorting(grouped
);
155 bool ViewProperties::groupedSorting() const
157 return m_node
->groupedSorting();
160 bool ViewProperties::hiddenFilesShown() const
162 return m_node
->hiddenFilesShown();
165 void ViewProperties::setSortRole(const QByteArray
& role
)
167 if (m_node
->sortRole() != role
) {
168 m_node
->setSortRole(role
);
173 QByteArray
ViewProperties::sortRole() const
175 return m_node
->sortRole().toLatin1();
178 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
180 if (m_node
->sortOrder() != sortOrder
) {
181 m_node
->setSortOrder(sortOrder
);
186 Qt::SortOrder
ViewProperties::sortOrder() const
188 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
191 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
193 if (m_node
->sortFoldersFirst() != foldersFirst
) {
194 m_node
->setSortFoldersFirst(foldersFirst
);
199 bool ViewProperties::sortFoldersFirst() const
201 return m_node
->sortFoldersFirst();
204 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
206 // See ViewProperties::visibleRoles() for the storage format
207 // of the additional information.
209 // Remove the old values stored for the current view-mode
210 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
211 const QString prefix
= viewModePrefix();
212 QStringList newVisibleRoles
= oldVisibleRoles
;
213 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
214 if (newVisibleRoles
[i
].startsWith(prefix
)) {
215 newVisibleRoles
.removeAt(i
);
219 // Add the updated values for the current view-mode
220 foreach (const QByteArray
& role
, roles
) {
221 newVisibleRoles
.append(prefix
+ role
);
224 if (oldVisibleRoles
!= newVisibleRoles
) {
225 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
226 && !newVisibleRoles
.contains(CustomizedDetailsString
);
227 if (markCustomizedDetails
) {
228 // The additional information of the details-view has been modified. Set a marker,
229 // so that it is allowed to also show no additional information without doing the
230 // fallback to show the size and date per default.
231 newVisibleRoles
.append(CustomizedDetailsString
);
234 m_node
->setVisibleRoles(newVisibleRoles
);
239 QList
<QByteArray
> ViewProperties::visibleRoles() const
241 // The shown additional information is stored for each view-mode separately as
242 // string with the view-mode as prefix. Example:
244 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
246 // To get the representation as QList<QByteArray>, the current
247 // view-mode must be checked and the values of this mode added to the list.
249 // For the details-view a special case must be respected: Per default the size
250 // and date should be shown without creating a .directory file. Only if
251 // the user explictly has modified the properties of the details view (marked
252 // by "CustomizedDetails"), also a details-view with no additional information
255 QList
<QByteArray
> roles
;
256 roles
.append("name");
258 // Iterate through all stored keys and append all roles that match to
259 // the curren view mode.
260 const QString prefix
= viewModePrefix();
261 const int prefixLength
= prefix
.length();
263 QStringList visibleRoles
= m_node
->visibleRoles();
264 if (visibleRoles
.isEmpty() && m_node
->version() <= AdditionalInfoViewPropertiesVersion
) {
265 // Convert the obsolete additionalInfo-property from older versions into the
266 // visibleRoles-property
267 visibleRoles
= const_cast<ViewProperties
*>(this)->convertAdditionalInfo();
270 foreach (const QString
& visibleRole
, visibleRoles
) {
271 if (visibleRole
.startsWith(prefix
)) {
272 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
273 if (role
!= "name") {
279 // For the details view the size and date should be shown per default
280 // until the additional information has been explicitly changed by the user
281 const bool useDefaultValues
= roles
.count() == 1 // "name"
282 && (m_node
->viewMode() == DolphinView::DetailsView
)
283 && !visibleRoles
.contains(CustomizedDetailsString
);
284 if (useDefaultValues
) {
285 roles
.append("size");
286 roles
.append("date");
292 void ViewProperties::setDirProperties(const ViewProperties
& props
)
294 setViewMode(props
.viewMode());
295 setPreviewsShown(props
.previewsShown());
296 setHiddenFilesShown(props
.hiddenFilesShown());
297 setGroupedSorting(props
.groupedSorting());
298 setSortRole(props
.sortRole());
299 setSortOrder(props
.sortOrder());
300 setSortFoldersFirst(props
.sortFoldersFirst());
301 setVisibleRoles(props
.visibleRoles());
304 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
306 m_autoSave
= autoSave
;
309 bool ViewProperties::isAutoSaveEnabled() const
314 void ViewProperties::update()
316 m_changedProps
= true;
317 m_node
->setTimestamp(QDateTime::currentDateTime());
320 void ViewProperties::save()
322 KStandardDirs::makeDir(m_filePath
);
323 m_node
->setVersion(CurrentViewPropertiesVersion
);
324 m_node
->writeConfig();
325 m_changedProps
= false;
328 KUrl
ViewProperties::mirroredDirectory()
330 QString basePath
= KGlobal::mainComponent().componentName();
331 basePath
.append("/view_properties/");
332 return KUrl(KStandardDirs::locateLocal("data", basePath
));
335 QString
ViewProperties::destinationDir(const QString
& subDir
) const
337 QString basePath
= KGlobal::mainComponent().componentName();
338 basePath
.append("/view_properties/").append(subDir
);
339 return KStandardDirs::locateLocal("data", basePath
);
342 QString
ViewProperties::viewModePrefix() const
346 switch (m_node
->viewMode()) {
347 case DolphinView::IconsView
: prefix
= "Icons_"; break;
348 case DolphinView::CompactView
: prefix
= "Compact_"; break;
349 case DolphinView::DetailsView
: prefix
= "Details_"; break;
350 default: kWarning() << "Unknown view-mode of the view properties";
356 QStringList
ViewProperties::convertAdditionalInfo()
358 QStringList visibleRoles
;
360 const QStringList additionalInfo
= m_node
->additionalInfo();
361 if (!additionalInfo
.isEmpty()) {
362 // Convert the obsolete values like Icons_Size, Details_Date, ...
363 // to Icons_size, Details_date, ... where the suffix just represents
364 // the internal role. One special-case must be handled: "LinkDestination"
365 // has been used for "destination".
366 visibleRoles
.reserve(additionalInfo
.count());
367 foreach (const QString
& info
, additionalInfo
) {
368 QString visibleRole
= info
;
369 int index
= visibleRole
.indexOf('_');
370 if (index
>= 0 && index
+ 1 < visibleRole
.length()) {
372 if (visibleRole
[index
] == QLatin1Char('L')) {
373 visibleRole
.replace("LinkDestination", "destination");
375 visibleRole
[index
] = visibleRole
[index
].toLower();
378 visibleRoles
.append(visibleRole
);
382 m_node
->setAdditionalInfo(QStringList());
383 m_node
->setVisibleRoles(visibleRoles
);
389 bool ViewProperties::isPartOfHome(const QString
& filePath
)
391 // For performance reasons cache the path in a static QString
392 // (see QDir::homePath() for more details)
393 static QString homePath
;
394 if (homePath
.isEmpty()) {
395 homePath
= QDir::homePath();
396 Q_ASSERT(!homePath
.isEmpty());
399 return filePath
.startsWith(homePath
);