]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewproperties.cpp
First step to introduce dynamic roles
[dolphin.git] / src / views / viewproperties.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Aaron J. Seigo <aseigo@kde.org> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "viewproperties.h"
22
23 #include "rolesaccessor.h"
24 #include "dolphin_directoryviewpropertysettings.h"
25 #include "dolphin_generalsettings.h"
26
27 #include <KComponentData>
28 #include <KLocale>
29 #include <KStandardDirs>
30 #include <KUrl>
31
32 #include <QDate>
33 #include <QFile>
34 #include <QFileInfo>
35
36 namespace {
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";
41 }
42
43 ViewProperties::ViewProperties(const KUrl& url) :
44 m_changedProps(false),
45 m_autoSave(true),
46 m_node(0)
47 {
48 GeneralSettings* settings = GeneralSettings::self();
49 const bool useGlobalViewProps = settings->globalViewProps();
50 bool useDetailsViewWithPath = false;
51
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;
68 }
69 } else {
70 m_filePath = destinationDir("remote") + m_filePath;
71 }
72
73 const QString file = m_filePath + QDir::separator() + QLatin1String(".directory");
74 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
75
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");
85 } else {
86 // The global view-properties act as default for directories without
87 // any view-property configuration
88 settings->setGlobalViewProps(true);
89
90 ViewProperties defaultProps(url);
91 setDirProperties(defaultProps);
92
93 settings->setGlobalViewProps(false);
94 m_changedProps = false;
95 }
96 }
97 }
98
99 ViewProperties::~ViewProperties()
100 {
101 if (m_changedProps && m_autoSave) {
102 save();
103 }
104
105 delete m_node;
106 m_node = 0;
107 }
108
109 void ViewProperties::setViewMode(DolphinView::Mode mode)
110 {
111 if (m_node->viewMode() != mode) {
112 m_node->setViewMode(mode);
113 update();
114 }
115 }
116
117 DolphinView::Mode ViewProperties::viewMode() const
118 {
119 const int mode = qBound(0, m_node->viewMode(), 2);
120 return static_cast<DolphinView::Mode>(mode);
121 }
122
123 void ViewProperties::setPreviewsShown(bool show)
124 {
125 if (m_node->previewsShown() != show) {
126 m_node->setPreviewsShown(show);
127 update();
128 }
129 }
130
131 bool ViewProperties::previewsShown() const
132 {
133 return m_node->previewsShown();
134 }
135
136 void ViewProperties::setHiddenFilesShown(bool show)
137 {
138 if (m_node->hiddenFilesShown() != show) {
139 m_node->setHiddenFilesShown(show);
140 update();
141 }
142 }
143
144 void ViewProperties::setGroupedSorting(bool grouped)
145 {
146 if (m_node->groupedSorting() != grouped) {
147 m_node->setGroupedSorting(grouped);
148 update();
149 }
150 }
151
152 bool ViewProperties::groupedSorting() const
153 {
154 return m_node->groupedSorting();
155 }
156
157 bool ViewProperties::hiddenFilesShown() const
158 {
159 return m_node->hiddenFilesShown();
160 }
161
162 void ViewProperties::setSortRole(const QByteArray& role)
163 {
164 if (m_node->sortRole() != role) {
165 m_node->setSortRole(role);
166 update();
167 }
168 }
169
170 QByteArray ViewProperties::sortRole() const
171 {
172 return m_node->sortRole().toLatin1();
173 }
174
175 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
176 {
177 if (m_node->sortOrder() != sortOrder) {
178 m_node->setSortOrder(sortOrder);
179 update();
180 }
181 }
182
183 Qt::SortOrder ViewProperties::sortOrder() const
184 {
185 return static_cast<Qt::SortOrder>(m_node->sortOrder());
186 }
187
188 void ViewProperties::setSortFoldersFirst(bool foldersFirst)
189 {
190 if (m_node->sortFoldersFirst() != foldersFirst) {
191 m_node->setSortFoldersFirst(foldersFirst);
192 update();
193 }
194 }
195
196 bool ViewProperties::sortFoldersFirst() const
197 {
198 return m_node->sortFoldersFirst();
199 }
200
201 void ViewProperties::setVisibleRoles(const QList<QByteArray>& roles)
202 {
203 // See ViewProperties::visibleRoles() for the storage format
204 // of the additional information.
205
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);
213 }
214 }
215
216 // Add the updated values for the current view-mode
217 foreach (const QByteArray& role, roles) {
218 newVisibleRoles.append(prefix + role);
219 }
220
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);
229 }
230
231 m_node->setVisibleRoles(newVisibleRoles);
232 update();
233 }
234 }
235
236 QList<QByteArray> ViewProperties::visibleRoles() const
237 {
238 // The shown additional information is stored for each view-mode separately as
239 // string with the view-mode as prefix. Example:
240 //
241 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
242 //
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.
245 //
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
250 // is accepted.
251
252 QList<QByteArray> roles;
253 roles.append("name");
254
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") {
264 roles.append(role);
265 }
266 }
267 }
268
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");
277 }
278
279 return roles;
280 }
281
282 void ViewProperties::setDirProperties(const ViewProperties& props)
283 {
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());
292 }
293
294 void ViewProperties::setAutoSaveEnabled(bool autoSave)
295 {
296 m_autoSave = autoSave;
297 }
298
299 bool ViewProperties::isAutoSaveEnabled() const
300 {
301 return m_autoSave;
302 }
303
304 void ViewProperties::update()
305 {
306 m_changedProps = true;
307 m_node->setTimestamp(QDateTime::currentDateTime());
308 }
309
310 void ViewProperties::save()
311 {
312 KStandardDirs::makeDir(m_filePath);
313 m_node->writeConfig();
314 m_changedProps = false;
315 }
316
317 KUrl ViewProperties::mirroredDirectory()
318 {
319 QString basePath = KGlobal::mainComponent().componentName();
320 basePath.append("/view_properties/");
321 return KUrl(KStandardDirs::locateLocal("data", basePath));
322 }
323
324 QString ViewProperties::destinationDir(const QString& subDir) const
325 {
326 QString basePath = KGlobal::mainComponent().componentName();
327 basePath.append("/view_properties/").append(subDir);
328 return KStandardDirs::locateLocal("data", basePath);
329 }
330
331 QString ViewProperties::viewModePrefix() const
332 {
333 QString prefix;
334
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";
340 }
341
342 return prefix;
343 }
344
345 bool ViewProperties::isPartOfHome(const QString& filePath)
346 {
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());
353 }
354
355 return filePath.startsWith(homePath);
356 }