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