1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (<peter.penz@gmx.at>) *
3 * Copyright (C) 2006 by Aaron J. Seigo (<aseigo@kde.org>) *
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. *
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. *
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 ***************************************************************************/
27 #include <kcomponentdata.h>
29 #include <kstandarddirs.h>
31 #include <kcomponentdata.h>
33 #include "viewproperties.h"
34 #include "dolphinsettings.h"
35 #include "dolphin_generalsettings.h"
37 #define FILE_NAME "/.directory"
39 ViewProperties::ViewProperties(const KUrl
& url
) :
40 m_changedProps(false),
46 m_filepath
= cleanUrl
.path();
48 if ((m_filepath
.length() < 1) || (m_filepath
.at(0) != QChar('/'))) {
49 m_node
= new ViewPropertySettings();
53 // We try and save it to a file in the directory being viewed.
54 // If the directory is not writable by the user or the directory is not local,
55 // we store the properties information in a local file.
56 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
57 const bool useGlobalViewProps
= settings
->globalViewProps();
58 if (useGlobalViewProps
) {
59 m_filepath
= destinationDir("global");
61 else if (cleanUrl
.isLocalFile()) {
62 const QFileInfo
info(m_filepath
);
63 if (!info
.isWritable()) {
64 m_filepath
= destinationDir("local") + m_filepath
;
68 m_filepath
= destinationDir("remote") + m_filepath
;
71 const QString
file(m_filepath
+ FILE_NAME
);
72 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
74 const bool useDefaultProps
= !useGlobalViewProps
&&
75 (!QFileInfo(file
).exists() ||
76 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
77 if (useDefaultProps
) {
78 // If the .directory file does not exist or the timestamp is too old,
79 // use the values from the global .directory file instead, which acts
80 // as default view for new folders in this case.
81 settings
->setGlobalViewProps(true);
83 ViewProperties
defaultProps(url
);
84 setDirProperties(defaultProps
);
86 settings
->setGlobalViewProps(false);
87 m_changedProps
= false;
91 ViewProperties::~ViewProperties()
93 if (m_changedProps
&& m_autoSave
) {
101 void ViewProperties::setViewMode(DolphinView::Mode mode
)
103 if (m_node
->viewMode() != mode
) {
104 m_node
->setViewMode(mode
);
109 DolphinView::Mode
ViewProperties::viewMode() const
111 return static_cast<DolphinView::Mode
>(m_node
->viewMode());
114 void ViewProperties::setShowPreview(bool show
)
116 if (m_node
->showPreview() != show
) {
117 m_node
->setShowPreview(show
);
122 bool ViewProperties::showPreview() const
124 return m_node
->showPreview();
128 void ViewProperties::setShowHiddenFiles(bool show
)
130 if (m_node
->showHiddenFiles() != show
) {
131 m_node
->setShowHiddenFiles(show
);
136 bool ViewProperties::showHiddenFiles() const
138 return m_node
->showHiddenFiles();
141 void ViewProperties::setSorting(DolphinView::Sorting sorting
)
143 if (m_node
->sorting() != sorting
) {
144 m_node
->setSorting(sorting
);
149 DolphinView::Sorting
ViewProperties::sorting() const
151 return static_cast<DolphinView::Sorting
>(m_node
->sorting());
154 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
156 if (m_node
->sortOrder() != sortOrder
) {
157 m_node
->setSortOrder(sortOrder
);
162 Qt::SortOrder
ViewProperties::sortOrder() const
164 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
167 void ViewProperties::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
169 if (m_node
->additionalInfo() != info
) {
170 m_node
->setAdditionalInfo(info
);
175 KFileItemDelegate::AdditionalInformation
ViewProperties::additionalInfo() const
177 return static_cast<KFileItemDelegate::AdditionalInformation
>(m_node
->additionalInfo());
181 void ViewProperties::setDirProperties(const ViewProperties
& props
)
183 setViewMode(props
.viewMode());
184 setShowPreview(props
.showPreview());
185 setShowHiddenFiles(props
.showHiddenFiles());
186 setSorting(props
.sorting());
187 setSortOrder(props
.sortOrder());
188 setAdditionalInfo(props
.additionalInfo());
191 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
193 m_autoSave
= autoSave
;
196 bool ViewProperties::isAutoSaveEnabled() const
201 void ViewProperties::updateTimeStamp()
203 m_changedProps
= true;
204 m_node
->setTimestamp(QDateTime::currentDateTime());
207 void ViewProperties::save()
209 KStandardDirs::makeDir(m_filepath
);
210 m_node
->writeConfig();
211 m_changedProps
= false;
214 KUrl
ViewProperties::mirroredDirectory()
216 QString basePath
= KGlobal::mainComponent().componentName();
217 basePath
.append("/view_properties/");
218 return KUrl(KStandardDirs::locateLocal("data", basePath
));
221 QString
ViewProperties::destinationDir(const QString
& subDir
) const
223 QString basePath
= KGlobal::mainComponent().componentName();
224 basePath
.append("/view_properties/").append(subDir
);
225 return KStandardDirs::locateLocal("data", basePath
);