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