]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
Added setters in urlnavigator to remove dependency on dolphinsettings.
[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 }
61 else if (cleanUrl.isLocalFile()) {
62 const QFileInfo info(m_filepath);
63 if (!info.isWritable()) {
64 m_filepath = destinationDir("local") + m_filepath;
65 }
66 }
67 else {
68 m_filepath = destinationDir("remote") + m_filepath;
69 }
70
71 const QString file(m_filepath + FILE_NAME);
72 m_node = new ViewPropertySettings(KSharedConfig::openConfig(file));
73
74 const bool useDefaultProps = !useGlobalViewProps &&
75 (!QFileInfo(file).exists() ||
76 (m_node->timestamp() < settings->viewPropsTimestamp()));
77 if (useDefaultProps) {
78 // If the .directory file does not exist or the timestamp is too old,
79 // use the values from the global .directory file instead, which acts
80 // as default view for new folders in this case.
81 settings->setGlobalViewProps(true);
82
83 ViewProperties defaultProps(url);
84 setDirProperties(defaultProps);
85
86 settings->setGlobalViewProps(false);
87 m_changedProps = false;
88 }
89 }
90
91 ViewProperties::~ViewProperties()
92 {
93 if (m_changedProps && m_autoSave) {
94 save();
95 }
96
97 delete m_node;
98 m_node = 0;
99 }
100
101 void ViewProperties::setViewMode(DolphinView::Mode mode)
102 {
103 if (m_node->viewMode() != mode) {
104 m_node->setViewMode(mode);
105 updateTimeStamp();
106 }
107 }
108
109 DolphinView::Mode ViewProperties::viewMode() const
110 {
111 return static_cast<DolphinView::Mode>(m_node->viewMode());
112 }
113
114 void ViewProperties::setShowPreview(bool show)
115 {
116 if (m_node->showPreview() != show) {
117 m_node->setShowPreview(show);
118 updateTimeStamp();
119 }
120 }
121
122 bool ViewProperties::showPreview() const
123 {
124 return m_node->showPreview();
125 }
126
127
128 void ViewProperties::setShowHiddenFiles(bool show)
129 {
130 if (m_node->showHiddenFiles() != show) {
131 m_node->setShowHiddenFiles(show);
132 updateTimeStamp();
133 }
134 }
135
136 bool ViewProperties::showHiddenFiles() const
137 {
138 return m_node->showHiddenFiles();
139 }
140
141 void ViewProperties::setSorting(DolphinView::Sorting sorting)
142 {
143 if (m_node->sorting() != sorting) {
144 m_node->setSorting(sorting);
145 updateTimeStamp();
146 }
147 }
148
149 DolphinView::Sorting ViewProperties::sorting() const
150 {
151 return static_cast<DolphinView::Sorting>(m_node->sorting());
152 }
153
154 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
155 {
156 if (m_node->sortOrder() != sortOrder) {
157 m_node->setSortOrder(sortOrder);
158 updateTimeStamp();
159 }
160 }
161
162 Qt::SortOrder ViewProperties::sortOrder() const
163 {
164 return static_cast<Qt::SortOrder>(m_node->sortOrder());
165 }
166
167 void ViewProperties::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
168 {
169 if (m_node->additionalInfo() != info) {
170 m_node->setAdditionalInfo(info);
171 updateTimeStamp();
172 }
173 }
174
175 KFileItemDelegate::AdditionalInformation ViewProperties::additionalInfo() const
176 {
177 return static_cast<KFileItemDelegate::AdditionalInformation>(m_node->additionalInfo());
178 }
179
180
181 void ViewProperties::setDirProperties(const ViewProperties& props)
182 {
183 setViewMode(props.viewMode());
184 setShowPreview(props.showPreview());
185 setShowHiddenFiles(props.showHiddenFiles());
186 setSorting(props.sorting());
187 setSortOrder(props.sortOrder());
188 setAdditionalInfo(props.additionalInfo());
189 }
190
191 void ViewProperties::setAutoSaveEnabled(bool autoSave)
192 {
193 m_autoSave = autoSave;
194 }
195
196 bool ViewProperties::isAutoSaveEnabled() const
197 {
198 return m_autoSave;
199 }
200
201 void ViewProperties::updateTimeStamp()
202 {
203 m_changedProps = true;
204 m_node->setTimestamp(QDateTime::currentDateTime());
205 }
206
207 void ViewProperties::save()
208 {
209 KStandardDirs::makeDir(m_filepath);
210 m_node->writeConfig();
211 m_changedProps = false;
212 }
213
214 KUrl ViewProperties::mirroredDirectory()
215 {
216 QString basePath = KGlobal::mainComponent().componentName();
217 basePath.append("/view_properties/");
218 return KUrl(KStandardDirs::locateLocal("data", basePath));
219 }
220
221 QString ViewProperties::destinationDir(const QString& subDir) const
222 {
223 QString basePath = KGlobal::mainComponent().componentName();
224 basePath.append("/view_properties/").append(subDir);
225 return KStandardDirs::locateLocal("data", basePath);
226 }