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>
32 #include "viewproperties.h"
33 #include "dolphinsettings.h"
34 #include "dolphin_directoryviewpropertysettings.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");
60 } else if (cleanUrl
.isLocalFile()) {
61 const QFileInfo
info(m_filepath
);
62 if (!info
.isWritable()) {
63 m_filepath
= destinationDir("local") + m_filepath
;
66 m_filepath
= destinationDir("remote") + m_filepath
;
69 const QString
file(m_filepath
+ FILE_NAME
);
70 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
72 const bool useDefaultProps
= !useGlobalViewProps
&&
73 (!QFileInfo(file
).exists() ||
74 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
75 if (useDefaultProps
) {
76 // If the .directory file does not exist or the timestamp is too old,
77 // use the values from the global .directory file instead, which acts
78 // as default view for new folders in this case.
79 settings
->setGlobalViewProps(true);
81 ViewProperties
defaultProps(url
);
82 setDirProperties(defaultProps
);
84 settings
->setGlobalViewProps(false);
85 m_changedProps
= false;
89 ViewProperties::~ViewProperties()
91 if (m_changedProps
&& m_autoSave
) {
99 void ViewProperties::setViewMode(DolphinView::Mode mode
)
101 if (m_node
->viewMode() != mode
) {
102 m_node
->setViewMode(mode
);
107 DolphinView::Mode
ViewProperties::viewMode() const
109 return static_cast<DolphinView::Mode
>(m_node
->viewMode());
112 void ViewProperties::setShowPreview(bool show
)
114 if (m_node
->showPreview() != show
) {
115 m_node
->setShowPreview(show
);
120 bool ViewProperties::showPreview() const
122 return m_node
->showPreview();
126 void ViewProperties::setShowHiddenFiles(bool show
)
128 if (m_node
->showHiddenFiles() != show
) {
129 m_node
->setShowHiddenFiles(show
);
134 void ViewProperties::setCategorizedSorting(bool categorized
)
136 if (m_node
->categorizedSorting() != categorized
) {
137 m_node
->setCategorizedSorting(categorized
);
142 bool ViewProperties::categorizedSorting() const
144 return m_node
->categorizedSorting();
148 bool ViewProperties::showHiddenFiles() const
150 return m_node
->showHiddenFiles();
153 void ViewProperties::setSorting(DolphinView::Sorting sorting
)
155 if (m_node
->sorting() != sorting
) {
156 m_node
->setSorting(sorting
);
161 DolphinView::Sorting
ViewProperties::sorting() const
163 return static_cast<DolphinView::Sorting
>(m_node
->sorting());
166 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
168 if (m_node
->sortOrder() != sortOrder
) {
169 m_node
->setSortOrder(sortOrder
);
174 Qt::SortOrder
ViewProperties::sortOrder() const
176 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
179 void ViewProperties::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info
)
181 if (m_node
->additionalInfo() != info
) {
182 m_node
->setAdditionalInfo(info
);
187 KFileItemDelegate::AdditionalInformation
ViewProperties::additionalInfo() const
189 return static_cast<KFileItemDelegate::AdditionalInformation
>(m_node
->additionalInfo());
193 void ViewProperties::setDirProperties(const ViewProperties
& props
)
195 setViewMode(props
.viewMode());
196 setShowPreview(props
.showPreview());
197 setShowHiddenFiles(props
.showHiddenFiles());
198 setCategorizedSorting(props
.categorizedSorting());
199 setSorting(props
.sorting());
200 setSortOrder(props
.sortOrder());
201 setAdditionalInfo(props
.additionalInfo());
204 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
206 m_autoSave
= autoSave
;
209 bool ViewProperties::isAutoSaveEnabled() const
214 void ViewProperties::updateTimeStamp()
216 m_changedProps
= true;
217 m_node
->setTimestamp(QDateTime::currentDateTime());
220 void ViewProperties::save()
222 KStandardDirs::makeDir(m_filepath
);
223 m_node
->writeConfig();
224 m_changedProps
= false;
227 KUrl
ViewProperties::mirroredDirectory()
229 QString basePath
= KGlobal::mainComponent().componentName();
230 basePath
.append("/view_properties/");
231 return KUrl(KStandardDirs::locateLocal("data", basePath
));
234 QString
ViewProperties::destinationDir(const QString
& subDir
) const
236 QString basePath
= KGlobal::mainComponent().componentName();
237 basePath
.append("/view_properties/").append(subDir
);
238 return KStandardDirs::locateLocal("data", basePath
);