]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemmodel.h
9c91f7b0824eb4f16324c8f23436ab83f6e2336e
[dolphin.git] / src / panels / places / placesitemmodel.h
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #ifndef PLACESITEMMODEL_H
21 #define PLACESITEMMODEL_H
22
23 #include <config-baloo.h>
24
25 #include <kitemviews/kstandarditemmodel.h>
26
27 #include <QUrl>
28 #include <QHash>
29 #include <QList>
30 #include <QSet>
31 #include <Solid/Predicate>
32 #include <Solid/StorageAccess>
33
34 class KBookmark;
35 class KBookmarkManager;
36 class PlacesItem;
37 class QAction;
38 class QTimer;
39
40 // #define PLACESITEMMODEL_DEBUG
41
42 /**
43 * @brief Model for maintaining the bookmarks of the places panel.
44 *
45 * It is compatible to the KFilePlacesModel from kdelibs but adds
46 * the ability to have groups for places.
47 */
48 class PlacesItemModel: public KStandardItemModel
49 {
50 Q_OBJECT
51
52 public:
53 explicit PlacesItemModel(QObject* parent = 0);
54 ~PlacesItemModel() override;
55
56 /**
57 * @return A new instance of a places item with the given
58 * attributes.
59 */
60 PlacesItem* createPlacesItem(const QString& text,
61 const QUrl& url,
62 const QString& iconName = QString());
63
64 PlacesItem* placesItem(int index) const;
65
66 /**
67 * If set to true, all items that are marked as hidden
68 * will be shown in the view. The items will
69 * stay marked as hidden, which is visually indicated
70 * by the view by desaturating the icon and the text.
71 */
72 void setHiddenItemsShown(bool show);
73 bool hiddenItemsShown() const;
74
75 /**
76 * @return Number of items that are marked as hidden.
77 * Note that this does not mean that the items
78 * are really hidden
79 * (see PlacesItemModel::setHiddenItemsShown()).
80 */
81 int hiddenCount() const;
82
83 /**
84 * Search the item which is equal to the URL or at least
85 * is a parent URL. If there are more than one possible
86 * candidates, return the item which covers the biggest
87 * range of the URL. -1 is returned if no closest item
88 * could be found.
89 */
90 int closestItem(const QUrl& url) const;
91
92 /**
93 * Appends the item \a item as last element of the group
94 * the item belongs to. If no item with the same group is
95 * present, the item gets appended as last element of the
96 * model. PlacesItemModel takes the ownership
97 * of the item.
98 */
99 void appendItemToGroup(PlacesItem* item);
100
101 QAction* ejectAction(int index) const;
102 QAction* teardownAction(int index) const;
103
104 void requestEject(int index);
105 void requestTearDown(int index);
106
107 bool storageSetupNeeded(int index) const;
108 void requestStorageSetup(int index);
109
110 QMimeData* createMimeData(const KItemSet& indexes) const override;
111
112 bool supportsDropping(int index) const override;
113
114 void dropMimeDataBefore(int index, const QMimeData* mimeData);
115
116 /**
117 * @return Converts the URL, which contains "virtual" URLs for system-items like
118 * "search:/documents" into a Query-URL that will be handled by
119 * the corresponding IO-slave. Virtual URLs for bookmarks are used to
120 * be independent from internal format changes.
121 */
122 static QUrl convertedUrl(const QUrl& url);
123
124 void clear() override;
125
126 void proceedWithTearDown();
127
128 /**
129 * Saves the bookmarks and indicates to other applications that the
130 * state of the bookmarks has been changed. Is only called by the
131 * timeout of m_saveBookmarksTimer to prevent unnecessary savings.
132 */
133 void saveBookmarks();
134
135 bool isDir(int index) const override;
136 signals:
137 void errorMessage(const QString& message);
138 void storageSetupDone(int index, bool success);
139 void storageTearDownRequested(const QString& mountPath);
140 void storageTearDownExternallyRequested(const QString& mountPath);
141
142 protected:
143 void onItemInserted(int index) override;
144 void onItemRemoved(int index, KStandardItem* removedItem) override;
145 void onItemChanged(int index, const QSet<QByteArray>& changedRoles) override;
146
147 private slots:
148 void slotDeviceAdded(const QString& udi);
149 void slotDeviceRemoved(const QString& udi);
150 void slotStorageTearDownDone(Solid::ErrorType error, const QVariant& errorData);
151 void slotStorageSetupDone(Solid::ErrorType error, const QVariant& errorData, const QString& udi);
152 void hideItem();
153
154 /**
155 * Updates the bookmarks from the model corresponding to the changed
156 * bookmarks stored by the bookmark-manager. Is called whenever the bookmarks
157 * have been changed by another application.
158 */
159 void updateBookmarks();
160
161 private:
162 struct SystemBookmarkData;
163
164 /**
165 * Loads the bookmarks from the bookmark-manager and creates items for
166 * the model or moves hidden items to m_bookmarkedItems.
167 */
168 void loadBookmarks();
169
170 /**
171 * @return True, if the bookmark can be accepted in the context of the
172 * current application (e.g. bookmarks from other applications
173 * will be ignored).
174 */
175 bool acceptBookmark(const KBookmark& bookmark,
176 const QSet<QString>& availableDevices) const;
177
178 /**
179 * Creates a PlacesItem for a system-bookmark:
180 * - PlacesItem::isSystemItem() will return true
181 * - Default view-properties will be created for "Search For" items
182 * The item is not inserted to the model yet.
183 */
184 PlacesItem* createSystemPlacesItem(const SystemBookmarkData& data);
185
186 /**
187 * Creates system bookmarks that are shown per default and can
188 * only be hidden but not removed. The result will be stored
189 * in m_systemBookmarks.
190 */
191 void createSystemBookmarks();
192
193 void initializeAvailableDevices();
194
195 /**
196 * @param index Item index related to the model.
197 * @return Corresponding index related to m_bookmarkedItems.
198 */
199 int bookmarkIndex(int index) const;
200
201 /**
202 * Marks the item with the index \a index as hidden and
203 * removes it from the model so that it gets invisible.
204 */
205 void hideItem(int index);
206
207 QString internalMimeType() const;
208
209 /**
210 * @return Adjusted drop index which assures that the item is aligned
211 * into the same group as specified by PlacesItem::groupType().
212 */
213 int groupedDropIndex(int index, const PlacesItem* item) const;
214
215 /**
216 * @return True if the bookmarks have the same identifiers. The identifier
217 * is the unique "ID"-property in case if no UDI is set, otherwise
218 * the UDI is used as identifier.
219 */
220 static bool equalBookmarkIdentifiers(const KBookmark& b1, const KBookmark& b2);
221
222 /**
223 * @return URL using the timeline-protocol for searching (see convertedUrl()).
224 */
225 static QUrl createTimelineUrl(const QUrl& url);
226
227 /**
228 * Helper method for createTimelineUrl().
229 * @return String that represents a date-path in the format that
230 * the timeline-protocol expects.
231 */
232 static QString timelineDateString(int year, int month, int day = 0);
233
234 /**
235 * @return URL that can be listed by KIO and results in searching
236 * for a given term. The URL \a url represents a places-internal
237 * URL like e.g. "search:/documents" (see convertedUrl()).
238 */
239 static QUrl createSearchUrl(const QUrl& url);
240
241 #ifdef HAVE_BALOO
242 /**
243 * Helper method for createSearchUrl()
244 * @return URL that can be listed by KIO and results in searching
245 * for the given type
246 */
247 static QUrl searchUrlForType(const QString& type);
248 #endif
249
250 #ifdef PLACESITEMMODEL_DEBUG
251 void showModelState();
252 #endif
253
254 private:
255 bool m_fileIndexingEnabled;
256 bool m_hiddenItemsShown;
257
258 QSet<QString> m_availableDevices;
259 Solid::Predicate m_predicate;
260 KBookmarkManager* m_bookmarkManager;
261
262 struct SystemBookmarkData
263 {
264 SystemBookmarkData(const QUrl& url,
265 const QString& icon,
266 const QString& text) :
267 url(url), icon(icon), text(text) {}
268 QUrl url;
269 QString icon;
270 QString text;
271 };
272
273 QList<SystemBookmarkData> m_systemBookmarks;
274 QHash<QUrl, int> m_systemBookmarksIndexes;
275
276 // Contains hidden and unhidden items that are stored as
277 // bookmark (the model itself only contains items that
278 // are shown in the view). If an entry is 0, then the
279 // places-item is part of the model. If an entry is not
280 // 0, the item is hidden and not part of the model.
281 QList<PlacesItem*> m_bookmarkedItems;
282
283 // Index of the hidden item that should be removed in
284 // removeHiddenItem(). The removing must be done
285 // asynchronously as in the scope of onItemChanged()
286 // removing an item is not allowed.
287 int m_hiddenItemToRemove;
288
289 Solid::StorageAccess *m_deviceToTearDown;
290
291 QTimer* m_updateBookmarksTimer;
292
293 QHash<QObject*, int> m_storageSetupInProgress;
294 };
295
296 #endif
297
298