add more spacing
[personal-kdebase.git] / apps / dolphin / src / viewproperties.cpp
blob62898e67004d43bb25a24db5a2194885244d5e7a
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 ***************************************************************************/
21 #include "viewproperties.h"
23 #include "settings/dolphinsettings.h"
24 #include "dolphin_directoryviewpropertysettings.h"
25 #include "dolphin_generalsettings.h"
27 #include <kcomponentdata.h>
28 #include <klocale.h>
29 #include <kstandarddirs.h>
30 #include <kurl.h>
32 #ifdef HAVE_NEPOMUK
33 #include <nepomuk/resourcemanager.h>
34 #endif
36 #include <QDate>
37 #include <QFile>
38 #include <QFileInfo>
40 bool ViewProperties::m_nepomukSupport = false;
42 #define FILE_NAME "/.directory"
44 ViewProperties::ViewProperties(const KUrl& url) :
45 m_changedProps(false),
46 m_autoSave(true),
47 m_node(0)
49 #ifdef HAVE_NEPOMUK
50 static bool checkedNepomukSupport = false;
51 if (!checkedNepomukSupport) {
52 m_nepomukSupport = !Nepomuk::ResourceManager::instance()->init();
53 checkedNepomukSupport = true;
55 #endif
57 KUrl cleanUrl(url);
58 cleanUrl.cleanPath();
59 m_filepath = cleanUrl.path();
61 if ((m_filepath.length() < 1) || (m_filepath.at(0) != QChar('/'))) {
62 const QString file = destinationDir("global") + FILE_NAME;
63 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
64 return;
67 // We try and save it to a file in the directory being viewed.
68 // If the directory is not writable by the user or the directory is not local,
69 // we store the properties information in a local file.
70 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
71 const bool useGlobalViewProps = settings->globalViewProps();
72 if (useGlobalViewProps) {
73 m_filepath = destinationDir("global");
74 } else if (cleanUrl.isLocalFile()) {
75 const QFileInfo info(m_filepath);
76 if (!info.isWritable()) {
77 m_filepath = destinationDir("local") + m_filepath;
79 } else {
80 m_filepath = destinationDir("remote") + m_filepath;
83 const QString file = m_filepath + FILE_NAME;
84 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
86 const bool useDefaultProps = !useGlobalViewProps &&
87 (!QFileInfo(file).exists() ||
88 (m_node->timestamp() < settings->viewPropsTimestamp()));
89 if (useDefaultProps) {
90 // If the .directory file does not exist or the timestamp is too old,
91 // use the values from the global .directory file instead, which acts
92 // as default view for new folders in this case.
93 settings->setGlobalViewProps(true);
95 ViewProperties defaultProps(url);
96 setDirProperties(defaultProps);
98 settings->setGlobalViewProps(false);
99 m_changedProps = false;
103 ViewProperties::~ViewProperties()
105 if (m_changedProps && m_autoSave) {
106 save();
109 delete m_node;
110 m_node = 0;
113 void ViewProperties::setViewMode(DolphinView::Mode mode)
115 if (m_node->viewMode() != mode) {
116 m_node->setViewMode(mode);
117 updateTimeStamp();
121 DolphinView::Mode ViewProperties::viewMode() const
123 return static_cast<DolphinView::Mode>(m_node->viewMode());
126 void ViewProperties::setShowPreview(bool show)
128 if (m_node->showPreview() != show) {
129 m_node->setShowPreview(show);
130 updateTimeStamp();
134 bool ViewProperties::showPreview() const
136 return m_node->showPreview();
140 void ViewProperties::setShowHiddenFiles(bool show)
142 if (m_node->showHiddenFiles() != show) {
143 m_node->setShowHiddenFiles(show);
144 updateTimeStamp();
148 void ViewProperties::setCategorizedSorting(bool categorized)
150 if (m_node->categorizedSorting() != categorized) {
151 m_node->setCategorizedSorting(categorized);
152 updateTimeStamp();
156 bool ViewProperties::categorizedSorting() const
158 return m_node->categorizedSorting();
162 bool ViewProperties::showHiddenFiles() const
164 return m_node->showHiddenFiles();
167 void ViewProperties::setSorting(DolphinView::Sorting sorting)
169 if (m_node->sorting() != sorting) {
170 m_node->setSorting(sorting);
171 updateTimeStamp();
175 DolphinView::Sorting ViewProperties::sorting() const
177 // If Nepomuk is not available, return SortByName as fallback if SortByRating
178 // or SortByTags is stored.
179 DolphinView::Sorting sorting = static_cast<DolphinView::Sorting>(m_node->sorting());
180 const bool sortByName = !m_nepomukSupport &&
181 ((sorting == DolphinView::SortByRating) || (sorting == DolphinView::SortByTags));
182 if (sortByName) {
183 sorting = DolphinView::SortByName;
185 return sorting;
188 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
190 if (m_node->sortOrder() != sortOrder) {
191 m_node->setSortOrder(sortOrder);
192 updateTimeStamp();
196 Qt::SortOrder ViewProperties::sortOrder() const
198 return static_cast<Qt::SortOrder>(m_node->sortOrder());
201 void ViewProperties::setAdditionalInfo(KFileItemDelegate::InformationList list)
203 int info = NoInfo;
204 foreach (KFileItemDelegate::Information currentInfo, list) {
205 switch (currentInfo) {
206 case KFileItemDelegate::Size:
207 info = info | SizeInfo;
208 break;
209 case KFileItemDelegate::ModificationTime:
210 info = info | DateInfo;
211 break;
212 case KFileItemDelegate::Permissions:
213 info = info | PermissionsInfo;
214 break;
215 case KFileItemDelegate::Owner:
216 info = info | OwnerInfo;
217 break;
218 case KFileItemDelegate::OwnerAndGroup:
219 info = info | GroupInfo;
220 break;
221 case KFileItemDelegate::FriendlyMimeType:
222 info = info | TypeInfo;
223 break;
224 default:
225 break;
229 const int encodedInfo = encodedAdditionalInfo(info);
230 if (m_node->additionalInfo() != encodedInfo) {
231 m_node->setAdditionalInfo(encodedInfo);
232 updateTimeStamp();
236 KFileItemDelegate::InformationList ViewProperties::additionalInfo() const
238 const int info = decodedAdditionalInfo();
240 KFileItemDelegate::InformationList list;
241 if (info & SizeInfo) {
242 list.append(KFileItemDelegate::Size);
244 if (info & DateInfo) {
245 list.append(KFileItemDelegate::ModificationTime);
247 if (info & PermissionsInfo) {
248 list.append(KFileItemDelegate::Permissions);
250 if (info & OwnerInfo) {
251 list.append(KFileItemDelegate::Owner);
253 if (info & GroupInfo) {
254 list.append(KFileItemDelegate::OwnerAndGroup);
256 if (info & TypeInfo) {
257 list.append(KFileItemDelegate::FriendlyMimeType);
260 return list;
264 void ViewProperties::setDirProperties(const ViewProperties& props)
266 setViewMode(props.viewMode());
267 setShowPreview(props.showPreview());
268 setShowHiddenFiles(props.showHiddenFiles());
269 setCategorizedSorting(props.categorizedSorting());
270 setSorting(props.sorting());
271 setSortOrder(props.sortOrder());
272 setAdditionalInfo(props.additionalInfo());
275 void ViewProperties::setAutoSaveEnabled(bool autoSave)
277 m_autoSave = autoSave;
280 bool ViewProperties::isAutoSaveEnabled() const
282 return m_autoSave;
285 void ViewProperties::updateTimeStamp()
287 m_changedProps = true;
288 m_node->setTimestamp(QDateTime::currentDateTime());
291 void ViewProperties::save()
293 KStandardDirs::makeDir(m_filepath);
294 m_node->writeConfig();
295 m_changedProps = false;
298 KUrl ViewProperties::mirroredDirectory()
300 QString basePath = KGlobal::mainComponent().componentName();
301 basePath.append("/view_properties/");
302 return KUrl(KStandardDirs::locateLocal("data", basePath));
305 QString ViewProperties::destinationDir(const QString& subDir) const
307 QString basePath = KGlobal::mainComponent().componentName();
308 basePath.append("/view_properties/").append(subDir);
309 return KStandardDirs::locateLocal("data", basePath);
312 int ViewProperties::encodedAdditionalInfo(int info) const
314 int encodedInfo = m_node->additionalInfo();
316 switch (viewMode()) {
317 case DolphinView::DetailsView:
318 encodedInfo = (encodedInfo & 0xFFFF00) | info;
319 break;
320 case DolphinView::IconsView:
321 encodedInfo = (encodedInfo & 0xFF00FF) | (info << 8);
322 break;
323 case DolphinView::ColumnView:
324 encodedInfo = (encodedInfo & 0x00FFFF) | (info << 16);
325 break;
326 default: break;
329 return encodedInfo;
332 int ViewProperties::decodedAdditionalInfo() const
334 int decodedInfo = m_node->additionalInfo();
336 switch (viewMode()) {
337 case DolphinView::DetailsView:
338 decodedInfo = decodedInfo & 0xFF;
339 if (decodedInfo == NoInfo) {
340 // a details view without any additional info makes no sense, hence
341 // provide at least a size-info and date-info as fallback
342 decodedInfo = SizeInfo | DateInfo;
344 break;
345 case DolphinView::IconsView:
346 decodedInfo = (decodedInfo >> 8) & 0xFF;
347 break;
348 case DolphinView::ColumnView:
349 decodedInfo = (decodedInfo >> 16) & 0xFF;
350 break;
351 default: break;
354 return decodedInfo;