]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
adapt Dolphin to kdelibs coding style (http://techbase.kde.org/Policies/Kdelibs_Codin...
[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 #include <kcomponentdata.h>
32
33 #include "viewproperties.h"
34 #include "dolphinsettings.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 bool ViewProperties::showHiddenFiles() const
135 {
136 return m_node->showHiddenFiles();
137 }
138
139 void ViewProperties::setSorting(DolphinView::Sorting sorting)
140 {
141 if (m_node->sorting() != sorting) {
142 m_node->setSorting(sorting);
143 updateTimeStamp();
144 }
145 }
146
147 DolphinView::Sorting ViewProperties::sorting() const
148 {
149 return static_cast<DolphinView::Sorting>(m_node->sorting());
150 }
151
152 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
153 {
154 if (m_node->sortOrder() != sortOrder) {
155 m_node->setSortOrder(sortOrder);
156 updateTimeStamp();
157 }
158 }
159
160 Qt::SortOrder ViewProperties::sortOrder() const
161 {
162 return static_cast<Qt::SortOrder>(m_node->sortOrder());
163 }
164
165 void ViewProperties::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
166 {
167 if (m_node->additionalInfo() != info) {
168 m_node->setAdditionalInfo(info);
169 updateTimeStamp();
170 }
171 }
172
173 KFileItemDelegate::AdditionalInformation ViewProperties::additionalInfo() const
174 {
175 return static_cast<KFileItemDelegate::AdditionalInformation>(m_node->additionalInfo());
176 }
177
178
179 void ViewProperties::setDirProperties(const ViewProperties& props)
180 {
181 setViewMode(props.viewMode());
182 setShowPreview(props.showPreview());
183 setShowHiddenFiles(props.showHiddenFiles());
184 setSorting(props.sorting());
185 setSortOrder(props.sortOrder());
186 setAdditionalInfo(props.additionalInfo());
187 }
188
189 void ViewProperties::setAutoSaveEnabled(bool autoSave)
190 {
191 m_autoSave = autoSave;
192 }
193
194 bool ViewProperties::isAutoSaveEnabled() const
195 {
196 return m_autoSave;
197 }
198
199 void ViewProperties::updateTimeStamp()
200 {
201 m_changedProps = true;
202 m_node->setTimestamp(QDateTime::currentDateTime());
203 }
204
205 void ViewProperties::save()
206 {
207 KStandardDirs::makeDir(m_filepath);
208 m_node->writeConfig();
209 m_changedProps = false;
210 }
211
212 KUrl ViewProperties::mirroredDirectory()
213 {
214 QString basePath = KGlobal::mainComponent().componentName();
215 basePath.append("/view_properties/");
216 return KUrl(KStandardDirs::locateLocal("data", basePath));
217 }
218
219 QString ViewProperties::destinationDir(const QString& subDir) const
220 {
221 QString basePath = KGlobal::mainComponent().componentName();
222 basePath.append("/view_properties/").append(subDir);
223 return KStandardDirs::locateLocal("data", basePath);
224 }