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