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