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 ***************************************************************************/
21 #include "viewproperties.h"
23 #include "dolphinsettings.h"
24 #include "dolphin_directoryviewpropertysettings.h"
25 #include "dolphin_generalsettings.h"
27 #include <kcomponentdata.h>
29 #include <kstandarddirs.h>
36 #define FILE_NAME "/.directory"
38 ViewProperties::ViewProperties(const KUrl
& url
) :
39 m_changedProps(false),
45 m_filepath
= cleanUrl
.path();
47 if ((m_filepath
.length() < 1) || (m_filepath
.at(0) != QChar('/'))) {
48 m_node
= new ViewPropertySettings();
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 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
56 const bool useGlobalViewProps
= settings
->globalViewProps();
57 if (useGlobalViewProps
) {
58 m_filepath
= destinationDir("global");
59 } else if (cleanUrl
.isLocalFile()) {
60 const QFileInfo
info(m_filepath
);
61 if (!info
.isWritable()) {
62 m_filepath
= destinationDir("local") + m_filepath
;
65 m_filepath
= destinationDir("remote") + m_filepath
;
68 const QString
file(m_filepath
+ FILE_NAME
);
69 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
71 const bool useDefaultProps
= !useGlobalViewProps
&&
72 (!QFileInfo(file
).exists() ||
73 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
74 if (useDefaultProps
) {
75 // If the .directory file does not exist or the timestamp is too old,
76 // use the values from the global .directory file instead, which acts
77 // as default view for new folders in this case.
78 settings
->setGlobalViewProps(true);
80 ViewProperties
defaultProps(url
);
81 setDirProperties(defaultProps
);
83 settings
->setGlobalViewProps(false);
84 m_changedProps
= false;
88 ViewProperties::~ViewProperties()
90 if (m_changedProps
&& m_autoSave
) {
98 void ViewProperties::setViewMode(DolphinView::Mode mode
)
100 if (m_node
->viewMode() != mode
) {
101 m_node
->setViewMode(mode
);
106 DolphinView::Mode
ViewProperties::viewMode() const
108 return static_cast<DolphinView::Mode
>(m_node
->viewMode());
111 void ViewProperties::setShowPreview(bool show
)
113 if (m_node
->showPreview() != show
) {
114 m_node
->setShowPreview(show
);
119 bool ViewProperties::showPreview() const
121 return m_node
->showPreview();
125 void ViewProperties::setShowHiddenFiles(bool show
)
127 if (m_node
->showHiddenFiles() != show
) {
128 m_node
->setShowHiddenFiles(show
);
133 void ViewProperties::setCategorizedSorting(bool categorized
)
135 if (m_node
->categorizedSorting() != categorized
) {
136 m_node
->setCategorizedSorting(categorized
);
141 bool ViewProperties::categorizedSorting() const
143 return m_node
->categorizedSorting();
147 bool ViewProperties::showHiddenFiles() const
149 return m_node
->showHiddenFiles();
152 void ViewProperties::setSorting(DolphinView::Sorting sorting
)
154 if (m_node
->sorting() != sorting
) {
155 m_node
->setSorting(sorting
);
160 DolphinView::Sorting
ViewProperties::sorting() const
162 return static_cast<DolphinView::Sorting
>(m_node
->sorting());
165 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
167 if (m_node
->sortOrder() != sortOrder
) {
168 m_node
->setSortOrder(sortOrder
);
173 Qt::SortOrder
ViewProperties::sortOrder() const
175 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
178 void ViewProperties::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
180 if (m_node
->additionalInfo() != info
) {
181 m_node
->setAdditionalInfo(info
);
186 KFileItemDelegate::AdditionalInformation
ViewProperties::additionalInfo() const
188 return static_cast<KFileItemDelegate::AdditionalInformation
>(m_node
->additionalInfo());
192 void ViewProperties::setDirProperties(const ViewProperties
& props
)
194 setViewMode(props
.viewMode());
195 setShowPreview(props
.showPreview());
196 setShowHiddenFiles(props
.showHiddenFiles());
197 setCategorizedSorting(props
.categorizedSorting());
198 setSorting(props
.sorting());
199 setSortOrder(props
.sortOrder());
200 setAdditionalInfo(props
.additionalInfo());
203 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
205 m_autoSave
= autoSave
;
208 bool ViewProperties::isAutoSaveEnabled() const
213 void ViewProperties::updateTimeStamp()
215 m_changedProps
= true;
216 m_node
->setTimestamp(QDateTime::currentDateTime());
219 void ViewProperties::save()
221 KStandardDirs::makeDir(m_filepath
);
222 m_node
->writeConfig();
223 m_changedProps
= false;
226 KUrl
ViewProperties::mirroredDirectory()
228 QString basePath
= KGlobal::mainComponent().componentName();
229 basePath
.append("/view_properties/");
230 return KUrl(KStandardDirs::locateLocal("data", basePath
));
233 QString
ViewProperties::destinationDir(const QString
& subDir
) const
235 QString basePath
= KGlobal::mainComponent().componentName();
236 basePath
.append("/view_properties/").append(subDir
);
237 return KStandardDirs::locateLocal("data", basePath
);