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