1 /***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
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>
37 // String representation to mark the additional properties of
38 // the details view as customized by the user. See
39 // ViewProperties::additionalInfoList() for more information.
40 const char* CustomizedDetailsString
= "CustomizedDetails";
43 ViewProperties::ViewProperties(const KUrl
& url
) :
44 m_changedProps(false),
48 GeneralSettings
* settings
= GeneralSettings::self();
49 const bool useGlobalViewProps
= settings
->globalViewProps();
50 bool useDetailsViewWithPath
= false;
52 // We try and save it to the file .directory in the directory being viewed.
53 // If the directory is not writable by the user or the directory is not local,
54 // we store the properties information in a local file.
55 if (useGlobalViewProps
) {
56 m_filePath
= destinationDir("global");
57 } else if (url
.protocol().contains("search")) {
58 m_filePath
= destinationDir("search");
59 useDetailsViewWithPath
= true;
60 } else if (url
.protocol() == QLatin1String("trash")) {
61 m_filePath
= destinationDir("trash");
62 useDetailsViewWithPath
= true;
63 } else if (url
.isLocalFile()) {
64 m_filePath
= url
.toLocalFile();
65 const QFileInfo
info(m_filePath
);
66 if (!info
.isWritable() || !isPartOfHome(m_filePath
)) {
67 m_filePath
= destinationDir("local") + m_filePath
;
70 m_filePath
= destinationDir("remote") + m_filePath
;
73 const QString file
= m_filePath
+ QDir::separator() + QLatin1String(".directory");
74 m_node
= new ViewPropertySettings(KSharedConfig::openConfig(file
));
76 // If the .directory file does not exist or the timestamp is too old,
77 // use default values instead.
78 const bool useDefaultProps
= (!useGlobalViewProps
|| useDetailsViewWithPath
) &&
79 (!QFileInfo(file
).exists() ||
80 (m_node
->timestamp() < settings
->viewPropsTimestamp()));
81 if (useDefaultProps
) {
82 if (useDetailsViewWithPath
) {
83 setViewMode(DolphinView::DetailsView
);
84 setAdditionalInfoList(QList
<DolphinView::AdditionalInfo
>() << DolphinView::PathInfo
);
86 // The global view-properties act as default for directories without
87 // any view-property configuration
88 settings
->setGlobalViewProps(true);
90 ViewProperties
defaultProps(url
);
91 setDirProperties(defaultProps
);
93 settings
->setGlobalViewProps(false);
94 m_changedProps
= false;
99 ViewProperties::~ViewProperties()
101 if (m_changedProps
&& m_autoSave
) {
109 void ViewProperties::setViewMode(DolphinView::Mode mode
)
111 if (m_node
->viewMode() != mode
) {
112 m_node
->setViewMode(mode
);
117 DolphinView::Mode
ViewProperties::viewMode() const
119 const int mode
= qBound(0, m_node
->viewMode(), 2);
120 return static_cast<DolphinView::Mode
>(mode
);
123 void ViewProperties::setPreviewsShown(bool show
)
125 if (m_node
->previewsShown() != show
) {
126 m_node
->setPreviewsShown(show
);
131 bool ViewProperties::previewsShown() const
133 return m_node
->previewsShown();
136 void ViewProperties::setHiddenFilesShown(bool show
)
138 if (m_node
->hiddenFilesShown() != show
) {
139 m_node
->setHiddenFilesShown(show
);
144 void ViewProperties::setGroupedSorting(bool grouped
)
146 if (m_node
->groupedSorting() != grouped
) {
147 m_node
->setGroupedSorting(grouped
);
152 bool ViewProperties::groupedSorting() const
154 return m_node
->groupedSorting();
157 bool ViewProperties::hiddenFilesShown() const
159 return m_node
->hiddenFilesShown();
162 void ViewProperties::setSorting(DolphinView::Sorting sorting
)
164 if (m_node
->sorting() != sorting
) {
165 m_node
->setSorting(sorting
);
170 DolphinView::Sorting
ViewProperties::sorting() const
172 return static_cast<DolphinView::Sorting
>(m_node
->sorting());
175 void ViewProperties::setSortOrder(Qt::SortOrder sortOrder
)
177 if (m_node
->sortOrder() != sortOrder
) {
178 m_node
->setSortOrder(sortOrder
);
183 Qt::SortOrder
ViewProperties::sortOrder() const
185 return static_cast<Qt::SortOrder
>(m_node
->sortOrder());
188 void ViewProperties::setSortFoldersFirst(bool foldersFirst
)
190 if (m_node
->sortFoldersFirst() != foldersFirst
) {
191 m_node
->setSortFoldersFirst(foldersFirst
);
196 bool ViewProperties::sortFoldersFirst() const
198 return m_node
->sortFoldersFirst();
201 void ViewProperties::setAdditionalInfoList(const QList
<DolphinView::AdditionalInfo
>& list
)
203 // See ViewProperties::additionalInfoList() for the storage format
204 // of the additional information.
206 // Remove the old values stored for the current view-mode
207 const QStringList oldInfoStringList
= m_node
->additionalInfo();
208 const QString prefix
= viewModePrefix();
209 QStringList newInfoStringList
= oldInfoStringList
;
210 for (int i
= newInfoStringList
.count() - 1; i
>= 0; --i
) {
211 if (newInfoStringList
.at(i
).startsWith(prefix
)) {
212 newInfoStringList
.removeAt(i
);
216 // Add the updated values for the current view-mode
217 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
218 foreach (DolphinView::AdditionalInfo info
, list
) {
219 newInfoStringList
.append(prefix
+ infoAccessor
.value(info
));
222 // Only update the information if it has been changed
223 bool changed
= oldInfoStringList
.count() != newInfoStringList
.count();
225 foreach (const QString
& oldInfoString
, oldInfoStringList
) {
226 if (!newInfoStringList
.contains(oldInfoString
)) {
234 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
235 && !newInfoStringList
.contains(CustomizedDetailsString
);
236 if (markCustomizedDetails
) {
237 // The additional information of the details-view has been modified. Set a marker,
238 // so that it is allowed to also show no additional information
239 // (see fallback in ViewProperties::additionalInfoV2, if no additional information is
241 newInfoStringList
.append(CustomizedDetailsString
);
244 m_node
->setAdditionalInfo(newInfoStringList
);
249 QList
<DolphinView::AdditionalInfo
> ViewProperties::additionalInfoList() const
251 // The shown additional information is stored for each view-mode separately as
252 // string with the view-mode as prefix. Example:
254 // AdditionalInfo=Details_Size,Details_Date,Details_Owner,Icon_Size
256 // To get the representation as QList<DolphinView::AdditionalInfo>, the current
257 // view-mode must be checked and the values of this mode added to the list.
259 // For the details-view a special case must be respected: Per default the size
260 // and date should be shown without creating a .directory file. Only if
261 // the user explictly has modified the properties of the details view (marked
262 // by "CustomizedDetails"), also a details-view with no additional information
265 QList
<DolphinView::AdditionalInfo
> usedInfo
;
267 // infoHash allows to get the mapped DolphinView::AdditionalInfo value
268 // for a stored string-value in a fast way
269 static QHash
<QString
, DolphinView::AdditionalInfo
> infoHash
;
270 if (infoHash
.isEmpty()) {
271 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
272 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
273 foreach (DolphinView::AdditionalInfo key
, keys
) {
274 infoHash
.insert(infoAccessor
.value(key
), key
);
278 // Iterate through all stored keys stored as strings and map them to
279 // the corresponding DolphinView::AdditionalInfo values.
280 const QString prefix
= viewModePrefix();
281 const int prefixLength
= prefix
.length();
282 const QStringList infoStringList
= m_node
->additionalInfo();
283 foreach (const QString
& infoString
, infoStringList
) {
284 if (infoString
.startsWith(prefix
)) {
285 const QString key
= infoString
.right(infoString
.length() - prefixLength
);
286 if (infoHash
.contains(key
)) {
287 usedInfo
.append(infoHash
.value(key
));
289 kWarning() << "Did not find the key" << key
<< "in the information string";
294 // For the details view the size and date should be shown per default
295 // until the additional information has been explicitly changed by the user
296 const bool useDefaultValues
= usedInfo
.isEmpty()
297 && (m_node
->viewMode() == DolphinView::DetailsView
)
298 && !infoStringList
.contains(CustomizedDetailsString
);
299 Q_UNUSED(useDefaultValues
);
300 if (useDefaultValues
) {
301 usedInfo
.append(DolphinView::SizeInfo
);
302 usedInfo
.append(DolphinView::DateInfo
);
308 void ViewProperties::setDirProperties(const ViewProperties
& props
)
310 setViewMode(props
.viewMode());
311 setPreviewsShown(props
.previewsShown());
312 setHiddenFilesShown(props
.hiddenFilesShown());
313 setGroupedSorting(props
.groupedSorting());
314 setSorting(props
.sorting());
315 setSortOrder(props
.sortOrder());
316 setSortFoldersFirst(props
.sortFoldersFirst());
317 setAdditionalInfoList(props
.additionalInfoList());
320 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
322 m_autoSave
= autoSave
;
325 bool ViewProperties::isAutoSaveEnabled() const
330 void ViewProperties::update()
332 m_changedProps
= true;
333 m_node
->setTimestamp(QDateTime::currentDateTime());
336 void ViewProperties::save()
338 KStandardDirs::makeDir(m_filePath
);
339 m_node
->writeConfig();
340 m_changedProps
= false;
343 KUrl
ViewProperties::mirroredDirectory()
345 QString basePath
= KGlobal::mainComponent().componentName();
346 basePath
.append("/view_properties/");
347 return KUrl(KStandardDirs::locateLocal("data", basePath
));
350 QString
ViewProperties::destinationDir(const QString
& subDir
) const
352 QString basePath
= KGlobal::mainComponent().componentName();
353 basePath
.append("/view_properties/").append(subDir
);
354 return KStandardDirs::locateLocal("data", basePath
);
357 QString
ViewProperties::viewModePrefix() const
361 switch (m_node
->viewMode()) {
362 case DolphinView::IconsView
: prefix
= "Icons_"; break;
363 case DolphinView::CompactView
: prefix
= "Compact_"; break;
364 case DolphinView::DetailsView
: prefix
= "Details_"; break;
365 default: kWarning() << "Unknown view-mode of the view properties";
371 bool ViewProperties::isPartOfHome(const QString
& filePath
)
373 // For performance reasons cache the path in a static QString
374 // (see QDir::homePath() for more details)
375 static QString homePath
;
376 if (homePath
.isEmpty()) {
377 homePath
= QDir::homePath();
378 Q_ASSERT(!homePath
.isEmpty());
381 return filePath
.startsWith(homePath
);