1/***************************************************************************
2 * Copyright (C) 2006-2010 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Aaron J. Seigo <aseigo@kde.org> *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21#ifndef VIEWPROPERTIES_H
22#define VIEWPROPERTIES_H
23
24#include <views/dolphinview.h>
25#include <KUrl>
26#include <libdolphin_export.h>
27
28class ViewPropertySettings;
29/**
30 * @brief Maintains the view properties like 'view mode' or
31 * 'show hidden files' for a directory.
32 *
33 * The view properties are automatically stored as part of the file
34 * .directory inside the corresponding path. To read out the view properties
35 * just construct an instance by passing the path of the directory:
36 *
37 * \code
38 * ViewProperties props(KUrl("/home/peter/Documents"));
39 * const DolphinView::Mode mode = props.viewMode();
40 * const bool hiddenFilesShown = props.hiddenFilesShown();
41 * \endcode
42 *
43 * When modifying a view property, the '.directory' file is automatically updated
44 * inside the destructor.
45 *
46 * If no .directory file is available or the global view mode is turned on
47 * (see GeneralSettings::globalViewMode()), the values from the global .directory file
48 * are used for initialization.
49 */
50class LIBDOLPHINPRIVATE_EXPORT ViewProperties
51{
52public:
53 explicit ViewProperties(const KUrl& url);
54 virtual ~ViewProperties();
55
56 void setViewMode(DolphinView::Mode mode);
57 DolphinView::Mode viewMode() const;
58
59 void setPreviewsShown(bool show);
60 bool previewsShown() const;
61
62 void setHiddenFilesShown(bool show);
63 bool hiddenFilesShown() const;
64
65 void setGroupedSorting(bool grouped);
66 bool groupedSorting() const;
67
68 void setSortRole(const QByteArray& role);
69 QByteArray sortRole() const;
70
71 void setSortOrder(Qt::SortOrder sortOrder);
72 Qt::SortOrder sortOrder() const;
73
74 void setSortFoldersFirst(bool foldersFirst);
75 bool sortFoldersFirst() const;
76
77 /**
78 * Sets the additional information for the current set view-mode.
79 * Note that the additional-info property is the only property where
80 * the value is dependent from another property (in this case the view-mode).
81 */
82 void setVisibleRoles(const QList<QByteArray>& info);
83
84 /**
85 * Returns the additional information for the current set view-mode.
86 * Note that the additional-info property is the only property where
87 * the value is dependent from another property (in this case the view-mode).
88 */
89 QList<QByteArray> visibleRoles() const;
90
91 void setHeaderColumnWidths(const QList<int>& widths);
92 QList<int> headerColumnWidths() const;
93
94 /**
95 * Sets the directory properties view mode, show preview,
96 * show hidden files, sorting and sort order like
97 * set in \a props.
98 */
99 void setDirProperties(const ViewProperties& props);
100
101 /**
102 * If \a autoSave is true, the properties are automatically
103 * saved when the destructor is called. Per default autosaving
104 * is enabled.
105 */
106 void setAutoSaveEnabled(bool autoSave);
107 bool isAutoSaveEnabled() const;
108
109 void update();
110
111 /**
112 * Saves the view properties for the directory specified
113 * in the constructor. The method is automatically
114 * invoked in the destructor, if
115 * ViewProperties::isAutoSaveEnabled() returns true and
116 * at least one property has been changed.
117 */
118 void save();
119
120 /**
121 * @return True if properties for the given URL exist:
122 * As soon as the properties for an URL have been saved with
123 * ViewProperties::save(), true will be returned. If false is
124 * returned, the default view-properties are used.
125 */
126 bool exist() const;
127
128private:
129 /**
130 * Returns the destination directory path where the view
131 * properties are stored. \a subDir specifies the used sub
132 * directory.
133 */
134 QString destinationDir(const QString& subDir) const;
135
136 /**
137 * Returns the view-mode prefix when storing additional properties for
138 * a view-mode.
139 */
140 QString viewModePrefix() const;
141
142 /**
143 * Provides backward compatibility with .directory files created with
144 * Dolphin < 2.0: Converts the old additionalInfo-property into
145 * the visibleRoles-property and clears the additionalInfo-property.
146 */
147 void convertAdditionalInfo();
148
149 /**
150 * Provides backward compatibility with .directory files created with
151 * Dolphin < 2.1: Converts the old name-role "name" to the generic
152 * role "text".
153 */
154 void convertNameRoleToTextRole();
155
156 /**
157 * Returns true, if \a filePath is part of the home-path (see QDir::homePath()).
158 */
159 static bool isPartOfHome(const QString& filePath);
160
161 /**
162 * @return A hash-value for an URL that can be used as directory name.
163 * Is used to be able to remember view-properties for long baloo-URLs.
164 */
165 static QString directoryHashForUrl(const KUrl& url);
166
167 /**
168 * Returns the URL of the directory, where the mirrored view properties
169 * are stored into. Mirrored view properties are used if:
170 * - there is no write access for storing the view properties into
171 * the original directory
172 * - for non local directories
173 */
174 static KUrl mirroredDirectory();
175
176 Q_DISABLE_COPY(ViewProperties)
177
178private:
179 bool m_changedProps;
180 bool m_autoSave;
181 QString m_filePath;
182 ViewPropertySettings* m_node;
183};
184
185#endif
186