]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewproperties.cpp
Provide backward compatibility with older .directory versions
[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 const int CurrentViewPropertiesVersion = 2;
38 const int AdditionalInfoViewPropertiesVersion = 1;
39
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";
44 }
45
46 ViewProperties::ViewProperties(const KUrl& url) :
47 m_changedProps(false),
48 m_autoSave(true),
49 m_node(0)
50 {
51 GeneralSettings* settings = GeneralSettings::self();
52 const bool useGlobalViewProps = settings->globalViewProps();
53 bool useDetailsViewWithPath = false;
54
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;
71 }
72 } else {
73 m_filePath = destinationDir("remote") + m_filePath;
74 }
75
76 const QString file = m_filePath + QDir::separator() + QLatin1String(".directory");
77 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
78
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");
88 } else {
89 // The global view-properties act as default for directories without
90 // any view-property configuration
91 settings->setGlobalViewProps(true);
92
93 ViewProperties defaultProps(url);
94 setDirProperties(defaultProps);
95
96 settings->setGlobalViewProps(false);
97 m_changedProps = false;
98 }
99 }
100 }
101
102 ViewProperties::~ViewProperties()
103 {
104 if (m_changedProps && m_autoSave) {
105 save();
106 }
107
108 delete m_node;
109 m_node = 0;
110 }
111
112 void ViewProperties::setViewMode(DolphinView::Mode mode)
113 {
114 if (m_node->viewMode() != mode) {
115 m_node->setViewMode(mode);
116 update();
117 }
118 }
119
120 DolphinView::Mode ViewProperties::viewMode() const
121 {
122 const int mode = qBound(0, m_node->viewMode(), 2);
123 return static_cast<DolphinView::Mode>(mode);
124 }
125
126 void ViewProperties::setPreviewsShown(bool show)
127 {
128 if (m_node->previewsShown() != show) {
129 m_node->setPreviewsShown(show);
130 update();
131 }
132 }
133
134 bool ViewProperties::previewsShown() const
135 {
136 return m_node->previewsShown();
137 }
138
139 void ViewProperties::setHiddenFilesShown(bool show)
140 {
141 if (m_node->hiddenFilesShown() != show) {
142 m_node->setHiddenFilesShown(show);
143 update();
144 }
145 }
146
147 void ViewProperties::setGroupedSorting(bool grouped)
148 {
149 if (m_node->groupedSorting() != grouped) {
150 m_node->setGroupedSorting(grouped);
151 update();
152 }
153 }
154
155 bool ViewProperties::groupedSorting() const
156 {
157 return m_node->groupedSorting();
158 }
159
160 bool ViewProperties::hiddenFilesShown() const
161 {
162 return m_node->hiddenFilesShown();
163 }
164
165 void ViewProperties::setSortRole(const QByteArray& role)
166 {
167 if (m_node->sortRole() != role) {
168 m_node->setSortRole(role);
169 update();
170 }
171 }
172
173 QByteArray ViewProperties::sortRole() const
174 {
175 return m_node->sortRole().toLatin1();
176 }
177
178 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
179 {
180 if (m_node->sortOrder() != sortOrder) {
181 m_node->setSortOrder(sortOrder);
182 update();
183 }
184 }
185
186 Qt::SortOrder ViewProperties::sortOrder() const
187 {
188 return static_cast<Qt::SortOrder>(m_node->sortOrder());
189 }
190
191 void ViewProperties::setSortFoldersFirst(bool foldersFirst)
192 {
193 if (m_node->sortFoldersFirst() != foldersFirst) {
194 m_node->setSortFoldersFirst(foldersFirst);
195 update();
196 }
197 }
198
199 bool ViewProperties::sortFoldersFirst() const
200 {
201 return m_node->sortFoldersFirst();
202 }
203
204 void ViewProperties::setVisibleRoles(const QList<QByteArray>& roles)
205 {
206 // See ViewProperties::visibleRoles() for the storage format
207 // of the additional information.
208
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);
216 }
217 }
218
219 // Add the updated values for the current view-mode
220 foreach (const QByteArray& role, roles) {
221 newVisibleRoles.append(prefix + role);
222 }
223
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);
232 }
233
234 m_node->setVisibleRoles(newVisibleRoles);
235 update();
236 }
237 }
238
239 QList<QByteArray> ViewProperties::visibleRoles() const
240 {
241 // The shown additional information is stored for each view-mode separately as
242 // string with the view-mode as prefix. Example:
243 //
244 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
245 //
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.
248 //
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
253 // is accepted.
254
255 QList<QByteArray> roles;
256 roles.append("name");
257
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();
262
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();
268 }
269
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") {
274 roles.append(role);
275 }
276 }
277 }
278
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");
287 }
288
289 return roles;
290 }
291
292 void ViewProperties::setDirProperties(const ViewProperties& props)
293 {
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());
302 }
303
304 void ViewProperties::setAutoSaveEnabled(bool autoSave)
305 {
306 m_autoSave = autoSave;
307 }
308
309 bool ViewProperties::isAutoSaveEnabled() const
310 {
311 return m_autoSave;
312 }
313
314 void ViewProperties::update()
315 {
316 m_changedProps = true;
317 m_node->setTimestamp(QDateTime::currentDateTime());
318 }
319
320 void ViewProperties::save()
321 {
322 KStandardDirs::makeDir(m_filePath);
323 m_node->setVersion(CurrentViewPropertiesVersion);
324 m_node->writeConfig();
325 m_changedProps = false;
326 }
327
328 KUrl ViewProperties::mirroredDirectory()
329 {
330 QString basePath = KGlobal::mainComponent().componentName();
331 basePath.append("/view_properties/");
332 return KUrl(KStandardDirs::locateLocal("data", basePath));
333 }
334
335 QString ViewProperties::destinationDir(const QString& subDir) const
336 {
337 QString basePath = KGlobal::mainComponent().componentName();
338 basePath.append("/view_properties/").append(subDir);
339 return KStandardDirs::locateLocal("data", basePath);
340 }
341
342 QString ViewProperties::viewModePrefix() const
343 {
344 QString prefix;
345
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";
351 }
352
353 return prefix;
354 }
355
356 QStringList ViewProperties::convertAdditionalInfo()
357 {
358 QStringList visibleRoles;
359
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()) {
371 ++index;
372 if (visibleRole[index] == QLatin1Char('L')) {
373 visibleRole.replace("LinkDestination", "destination");
374 } else {
375 visibleRole[index] = visibleRole[index].toLower();
376 }
377 }
378 visibleRoles.append(visibleRole);
379 }
380 }
381
382 m_node->setAdditionalInfo(QStringList());
383 m_node->setVisibleRoles(visibleRoles);
384 update();
385
386 return visibleRoles;
387 }
388
389 bool ViewProperties::isPartOfHome(const QString& filePath)
390 {
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());
397 }
398
399 return filePath.startsWith(homePath);
400 }