]> cloud.milkyroute.net Git - dolphin.git/blob - src/viewproperties.cpp
update to changes in class KGlobal (kdelibs)
[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 * 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 <kinstance.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 const bool useGlobalViewProps = DolphinSettings::instance().generalSettings()->globalViewProps();
56 if (useGlobalViewProps) {
57 m_filepath = destinationDir("global");
58 }
59 else if (cleanUrl.isLocalFile()) {
60 const QFileInfo info(m_filepath);
61 if (!info.isWritable()) {
62 m_filepath = destinationDir("local") + m_filepath;
63 }
64 }
65 else {
66 m_filepath = destinationDir("remote") + m_filepath;
67 }
68
69 m_node = new ViewPropertySettings(KSharedConfig::openConfig(m_filepath + FILE_NAME));
70 }
71
72 ViewProperties::~ViewProperties()
73 {
74 if (m_changedProps && m_autoSave) {
75 save();
76 }
77
78 delete m_node;
79 m_node = 0;
80 }
81
82 void ViewProperties::setViewMode(DolphinView::Mode mode)
83 {
84 if (m_node->viewMode() != mode) {
85 m_node->setViewMode(mode);
86 updateTimeStamp();
87 }
88 }
89
90 DolphinView::Mode ViewProperties::viewMode() const
91 {
92 return static_cast<DolphinView::Mode>(m_node->viewMode());
93 }
94
95 void ViewProperties::setShowPreview(bool show)
96 {
97 if (m_node->showPreview() != show) {
98 m_node->setShowPreview(show);
99 updateTimeStamp();
100 }
101 }
102
103 bool ViewProperties::showPreview() const
104 {
105 return m_node->showPreview();
106 }
107
108
109 void ViewProperties::setShowHiddenFiles(bool show)
110 {
111 if (m_node->showHiddenFiles() != show) {
112 m_node->setShowHiddenFiles(show);
113 updateTimeStamp();
114 }
115 }
116
117 bool ViewProperties::showHiddenFiles() const
118 {
119 return m_node->showHiddenFiles();
120 }
121
122 void ViewProperties::setSorting(DolphinView::Sorting sorting)
123 {
124 if (m_node->sorting() != sorting) {
125 m_node->setSorting(sorting);
126 updateTimeStamp();
127 }
128 }
129
130 DolphinView::Sorting ViewProperties::sorting() const
131 {
132 return static_cast<DolphinView::Sorting>(m_node->sorting());
133 }
134
135 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder)
136 {
137 if (m_node->sortOrder() != sortOrder) {
138 m_node->setSortOrder(sortOrder);
139 updateTimeStamp();
140 }
141 }
142
143 Qt::SortOrder ViewProperties::sortOrder() const
144 {
145 return static_cast<Qt::SortOrder>(m_node->sortOrder());
146 }
147
148 void ViewProperties::setDirProperties(const ViewProperties& props)
149 {
150 setViewMode(props.viewMode());
151 setShowPreview(props.showPreview());
152 setShowHiddenFiles(props.showHiddenFiles());
153 setSorting(props.sorting());
154 setSortOrder(props.sortOrder());
155 }
156
157 void ViewProperties::setAutoSaveEnabled(bool autoSave)
158 {
159 m_autoSave = autoSave;
160 }
161
162 bool ViewProperties::isAutoSaveEnabled() const
163 {
164 return m_autoSave;
165 }
166
167 void ViewProperties::updateTimeStamp()
168 {
169 m_changedProps = true;
170 m_node->setTimestamp(QDateTime::currentDateTime());
171 }
172
173 void ViewProperties::save()
174 {
175 KStandardDirs::makeDir(m_filepath);
176 m_node->writeConfig();
177 m_changedProps = false;
178 }
179
180 QString ViewProperties::destinationDir(const QString& subDir) const
181 {
182 QString basePath = KGlobal::mainComponent().componentName();
183 basePath.append("/view_properties/").append(subDir);
184 return KStandardDirs::locateLocal("data", basePath);
185 }
186
187 ViewProperties::ViewProperties(const ViewProperties& props)
188 {
189 assert(false);
190 }
191
192 ViewProperties& ViewProperties::operator = (const ViewProperties& props)
193 {
194 assert(false);
195 }