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"
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();
53 // We try and save it to the file .directory 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 const bool isSearchUrl
= url
.protocol().contains("search");
58 m_filePath
= destinationDir("search");
59 } else if (useGlobalViewProps
) {
60 m_filePath
= destinationDir("global");
61 } else if (url
.isLocalFile()) {
62 m_filePath
= url
.toLocalFile();
63 const QFileInfo
info(m_filePath
);
64 if (!info
.isWritable() || !isPartOfHome(m_filePath
)) {
65 m_filePath
= destinationDir("local") + m_filePath
;
68 m_filePath
= destinationDir("remote") + m_filePath
;
71 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
72 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
74 // If the .directory file does not exist or the timestamp is too old,
75 // use default values instead.
76 const bool useDefaultProps
= (!useGlobalViewProps
|| isSearchUrl
) &&
77 (!QFileInfo(file
).exists() ||
78 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
79 if (useDefaultProps
) {
81 setViewMode(DolphinView::DetailsView
);
82 setAdditionalInfo(KFileItemDelegate::InformationList() << KFileItemDelegate::LocalPathOrUrl
);
84 // The global view-properties act as default for directories without
85 // any view-property configuration
86 settings
->setGlobalViewProps(true);
88 ViewProperties
defaultProps(url
);
89 setDirProperties(defaultProps
);
91 settings
->setGlobalViewProps(false);
92 m_changedProps
= false;
97 ViewProperties::~ViewProperties()
99 if (m_changedProps
&& m_autoSave
) {
107 void ViewProperties::setViewMode(DolphinView::Mode mode
)
109 if (m_node
->viewMode() != mode
) {
110 m_node
->setViewMode(mode
);
115 DolphinView::Mode
ViewProperties::viewMode() const
117 return static_cast<DolphinView::Mode
>(m_node
->viewMode());
120 void ViewProperties::setShowPreview(bool show
)
122 if (m_node
->showPreview() != show
) {
123 m_node
->setShowPreview(show
);
128 bool ViewProperties::showPreview() const
130 return m_node
->showPreview();
133 void ViewProperties::setShowHiddenFiles(bool show
)
135 if (m_node
->showHiddenFiles() != show
) {
136 m_node
->setShowHiddenFiles(show
);
141 void ViewProperties::setCategorizedSorting(bool categorized
)
143 if (m_node
->categorizedSorting() != categorized
) {
144 m_node
->setCategorizedSorting(categorized
);
149 bool ViewProperties::categorizedSorting() const
151 return m_node
->categorizedSorting();
154 bool ViewProperties::showHiddenFiles() const
156 return m_node
->showHiddenFiles();
159 void ViewProperties::setSorting(DolphinView::Sorting sorting
)
161 if (m_node
->sorting() != sorting
) {
162 m_node
->setSorting(sorting
);
167 DolphinView::Sorting
ViewProperties::sorting() const
169 return static_cast<DolphinView::Sorting
>(m_node
->sorting());
172 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
174 if (m_node
->sortOrder() != sortOrder
) {
175 m_node
->setSortOrder(sortOrder
);
180 Qt::SortOrder
ViewProperties::sortOrder() const
182 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
185 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
187 if (m_node
->sortFoldersFirst() != foldersFirst
) {
188 m_node
->setSortFoldersFirst(foldersFirst
);
193 bool ViewProperties::sortFoldersFirst() const
195 return m_node
->sortFoldersFirst();
198 void ViewProperties::setAdditionalInfo(const KFileItemDelegate::InformationList
& list
)
200 // See ViewProperties::additionalInfoV2() for the storage format
201 // of the additional information.
203 // Remove the old values stored for the current view-mode
204 const QStringList oldInfoStringList
= m_node
->additionalInfoV2();
205 const QString prefix
= viewModePrefix();
206 QStringList newInfoStringList
= oldInfoStringList
;
207 for (int i
= newInfoStringList
.count() - 1; i
>= 0; --i
) {
208 if (newInfoStringList
.at(i
).startsWith(prefix
)) {
209 newInfoStringList
.removeAt(i
);
213 // Add the updated values for the current view-mode
214 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
215 foreach (KFileItemDelegate::Information info
, list
) {
216 newInfoStringList
.append(prefix
+ infoAccessor
.value(info
));
219 // Only update the information if it has been changed
220 bool changed
= oldInfoStringList
.count() != newInfoStringList
.count();
222 foreach (const QString
& oldInfoString
, oldInfoStringList
) {
223 if (!newInfoStringList
.contains(oldInfoString
)) {
231 if (m_node
->version() < 2) {
232 m_node
->setVersion(2);
235 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
236 && !newInfoStringList
.contains(CustomizedDetailsString
);
237 if (markCustomizedDetails
) {
238 // The additional information of the details-view has been modified. Set a marker,
239 // so that it is allowed to also show no additional information
240 // (see fallback in ViewProperties::additionalInfoV2, if no additional information is
242 newInfoStringList
.append(CustomizedDetailsString
);
245 m_node
->setAdditionalInfoV2(newInfoStringList
);
250 KFileItemDelegate::InformationList
ViewProperties::additionalInfo() const
252 KFileItemDelegate::InformationList usedInfo
;
254 switch (m_node
->version()) {
255 case 1: usedInfo
= additionalInfoV1(); break;
256 case 2: usedInfo
= additionalInfoV2(); break;
257 default: kWarning() << "Unknown version of the view properties";
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 setSortFoldersFirst(props
.sortFoldersFirst());
273 setAdditionalInfo(props
.additionalInfo());
276 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
278 m_autoSave
= autoSave
;
281 bool ViewProperties::isAutoSaveEnabled() const
286 void ViewProperties::update()
288 m_changedProps
= true;
289 m_node
->setTimestamp(QDateTime::currentDateTime());
291 // If the view-properties are stored in an older format, take
292 // care to update them to the current format.
293 switch (m_node
->version()) {
295 const KFileItemDelegate::InformationList infoList
= additionalInfoV1();
296 m_node
->setVersion(2);
297 setAdditionalInfo(infoList
);
301 // Current version. Nothing needs to get converted.
304 kWarning() << "Unknown version of the view properties";
308 void ViewProperties::save()
310 KStandardDirs::makeDir(m_filePath
);
311 m_node
->writeConfig();
312 m_changedProps
= false;
315 KUrl
ViewProperties::mirroredDirectory()
317 QString basePath
= KGlobal::mainComponent().componentName();
318 basePath
.append("/view_properties/");
319 return KUrl(KStandardDirs::locateLocal("data", basePath
));
322 QString
ViewProperties::destinationDir(const QString
& subDir
) const
324 QString basePath
= KGlobal::mainComponent().componentName();
325 basePath
.append("/view_properties/").append(subDir
);
326 return KStandardDirs::locateLocal("data", basePath
);
329 KFileItemDelegate::InformationList
ViewProperties::additionalInfoV1() const
331 KFileItemDelegate::InformationList usedInfo
;
333 int decodedInfo
= m_node
->additionalInfo();
335 switch (viewMode()) {
336 case DolphinView::DetailsView
:
337 decodedInfo
= decodedInfo
& 0xFF;
338 if (decodedInfo
== 0) {
339 // A details view without any additional info makes no sense, hence
340 // provide at least a size-info and date-info as fallback
341 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
342 decodedInfo
= infoAccessor
.bitValue(KFileItemDelegate::Size
) |
343 infoAccessor
.bitValue(KFileItemDelegate::ModificationTime
);
346 case DolphinView::IconsView
:
347 decodedInfo
= (decodedInfo
>> 8) & 0xFF;
349 case DolphinView::ColumnView
:
350 decodedInfo
= (decodedInfo
>> 16) & 0xFF;
355 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
356 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
358 foreach (const KFileItemDelegate::Information info
, infoKeys
) {
359 if (decodedInfo
& infoAccessor
.bitValue(info
)) {
360 usedInfo
.append(info
);
367 KFileItemDelegate::InformationList
ViewProperties::additionalInfoV2() const
369 // The shown additional information is stored for each view-mode separately as
370 // string with the view-mode as prefix. Example:
372 // AdditionalInfoV2=Details_Size,Details_Date,Details_Owner,Icon_Size
374 // To get the representation as KFileItemDelegate::InformationList, the current
375 // view-mode must be checked and the values of this mode added to the list.
377 // For the details-view a special case must be respected: Per default the size
378 // and date should be shown without creating a .directory file. Only if
379 // the user explictly has modified the properties of the details view (marked
380 // by "CustomizedDetails"), also a details-view with no additional information
383 KFileItemDelegate::InformationList usedInfo
;
385 // infoHash allows to get the mapped KFileItemDelegate::Information value
386 // for a stored string-value in a fast way
387 static QHash
<QString
, KFileItemDelegate::Information
> infoHash
;
388 if (infoHash
.isEmpty()) {
389 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
390 const KFileItemDelegate::InformationList keys
= infoAccessor
.keys();
391 foreach (const KFileItemDelegate::Information key
, keys
) {
392 infoHash
.insert(infoAccessor
.value(key
), key
);
396 // Iterate through all stored keys stored as strings and map them to
397 // the corresponding KFileItemDelegate::Information values.
398 const QString prefix
= viewModePrefix();
399 const int prefixLength
= prefix
.length();
400 const QStringList infoStringList
= m_node
->additionalInfoV2();
401 foreach (const QString
& infoString
, infoStringList
) {
402 if (infoString
.startsWith(prefix
)) {
403 const QString key
= infoString
.right(infoString
.length() - prefixLength
);
404 Q_ASSERT(infoHash
.contains(key
));
405 usedInfo
.append(infoHash
.value(key
));
409 // For the details view the size and date should be shown per default
410 // until the additional information has been explicitly changed by the user
411 const bool useDefaultValues
= usedInfo
.isEmpty()
412 && (m_node
->viewMode() == DolphinView::DetailsView
)
413 && !infoStringList
.contains(CustomizedDetailsString
);
414 if (useDefaultValues
) {
415 usedInfo
.append(KFileItemDelegate::Size
);
416 usedInfo
.append(KFileItemDelegate::ModificationTime
);
422 QString
ViewProperties::viewModePrefix() const
426 switch (m_node
->viewMode()) {
427 case DolphinView::DetailsView
: prefix
= "Details_"; break;
428 case DolphinView::IconsView
: prefix
= "Icons_"; break;
429 case DolphinView::ColumnView
: prefix
= "Column_"; break;
430 default: kWarning() << "Unknown view-mode of the view properties";
436 bool ViewProperties::isPartOfHome(const QString
& filePath
)
438 // For performance reasons cache the path in a static QString
439 // (see QDir::homePath() for more details)
440 static QString homePath
;
441 if (homePath
.isEmpty()) {
442 homePath
= QDir::homePath();
443 Q_ASSERT(!homePath
.isEmpty());
446 return filePath
.startsWith(homePath
);