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