]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewproperties.cpp
e67cbd87a3f66555651cc2d980522dc816af8fd4
[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 "additionalinfoaccessor.h"
24 #include "dolphin_directoryviewpropertysettings.h"
25 #include "dolphin_generalsettings.h"
26
27 #include <KComponentData>
28 #include <KLocale>
29 #include <KStandardDirs>
30 #include <KUrl>
31
32 #include <QDate>
33 #include <QFile>
34 #include <QFileInfo>
35
36 #include "settings/dolphinsettings.h"
37
38 namespace {
39 // String representation to mark the additional properties of
40 // the details view as customized by the user. See
41 // ViewProperties::additionalInfoList() 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 = DolphinSettings::instance().generalSettings();
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 setAdditionalInfoList(QList<DolphinView::AdditionalInfo>() << DolphinView::PathInfo);
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::setCategorizedSorting(bool categorized)
147 {
148 if (m_node->categorizedSorting() != categorized) {
149 m_node->setCategorizedSorting(categorized);
150 update();
151 }
152 }
153
154 bool ViewProperties::categorizedSorting() const
155 {
156 return m_node->categorizedSorting();
157 }
158
159 bool ViewProperties::hiddenFilesShown() const
160 {
161 return m_node->hiddenFilesShown();
162 }
163
164 void ViewProperties::setSorting(DolphinView::Sorting sorting)
165 {
166 if (m_node->sorting() != sorting) {
167 m_node->setSorting(sorting);
168 update();
169 }
170 }
171
172 DolphinView::Sorting ViewProperties::sorting() const
173 {
174 return static_cast<DolphinView::Sorting>(m_node->sorting());
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::setAdditionalInfoList(const QList<DolphinView::AdditionalInfo>& list)
204 {
205 // See ViewProperties::additionalInfoList() for the storage format
206 // of the additional information.
207
208 // Remove the old values stored for the current view-mode
209 const QStringList oldInfoStringList = m_node->additionalInfo();
210 const QString prefix = viewModePrefix();
211 QStringList newInfoStringList = oldInfoStringList;
212 for (int i = newInfoStringList.count() - 1; i >= 0; --i) {
213 if (newInfoStringList.at(i).startsWith(prefix)) {
214 newInfoStringList.removeAt(i);
215 }
216 }
217
218 // Add the updated values for the current view-mode
219 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
220 foreach (DolphinView::AdditionalInfo info, list) {
221 newInfoStringList.append(prefix + infoAccessor.value(info));
222 }
223
224 // Only update the information if it has been changed
225 bool changed = oldInfoStringList.count() != newInfoStringList.count();
226 if (!changed) {
227 foreach (const QString& oldInfoString, oldInfoStringList) {
228 if (!newInfoStringList.contains(oldInfoString)) {
229 changed = true;
230 break;
231 }
232 }
233 }
234
235 if (changed) {
236 const bool markCustomizedDetails = (m_node->viewMode() == DolphinView::DetailsView)
237 && !newInfoStringList.contains(CustomizedDetailsString);
238 if (markCustomizedDetails) {
239 // The additional information of the details-view has been modified. Set a marker,
240 // so that it is allowed to also show no additional information
241 // (see fallback in ViewProperties::additionalInfoV2, if no additional information is
242 // available).
243 newInfoStringList.append(CustomizedDetailsString);
244 }
245
246 m_node->setAdditionalInfo(newInfoStringList);
247 update();
248 }
249 }
250
251 QList<DolphinView::AdditionalInfo> ViewProperties::additionalInfoList() const
252 {
253 // The shown additional information is stored for each view-mode separately as
254 // string with the view-mode as prefix. Example:
255 //
256 // AdditionalInfo=Details_Size,Details_Date,Details_Owner,Icon_Size
257 //
258 // To get the representation as QList<DolphinView::AdditionalInfo>, the current
259 // view-mode must be checked and the values of this mode added to the list.
260 //
261 // For the details-view a special case must be respected: Per default the size
262 // and date should be shown without creating a .directory file. Only if
263 // the user explictly has modified the properties of the details view (marked
264 // by "CustomizedDetails"), also a details-view with no additional information
265 // is accepted.
266
267 QList<DolphinView::AdditionalInfo> usedInfo;
268
269 // infoHash allows to get the mapped DolphinView::AdditionalInfo value
270 // for a stored string-value in a fast way
271 static QHash<QString, DolphinView::AdditionalInfo> infoHash;
272 if (infoHash.isEmpty()) {
273 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
274 const QList<DolphinView::AdditionalInfo> keys = infoAccessor.keys();
275 foreach (DolphinView::AdditionalInfo key, keys) {
276 infoHash.insert(infoAccessor.value(key), key);
277 }
278 }
279
280 // Iterate through all stored keys stored as strings and map them to
281 // the corresponding DolphinView::AdditionalInfo values.
282 const QString prefix = viewModePrefix();
283 const int prefixLength = prefix.length();
284 const QStringList infoStringList = m_node->additionalInfo();
285 foreach (const QString& infoString, infoStringList) {
286 if (infoString.startsWith(prefix)) {
287 const QString key = infoString.right(infoString.length() - prefixLength);
288 if (infoHash.contains(key)) {
289 usedInfo.append(infoHash.value(key));
290 } else {
291 kWarning() << "Did not find the key" << key << "in the information string";
292 }
293 }
294 }
295
296 // For the details view the size and date should be shown per default
297 // until the additional information has been explicitly changed by the user
298 const bool useDefaultValues = usedInfo.isEmpty()
299 && (m_node->viewMode() == DolphinView::DetailsView)
300 && !infoStringList.contains(CustomizedDetailsString);
301 Q_UNUSED(useDefaultValues);
302 if (useDefaultValues) {
303 usedInfo.append(DolphinView::SizeInfo);
304 usedInfo.append(DolphinView::DateInfo);
305 }
306
307 return usedInfo;
308 }
309
310 void ViewProperties::setDirProperties(const ViewProperties& props)
311 {
312 setViewMode(props.viewMode());
313 setPreviewsShown(props.previewsShown());
314 setHiddenFilesShown(props.hiddenFilesShown());
315 setCategorizedSorting(props.categorizedSorting());
316 setSorting(props.sorting());
317 setSortOrder(props.sortOrder());
318 setSortFoldersFirst(props.sortFoldersFirst());
319 setAdditionalInfoList(props.additionalInfoList());
320 }
321
322 void ViewProperties::setAutoSaveEnabled(bool autoSave)
323 {
324 m_autoSave = autoSave;
325 }
326
327 bool ViewProperties::isAutoSaveEnabled() const
328 {
329 return m_autoSave;
330 }
331
332 void ViewProperties::update()
333 {
334 m_changedProps = true;
335 m_node->setTimestamp(QDateTime::currentDateTime());
336 }
337
338 void ViewProperties::save()
339 {
340 KStandardDirs::makeDir(m_filePath);
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 bool ViewProperties::isPartOfHome(const QString& filePath)
374 {
375 // For performance reasons cache the path in a static QString
376 // (see QDir::homePath() for more details)
377 static QString homePath;
378 if (homePath.isEmpty()) {
379 homePath = QDir::homePath();
380 Q_ASSERT(!homePath.isEmpty());
381 }
382
383 return filePath.startsWith(homePath);
384 }