]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewproperties.cpp
Krazy fixes
[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 "dolphin_directoryviewpropertysettings.h"
24 #include "dolphin_generalsettings.h"
25
26 #include <KComponentData>
27 #include <KLocale>
28 #include <KStandardDirs>
29 #include <KUrl>
30
31 #include <QCryptographicHash>
32 #include <QDate>
33 #include <QFile>
34 #include <QFileInfo>
35
36 namespace {
37 const int AdditionalInfoViewPropertiesVersion = 1;
38 const int NameRolePropertiesVersion = 2;
39 const int CurrentViewPropertiesVersion = 3;
40
41 // String representation to mark the additional properties of
42 // the details view as customized by the user. See
43 // ViewProperties::visibleRoles() for more information.
44 const char* CustomizedDetailsString = "CustomizedDetails";
45
46 // Filename that is used for storing the properties
47 const char* ViewPropertiesFileName = ".directory";
48 }
49
50 ViewProperties::ViewProperties(const KUrl& url) :
51 m_changedProps(false),
52 m_autoSave(true),
53 m_node(0)
54 {
55 GeneralSettings* settings = GeneralSettings::self();
56 const bool useGlobalViewProps = settings->globalViewProps();
57 bool useDetailsViewWithPath = false;
58
59 // We try and save it to the file .directory in the directory being viewed.
60 // If the directory is not writable by the user or the directory is not local,
61 // we store the properties information in a local file.
62 if (useGlobalViewProps) {
63 m_filePath = destinationDir("global");
64 } else if (url.protocol().contains("search")) {
65 m_filePath = destinationDir("search/") + directoryHashForUrl(url);
66 useDetailsViewWithPath = true;
67 } else if (url.protocol() == QLatin1String("trash")) {
68 m_filePath = destinationDir("trash");
69 useDetailsViewWithPath = true;
70 } else if (url.isLocalFile()) {
71 m_filePath = url.toLocalFile();
72 const QFileInfo info(m_filePath);
73 if (!info.isWritable() || !isPartOfHome(m_filePath)) {
74 #ifdef Q_OS_WIN
75 // m_filePath probably begins with C:/ - the colon is not a valid character for paths though
76 m_filePath = QDir::separator() + m_filePath.remove(QLatin1Char(':'));
77 #endif
78 m_filePath = destinationDir("local") + m_filePath;
79 }
80 } else {
81 m_filePath = destinationDir("remote") + m_filePath;
82 }
83
84 const QString file = m_filePath + QDir::separator() + ViewPropertiesFileName;
85 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
86
87 // If the .directory file does not exist or the timestamp is too old,
88 // use default values instead.
89 const bool useDefaultProps = (!useGlobalViewProps || useDetailsViewWithPath) &&
90 (!QFile::exists(file) ||
91 (m_node->timestamp() < settings->viewPropsTimestamp()));
92 if (useDefaultProps) {
93 if (useDetailsViewWithPath) {
94 setViewMode(DolphinView::DetailsView);
95 setVisibleRoles(QList<QByteArray>() << "path");
96 } else {
97 // The global view-properties act as default for directories without
98 // any view-property configuration
99 settings->setGlobalViewProps(true);
100
101 ViewProperties defaultProps(url);
102 setDirProperties(defaultProps);
103
104 settings->setGlobalViewProps(false);
105 m_changedProps = false;
106 }
107 }
108
109 if (m_node->version() < CurrentViewPropertiesVersion) {
110 // The view-properties have an outdated version. Convert the properties
111 // to the changes of the current version.
112 if (m_node->version() < AdditionalInfoViewPropertiesVersion) {
113 convertAdditionalInfo();
114 Q_ASSERT(m_node->version() == AdditionalInfoViewPropertiesVersion);
115 }
116
117 if (m_node->version() < NameRolePropertiesVersion) {
118 convertNameRoleToTextRole();
119 Q_ASSERT(m_node->version() == NameRolePropertiesVersion);
120 }
121
122 m_node->setVersion(CurrentViewPropertiesVersion);
123 }
124 }
125
126 ViewProperties::~ViewProperties()
127 {
128 if (m_changedProps && m_autoSave) {
129 save();
130 }
131
132 delete m_node;
133 m_node = 0;
134 }
135
136 void ViewProperties::setViewMode(DolphinView::Mode mode)
137 {
138 if (m_node->viewMode() != mode) {
139 m_node->setViewMode(mode);
140 update();
141 }
142 }
143
144 DolphinView::Mode ViewProperties::viewMode() const
145 {
146 const int mode = qBound(0, m_node->viewMode(), 2);
147 return static_cast<DolphinView::Mode>(mode);
148 }
149
150 void ViewProperties::setPreviewsShown(bool show)
151 {
152 if (m_node->previewsShown() != show) {
153 m_node->setPreviewsShown(show);
154 update();
155 }
156 }
157
158 bool ViewProperties::previewsShown() const
159 {
160 return m_node->previewsShown();
161 }
162
163 void ViewProperties::setHiddenFilesShown(bool show)
164 {
165 if (m_node->hiddenFilesShown() != show) {
166 m_node->setHiddenFilesShown(show);
167 update();
168 }
169 }
170
171 void ViewProperties::setGroupedSorting(bool grouped)
172 {
173 if (m_node->groupedSorting() != grouped) {
174 m_node->setGroupedSorting(grouped);
175 update();
176 }
177 }
178
179 bool ViewProperties::groupedSorting() const
180 {
181 return m_node->groupedSorting();
182 }
183
184 bool ViewProperties::hiddenFilesShown() const
185 {
186 return m_node->hiddenFilesShown();
187 }
188
189 void ViewProperties::setSortRole(const QByteArray& role)
190 {
191 if (m_node->sortRole() != role) {
192 m_node->setSortRole(role);
193 update();
194 }
195 }
196
197 QByteArray ViewProperties::sortRole() const
198 {
199 return m_node->sortRole().toLatin1();
200 }
201
202 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
203 {
204 if (m_node->sortOrder() != sortOrder) {
205 m_node->setSortOrder(sortOrder);
206 update();
207 }
208 }
209
210 Qt::SortOrder ViewProperties::sortOrder() const
211 {
212 return static_cast<Qt::SortOrder>(m_node->sortOrder());
213 }
214
215 void ViewProperties::setSortFoldersFirst(bool foldersFirst)
216 {
217 if (m_node->sortFoldersFirst() != foldersFirst) {
218 m_node->setSortFoldersFirst(foldersFirst);
219 update();
220 }
221 }
222
223 bool ViewProperties::sortFoldersFirst() const
224 {
225 return m_node->sortFoldersFirst();
226 }
227
228 void ViewProperties::setVisibleRoles(const QList<QByteArray>& roles)
229 {
230 if (roles == visibleRoles()) {
231 return;
232 }
233
234 // See ViewProperties::visibleRoles() for the storage format
235 // of the additional information.
236
237 // Remove the old values stored for the current view-mode
238 const QStringList oldVisibleRoles = m_node->visibleRoles();
239 const QString prefix = viewModePrefix();
240 QStringList newVisibleRoles = oldVisibleRoles;
241 for (int i = newVisibleRoles.count() - 1; i >= 0; --i) {
242 if (newVisibleRoles[i].startsWith(prefix)) {
243 newVisibleRoles.removeAt(i);
244 }
245 }
246
247 // Add the updated values for the current view-mode
248 foreach (const QByteArray& role, roles) {
249 newVisibleRoles.append(prefix + role);
250 }
251
252 if (oldVisibleRoles != newVisibleRoles) {
253 const bool markCustomizedDetails = (m_node->viewMode() == DolphinView::DetailsView)
254 && !newVisibleRoles.contains(CustomizedDetailsString);
255 if (markCustomizedDetails) {
256 // The additional information of the details-view has been modified. Set a marker,
257 // so that it is allowed to also show no additional information without doing the
258 // fallback to show the size and date per default.
259 newVisibleRoles.append(CustomizedDetailsString);
260 }
261
262 m_node->setVisibleRoles(newVisibleRoles);
263 update();
264 }
265 }
266
267 QList<QByteArray> ViewProperties::visibleRoles() const
268 {
269 // The shown additional information is stored for each view-mode separately as
270 // string with the view-mode as prefix. Example:
271 //
272 // AdditionalInfo=Details_size,Details_date,Details_owner,Icons_size
273 //
274 // To get the representation as QList<QByteArray>, the current
275 // view-mode must be checked and the values of this mode added to the list.
276 //
277 // For the details-view a special case must be respected: Per default the size
278 // and date should be shown without creating a .directory file. Only if
279 // the user explictly has modified the properties of the details view (marked
280 // by "CustomizedDetails"), also a details-view with no additional information
281 // is accepted.
282
283 QList<QByteArray> roles;
284 roles.append("text");
285
286 // Iterate through all stored keys and append all roles that match to
287 // the current view mode.
288 const QString prefix = viewModePrefix();
289 const int prefixLength = prefix.length();
290
291 const QStringList visibleRoles = m_node->visibleRoles();
292 foreach (const QString& visibleRole, visibleRoles) {
293 if (visibleRole.startsWith(prefix)) {
294 const QByteArray role = visibleRole.right(visibleRole.length() - prefixLength).toLatin1();
295 if (role != "text") {
296 roles.append(role);
297 }
298 }
299 }
300
301 // For the details view the size and date should be shown per default
302 // until the additional information has been explicitly changed by the user
303 const bool useDefaultValues = roles.count() == 1 // "text"
304 && (m_node->viewMode() == DolphinView::DetailsView)
305 && !visibleRoles.contains(CustomizedDetailsString);
306 if (useDefaultValues) {
307 roles.append("size");
308 roles.append("date");
309 }
310
311 return roles;
312 }
313
314 void ViewProperties::setHeaderColumnWidths(const QList<int>& widths)
315 {
316 if (m_node->headerColumnWidths() != widths) {
317 m_node->setHeaderColumnWidths(widths);
318 update();
319 }
320 }
321
322 QList<int> ViewProperties::headerColumnWidths() const
323 {
324 return m_node->headerColumnWidths();
325 }
326
327 void ViewProperties::setDirProperties(const ViewProperties& props)
328 {
329 setViewMode(props.viewMode());
330 setPreviewsShown(props.previewsShown());
331 setHiddenFilesShown(props.hiddenFilesShown());
332 setGroupedSorting(props.groupedSorting());
333 setSortRole(props.sortRole());
334 setSortOrder(props.sortOrder());
335 setSortFoldersFirst(props.sortFoldersFirst());
336 setVisibleRoles(props.visibleRoles());
337 setHeaderColumnWidths(props.headerColumnWidths());
338 m_node->setVersion(props.m_node->version());
339 }
340
341 void ViewProperties::setAutoSaveEnabled(bool autoSave)
342 {
343 m_autoSave = autoSave;
344 }
345
346 bool ViewProperties::isAutoSaveEnabled() const
347 {
348 return m_autoSave;
349 }
350
351 void ViewProperties::update()
352 {
353 m_changedProps = true;
354 m_node->setTimestamp(QDateTime::currentDateTime());
355 }
356
357 void ViewProperties::save()
358 {
359 kDebug() << "Saving view-properties to" << m_filePath;
360 KStandardDirs::makeDir(m_filePath);
361 m_node->setVersion(CurrentViewPropertiesVersion);
362 m_node->writeConfig();
363 m_changedProps = false;
364 }
365
366 bool ViewProperties::exist() const
367 {
368 const QString file = m_filePath + QDir::separator() + ViewPropertiesFileName;
369 return QFile::exists(file);
370 }
371
372 QString ViewProperties::destinationDir(const QString& subDir) const
373 {
374 QString basePath = KGlobal::mainComponent().componentName();
375 basePath.append("/view_properties/").append(subDir);
376 return KStandardDirs::locateLocal("data", basePath);
377 }
378
379 QString ViewProperties::viewModePrefix() const
380 {
381 QString prefix;
382
383 switch (m_node->viewMode()) {
384 case DolphinView::IconsView: prefix = "Icons_"; break;
385 case DolphinView::CompactView: prefix = "Compact_"; break;
386 case DolphinView::DetailsView: prefix = "Details_"; break;
387 default: kWarning() << "Unknown view-mode of the view properties";
388 }
389
390 return prefix;
391 }
392
393 void ViewProperties::convertAdditionalInfo()
394 {
395 QStringList visibleRoles;
396
397 const QStringList additionalInfo = m_node->additionalInfo();
398 if (!additionalInfo.isEmpty()) {
399 // Convert the obsolete values like Icons_Size, Details_Date, ...
400 // to Icons_size, Details_date, ... where the suffix just represents
401 // the internal role. One special-case must be handled: "LinkDestination"
402 // has been used for "destination".
403 visibleRoles.reserve(additionalInfo.count());
404 foreach (const QString& info, additionalInfo) {
405 QString visibleRole = info;
406 int index = visibleRole.indexOf('_');
407 if (index >= 0 && index + 1 < visibleRole.length()) {
408 ++index;
409 if (visibleRole[index] == QLatin1Char('L')) {
410 visibleRole.replace("LinkDestination", "destination");
411 } else {
412 visibleRole[index] = visibleRole[index].toLower();
413 }
414 }
415 visibleRoles.append(visibleRole);
416 }
417 }
418
419 m_node->setAdditionalInfo(QStringList());
420 m_node->setVisibleRoles(visibleRoles);
421 m_node->setVersion(AdditionalInfoViewPropertiesVersion);
422 update();
423 }
424
425 void ViewProperties::convertNameRoleToTextRole()
426 {
427 QStringList visibleRoles = m_node->visibleRoles();
428 for (int i = 0; i < visibleRoles.count(); ++i) {
429 if (visibleRoles[i].endsWith(QLatin1String("_name"))) {
430 const int leftLength = visibleRoles[i].length() - 5;
431 visibleRoles[i] = visibleRoles[i].left(leftLength) + "_text";
432 }
433 }
434
435 QString sortRole = m_node->sortRole();
436 if (sortRole == QLatin1String("name")) {
437 sortRole = QLatin1String("text");
438 }
439
440 m_node->setVisibleRoles(visibleRoles);
441 m_node->setSortRole(sortRole);
442 m_node->setVersion(NameRolePropertiesVersion);
443 update();
444 }
445
446 bool ViewProperties::isPartOfHome(const QString& filePath)
447 {
448 // For performance reasons cache the path in a static QString
449 // (see QDir::homePath() for more details)
450 static QString homePath;
451 if (homePath.isEmpty()) {
452 homePath = QDir::homePath();
453 Q_ASSERT(!homePath.isEmpty());
454 }
455
456 return filePath.startsWith(homePath);
457 }
458
459 QString ViewProperties::directoryHashForUrl(const KUrl& url)
460 {
461 const QByteArray hashValue = QCryptographicHash::hash(url.prettyUrl().toLatin1(),
462 QCryptographicHash::Sha1);
463 QString hashString = hashValue.toBase64();
464 hashString.replace('/', '-');
465 return hashString;
466 }
467
468 KUrl ViewProperties::mirroredDirectory()
469 {
470 QString basePath = KGlobal::mainComponent().componentName();
471 basePath.append("/view_properties/");
472 return KUrl(KStandardDirs::locateLocal("data", basePath));
473 }