]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
21f0c39da8844c35ed26a1d9bc9e2ee77eb86e1c
[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 <assert.h>
22
23 #include <QDateTime>
24 #include <QFile>
25 #include <QFileInfo>
26
27 #include <kcomponentdata.h>
28 #include <klocale.h>
29 #include <kstandarddirs.h>
30 #include <kurl.h>
31
32 #include "viewproperties.h"
33 #include "dolphinsettings.h"
34 #include "dolphin_directoryviewpropertysettings.h"
35 #include "dolphin_generalsettings.h"
36
37 #define FILE_NAME "/.directory"
38
39 ViewProperties::ViewProperties(const KUrl& url) :
40 m_changedProps(false),
41 m_autoSave(true),
42 m_node(0)
43 {
44 KUrl cleanUrl(url);
45 cleanUrl.cleanPath();
46 m_filepath = cleanUrl.path();
47
48 if ((m_filepath.length() < 1) || (m_filepath.at(0) != QChar('/'))) {
49 m_node = new ViewPropertySettings();
50 return;
51 }
52
53 // We try and save it to a file in the directory being viewed.
54 // If the directory is not writable by the user or the directory is not local,
55 // we store the properties information in a local file.
56 GeneralSettings* settings = DolphinSettings::instance().generalSettings();
57 const bool useGlobalViewProps = settings->globalViewProps();
58 if (useGlobalViewProps) {
59 m_filepath = destinationDir("global");
60 } else if (cleanUrl.isLocalFile()) {
61 const QFileInfo info(m_filepath);
62 if (!info.isWritable()) {
63 m_filepath = destinationDir("local") + m_filepath;
64 }
65 } else {
66 m_filepath = destinationDir("remote") + m_filepath;
67 }
68
69 const QString file(m_filepath + FILE_NAME);
70 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
71
72 const bool useDefaultProps = !useGlobalViewProps &&
73 (!QFileInfo(file).exists() ||
74 (m_node->timestamp() < settings->viewPropsTimestamp()));
75 if (useDefaultProps) {
76 // If the .directory file does not exist or the timestamp is too old,
77 // use the values from the global .directory file instead, which acts
78 // as default view for new folders in this case.
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 ViewProperties::~ViewProperties()
90 {
91 if (m_changedProps && m_autoSave) {
92 save();
93 }
94
95 delete m_node;
96 m_node = 0;
97 }
98
99 void ViewProperties::setViewMode(DolphinView::Mode mode)
100 {
101 if (m_node->viewMode() != mode) {
102 m_node->setViewMode(mode);
103 updateTimeStamp();
104 }
105 }
106
107 DolphinView::Mode ViewProperties::viewMode() const
108 {
109 return static_cast<DolphinView::Mode>(m_node->viewMode());
110 }
111
112 void ViewProperties::setShowPreview(bool show)
113 {
114 if (m_node->showPreview() != show) {
115 m_node->setShowPreview(show);
116 updateTimeStamp();
117 }
118 }
119
120 bool ViewProperties::showPreview() const
121 {
122 return m_node->showPreview();
123 }
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
148 bool ViewProperties::showHiddenFiles() const
149 {
150 return m_node->showHiddenFiles();
151 }
152
153 void ViewProperties::setSorting(DolphinView::Sorting sorting)
154 {
155 if (m_node->sorting() != sorting) {
156 m_node->setSorting(sorting);
157 updateTimeStamp();
158 }
159 }
160
161 DolphinView::Sorting ViewProperties::sorting() const
162 {
163 return static_cast<DolphinView::Sorting>(m_node->sorting());
164 }
165
166 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
167 {
168 if (m_node->sortOrder() != sortOrder) {
169 m_node->setSortOrder(sortOrder);
170 updateTimeStamp();
171 }
172 }
173
174 Qt::SortOrder ViewProperties::sortOrder() const
175 {
176 return static_cast<Qt::SortOrder>(m_node->sortOrder());
177 }
178
179 void ViewProperties::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
180 {
181 if (m_node->additionalInfo() != info) {
182 m_node->setAdditionalInfo(info);
183 updateTimeStamp();
184 }
185 }
186
187 KFileItemDelegate::AdditionalInformation ViewProperties::additionalInfo() const
188 {
189 return static_cast<KFileItemDelegate::AdditionalInformation>(m_node->additionalInfo());
190 }
191
192
193 void ViewProperties::setDirProperties(const ViewProperties& props)
194 {
195 setViewMode(props.viewMode());
196 setShowPreview(props.showPreview());
197 setShowHiddenFiles(props.showHiddenFiles());
198 setCategorizedSorting(props.categorizedSorting());
199 setSorting(props.sorting());
200 setSortOrder(props.sortOrder());
201 setAdditionalInfo(props.additionalInfo());
202 }
203
204 void ViewProperties::setAutoSaveEnabled(bool autoSave)
205 {
206 m_autoSave = autoSave;
207 }
208
209 bool ViewProperties::isAutoSaveEnabled() const
210 {
211 return m_autoSave;
212 }
213
214 void ViewProperties::updateTimeStamp()
215 {
216 m_changedProps = true;
217 m_node->setTimestamp(QDateTime::currentDateTime());
218 }
219
220 void ViewProperties::save()
221 {
222 KStandardDirs::makeDir(m_filepath);
223 m_node->writeConfig();
224 m_changedProps = false;
225 }
226
227 KUrl ViewProperties::mirroredDirectory()
228 {
229 QString basePath = KGlobal::mainComponent().componentName();
230 basePath.append("/view_properties/");
231 return KUrl(KStandardDirs::locateLocal("data", basePath));
232 }
233
234 QString ViewProperties::destinationDir(const QString& subDir) const
235 {
236 QString basePath = KGlobal::mainComponent().componentName();
237 basePath.append("/view_properties/").append(subDir);
238 return KStandardDirs::locateLocal("data", basePath);
239 }