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 "settings/dolphinsettings.h"
24 #include "dolphin_directoryviewpropertysettings.h"
25 #include "dolphin_generalsettings.h"
27 #include <kcomponentdata.h>
29 #include <kstandarddirs.h>
33 #include <nepomuk/resourcemanager.h>
40 bool ViewProperties::m_nepomukSupport
= false;
42 #define FILE_NAME "/.directory"
44 ViewProperties::ViewProperties(const KUrl
& url
) :
45 m_changedProps(false),
50 static bool checkedNepomukSupport
= false;
51 if (!checkedNepomukSupport
) {
52 m_nepomukSupport
= !Nepomuk::ResourceManager::instance()->init();
53 checkedNepomukSupport
= true;
59 m_filepath
= cleanUrl
.path();
61 if ((m_filepath
.length() < 1) || (m_filepath
.at(0) != QChar('/'))) {
62 const QString file
= destinationDir("global") + FILE_NAME
;
63 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
67 // We try and save it to a file in the directory being viewed.
68 // If the directory is not writable by the user or the directory is not local,
69 // we store the properties information in a local file.
70 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
71 const bool useGlobalViewProps
= settings
->globalViewProps();
72 if (useGlobalViewProps
) {
73 m_filepath
= destinationDir("global");
74 } else if (cleanUrl
.isLocalFile()) {
75 const QFileInfo
info(m_filepath
);
76 if (!info
.isWritable()) {
77 m_filepath
= destinationDir("local") + m_filepath
;
80 m_filepath
= destinationDir("remote") + m_filepath
;
83 const QString file
= m_filepath
+ FILE_NAME
;
84 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
86 const bool useDefaultProps
= !useGlobalViewProps
&&
87 (!QFileInfo(file
).exists() ||
88 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
89 if (useDefaultProps
) {
90 // If the .directory file does not exist or the timestamp is too old,
91 // use the values from the global .directory file instead, which acts
92 // as default view for new folders in this case.
93 settings
->setGlobalViewProps(true);
95 ViewProperties
defaultProps(url
);
96 setDirProperties(defaultProps
);
98 settings
->setGlobalViewProps(false);
99 m_changedProps
= false;
103 ViewProperties::~ViewProperties()
105 if (m_changedProps
&& m_autoSave
) {
113 void ViewProperties::setViewMode(DolphinView::Mode mode
)
115 if (m_node
->viewMode() != mode
) {
116 m_node
->setViewMode(mode
);
121 DolphinView::Mode
ViewProperties::viewMode() const
123 return static_cast<DolphinView::Mode
>(m_node
->viewMode());
126 void ViewProperties::setShowPreview(bool show
)
128 if (m_node
->showPreview() != show
) {
129 m_node
->setShowPreview(show
);
134 bool ViewProperties::showPreview() const
136 return m_node
->showPreview();
140 void ViewProperties::setShowHiddenFiles(bool show
)
142 if (m_node
->showHiddenFiles() != show
) {
143 m_node
->setShowHiddenFiles(show
);
148 void ViewProperties::setCategorizedSorting(bool categorized
)
150 if (m_node
->categorizedSorting() != categorized
) {
151 m_node
->setCategorizedSorting(categorized
);
156 bool ViewProperties::categorizedSorting() const
158 return m_node
->categorizedSorting();
162 bool ViewProperties::showHiddenFiles() const
164 return m_node
->showHiddenFiles();
167 void ViewProperties::setSorting(DolphinView::Sorting sorting
)
169 if (m_node
->sorting() != sorting
) {
170 m_node
->setSorting(sorting
);
175 DolphinView::Sorting
ViewProperties::sorting() const
177 // If Nepomuk is not available, return SortByName as fallback if SortByRating
178 // or SortByTags is stored.
179 DolphinView::Sorting sorting
= static_cast<DolphinView::Sorting
>(m_node
->sorting());
180 const bool sortByName
= !m_nepomukSupport
&&
181 ((sorting
== DolphinView::SortByRating
) || (sorting
== DolphinView::SortByTags
));
183 sorting
= DolphinView::SortByName
;
188 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
190 if (m_node
->sortOrder() != sortOrder
) {
191 m_node
->setSortOrder(sortOrder
);
196 Qt::SortOrder
ViewProperties::sortOrder() const
198 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
201 void ViewProperties::setAdditionalInfo(KFileItemDelegate::InformationList list
)
204 foreach (KFileItemDelegate::Information currentInfo
, list
) {
205 switch (currentInfo
) {
206 case KFileItemDelegate::Size
:
207 info
= info
| SizeInfo
;
209 case KFileItemDelegate::ModificationTime
:
210 info
= info
| DateInfo
;
212 case KFileItemDelegate::Permissions
:
213 info
= info
| PermissionsInfo
;
215 case KFileItemDelegate::Owner
:
216 info
= info
| OwnerInfo
;
218 case KFileItemDelegate::OwnerAndGroup
:
219 info
= info
| GroupInfo
;
221 case KFileItemDelegate::FriendlyMimeType
:
222 info
= info
| TypeInfo
;
229 const int encodedInfo
= encodedAdditionalInfo(info
);
230 if (m_node
->additionalInfo() != encodedInfo
) {
231 m_node
->setAdditionalInfo(encodedInfo
);
236 KFileItemDelegate::InformationList
ViewProperties::additionalInfo() const
238 const int info
= decodedAdditionalInfo();
240 KFileItemDelegate::InformationList list
;
241 if (info
& SizeInfo
) {
242 list
.append(KFileItemDelegate::Size
);
244 if (info
& DateInfo
) {
245 list
.append(KFileItemDelegate::ModificationTime
);
247 if (info
& PermissionsInfo
) {
248 list
.append(KFileItemDelegate::Permissions
);
250 if (info
& OwnerInfo
) {
251 list
.append(KFileItemDelegate::Owner
);
253 if (info
& GroupInfo
) {
254 list
.append(KFileItemDelegate::OwnerAndGroup
);
256 if (info
& TypeInfo
) {
257 list
.append(KFileItemDelegate::FriendlyMimeType
);
264 void ViewProperties::setDirProperties(const ViewProperties
& props
)
266 setViewMode(props
.viewMode());
267 setShowPreview(props
.showPreview());
268 setShowHiddenFiles(props
.showHiddenFiles());
269 setCategorizedSorting(props
.categorizedSorting());
270 setSorting(props
.sorting());
271 setSortOrder(props
.sortOrder());
272 setAdditionalInfo(props
.additionalInfo());
275 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
277 m_autoSave
= autoSave
;
280 bool ViewProperties::isAutoSaveEnabled() const
285 void ViewProperties::updateTimeStamp()
287 m_changedProps
= true;
288 m_node
->setTimestamp(QDateTime::currentDateTime());
291 void ViewProperties::save()
293 KStandardDirs::makeDir(m_filepath
);
294 m_node
->writeConfig();
295 m_changedProps
= false;
298 KUrl
ViewProperties::mirroredDirectory()
300 QString basePath
= KGlobal::mainComponent().componentName();
301 basePath
.append("/view_properties/");
302 return KUrl(KStandardDirs::locateLocal("data", basePath
));
305 QString
ViewProperties::destinationDir(const QString
& subDir
) const
307 QString basePath
= KGlobal::mainComponent().componentName();
308 basePath
.append("/view_properties/").append(subDir
);
309 return KStandardDirs::locateLocal("data", basePath
);
312 int ViewProperties::encodedAdditionalInfo(int info
) const
314 int encodedInfo
= m_node
->additionalInfo();
316 switch (viewMode()) {
317 case DolphinView::DetailsView
:
318 encodedInfo
= (encodedInfo
& 0xFFFF00) | info
;
320 case DolphinView::IconsView
:
321 encodedInfo
= (encodedInfo
& 0xFF00FF) | (info
<< 8);
323 case DolphinView::ColumnView
:
324 encodedInfo
= (encodedInfo
& 0x00FFFF) | (info
<< 16);
332 int ViewProperties::decodedAdditionalInfo() const
334 int decodedInfo
= m_node
->additionalInfo();
336 switch (viewMode()) {
337 case DolphinView::DetailsView
:
338 decodedInfo
= decodedInfo
& 0xFF;
339 if (decodedInfo
== NoInfo
) {
340 // a details view without any additional info makes no sense, hence
341 // provide at least a size-info and date-info as fallback
342 decodedInfo
= SizeInfo
| DateInfo
;
345 case DolphinView::IconsView
:
346 decodedInfo
= (decodedInfo
>> 8) & 0xFF;
348 case DolphinView::ColumnView
:
349 decodedInfo
= (decodedInfo
>> 16) & 0xFF;