]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
Fix small problems with enums. Sort by type works like a charm :)
[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 #include <QDate>
33 #include <QFile>
34 #include <QFileInfo>
35
36 #define FILE_NAME "/.directory"
37
38 ViewProperties::ViewProperties(const KUrl& url) :
39 m_changedProps(false),
40 m_autoSave(true),
41 m_node(0)
42 {
43 KUrl cleanUrl(url);
44 cleanUrl.cleanPath();
45 m_filepath = cleanUrl.path();
46
47 if ((m_filepath.length() < 1) || (m_filepath.at(0) != QChar('/'))) {
48 m_node = new ViewPropertySettings();
49 return;
50 }
51
52 // We try and save it to a file in the directory being viewed.
53 // If the directory is not writable by the user or the directory is not local,
54 // we store the properties information in a local file.
55 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
56 const bool useGlobalViewProps = settings->globalViewProps();
57 if (useGlobalViewProps) {
58 m_filepath = destinationDir("global");
59 } else if (cleanUrl.isLocalFile()) {
60 const QFileInfo info(m_filepath);
61 if (!info.isWritable()) {
62 m_filepath = destinationDir("local") + m_filepath;
63 }
64 } else {
65 m_filepath = destinationDir("remote") + m_filepath;
66 }
67
68 const QString file(m_filepath + FILE_NAME);
69 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
70
71 const bool useDefaultProps = !useGlobalViewProps &&
72 (!QFileInfo(file).exists() ||
73 (m_node->timestamp() < settings->viewPropsTimestamp()));
74 if (useDefaultProps) {
75 // If the .directory file does not exist or the timestamp is too old,
76 // use the values from the global .directory file instead, which acts
77 // as default view for new folders in this case.
78 settings->setGlobalViewProps(true);
79
80 ViewProperties defaultProps(url);
81 setDirProperties(defaultProps);
82
83 settings->setGlobalViewProps(false);
84 m_changedProps = false;
85 }
86 }
87
88 ViewProperties::~ViewProperties()
89 {
90 if (m_changedProps && m_autoSave) {
91 save();
92 }
93
94 delete m_node;
95 m_node = 0;
96 }
97
98 void ViewProperties::setViewMode(DolphinView::Mode mode)
99 {
100 if (m_node->viewMode() != mode) {
101 m_node->setViewMode(mode);
102 updateTimeStamp();
103 }
104 }
105
106 DolphinView::Mode ViewProperties::viewMode() const
107 {
108 return static_cast<DolphinView::Mode>(m_node->viewMode());
109 }
110
111 void ViewProperties::setShowPreview(bool show)
112 {
113 if (m_node->showPreview() != show) {
114 m_node->setShowPreview(show);
115 updateTimeStamp();
116 }
117 }
118
119 bool ViewProperties::showPreview() const
120 {
121 return m_node->showPreview();
122 }
123
124
125 void ViewProperties::setShowHiddenFiles(bool show)
126 {
127 if (m_node->showHiddenFiles() != show) {
128 m_node->setShowHiddenFiles(show);
129 updateTimeStamp();
130 }
131 }
132
133 void ViewProperties::setCategorizedSorting(bool categorized)
134 {
135 if (m_node->categorizedSorting() != categorized) {
136 m_node->setCategorizedSorting(categorized);
137 updateTimeStamp();
138 }
139 }
140
141 bool ViewProperties::categorizedSorting() const
142 {
143 return m_node->categorizedSorting();
144 }
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::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
179 {
180 if (m_node->additionalInfo() != info) {
181 m_node->setAdditionalInfo(info);
182 updateTimeStamp();
183 }
184 }
185
186 KFileItemDelegate::AdditionalInformation ViewProperties::additionalInfo() const
187 {
188 return static_cast<KFileItemDelegate::AdditionalInformation>(m_node->additionalInfo());
189 }
190
191
192 void ViewProperties::setDirProperties(const ViewProperties& props)
193 {
194 setViewMode(props.viewMode());
195 setShowPreview(props.showPreview());
196 setShowHiddenFiles(props.showHiddenFiles());
197 setCategorizedSorting(props.categorizedSorting());
198 setSorting(props.sorting());
199 setSortOrder(props.sortOrder());
200 setAdditionalInfo(props.additionalInfo());
201 }
202
203 void ViewProperties::setAutoSaveEnabled(bool autoSave)
204 {
205 m_autoSave = autoSave;
206 }
207
208 bool ViewProperties::isAutoSaveEnabled() const
209 {
210 return m_autoSave;
211 }
212
213 void ViewProperties::updateTimeStamp()
214 {
215 m_changedProps = true;
216 m_node->setTimestamp(QDateTime::currentDateTime());
217 }
218
219 void ViewProperties::save()
220 {
221 KStandardDirs::makeDir(m_filepath);
222 m_node->writeConfig();
223 m_changedProps = false;
224 }
225
226 KUrl ViewProperties::mirroredDirectory()
227 {
228 QString basePath = KGlobal::mainComponent().componentName();
229 basePath.append("/view_properties/");
230 return KUrl(KStandardDirs::locateLocal("data", basePath));
231 }
232
233 QString ViewProperties::destinationDir(const QString& subDir) const
234 {
235 QString basePath = KGlobal::mainComponent().componentName();
236 basePath.append("/view_properties/").append(subDir);
237 return KStandardDirs::locateLocal("data", basePath);
238 }