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 // String representation to mark the additional properties of
38 // the details view as customized by the user. See
39 // ViewProperties::visibleRoles() for more information.
40 const char* CustomizedDetailsString
= "CustomizedDetails";
43 ViewProperties::ViewProperties(const KUrl
& url
) :
44 m_changedProps(false),
48 GeneralSettings
* settings
= GeneralSettings::self();
49 const bool useGlobalViewProps
= settings
->globalViewProps();
50 bool useDetailsViewWithPath
= false;
52 // We try and save it to the file .directory in the directory being viewed.
53 // If the directory is not writable by the user or the directory is not local,
54 // we store the properties information in a local file.
55 if (useGlobalViewProps
) {
56 m_filePath
= destinationDir("global");
57 } else if (url
.protocol().contains("search")) {
58 m_filePath
= destinationDir("search");
59 useDetailsViewWithPath
= true;
60 } else if (url
.protocol() == QLatin1String("trash")) {
61 m_filePath
= destinationDir("trash");
62 useDetailsViewWithPath
= true;
63 } else if (url
.isLocalFile()) {
64 m_filePath
= url
.toLocalFile();
65 const QFileInfo
info(m_filePath
);
66 if (!info
.isWritable() || !isPartOfHome(m_filePath
)) {
67 m_filePath
= destinationDir("local") + m_filePath
;
70 m_filePath
= destinationDir("remote") + m_filePath
;
73 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
74 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
76 // If the .directory file does not exist or the timestamp is too old,
77 // use default values instead.
78 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
79 (!QFileInfo(file
).exists() ||
80 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
81 if (useDefaultProps
) {
82 if (useDetailsViewWithPath
) {
83 setViewMode(DolphinView::DetailsView
);
84 setVisibleRoles(QList
<QByteArray
>() << "path");
86 // The global view-properties act as default for directories without
87 // any view-property configuration
88 settings
->setGlobalViewProps(true);
90 ViewProperties
defaultProps(url
);
91 setDirProperties(defaultProps
);
93 settings
->setGlobalViewProps(false);
94 m_changedProps
= false;
99 ViewProperties::~ViewProperties()
101 if (m_changedProps
&& m_autoSave
) {
109 void ViewProperties::setViewMode(DolphinView::Mode mode
)
111 if (m_node
->viewMode() != mode
) {
112 m_node
->setViewMode(mode
);
117 DolphinView::Mode
ViewProperties::viewMode() const
119 const int mode
= qBound(0, m_node
->viewMode(), 2);
120 return static_cast<DolphinView::Mode
>(mode
);
123 void ViewProperties::setPreviewsShown(bool show
)
125 if (m_node
->previewsShown() != show
) {
126 m_node
->setPreviewsShown(show
);
131 bool ViewProperties::previewsShown() const
133 return m_node
->previewsShown();
136 void ViewProperties::setHiddenFilesShown(bool show
)
138 if (m_node
->hiddenFilesShown() != show
) {
139 m_node
->setHiddenFilesShown(show
);
144 void ViewProperties::setGroupedSorting(bool grouped
)
146 if (m_node
->groupedSorting() != grouped
) {
147 m_node
->setGroupedSorting(grouped
);
152 bool ViewProperties::groupedSorting() const
154 return m_node
->groupedSorting();
157 bool ViewProperties::hiddenFilesShown() const
159 return m_node
->hiddenFilesShown();
162 void ViewProperties::setSortRole(const QByteArray
& role
)
164 if (m_node
->sortRole() != role
) {
165 m_node
->setSortRole(role
);
170 QByteArray
ViewProperties::sortRole() const
172 return m_node
->sortRole().toLatin1();
175 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
177 if (m_node
->sortOrder() != sortOrder
) {
178 m_node
->setSortOrder(sortOrder
);
183 Qt::SortOrder
ViewProperties::sortOrder() const
185 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
188 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
190 if (m_node
->sortFoldersFirst() != foldersFirst
) {
191 m_node
->setSortFoldersFirst(foldersFirst
);
196 bool ViewProperties::sortFoldersFirst() const
198 return m_node
->sortFoldersFirst();
201 void ViewProperties::setVisibleRoles(const QList
<QByteArray
>& roles
)
203 // See ViewProperties::visibleRoles() for the storage format
204 // of the additional information.
206 // Remove the old values stored for the current view-mode
207 const QStringList oldVisibleRoles
= m_node
->visibleRoles();
208 const QString prefix
= viewModePrefix();
209 QStringList newVisibleRoles
= oldVisibleRoles
;
210 for (int i
= newVisibleRoles
.count() - 1; i
>= 0; --i
) {
211 if (newVisibleRoles
[i
].startsWith(prefix
)) {
212 newVisibleRoles
.removeAt(i
);
216 // Add the updated values for the current view-mode
217 foreach (const QByteArray
& role
, roles
) {
218 newVisibleRoles
.append(prefix
+ role
);
221 if (oldVisibleRoles
!= newVisibleRoles
) {
222 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
223 && !newVisibleRoles
.contains(CustomizedDetailsString
);
224 if (markCustomizedDetails
) {
225 // The additional information of the details-view has been modified. Set a marker,
226 // so that it is allowed to also show no additional information without doing the
227 // fallback to show the size and date per default.
228 newVisibleRoles
.append(CustomizedDetailsString
);
231 m_node
->setVisibleRoles(newVisibleRoles
);
236 QList
<QByteArray
> ViewProperties::visibleRoles() const
238 // The shown additional information is stored for each view-mode separately as
239 // string with the view-mode as prefix. Example:
241 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
243 // To get the representation as QList<QByteArray>, the current
244 // view-mode must be checked and the values of this mode added to the list.
246 // For the details-view a special case must be respected: Per default the size
247 // and date should be shown without creating a .directory file. Only if
248 // the user explictly has modified the properties of the details view (marked
249 // by "CustomizedDetails"), also a details-view with no additional information
252 QList
<QByteArray
> roles
;
253 roles
.append("name");
255 // Iterate through all stored keys and append all roles that match to
256 // the curren view mode.
257 const QString prefix
= viewModePrefix();
258 const int prefixLength
= prefix
.length();
259 const QStringList visibleRoles
= m_node
->visibleRoles();
260 foreach (const QString
& visibleRole
, visibleRoles
) {
261 if (visibleRole
.startsWith(prefix
)) {
262 const QByteArray role
= visibleRole
.right(visibleRole
.length() - prefixLength
).toLatin1();
263 if (role
!= "name") {
269 // For the details view the size and date should be shown per default
270 // until the additional information has been explicitly changed by the user
271 const bool useDefaultValues
= roles
.count() == 1 // "name"
272 && (m_node
->viewMode() == DolphinView::DetailsView
)
273 && !visibleRoles
.contains(CustomizedDetailsString
);
274 if (useDefaultValues
) {
275 roles
.append("size");
276 roles
.append("date");
282 void ViewProperties::setDirProperties(const ViewProperties
& props
)
284 setViewMode(props
.viewMode());
285 setPreviewsShown(props
.previewsShown());
286 setHiddenFilesShown(props
.hiddenFilesShown());
287 setGroupedSorting(props
.groupedSorting());
288 setSortRole(props
.sortRole());
289 setSortOrder(props
.sortOrder());
290 setSortFoldersFirst(props
.sortFoldersFirst());
291 setVisibleRoles(props
.visibleRoles());
294 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
296 m_autoSave
= autoSave
;
299 bool ViewProperties::isAutoSaveEnabled() const
304 void ViewProperties::update()
306 m_changedProps
= true;
307 m_node
->setTimestamp(QDateTime::currentDateTime());
310 void ViewProperties::save()
312 KStandardDirs::makeDir(m_filePath
);
313 m_node
->writeConfig();
314 m_changedProps
= false;
317 KUrl
ViewProperties::mirroredDirectory()
319 QString basePath
= KGlobal::mainComponent().componentName();
320 basePath
.append("/view_properties/");
321 return KUrl(KStandardDirs::locateLocal("data", basePath
));
324 QString
ViewProperties::destinationDir(const QString
& subDir
) const
326 QString basePath
= KGlobal::mainComponent().componentName();
327 basePath
.append("/view_properties/").append(subDir
);
328 return KStandardDirs::locateLocal("data", basePath
);
331 QString
ViewProperties::viewModePrefix() const
335 switch (m_node
->viewMode()) {
336 case DolphinView::IconsView
: prefix
= "Icons_"; break;
337 case DolphinView::CompactView
: prefix
= "Compact_"; break;
338 case DolphinView::DetailsView
: prefix
= "Details_"; break;
339 default: kWarning() << "Unknown view-mode of the view properties";
345 bool ViewProperties::isPartOfHome(const QString
& filePath
)
347 // For performance reasons cache the path in a static QString
348 // (see QDir::homePath() for more details)
349 static QString homePath
;
350 if (homePath
.isEmpty()) {
351 homePath
= QDir::homePath();
352 Q_ASSERT(!homePath
.isEmpty());
355 return filePath
.startsWith(homePath
);