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