]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
commited initial version of Dolphin
[dolphin.git] / src / viewproperties.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include <assert.h>
22
23 #include <qdatetime.h>
24 #include <qdir.h>
25 #include <qfile.h>
26
27 #include <klocale.h>
28 #include <kstandarddirs.h>
29 #include <kurl.h>
30
31 #include "viewproperties.h"
32
33 #include "dolphinsettings.h"
34
35 #define FILE_NAME "/.directory"
36
37 ViewProperties::ViewProperties(KUrl url) :
38 m_changedProps(false),
39 m_autoSave(true),
40 m_subDirValidityHidden(false),
41 m_node(0)
42 {
43 url.cleanPath(true);
44 m_filepath = url.path();
45
46 if ((m_filepath.length() < 1) || (m_filepath.at(0) != QChar('/'))) {
47 m_node = new ViewPropertySettings();
48 return;
49 }
50
51 // we try and save it to a file in the directory being viewed
52 // if the directory is not writable by the user or the directory is not local
53 // we store the properties information in a local file
54 QString rootDir("/"); // TODO: should this be set to the root of the bookmark, if any?
55 if (url.isLocalFile()) {
56 QFileInfo info(m_filepath);
57
58 if (!info.isWritable()) {
59 QString basePath = KGlobal::instance()->instanceName();
60 basePath.append("/view_properties/local");
61 rootDir = locateLocal("data", basePath);
62 m_filepath = rootDir + m_filepath;
63 }
64 }
65 else {
66 QString basePath = KGlobal::instance()->instanceName();
67 basePath.append("/view_properties/remote/").append(url.host());
68 rootDir = locateLocal("data", basePath);
69 m_filepath = rootDir + m_filepath;
70 }
71
72 m_node = new ViewPropertySettings(KSharedConfig::openConfig(m_filepath + FILE_NAME));
73
74 QDir dir(m_filepath);
75 const bool isValidForSubDirs = m_node->validForSubDirs();
76 while ((dir.path() != rootDir) && dir.cdUp()) {
77 QString parentPath(dir.path() + FILE_NAME);
78
79 if (!QFile::exists(parentPath))
80 {
81 continue;
82 }
83
84 ViewPropertySettings parentNode(KSharedConfig::openConfig(dir.path() + FILE_NAME));
85 const bool inheritProps = parentNode.validForSubDirs() &&
86 (parentNode.timestamp() > m_node->timestamp());
87
88 if (inheritProps) {
89 *m_node = parentNode;
90 break;
91 }
92 }
93
94 if (isValidForSubDirs) {
95 m_subDirValidityHidden = true;
96 }
97 }
98
99 ViewProperties::~ViewProperties()
100 {
101 if (m_changedProps && m_autoSave) {
102 save();
103 }
104
105 delete m_node;
106 }
107
108 void ViewProperties::setViewMode(DolphinView::Mode mode)
109 {
110 if (m_node->viewMode() != mode) {
111 m_node->setViewMode(mode);
112 updateTimeStamp();
113 }
114 }
115
116 DolphinView::Mode ViewProperties::viewMode() const
117 {
118 return static_cast<DolphinView::Mode>(m_node->viewMode());
119 }
120
121 void ViewProperties::setShowHiddenFilesEnabled(bool show)
122 {
123 if (m_node->showHiddenFiles() != show) {
124 m_node->setShowHiddenFiles(show);
125 updateTimeStamp();
126 }
127 }
128
129 bool ViewProperties::isShowHiddenFilesEnabled() const
130 {
131 return m_node->showHiddenFiles();
132 }
133
134 void ViewProperties::setSorting(DolphinView::Sorting sorting)
135 {
136 if (m_node->sorting() != sorting) {
137 m_node->setSorting(sorting);
138 updateTimeStamp();
139 }
140 }
141
142 DolphinView::Sorting ViewProperties::sorting() const
143 {
144 return static_cast<DolphinView::Sorting>(m_node->sorting());
145 }
146
147 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
148 {
149 if (m_node->sortOrder() != sortOrder) {
150 m_node->setSortOrder(sortOrder);
151 updateTimeStamp();
152 }
153 }
154
155 Qt::SortOrder ViewProperties::sortOrder() const
156 {
157 return static_cast<Qt::SortOrder>(m_node->sortOrder());
158 }
159
160 void ViewProperties::setValidForSubDirs(bool valid)
161 {
162 if (m_node->validForSubDirs() != valid) {
163 m_node->setValidForSubDirs(valid);
164 updateTimeStamp();
165 }
166 }
167
168 bool ViewProperties::isValidForSubDirs() const
169 {
170 return m_node->validForSubDirs();
171 }
172
173 void ViewProperties::setAutoSaveEnabled(bool autoSave)
174 {
175 m_autoSave = autoSave;
176 }
177
178 bool ViewProperties::isAutoSaveEnabled() const
179 {
180 return m_autoSave;
181 }
182
183 void ViewProperties::updateTimeStamp()
184 {
185 m_changedProps = true;
186 m_node->setTimestamp(QDateTime::currentDateTime());
187 }
188
189 void ViewProperties::save()
190 {
191 KStandardDirs::makeDir(m_filepath);
192 m_node->writeConfig();
193 m_changedProps = false;
194 }
195
196 ViewProperties& ViewProperties::operator = (const ViewProperties& props)
197 {
198 if (&props != this) {
199 m_changedProps = props.m_changedProps;
200 m_autoSave = props.m_autoSave;
201 m_subDirValidityHidden = props.m_subDirValidityHidden;
202 m_filepath = props.m_filepath;
203 m_node = new ViewPropertySettings();
204 //*m_node = *(props.m_node);
205 }
206
207 return *this;
208 }