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