1 /***************************************************************************
2 * Copyright (C) 2006-2010 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 "additionalinfoaccessor.h"
24 #include "dolphin_directoryviewpropertysettings.h"
25 #include "dolphin_generalsettings.h"
27 #include <kcomponentdata.h>
29 #include <kstandarddirs.h>
36 #include "settings/dolphinsettings.h"
38 ViewProperties::ViewProperties(const KUrl
& url
) :
39 m_changedProps(false),
43 // We try and save it to the file .directory in the directory being viewed.
44 // If the directory is not writable by the user or the directory is not local,
45 // we store the properties information in a local file.
46 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
47 const bool useGlobalViewProps
= settings
->globalViewProps();
48 if (useGlobalViewProps
) {
49 m_filePath
= destinationDir("global");
50 } else if (url
.isLocalFile()) {
51 m_filePath
= url
.toLocalFile();
52 const QFileInfo
info(m_filePath
);
53 if (!info
.isWritable()) {
54 m_filePath
= destinationDir("local") + m_filePath
;
57 m_filePath
= destinationDir("remote") + m_filePath
;
60 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
61 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
63 const bool useDefaultProps
= !useGlobalViewProps
&&
64 (!QFileInfo(file
).exists() ||
65 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
66 if (useDefaultProps
) {
67 // If the .directory file does not exist or the timestamp is too old,
68 // use the values from the global .directory file instead, which acts
69 // as default view for new folders in this case.
70 settings
->setGlobalViewProps(true);
72 ViewProperties
defaultProps(url
);
73 setDirProperties(defaultProps
);
75 settings
->setGlobalViewProps(false);
76 m_changedProps
= false;
80 ViewProperties::~ViewProperties()
82 if (m_changedProps
&& m_autoSave
) {
90 void ViewProperties::setViewMode(DolphinView::Mode mode
)
92 if (m_node
->viewMode() != mode
) {
93 m_node
->setViewMode(mode
);
98 DolphinView::Mode
ViewProperties::viewMode() const
100 return static_cast<DolphinView::Mode
>(m_node
->viewMode());
103 void ViewProperties::setShowPreview(bool show
)
105 if (m_node
->showPreview() != show
) {
106 m_node
->setShowPreview(show
);
111 bool ViewProperties::showPreview() const
113 return m_node
->showPreview();
116 void ViewProperties::setShowHiddenFiles(bool show
)
118 if (m_node
->showHiddenFiles() != show
) {
119 m_node
->setShowHiddenFiles(show
);
124 void ViewProperties::setCategorizedSorting(bool categorized
)
126 if (m_node
->categorizedSorting() != categorized
) {
127 m_node
->setCategorizedSorting(categorized
);
132 bool ViewProperties::categorizedSorting() const
134 return m_node
->categorizedSorting();
137 bool ViewProperties::showHiddenFiles() const
139 return m_node
->showHiddenFiles();
142 void ViewProperties::setSorting(DolphinView::Sorting sorting
)
144 if (m_node
->sorting() != sorting
) {
145 m_node
->setSorting(sorting
);
150 DolphinView::Sorting
ViewProperties::sorting() const
152 return static_cast<DolphinView::Sorting
>(m_node
->sorting());
155 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
157 if (m_node
->sortOrder() != sortOrder
) {
158 m_node
->setSortOrder(sortOrder
);
163 Qt::SortOrder
ViewProperties::sortOrder() const
165 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
168 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
170 if (m_node
->sortFoldersFirst() != foldersFirst
) {
171 m_node
->setSortFoldersFirst(foldersFirst
);
176 bool ViewProperties::sortFoldersFirst() const
178 return m_node
->sortFoldersFirst();
181 void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList
& list
)
183 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
186 foreach (KFileItemDelegate::Information currentInfo
, list
) {
187 infoMask
= infoMask
| infoAccessor
.bitValue(currentInfo
);
190 const int encodedInfo
= encodedAdditionalInfo(infoMask
);
191 if (m_node
->additionalInfo() != encodedInfo
) {
192 m_node
->setAdditionalInfo(encodedInfo
);
197 KFileItemDelegate::InformationList
ViewProperties::additionalInfo() const
199 KFileItemDelegate::InformationList usedInfos
;
201 const int decodedInfo
= decodedAdditionalInfo();
203 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
204 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
206 foreach (const KFileItemDelegate::Information info
, infoKeys
) {
207 if (decodedInfo
& infoAccessor
.bitValue(info
)) {
208 usedInfos
.append(info
);
216 void ViewProperties::setDirProperties(const ViewProperties
& props
)
218 setViewMode(props
.viewMode());
219 setShowPreview(props
.showPreview());
220 setShowHiddenFiles(props
.showHiddenFiles());
221 setCategorizedSorting(props
.categorizedSorting());
222 setSorting(props
.sorting());
223 setSortOrder(props
.sortOrder());
224 setSortFoldersFirst(props
.sortFoldersFirst());
225 setAdditionalInfo(props
.additionalInfo());
228 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
230 m_autoSave
= autoSave
;
233 bool ViewProperties::isAutoSaveEnabled() const
238 void ViewProperties::updateTimeStamp()
240 m_changedProps
= true;
241 m_node
->setTimestamp(QDateTime::currentDateTime());
244 void ViewProperties::save()
246 KStandardDirs::makeDir(m_filePath
);
247 m_node
->writeConfig();
248 m_changedProps
= false;
251 KUrl
ViewProperties::mirroredDirectory()
253 QString basePath
= KGlobal::mainComponent().componentName();
254 basePath
.append("/view_properties/");
255 return KUrl(KStandardDirs::locateLocal("data", basePath
));
258 QString
ViewProperties::destinationDir(const QString
& subDir
) const
260 QString basePath
= KGlobal::mainComponent().componentName();
261 basePath
.append("/view_properties/").append(subDir
);
262 return KStandardDirs::locateLocal("data", basePath
);
265 int ViewProperties::encodedAdditionalInfo(int info
) const
267 int encodedInfo
= m_node
->additionalInfo();
269 switch (viewMode()) {
270 case DolphinView::DetailsView
:
271 encodedInfo
= (encodedInfo
& 0xFFFF00) | info
;
273 case DolphinView::IconsView
:
274 encodedInfo
= (encodedInfo
& 0xFF00FF) | (info
<< 8);
276 case DolphinView::ColumnView
:
277 encodedInfo
= (encodedInfo
& 0x00FFFF) | (info
<< 16);
285 int ViewProperties::decodedAdditionalInfo() const
287 int decodedInfo
= m_node
->additionalInfo();
289 switch (viewMode()) {
290 case DolphinView::DetailsView
:
291 decodedInfo
= decodedInfo
& 0xFF;
292 if (decodedInfo
== 0) {
293 // A details view without any additional info makes no sense, hence
294 // provide at least a size-info and date-info as fallback
295 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
296 decodedInfo
= infoAccessor
.bitValue(KFileItemDelegate::Size
) |
297 infoAccessor
.bitValue(KFileItemDelegate::ModificationTime
);
300 case DolphinView::IconsView
:
301 decodedInfo
= (decodedInfo
>> 8) & 0xFF;
303 case DolphinView::ColumnView
:
304 decodedInfo
= (decodedInfo
>> 16) & 0xFF;