]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/viewproperties.cpp
36ec6bdb087857531147b613cf115f0a5925cf7c
[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 namespace {
37 // String representation to mark the additional properties of
38 // the details view as customized by the user. See
39 // ViewProperties::additionalInfoList() for more information.
40 const char* CustomizedDetailsString = "CustomizedDetails";
41 }
42
43 ViewProperties::ViewProperties(const KUrl& url) :
44 m_changedProps(false),
45 m_autoSave(true),
46 m_node(0)
47 {
48 GeneralSettings* settings = GeneralSettings::self();
49 const bool useGlobalViewProps = settings->globalViewProps();
50 bool useDetailsViewWithPath = false;
51
52 // We try and save it to the file .directory in the directory being viewed.
53 // If the directory is not writable by the user or the directory is not local,
54 // we store the properties information in a local file.
55 if (useGlobalViewProps) {
56 m_filePath = destinationDir("global");
57 } else if (url.protocol().contains("search")) {
58 m_filePath = destinationDir("search");
59 useDetailsViewWithPath = true;
60 } else if (url.protocol() == QLatin1String("trash")) {
61 m_filePath = destinationDir("trash");
62 useDetailsViewWithPath = true;
63 } else if (url.isLocalFile()) {
64 m_filePath = url.toLocalFile();
65 const QFileInfo info(m_filePath);
66 if (!info.isWritable() || !isPartOfHome(m_filePath)) {
67 m_filePath = destinationDir("local") + m_filePath;
68 }
69 } else {
70 m_filePath = destinationDir("remote") + m_filePath;
71 }
72
73 const QString file = m_filePath + QDir::separator() + QLatin1String(".directory");
74 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
75
76 // If the .directory file does not exist or the timestamp is too old,
77 // use default values instead.
78 const bool useDefaultProps = (!useGlobalViewProps || useDetailsViewWithPath) &&
79 (!QFileInfo(file).exists() ||
80 (m_node->timestamp() < settings->viewPropsTimestamp()));
81 if (useDefaultProps) {
82 if (useDetailsViewWithPath) {
83 setViewMode(DolphinView::DetailsView);
84 setAdditionalInfoList(QList<DolphinView::AdditionalInfo>() << DolphinView::PathInfo);
85 } else {
86 // The global view-properties act as default for directories without
87 // any view-property configuration
88 settings->setGlobalViewProps(true);
89
90 ViewProperties defaultProps(url);
91 setDirProperties(defaultProps);
92
93 settings->setGlobalViewProps(false);
94 m_changedProps = false;
95 }
96 }
97 }
98
99 ViewProperties::~ViewProperties()
100 {
101 if (m_changedProps && m_autoSave) {
102 save();
103 }
104
105 delete m_node;
106 m_node = 0;
107 }
108
109 void ViewProperties::setViewMode(DolphinView::Mode mode)
110 {
111 if (m_node->viewMode() != mode) {
112 m_node->setViewMode(mode);
113 update();
114 }
115 }
116
117 DolphinView::Mode ViewProperties::viewMode() const
118 {
119 const int mode = qBound(0, m_node->viewMode(), 2);
120 return static_cast<DolphinView::Mode>(mode);
121 }
122
123 void ViewProperties::setPreviewsShown(bool show)
124 {
125 if (m_node->previewsShown() != show) {
126 m_node->setPreviewsShown(show);
127 update();
128 }
129 }
130
131 bool ViewProperties::previewsShown() const
132 {
133 return m_node->previewsShown();
134 }
135
136 void ViewProperties::setHiddenFilesShown(bool show)
137 {
138 if (m_node->hiddenFilesShown() != show) {
139 m_node->setHiddenFilesShown(show);
140 update();
141 }
142 }
143
144 void ViewProperties::setGroupedSorting(bool grouped)
145 {
146 if (m_node->groupedSorting() != grouped) {
147 m_node->setGroupedSorting(grouped);
148 update();
149 }
150 }
151
152 bool ViewProperties::groupedSorting() const
153 {
154 return m_node->groupedSorting();
155 }
156
157 bool ViewProperties::hiddenFilesShown() const
158 {
159 return m_node->hiddenFilesShown();
160 }
161
162 void ViewProperties::setSorting(DolphinView::Sorting sorting)
163 {
164 if (m_node->sorting() != sorting) {
165 m_node->setSorting(sorting);
166 update();
167 }
168 }
169
170 DolphinView::Sorting ViewProperties::sorting() const
171 {
172 return static_cast<DolphinView::Sorting>(m_node->sorting());
173 }
174
175 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
176 {
177 if (m_node->sortOrder() != sortOrder) {
178 m_node->setSortOrder(sortOrder);
179 update();
180 }
181 }
182
183 Qt::SortOrder ViewProperties::sortOrder() const
184 {
185 return static_cast<Qt::SortOrder>(m_node->sortOrder());
186 }
187
188 void ViewProperties::setSortFoldersFirst(bool foldersFirst)
189 {
190 if (m_node->sortFoldersFirst() != foldersFirst) {
191 m_node->setSortFoldersFirst(foldersFirst);
192 update();
193 }
194 }
195
196 bool ViewProperties::sortFoldersFirst() const
197 {
198 return m_node->sortFoldersFirst();
199 }
200
201 void ViewProperties::setAdditionalInfoList(const QList<DolphinView::AdditionalInfo>& list)
202 {
203 // See ViewProperties::additionalInfoList() for the storage format
204 // of the additional information.
205
206 // Remove the old values stored for the current view-mode
207 const QStringList oldInfoStringList = m_node->additionalInfo();
208 const QString prefix = viewModePrefix();
209 QStringList newInfoStringList = oldInfoStringList;
210 for (int i = newInfoStringList.count() - 1; i >= 0; --i) {
211 if (newInfoStringList.at(i).startsWith(prefix)) {
212 newInfoStringList.removeAt(i);
213 }
214 }
215
216 // Add the updated values for the current view-mode
217 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
218 foreach (DolphinView::AdditionalInfo info, list) {
219 newInfoStringList.append(prefix + infoAccessor.value(info));
220 }
221
222 if (oldInfoStringList != newInfoStringList) {
223 const bool markCustomizedDetails = (m_node->viewMode() == DolphinView::DetailsView)
224 && !newInfoStringList.contains(CustomizedDetailsString);
225 if (markCustomizedDetails) {
226 // The additional information of the details-view has been modified. Set a marker,
227 // so that it is allowed to also show no additional information
228 // (see fallback in ViewProperties::additionalInfoV2, if no additional information is
229 // available).
230 newInfoStringList.append(CustomizedDetailsString);
231 }
232
233 m_node->setAdditionalInfo(newInfoStringList);
234 update();
235 }
236 }
237
238 QList<DolphinView::AdditionalInfo> ViewProperties::additionalInfoList() 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,Icon_Size
244 //
245 // To get the representation as QList<DolphinView::AdditionalInfo>, 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<DolphinView::AdditionalInfo> usedInfo;
255
256 // infoHash allows to get the mapped DolphinView::AdditionalInfo value
257 // for a stored string-value in a fast way
258 static QHash<QString, DolphinView::AdditionalInfo> infoHash;
259 if (infoHash.isEmpty()) {
260 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
261 const QList<DolphinView::AdditionalInfo> keys = infoAccessor.keys();
262 foreach (DolphinView::AdditionalInfo key, keys) {
263 infoHash.insert(infoAccessor.value(key), key);
264 }
265 }
266
267 // Iterate through all stored keys stored as strings and map them to
268 // the corresponding DolphinView::AdditionalInfo values.
269 const QString prefix = viewModePrefix();
270 const int prefixLength = prefix.length();
271 const QStringList infoStringList = m_node->additionalInfo();
272 foreach (const QString& infoString, infoStringList) {
273 if (infoString.startsWith(prefix)) {
274 const QString key = infoString.right(infoString.length() - prefixLength);
275 if (infoHash.contains(key)) {
276 usedInfo.append(infoHash.value(key));
277 } else {
278 kWarning() << "Did not find the key" << key << "in the information string";
279 }
280 }
281 }
282
283 // For the details view the size and date should be shown per default
284 // until the additional information has been explicitly changed by the user
285 const bool useDefaultValues = usedInfo.isEmpty()
286 && (m_node->viewMode() == DolphinView::DetailsView)
287 && !infoStringList.contains(CustomizedDetailsString);
288 Q_UNUSED(useDefaultValues);
289 if (useDefaultValues) {
290 usedInfo.append(DolphinView::SizeInfo);
291 usedInfo.append(DolphinView::DateInfo);
292 }
293
294 return usedInfo;
295 }
296
297 void ViewProperties::setDirProperties(const ViewProperties& props)
298 {
299 setViewMode(props.viewMode());
300 setPreviewsShown(props.previewsShown());
301 setHiddenFilesShown(props.hiddenFilesShown());
302 setGroupedSorting(props.groupedSorting());
303 setSorting(props.sorting());
304 setSortOrder(props.sortOrder());
305 setSortFoldersFirst(props.sortFoldersFirst());
306 setAdditionalInfoList(props.additionalInfoList());
307 }
308
309 void ViewProperties::setAutoSaveEnabled(bool autoSave)
310 {
311 m_autoSave = autoSave;
312 }
313
314 bool ViewProperties::isAutoSaveEnabled() const
315 {
316 return m_autoSave;
317 }
318
319 void ViewProperties::update()
320 {
321 m_changedProps = true;
322 m_node->setTimestamp(QDateTime::currentDateTime());
323 }
324
325 void ViewProperties::save()
326 {
327 KStandardDirs::makeDir(m_filePath);
328 m_node->writeConfig();
329 m_changedProps = false;
330 }
331
332 KUrl ViewProperties::mirroredDirectory()
333 {
334 QString basePath = KGlobal::mainComponent().componentName();
335 basePath.append("/view_properties/");
336 return KUrl(KStandardDirs::locateLocal("data", basePath));
337 }
338
339 QString ViewProperties::destinationDir(const QString& subDir) const
340 {
341 QString basePath = KGlobal::mainComponent().componentName();
342 basePath.append("/view_properties/").append(subDir);
343 return KStandardDirs::locateLocal("data", basePath);
344 }
345
346 QString ViewProperties::viewModePrefix() const
347 {
348 QString prefix;
349
350 switch (m_node->viewMode()) {
351 case DolphinView::IconsView: prefix = "Icons_"; break;
352 case DolphinView::CompactView: prefix = "Compact_"; break;
353 case DolphinView::DetailsView: prefix = "Details_"; break;
354 default: kWarning() << "Unknown view-mode of the view properties";
355 }
356
357 return prefix;
358 }
359
360 bool ViewProperties::isPartOfHome(const QString& filePath)
361 {
362 // For performance reasons cache the path in a static QString
363 // (see QDir::homePath() for more details)
364 static QString homePath;
365 if (homePath.isEmpty()) {
366 homePath = QDir::homePath();
367 Q_ASSERT(!homePath.isEmpty());
368 }
369
370 return filePath.startsWith(homePath);
371 }