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 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
44 const bool useGlobalViewProps
= settings
->globalViewProps();
46 // We try and save it to the file .directory in the directory being viewed.
47 // If the directory is not writable by the user or the directory is not local,
48 // we store the properties information in a local file.
49 const bool isSearchUrl
= url
.protocol().contains("search");
51 m_filePath
= destinationDir("search");
52 } else if (useGlobalViewProps
) {
53 m_filePath
= destinationDir("global");
54 } else if (url
.isLocalFile()) {
55 m_filePath
= url
.toLocalFile();
56 const QFileInfo
info(m_filePath
);
57 if (!info
.isWritable()) {
58 m_filePath
= destinationDir("local") + m_filePath
;
61 m_filePath
= destinationDir("remote") + m_filePath
;
64 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
65 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
67 // If the .directory file does not exist or the timestamp is too old,
68 // use default values instead.
69 const bool useDefaultProps
= (!useGlobalViewProps
|| isSearchUrl
) &&
70 (!QFileInfo(file
).exists() ||
71 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
72 if (useDefaultProps
) {
74 setViewMode(DolphinView::DetailsView
);
75 setAdditionalInfo(KFileItemDelegate::InformationList() << KFileItemDelegate::LocalPathOrUrl
);
77 // The global view-properties act as default for directories without
78 // any view-property configuration
79 settings
->setGlobalViewProps(true);
81 ViewProperties
defaultProps(url
);
82 setDirProperties(defaultProps
);
84 settings
->setGlobalViewProps(false);
85 m_changedProps
= false;
90 ViewProperties::~ViewProperties()
92 if (m_changedProps
&& m_autoSave
) {
100 void ViewProperties::setViewMode(DolphinView::Mode mode
)
102 if (m_node
->viewMode() != mode
) {
103 m_node
->setViewMode(mode
);
108 DolphinView::Mode
ViewProperties::viewMode() const
110 return static_cast<DolphinView::Mode
>(m_node
->viewMode());
113 void ViewProperties::setShowPreview(bool show
)
115 if (m_node
->showPreview() != show
) {
116 m_node
->setShowPreview(show
);
121 bool ViewProperties::showPreview() const
123 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();
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::setSortFoldersFirst(bool foldersFirst
)
180 if (m_node
->sortFoldersFirst() != foldersFirst
) {
181 m_node
->setSortFoldersFirst(foldersFirst
);
186 bool ViewProperties::sortFoldersFirst() const
188 return m_node
->sortFoldersFirst();
191 void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList
& list
)
193 // See ViewProperties::additionalInfoV2() for the storage format
194 // of the additional information.
196 // Remove the old values stored for the current view-mode
197 const QStringList oldInfoStringList
= m_node
->additionalInfoV2();
198 const QString prefix
= viewModePrefix();
199 QStringList newInfoStringList
= oldInfoStringList
;
200 for (int i
= newInfoStringList
.count() - 1; i
>= 0; --i
) {
201 if (newInfoStringList
.at(i
).startsWith(prefix
)) {
202 newInfoStringList
.removeAt(i
);
206 // Add the updated values for the current view-mode
207 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
208 foreach (KFileItemDelegate::Information info
, list
) {
209 newInfoStringList
.append(prefix
+ infoAccessor
.value(info
));
212 // Only update the information if it has been changed
213 bool changed
= oldInfoStringList
.count() != newInfoStringList
.count();
215 foreach (const QString
& oldInfoString
, oldInfoStringList
) {
216 if (!newInfoStringList
.contains(oldInfoString
)) {
224 if (m_node
->version() < 2) {
225 m_node
->setVersion(2);
227 m_node
->setAdditionalInfoV2(newInfoStringList
);
232 KFileItemDelegate::InformationList
ViewProperties::additionalInfo() const
234 KFileItemDelegate::InformationList usedInfos
;
236 switch (m_node
->version()) {
237 case 1: usedInfos
= additionalInfoV1(); break;
238 case 2: usedInfos
= additionalInfoV2(); break;
239 default: kWarning() << "Unknown version of the view properties";
246 void ViewProperties::setDirProperties(const ViewProperties
& props
)
248 setViewMode(props
.viewMode());
249 setShowPreview(props
.showPreview());
250 setShowHiddenFiles(props
.showHiddenFiles());
251 setCategorizedSorting(props
.categorizedSorting());
252 setSorting(props
.sorting());
253 setSortOrder(props
.sortOrder());
254 setSortFoldersFirst(props
.sortFoldersFirst());
255 setAdditionalInfo(props
.additionalInfo());
258 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
260 m_autoSave
= autoSave
;
263 bool ViewProperties::isAutoSaveEnabled() const
268 void ViewProperties::update()
270 m_changedProps
= true;
271 m_node
->setTimestamp(QDateTime::currentDateTime());
273 // If the view-properties are stored in an older format, take
274 // care to update them to the current format.
275 switch (m_node
->version()) {
277 const KFileItemDelegate::InformationList infoList
= additionalInfoV1();
278 m_node
->setVersion(2);
279 setAdditionalInfo(infoList
);
283 // Current version. Nothing needs to get converted.
286 kWarning() << "Unknown version of the view properties";
290 void ViewProperties::save()
292 KStandardDirs::makeDir(m_filePath
);
293 m_node
->writeConfig();
294 m_changedProps
= false;
297 KUrl
ViewProperties::mirroredDirectory()
299 QString basePath
= KGlobal::mainComponent().componentName();
300 basePath
.append("/view_properties/");
301 return KUrl(KStandardDirs::locateLocal("data", basePath
));
304 QString
ViewProperties::destinationDir(const QString
& subDir
) const
306 QString basePath
= KGlobal::mainComponent().componentName();
307 basePath
.append("/view_properties/").append(subDir
);
308 return KStandardDirs::locateLocal("data", basePath
);
311 KFileItemDelegate::InformationList
ViewProperties::additionalInfoV1() const
313 KFileItemDelegate::InformationList usedInfos
;
315 int decodedInfo
= m_node
->additionalInfo();
317 switch (viewMode()) {
318 case DolphinView::DetailsView
:
319 decodedInfo
= decodedInfo
& 0xFF;
320 if (decodedInfo
== 0) {
321 // A details view without any additional info makes no sense, hence
322 // provide at least a size-info and date-info as fallback
323 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
324 decodedInfo
= infoAccessor
.bitValue(KFileItemDelegate::Size
) |
325 infoAccessor
.bitValue(KFileItemDelegate::ModificationTime
);
328 case DolphinView::IconsView
:
329 decodedInfo
= (decodedInfo
>> 8) & 0xFF;
331 case DolphinView::ColumnView
:
332 decodedInfo
= (decodedInfo
>> 16) & 0xFF;
337 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
338 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
340 foreach (const KFileItemDelegate::Information info
, infoKeys
) {
341 if (decodedInfo
& infoAccessor
.bitValue(info
)) {
342 usedInfos
.append(info
);
349 KFileItemDelegate::InformationList
ViewProperties::additionalInfoV2() const
351 // The shown additional information is stored for each view-mode separately as
352 // string with the view-mode as prefix. Example:
354 // AdditionalInfoV2=Details_Size,Details_Date,Details_Owner,Icon_Size
356 // To get the representation as KFileItemDelegate::InformationList, the current
357 // view-mode must be checked and the values of this mode added to the list.
359 KFileItemDelegate::InformationList usedInfos
;
361 // infoHash allows to get the mapped KFileItemDelegate::Information value
362 // for a stored string-value in a fast way
363 static QHash
<QString
, KFileItemDelegate::Information
> infoHash
;
364 if (infoHash
.isEmpty()) {
365 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
366 const KFileItemDelegate::InformationList keys
= infoAccessor
.keys();
367 foreach (const KFileItemDelegate::Information key
, keys
) {
368 infoHash
.insert(infoAccessor
.value(key
), key
);
372 // Iterate through all stored keys stored as strings and map them to
373 // the corresponding KFileItemDelegate::Information values.
374 const QString prefix
= viewModePrefix();
375 const int prefixLength
= prefix
.length();
376 const QStringList infoStringList
= m_node
->additionalInfoV2();
377 foreach (const QString
& infoString
, infoStringList
) {
378 if (infoString
.startsWith(prefix
)) {
379 const QString key
= infoString
.right(infoString
.length() - prefixLength
);
380 Q_ASSERT(infoHash
.contains(key
));
381 usedInfos
.append(infoHash
.value(key
));
388 QString
ViewProperties::viewModePrefix() const
392 switch (m_node
->viewMode()) {
393 case DolphinView::DetailsView
: prefix
= "Details_"; break;
394 case DolphinView::IconsView
: prefix
= "Icons_"; break;
395 case DolphinView::ColumnView
: prefix
= "Column_"; break;
396 default: kWarning() << "Unknown view-mode of the view properties";