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 if (oldInfoStringList
!= newInfoStringList
) {
223 const bool markCustomizedDetails
= (m_node
->viewMode() == DolphinView::DetailsView
)
224 && !newInfoStringList
.contains(CustomizedDetailsString
);
225 if (markCustomizedDetails
) {
226 // The additional information of the details-view has been modified. Set a marker,
227 // so that it is allowed to also show no additional information
228 // (see fallback in ViewProperties::additionalInfoV2, if no additional information is
230 newInfoStringList
.append(CustomizedDetailsString
);
233 m_node
->setAdditionalInfo(newInfoStringList
);
238 QList
<DolphinView::AdditionalInfo
> ViewProperties::additionalInfoList() const
240 // The shown additional information is stored for each view-mode separately as
241 // string with the view-mode as prefix. Example:
243 // AdditionalInfo=Details_Size,Details_Date,Details_Owner,Icon_Size
245 // To get the representation as QList<DolphinView::AdditionalInfo>, the current
246 // view-mode must be checked and the values of this mode added to the list.
248 // For the details-view a special case must be respected: Per default the size
249 // and date should be shown without creating a .directory file. Only if
250 // the user explictly has modified the properties of the details view (marked
251 // by "CustomizedDetails"), also a details-view with no additional information
254 QList
<DolphinView::AdditionalInfo
> usedInfo
;
256 // infoHash allows to get the mapped DolphinView::AdditionalInfo value
257 // for a stored string-value in a fast way
258 static QHash
<QString
, DolphinView::AdditionalInfo
> infoHash
;
259 if (infoHash
.isEmpty()) {
260 AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
261 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
262 foreach (DolphinView::AdditionalInfo key
, keys
) {
263 infoHash
.insert(infoAccessor
.value(key
), key
);
267 // Iterate through all stored keys stored as strings and map them to
268 // the corresponding DolphinView::AdditionalInfo values.
269 const QString prefix
= viewModePrefix();
270 const int prefixLength
= prefix
.length();
271 const QStringList infoStringList
= m_node
->additionalInfo();
272 foreach (const QString
& infoString
, infoStringList
) {
273 if (infoString
.startsWith(prefix
)) {
274 const QString key
= infoString
.right(infoString
.length() - prefixLength
);
275 if (infoHash
.contains(key
)) {
276 usedInfo
.append(infoHash
.value(key
));
278 kWarning() << "Did not find the key" << key
<< "in the information string";
283 // For the details view the size and date should be shown per default
284 // until the additional information has been explicitly changed by the user
285 const bool useDefaultValues
= usedInfo
.isEmpty()
286 && (m_node
->viewMode() == DolphinView::DetailsView
)
287 && !infoStringList
.contains(CustomizedDetailsString
);
288 Q_UNUSED(useDefaultValues
);
289 if (useDefaultValues
) {
290 usedInfo
.append(DolphinView::SizeInfo
);
291 usedInfo
.append(DolphinView::DateInfo
);
297 void ViewProperties::setDirProperties(const ViewProperties
& props
)
299 setViewMode(props
.viewMode());
300 setPreviewsShown(props
.previewsShown());
301 setHiddenFilesShown(props
.hiddenFilesShown());
302 setGroupedSorting(props
.groupedSorting());
303 setSorting(props
.sorting());
304 setSortOrder(props
.sortOrder());
305 setSortFoldersFirst(props
.sortFoldersFirst());
306 setAdditionalInfoList(props
.additionalInfoList());
309 void ViewProperties::setAutoSaveEnabled(bool autoSave
)
311 m_autoSave
= autoSave
;
314 bool ViewProperties::isAutoSaveEnabled() const
319 void ViewProperties::update()
321 m_changedProps
= true;
322 m_node
->setTimestamp(QDateTime::currentDateTime());
325 void ViewProperties::save()
327 KStandardDirs::makeDir(m_filePath
);
328 m_node
->writeConfig();
329 m_changedProps
= false;
332 KUrl
ViewProperties::mirroredDirectory()
334 QString basePath
= KGlobal::mainComponent().componentName();
335 basePath
.append("/view_properties/");
336 return KUrl(KStandardDirs::locateLocal("data", basePath
));
339 QString
ViewProperties::destinationDir(const QString
& subDir
) const
341 QString basePath
= KGlobal::mainComponent().componentName();
342 basePath
.append("/view_properties/").append(subDir
);
343 return KStandardDirs::locateLocal("data", basePath
);
346 QString
ViewProperties::viewModePrefix() const
350 switch (m_node
->viewMode()) {
351 case DolphinView::IconsView
: prefix
= "Icons_"; break;
352 case DolphinView::CompactView
: prefix
= "Compact_"; break;
353 case DolphinView::DetailsView
: prefix
= "Details_"; break;
354 default: kWarning() << "Unknown view-mode of the view properties";
360 bool ViewProperties::isPartOfHome(const QString
& filePath
)
362 // For performance reasons cache the path in a static QString
363 // (see QDir::homePath() for more details)
364 static QString homePath
;
365 if (homePath
.isEmpty()) {
366 homePath
= QDir::homePath();
367 Q_ASSERT(!homePath
.isEmpty());
370 return filePath
.startsWith(homePath
);