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