]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
Allow showing additional information like type, size and date in parallel for the...
[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 "dolphinsettings.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 #ifdef HAVE_NEPOMUK
33 #include <nepomuk/resourcemanager.h>
34 #endif
35
36 #include <QDate>
37 #include <QFile>
38 #include <QFileInfo>
39
40 bool ViewProperties::m_nepomukSupport = false;
41
42 #define FILE_NAME "/.directory"
43
44 ViewProperties::ViewProperties(const KUrl& url) :
45 m_changedProps(false),
46 m_autoSave(true),
47 m_node(0)
48 {
49 #ifdef HAVE_NEPOMUK
50 static bool checkedNepomukSupport = false;
51 if (!checkedNepomukSupport) {
52 m_nepomukSupport = !Nepomuk::ResourceManager::instance()->init();
53 checkedNepomukSupport = true;
54 }
55 #endif
56
57 KUrl cleanUrl(url);
58 cleanUrl.cleanPath();
59 m_filepath = cleanUrl.path();
60
61 if ((m_filepath.length() < 1) || (m_filepath.at(0) != QChar('/'))) {
62 m_node = new ViewPropertySettings();
63 return;
64 }
65
66 // We try and save it to a file in the directory being viewed.
67 // If the directory is not writable by the user or the directory is not local,
68 // we store the properties information in a local file.
69 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
70 const bool useGlobalViewProps = settings->globalViewProps();
71 if (useGlobalViewProps) {
72 m_filepath = destinationDir("global");
73 } else if (cleanUrl.isLocalFile()) {
74 const QFileInfo info(m_filepath);
75 if (!info.isWritable()) {
76 m_filepath = destinationDir("local") + m_filepath;
77 }
78 } else {
79 m_filepath = destinationDir("remote") + m_filepath;
80 }
81
82 const QString file(m_filepath + FILE_NAME);
83 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
84
85 const bool useDefaultProps = !useGlobalViewProps &&
86 (!QFileInfo(file).exists() ||
87 (m_node->timestamp() < settings->viewPropsTimestamp()));
88 if (useDefaultProps) {
89 // If the .directory file does not exist or the timestamp is too old,
90 // use the values from the global .directory file instead, which acts
91 // as default view for new folders in this case.
92 settings->setGlobalViewProps(true);
93
94 ViewProperties defaultProps(url);
95 setDirProperties(defaultProps);
96
97 settings->setGlobalViewProps(false);
98 m_changedProps = false;
99 }
100 }
101
102 ViewProperties::~ViewProperties()
103 {
104 if (m_changedProps && m_autoSave) {
105 save();
106 }
107
108 delete m_node;
109 m_node = 0;
110 }
111
112 void ViewProperties::setViewMode(DolphinView::Mode mode)
113 {
114 if (m_node->viewMode() != mode) {
115 m_node->setViewMode(mode);
116 updateTimeStamp();
117 }
118 }
119
120 DolphinView::Mode ViewProperties::viewMode() const
121 {
122 return static_cast<DolphinView::Mode>(m_node->viewMode());
123 }
124
125 void ViewProperties::setShowPreview(bool show)
126 {
127 if (m_node->showPreview() != show) {
128 m_node->setShowPreview(show);
129 updateTimeStamp();
130 }
131 }
132
133 bool ViewProperties::showPreview() const
134 {
135 return m_node->showPreview();
136 }
137
138
139 void ViewProperties::setShowHiddenFiles(bool show)
140 {
141 if (m_node->showHiddenFiles() != show) {
142 m_node->setShowHiddenFiles(show);
143 updateTimeStamp();
144 }
145 }
146
147 void ViewProperties::setCategorizedSorting(bool categorized)
148 {
149 if (m_node->categorizedSorting() != categorized) {
150 m_node->setCategorizedSorting(categorized);
151 updateTimeStamp();
152 }
153 }
154
155 bool ViewProperties::categorizedSorting() const
156 {
157 return m_node->categorizedSorting();
158 }
159
160
161 bool ViewProperties::showHiddenFiles() const
162 {
163 return m_node->showHiddenFiles();
164 }
165
166 void ViewProperties::setSorting(DolphinView::Sorting sorting)
167 {
168 if (m_node->sorting() != sorting) {
169 m_node->setSorting(sorting);
170 updateTimeStamp();
171 }
172 }
173
174 DolphinView::Sorting ViewProperties::sorting() const
175 {
176 // If Nepomuk is not available, return SortByName as fallback if SortByRating
177 // or SortByTags is stored.
178 DolphinView::Sorting sorting = static_cast<DolphinView::Sorting>(m_node->sorting());
179 const bool sortByName = !m_nepomukSupport &&
180 ((sorting == DolphinView::SortByRating) || (sorting == DolphinView::SortByTags));
181 if (sortByName) {
182 sorting = DolphinView::SortByName;
183 }
184 return sorting;
185 }
186
187 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
188 {
189 if (m_node->sortOrder() != sortOrder) {
190 m_node->setSortOrder(sortOrder);
191 updateTimeStamp();
192 }
193 }
194
195 Qt::SortOrder ViewProperties::sortOrder() const
196 {
197 return static_cast<Qt::SortOrder>(m_node->sortOrder());
198 }
199
200 void ViewProperties::setAdditionalInfo(KFileItemDelegate::InformationList list)
201 {
202 int info = NoInfo;
203 foreach (KFileItemDelegate::Information currentInfo, list) {
204 switch (currentInfo) {
205 case KFileItemDelegate::FriendlyMimeType:
206 info = info | TypeInfo;
207 break;
208 case KFileItemDelegate::Size:
209 info = info | SizeInfo;
210 break;
211 case KFileItemDelegate::ModificationTime:
212 info = info | DateInfo;
213 break;
214 default:
215 break;
216 }
217 }
218
219 if (m_node->additionalInfo() != info) {
220 m_node->setAdditionalInfo(info);
221 updateTimeStamp();
222 }
223 }
224
225 KFileItemDelegate::InformationList ViewProperties::additionalInfo() const
226 {
227 const int info = m_node->additionalInfo();
228
229 KFileItemDelegate::InformationList list;
230 if (info & TypeInfo) {
231 list.append(KFileItemDelegate::FriendlyMimeType);
232 }
233 if (info & SizeInfo) {
234 list.append(KFileItemDelegate::Size);
235 }
236 if (info & DateInfo) {
237 list.append(KFileItemDelegate::ModificationTime);
238 }
239
240 return list;
241 }
242
243
244 void ViewProperties::setDirProperties(const ViewProperties& props)
245 {
246 setViewMode(props.viewMode());
247 setShowPreview(props.showPreview());
248 setShowHiddenFiles(props.showHiddenFiles());
249 setCategorizedSorting(props.categorizedSorting());
250 setSorting(props.sorting());
251 setSortOrder(props.sortOrder());
252 setAdditionalInfo(props.additionalInfo());
253 }
254
255 void ViewProperties::setAutoSaveEnabled(bool autoSave)
256 {
257 m_autoSave = autoSave;
258 }
259
260 bool ViewProperties::isAutoSaveEnabled() const
261 {
262 return m_autoSave;
263 }
264
265 void ViewProperties::updateTimeStamp()
266 {
267 m_changedProps = true;
268 m_node->setTimestamp(QDateTime::currentDateTime());
269 }
270
271 void ViewProperties::save()
272 {
273 KStandardDirs::makeDir(m_filepath);
274 m_node->writeConfig();
275 m_changedProps = false;
276 }
277
278 KUrl ViewProperties::mirroredDirectory()
279 {
280 QString basePath = KGlobal::mainComponent().componentName();
281 basePath.append("/view_properties/");
282 return KUrl(KStandardDirs::locateLocal("data", basePath));
283 }
284
285 QString ViewProperties::destinationDir(const QString& subDir) const
286 {
287 QString basePath = KGlobal::mainComponent().componentName();
288 basePath.append("/view_properties/").append(subDir);
289 return KStandardDirs::locateLocal("data", basePath);
290 }