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