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