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