]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
Initial step for moving to KDirModel. Large code parts have been deleted, as a step...
[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 #include <kinstance.h>
31
32 #include "viewproperties.h"
33
34 #include "dolphinsettings.h"
35
36 #define FILE_NAME "/.directory"
37
38 ViewProperties::ViewProperties(KUrl url) :
39 m_changedProps(false),
40 m_autoSave(true),
41 m_subDirValidityHidden(false),
42 m_node(0)
43 {
44 url.cleanPath();
45 m_filepath = url.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 QString rootDir("/"); // TODO: should this be set to the root of the bookmark, if any?
56 if (url.isLocalFile()) {
57 QFileInfo info(m_filepath);
58
59 if (!info.isWritable()) {
60 QString basePath = KGlobal::instance()->instanceName();
61 basePath.append("/view_properties/local");
62 rootDir = KStandardDirs::locateLocal("data", basePath);
63 m_filepath = rootDir + m_filepath;
64 }
65 }
66 else {
67 QString basePath = KGlobal::instance()->instanceName();
68 basePath.append("/view_properties/remote/").append(url.host());
69 rootDir = KStandardDirs::locateLocal("data", basePath);
70 m_filepath = rootDir + m_filepath;
71 }
72
73 m_node = new ViewPropertySettings(KSharedConfig::openConfig(m_filepath + FILE_NAME));
74
75 QDir dir(m_filepath);
76 const bool isValidForSubDirs = m_node->validForSubDirs();
77 while ((dir.path() != rootDir) && dir.cdUp()) {
78 QString parentPath(dir.path() + FILE_NAME);
79
80 if (!QFile::exists(parentPath))
81 {
82 continue;
83 }
84
85 ViewPropertySettings parentNode(KSharedConfig::openConfig(dir.path() + FILE_NAME));
86 const bool inheritProps = parentNode.validForSubDirs() &&
87 (parentNode.timestamp() > m_node->timestamp());
88
89 if (inheritProps) {
90 delete m_node;
91 m_node = new ViewPropertySettings(KSharedConfig::openConfig(dir.path() + FILE_NAME));
92 break;
93 }
94 }
95
96 if (isValidForSubDirs) {
97 m_subDirValidityHidden = true;
98 }
99 }
100
101 ViewProperties::~ViewProperties()
102 {
103 if (m_changedProps && m_autoSave) {
104 save();
105 }
106
107 delete m_node;
108 }
109
110 void ViewProperties::setViewMode(DolphinView::Mode mode)
111 {
112 if (m_node->viewMode() != mode) {
113 m_node->setViewMode(mode);
114 updateTimeStamp();
115 }
116 }
117
118 DolphinView::Mode ViewProperties::viewMode() const
119 {
120 return static_cast<DolphinView::Mode>(m_node->viewMode());
121 }
122
123 void ViewProperties::setShowHiddenFilesEnabled(bool show)
124 {
125 if (m_node->showHiddenFiles() != show) {
126 m_node->setShowHiddenFiles(show);
127 updateTimeStamp();
128 }
129 }
130
131 bool ViewProperties::isShowHiddenFilesEnabled() const
132 {
133 return m_node->showHiddenFiles();
134 }
135
136 void ViewProperties::setSorting(DolphinView::Sorting sorting)
137 {
138 if (m_node->sorting() != sorting) {
139 m_node->setSorting(sorting);
140 updateTimeStamp();
141 }
142 }
143
144 DolphinView::Sorting ViewProperties::sorting() const
145 {
146 return static_cast<DolphinView::Sorting>(m_node->sorting());
147 }
148
149 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
150 {
151 if (m_node->sortOrder() != sortOrder) {
152 m_node->setSortOrder(sortOrder);
153 updateTimeStamp();
154 }
155 }
156
157 Qt::SortOrder ViewProperties::sortOrder() const
158 {
159 return static_cast<Qt::SortOrder>(m_node->sortOrder());
160 }
161
162 void ViewProperties::setValidForSubDirs(bool valid)
163 {
164 if (m_node->validForSubDirs() != valid) {
165 m_node->setValidForSubDirs(valid);
166 updateTimeStamp();
167 }
168 }
169
170 bool ViewProperties::isValidForSubDirs() const
171 {
172 return m_node->validForSubDirs();
173 }
174
175 void ViewProperties::setAutoSaveEnabled(bool autoSave)
176 {
177 m_autoSave = autoSave;
178 }
179
180 bool ViewProperties::isAutoSaveEnabled() const
181 {
182 return m_autoSave;
183 }
184
185 void ViewProperties::updateTimeStamp()
186 {
187 m_changedProps = true;
188 m_node->setTimestamp(QDateTime::currentDateTime());
189 }
190
191 void ViewProperties::save()
192 {
193 KStandardDirs::makeDir(m_filepath);
194 m_node->writeConfig();
195 m_changedProps = false;
196 }
197
198 ViewProperties& ViewProperties::operator = (const ViewProperties& props)
199 {
200 if (&props != this) {
201 m_changedProps = props.m_changedProps;
202 m_autoSave = props.m_autoSave;
203 m_subDirValidityHidden = props.m_subDirValidityHidden;
204 m_filepath = props.m_filepath;
205 m_node = new ViewPropertySettings();
206 //*m_node = *(props.m_node);
207 }
208
209 return *this;
210 }