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>
29 #include <KStandardDirs>
36 #include "settings/dolphinsettings.h"
39 // String representation to mark the additional properties of
40 // the details view as customized by the user. See
41 // ViewProperties::additionalInfoV2() for more information.
42 const char* CustomizedDetailsString
= "CustomizedDetails";
45 ViewProperties::ViewProperties(const KUrl
& url
) :
46 m_changedProps(false),
50 GeneralSettings
* settings
= DolphinSettings::instance().generalSettings();
51 const bool useGlobalViewProps
= settings
->globalViewProps();
52 bool useDetailsViewWithPath
= false;
54 // We try and save it to the file .directory in the directory being viewed.
55 // If the directory is not writable by the user or the directory is not local,
56 // we store the properties information in a local file.
57 if (useGlobalViewProps
) {
58 m_filePath
= destinationDir("global");
59 } else if (url
.protocol().contains("search")) {
60 m_filePath
= destinationDir("search");
61 useDetailsViewWithPath
= true;
62 } else if (url
.protocol() == QLatin1String("trash")) {
63 m_filePath
= destinationDir("trash");
64 useDetailsViewWithPath
= true;
65 } else if (url
.isLocalFile()) {
66 m_filePath
= url
.toLocalFile();
67 const QFileInfo
info(m_filePath
);
68 if (!info
.isWritable() || !isPartOfHome(m_filePath
)) {
69 m_filePath
= destinationDir("local") + m_filePath
;
72 m_filePath
= destinationDir("remote") + m_filePath
;
75 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
76 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
78 // If the .directory file does not exist or the timestamp is too old,
79 // use default values instead.
80 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
81 (!QFileInfo(file
).exists() ||
82 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
83 if (useDefaultProps
) {
84 if (useDetailsViewWithPath
) {
85 setViewMode(DolphinView::DetailsView
);
86 setAdditionalInfo(KFileItemDelegate::InformationList() << KFileItemDelegate::LocalPathOrUrl
);
88 // The global view-properties act as default for directories without
89 // any view-property configuration
90 settings
->setGlobalViewProps(true);
92 ViewProperties
defaultProps(url
);
93 setDirProperties(defaultProps
);
95 settings
->setGlobalViewProps(false);
96 m_changedProps
= false;
101 ViewProperties::~ViewProperties()
103 if (m_changedProps
&& m_autoSave
) {
111 void ViewProperties::setViewMode(DolphinView::Mode mode
)
113 if (m_node
->viewMode() != mode
) {
114 m_node
->setViewMode(mode
);
119 DolphinView::Mode
ViewProperties::viewMode() const
121 return static_cast<DolphinView::Mode
>(m_node
->viewMode());
124 void ViewProperties::setShowPreview(bool show
)
126 if (m_node
->showPreview() != show
) {
127 m_node
->setShowPreview(show
);
132 bool ViewProperties::showPreview() const
134 return m_node
->showPreview();
137 void ViewProperties::setShowHiddenFiles(bool show
)
139 if (m_node
->showHiddenFiles() != show
) {
140 m_node
->setShowHiddenFiles(show
);
145 void ViewProperties::setCategorizedSorting(bool categorized
)
147 if (m_node
->categorizedSorting() != categorized
) {
148 m_node
->setCategorizedSorting(categorized
);
153 bool ViewProperties::categorizedSorting() const
155 return m_node
->categorizedSorting();
158 bool ViewProperties::showHiddenFiles() const
160 return m_node
->showHiddenFiles();
163 void ViewProperties::setSorting(DolphinView::Sorting sorting
)
165 if (m_node
->sorting() != sorting
) {
166 m_node
->setSorting(sorting
);
171 DolphinView::Sorting
ViewProperties::sorting() const
173 return static_cast<DolphinView::Sorting
>(m_node
->sorting());
176 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
178 if (m_node
->sortOrder() != sortOrder
) {
179 m_node
->setSortOrder(sortOrder
);
184 Qt::SortOrder
ViewProperties::sortOrder() const
186 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
189 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
191 if (m_node
->sortFoldersFirst() != foldersFirst
) {
192 m_node
->setSortFoldersFirst(foldersFirst
);
197 bool ViewProperties::sortFoldersFirst() const
199 return m_node
->sortFoldersFirst();
202 void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList
& list
)
204 // See ViewProperties::additionalInfoV2() for the storage format
205 // of the additional information.
207 // Remove the old values stored for the current view-mode
208 const QStringList oldInfoStringList
= m_node
->additionalInfoV2();
209 const QString prefix
= viewModePrefix();
210 QStringList newInfoStringList
= oldInfoStringList
;
211 for (int i
= newInfoStringList
.count() - 1; i
>= 0; --i
) {
212 if (newInfoStringList
.at(i
).startsWith(prefix
)) {
213 newInfoStringList
.removeAt(i
);
217 // Add the updated values for the current view-mode
218 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
219 foreach (KFileItemDelegate::Information info
, list
) {
220 newInfoStringList
.append(prefix
+ infoAccessor
.value(info
));
223 // Only update the information if it has been changed
224 bool changed
= oldInfoStringList
.count() != newInfoStringList
.count();
226 foreach (const QString
& oldInfoString
, oldInfoStringList
) {
227 if (!newInfoStringList
.contains(oldInfoString
)) {
235 if (m_node
->version() < 2) {
236 m_node
->setVersion(2);
239 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
240 && !newInfoStringList
.contains(CustomizedDetailsString
);
241 if (markCustomizedDetails
) {
242 // The additional information of the details-view has been modified. Set a marker,
243 // so that it is allowed to also show no additional information
244 // (see fallback in ViewProperties::additionalInfoV2, if no additional information is
246 newInfoStringList
.append(CustomizedDetailsString
);
249 m_node
->setAdditionalInfoV2(newInfoStringList
);
254 KFileItemDelegate::InformationList
ViewProperties::additionalInfo() const
256 KFileItemDelegate::InformationList usedInfo
;
258 switch (m_node
->version()) {
259 case 1: usedInfo
= additionalInfoV1(); break;
260 case 2: usedInfo
= additionalInfoV2(); break;
261 default: kWarning() << "Unknown version of the view properties";
268 void ViewProperties::setDirProperties(const ViewProperties
& props
)
270 setViewMode(props
.viewMode());
271 setShowPreview(props
.showPreview());
272 setShowHiddenFiles(props
.showHiddenFiles());
273 setCategorizedSorting(props
.categorizedSorting());
274 setSorting(props
.sorting());
275 setSortOrder(props
.sortOrder());
276 setSortFoldersFirst(props
.sortFoldersFirst());
277 setAdditionalInfo(props
.additionalInfo());
280 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
282 m_autoSave
= autoSave
;
285 bool ViewProperties::isAutoSaveEnabled() const
290 void ViewProperties::update()
292 m_changedProps
= true;
293 m_node
->setTimestamp(QDateTime::currentDateTime());
295 // If the view-properties are stored in an older format, take
296 // care to update them to the current format.
297 switch (m_node
->version()) {
299 const KFileItemDelegate::InformationList infoList
= additionalInfoV1();
300 m_node
->setVersion(2);
301 setAdditionalInfo(infoList
);
305 // Current version. Nothing needs to get converted.
308 kWarning() << "Unknown version of the view properties";
312 void ViewProperties::save()
314 KStandardDirs::makeDir(m_filePath
);
315 m_node
->writeConfig();
316 m_changedProps
= false;
319 KUrl
ViewProperties::mirroredDirectory()
321 QString basePath
= KGlobal::mainComponent().componentName();
322 basePath
.append("/view_properties/");
323 return KUrl(KStandardDirs::locateLocal("data", basePath
));
326 QString
ViewProperties::destinationDir(const QString
& subDir
) const
328 QString basePath
= KGlobal::mainComponent().componentName();
329 basePath
.append("/view_properties/").append(subDir
);
330 return KStandardDirs::locateLocal("data", basePath
);
333 KFileItemDelegate::InformationList
ViewProperties::additionalInfoV1() const
335 KFileItemDelegate::InformationList usedInfo
;
337 int decodedInfo
= m_node
->additionalInfo();
339 switch (viewMode()) {
340 case DolphinView::DetailsView
:
341 decodedInfo
= decodedInfo
& 0xFF;
342 if (decodedInfo
== 0) {
343 // A details view without any additional info makes no sense, hence
344 // provide at least a size-info and date-info as fallback
345 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
346 decodedInfo
= infoAccessor
.bitValue(KFileItemDelegate::Size
) |
347 infoAccessor
.bitValue(KFileItemDelegate::ModificationTime
);
350 case DolphinView::IconsView
:
351 decodedInfo
= (decodedInfo
>> 8) & 0xFF;
353 case DolphinView::ColumnView
:
354 decodedInfo
= (decodedInfo
>> 16) & 0xFF;
359 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
360 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
362 foreach (const KFileItemDelegate::Information info
, infoKeys
) {
363 if (decodedInfo
& infoAccessor
.bitValue(info
)) {
364 usedInfo
.append(info
);
371 KFileItemDelegate::InformationList
ViewProperties::additionalInfoV2() const
373 // The shown additional information is stored for each view-mode separately as
374 // string with the view-mode as prefix. Example:
376 // AdditionalInfoV2=Details_Size,Details_Date,Details_Owner,Icon_Size
378 // To get the representation as KFileItemDelegate::InformationList, the current
379 // view-mode must be checked and the values of this mode added to the list.
381 // For the details-view a special case must be respected: Per default the size
382 // and date should be shown without creating a .directory file. Only if
383 // the user explictly has modified the properties of the details view (marked
384 // by "CustomizedDetails"), also a details-view with no additional information
387 KFileItemDelegate::InformationList usedInfo
;
389 // infoHash allows to get the mapped KFileItemDelegate::Information value
390 // for a stored string-value in a fast way
391 static QHash
<QString
, KFileItemDelegate::Information
> infoHash
;
392 if (infoHash
.isEmpty()) {
393 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
394 const KFileItemDelegate::InformationList keys
= infoAccessor
.keys();
395 foreach (const KFileItemDelegate::Information key
, keys
) {
396 infoHash
.insert(infoAccessor
.value(key
), key
);
400 // Iterate through all stored keys stored as strings and map them to
401 // the corresponding KFileItemDelegate::Information values.
402 const QString prefix
= viewModePrefix();
403 const int prefixLength
= prefix
.length();
404 const QStringList infoStringList
= m_node
->additionalInfoV2();
405 foreach (const QString
& infoString
, infoStringList
) {
406 if (infoString
.startsWith(prefix
)) {
407 const QString key
= infoString
.right(infoString
.length() - prefixLength
);
408 Q_ASSERT(infoHash
.contains(key
));
409 usedInfo
.append(infoHash
.value(key
));
413 // For the details view the size and date should be shown per default
414 // until the additional information has been explicitly changed by the user
415 const bool useDefaultValues
= usedInfo
.isEmpty()
416 && (m_node
->viewMode() == DolphinView::DetailsView
)
417 && !infoStringList
.contains(CustomizedDetailsString
);
418 if (useDefaultValues
) {
419 usedInfo
.append(KFileItemDelegate::Size
);
420 usedInfo
.append(KFileItemDelegate::ModificationTime
);
426 QString
ViewProperties::viewModePrefix() const
430 switch (m_node
->viewMode()) {
431 case DolphinView::DetailsView
: prefix
= "Details_"; break;
432 case DolphinView::IconsView
: prefix
= "Icons_"; break;
433 case DolphinView::ColumnView
: prefix
= "Column_"; break;
434 default: kWarning() << "Unknown view-mode of the view properties";
440 bool ViewProperties::isPartOfHome(const QString
& filePath
)
442 // For performance reasons cache the path in a static QString
443 // (see QDir::homePath() for more details)
444 static QString homePath
;
445 if (homePath
.isEmpty()) {
446 homePath
= QDir::homePath();
447 Q_ASSERT(!homePath
.isEmpty());
450 return filePath
.startsWith(homePath
);