]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
Disable the automatic error handling in the DolphinDirLister, the error message is...
[dolphin.git] / src / viewproperties.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (<peter.penz@gmx.at>) *
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 "settings/dolphinsettings.h"
25 #include "dolphin_directoryviewpropertysettings.h"
26 #include "dolphin_generalsettings.h"
27
28 #include <kcomponentdata.h>
29 #include <klocale.h>
30 #include <kstandarddirs.h>
31 #include <kurl.h>
32
33 #include <QDate>
34 #include <QFile>
35 #include <QFileInfo>
36
37 #include <kdebug.h>
38
39 ViewProperties::ViewProperties(const KUrl& url) :
40 m_changedProps(false),
41 m_autoSave(true),
42 m_node(0)
43 {
44 KUrl cleanUrl(url);
45 cleanUrl.cleanPath();
46 m_filepath = cleanUrl.toLocalFile();
47
48 const QLatin1String fileName("/.directory");
49
50 if ((m_filepath.length() < 1) || !QDir::isAbsolutePath(m_filepath)) {
51 kDebug() << "---- using global path: global/.directory";
52 const QString file = destinationDir("global") + fileName;
53 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
54 return;
55 }
56
57 // We try and save it to a file in the directory being viewed.
58 // If the directory is not writable by the user or the directory is not local,
59 // we store the properties information in a local file.
60 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
61 const bool useGlobalViewProps = settings->globalViewProps();
62 if (useGlobalViewProps) {
63 m_filepath = destinationDir("global");
64 } else if (cleanUrl.isLocalFile()) {
65 const QFileInfo info(m_filepath);
66 if (!info.isWritable()) {
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 + fileName;
74 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
75
76 const bool useDefaultProps = !useGlobalViewProps &&
77 (!QFileInfo(file).exists() ||
78 (m_node->timestamp() < settings->viewPropsTimestamp()));
79 if (useDefaultProps) {
80 // If the .directory file does not exist or the timestamp is too old,
81 // use the values from the global .directory file instead, which acts
82 // as default view for new folders in this case.
83 settings->setGlobalViewProps(true);
84
85 ViewProperties defaultProps(url);
86 setDirProperties(defaultProps);
87
88 settings->setGlobalViewProps(false);
89 m_changedProps = false;
90 }
91 }
92
93 ViewProperties::~ViewProperties()
94 {
95 if (m_changedProps && m_autoSave) {
96 save();
97 }
98
99 delete m_node;
100 m_node = 0;
101 }
102
103 void ViewProperties::setViewMode(DolphinView::Mode mode)
104 {
105 if (m_node->viewMode() != mode) {
106 m_node->setViewMode(mode);
107 updateTimeStamp();
108 }
109 }
110
111 DolphinView::Mode ViewProperties::viewMode() const
112 {
113 return static_cast<DolphinView::Mode>(m_node->viewMode());
114 }
115
116 void ViewProperties::setShowPreview(bool show)
117 {
118 if (m_node->showPreview() != show) {
119 m_node->setShowPreview(show);
120 updateTimeStamp();
121 }
122 }
123
124 bool ViewProperties::showPreview() const
125 {
126 return m_node->showPreview();
127 }
128
129
130 void ViewProperties::setShowHiddenFiles(bool show)
131 {
132 if (m_node->showHiddenFiles() != show) {
133 m_node->setShowHiddenFiles(show);
134 updateTimeStamp();
135 }
136 }
137
138 void ViewProperties::setCategorizedSorting(bool categorized)
139 {
140 if (m_node->categorizedSorting() != categorized) {
141 m_node->setCategorizedSorting(categorized);
142 updateTimeStamp();
143 }
144 }
145
146 bool ViewProperties::categorizedSorting() const
147 {
148 return m_node->categorizedSorting();
149 }
150
151
152 bool ViewProperties::showHiddenFiles() const
153 {
154 return m_node->showHiddenFiles();
155 }
156
157 void ViewProperties::setSorting(DolphinView::Sorting sorting)
158 {
159 if (m_node->sorting() != sorting) {
160 m_node->setSorting(sorting);
161 updateTimeStamp();
162 }
163 }
164
165 DolphinView::Sorting ViewProperties::sorting() const
166 {
167 return static_cast<DolphinView::Sorting>(m_node->sorting());
168 }
169
170 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
171 {
172 if (m_node->sortOrder() != sortOrder) {
173 m_node->setSortOrder(sortOrder);
174 updateTimeStamp();
175 }
176 }
177
178 Qt::SortOrder ViewProperties::sortOrder() const
179 {
180 return static_cast<Qt::SortOrder>(m_node->sortOrder());
181 }
182
183 void ViewProperties::setSortFoldersFirst(bool foldersFirst)
184 {
185 if (m_node->sortFoldersFirst() != foldersFirst) {
186 m_node->setSortFoldersFirst(foldersFirst);
187 updateTimeStamp();
188 }
189 }
190
191 bool ViewProperties::sortFoldersFirst() const
192 {
193 return m_node->sortFoldersFirst();
194 }
195
196 void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList& list)
197 {
198 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
199
200 int infoMask = 0;
201 foreach (KFileItemDelegate::Information currentInfo, list) {
202 infoMask = infoMask | infoAccessor.bitValue(currentInfo);
203 }
204
205 const int encodedInfo = encodedAdditionalInfo(infoMask);
206 if (m_node->additionalInfo() != encodedInfo) {
207 m_node->setAdditionalInfo(encodedInfo);
208 updateTimeStamp();
209 }
210 }
211
212 KFileItemDelegate::InformationList ViewProperties::additionalInfo() const
213 {
214 KFileItemDelegate::InformationList usedInfos;
215
216 const int decodedInfo = decodedAdditionalInfo();
217
218 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
219 const KFileItemDelegate::InformationList infos = infoAccessor.keys();
220
221 foreach (const KFileItemDelegate::Information info, infos) {
222 if (decodedInfo & infoAccessor.bitValue(info)) {
223 usedInfos.append(info);
224 }
225 }
226
227 return usedInfos;
228 }
229
230
231 void ViewProperties::setDirProperties(const ViewProperties& props)
232 {
233 setViewMode(props.viewMode());
234 setShowPreview(props.showPreview());
235 setShowHiddenFiles(props.showHiddenFiles());
236 setCategorizedSorting(props.categorizedSorting());
237 setSorting(props.sorting());
238 setSortOrder(props.sortOrder());
239 setSortFoldersFirst(props.sortFoldersFirst());
240 setAdditionalInfo(props.additionalInfo());
241 }
242
243 void ViewProperties::setAutoSaveEnabled(bool autoSave)
244 {
245 m_autoSave = autoSave;
246 }
247
248 bool ViewProperties::isAutoSaveEnabled() const
249 {
250 return m_autoSave;
251 }
252
253 void ViewProperties::updateTimeStamp()
254 {
255 m_changedProps = true;
256 m_node->setTimestamp(QDateTime::currentDateTime());
257 }
258
259 void ViewProperties::save()
260 {
261 KStandardDirs::makeDir(m_filepath);
262 m_node->writeConfig();
263 m_changedProps = false;
264 }
265
266 KUrl ViewProperties::mirroredDirectory()
267 {
268 QString basePath = KGlobal::mainComponent().componentName();
269 basePath.append("/view_properties/");
270 return KUrl(KStandardDirs::locateLocal("data", basePath));
271 }
272
273 QString ViewProperties::destinationDir(const QString& subDir) const
274 {
275 QString basePath = KGlobal::mainComponent().componentName();
276 basePath.append("/view_properties/").append(subDir);
277 return KStandardDirs::locateLocal("data", basePath);
278 }
279
280 int ViewProperties::encodedAdditionalInfo(int info) const
281 {
282 int encodedInfo = m_node->additionalInfo();
283
284 switch (viewMode()) {
285 case DolphinView::DetailsView:
286 encodedInfo = (encodedInfo & 0xFFFF00) | info;
287 break;
288 case DolphinView::IconsView:
289 encodedInfo = (encodedInfo & 0xFF00FF) | (info << 8);
290 break;
291 case DolphinView::ColumnView:
292 encodedInfo = (encodedInfo & 0x00FFFF) | (info << 16);
293 break;
294 default: break;
295 }
296
297 return encodedInfo;
298 }
299
300 int ViewProperties::decodedAdditionalInfo() const
301 {
302 int decodedInfo = m_node->additionalInfo();
303
304 switch (viewMode()) {
305 case DolphinView::DetailsView:
306 decodedInfo = decodedInfo & 0xFF;
307 if (decodedInfo == 0) {
308 // A details view without any additional info makes no sense, hence
309 // provide at least a size-info and date-info as fallback
310 AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
311 decodedInfo = infoAccessor.bitValue(KFileItemDelegate::Size) |
312 infoAccessor.bitValue(KFileItemDelegate::ModificationTime);
313 }
314 break;
315 case DolphinView::IconsView:
316 decodedInfo = (decodedInfo >> 8) & 0xFF;
317 break;
318 case DolphinView::ColumnView:
319 decodedInfo = (decodedInfo >> 16) & 0xFF;
320 break;
321 default: break;
322 }
323
324 return decodedInfo;
325 }