]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemmodel.h
Merge remote-tracking branch 'origin/KDE/4.9'
[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-nepomuk.h>
24
25 #include <kitemviews/kstandarditemmodel.h>
26
27 #include <KUrl>
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 #ifdef HAVE_NEPOMUK
41 namespace Nepomuk2
42 {
43 namespace Query
44 {
45 class Term;
46 }
47 }
48 #endif
49
50 // #define PLACESITEMMODEL_DEBUG
51
52 /**
53 * @brief Model for maintaining the bookmarks of the places panel.
54 *
55 * It is compatible to the KFilePlacesModel from kdelibs but adds
56 * the ability to have groups for places.
57 */
58 class PlacesItemModel: public KStandardItemModel
59 {
60 Q_OBJECT
61
62 public:
63 explicit PlacesItemModel(QObject* parent = 0);
64 virtual ~PlacesItemModel();
65
66 /**
67 * @return A new instance of a places item with the given
68 * attributes.
69 */
70 PlacesItem* createPlacesItem(const QString& text,
71 const KUrl& url,
72 const QString& iconName = QString());
73
74 PlacesItem* placesItem(int index) const;
75
76 /**
77 * If set to true, all items that are marked as hidden
78 * will be shown in the view. The items will
79 * stay marked as hidden, which is visually indicated
80 * by the view by desaturating the icon and the text.
81 */
82 void setHiddenItemsShown(bool show);
83 bool hiddenItemsShown() const;
84
85 /**
86 * @return Number of items that are marked as hidden.
87 * Note that this does not mean that the items
88 * are really hidden
89 * (see PlacesItemModel::setHiddenItemsShown()).
90 */
91 int hiddenCount() const;
92
93 /**
94 * Search the item which is equal to the URL or at least
95 * is a parent URL. If there are more than one possible
96 * candidates, return the item which covers the biggest
97 * range of the URL. -1 is returned if no closest item
98 * could be found.
99 */
100 int closestItem(const KUrl& url) const;
101
102 /**
103 * Appends the item \a item as last element of the group
104 * the item belongs to. If no item with the same group is
105 * present, the item gets appended as last element of the
106 * model. PlacesItemModel takes the ownership
107 * of the item.
108 */
109 void appendItemToGroup(PlacesItem* item);
110
111 QAction* ejectAction(int index) const;
112 QAction* teardownAction(int index) const;
113
114 void requestEject(int index);
115 void requestTeardown(int index);
116
117 bool storageSetupNeeded(int index) const;
118 void requestStorageSetup(int index);
119
120 /** @reimp */
121 virtual QMimeData* createMimeData(const QSet<int>& indexes) const;
122
123 /** @reimp */
124 virtual bool supportsDropping(int index) const;
125
126 void dropMimeDataBefore(int index, const QMimeData* mimeData);
127
128 /**
129 * @return Converts the URL, which contains "virtual" URLs for system-items like
130 * "search:/documents" into a Nepomuk-Query-URL that will be handled by
131 * the corresponding IO-slave. Virtual URLs for bookmarks are used to
132 * be independent from internal format changes.
133 */
134 static KUrl convertedUrl(const KUrl& url);
135
136 signals:
137 void errorMessage(const QString& message);
138 void storageSetupDone(int index, bool success);
139
140 protected:
141 virtual void onItemInserted(int index);
142 virtual void onItemRemoved(int index, KStandardItem* removedItem);
143 virtual void onItemChanged(int index, const QSet<QByteArray>& changedRoles);
144
145 private slots:
146 void slotDeviceAdded(const QString& udi);
147 void slotDeviceRemoved(const QString& udi);
148 void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData);
149 void slotStorageSetupDone(Solid::ErrorType error, const QVariant& errorData, const QString& udi);
150 void hideItem();
151
152 /**
153 * Updates the bookmarks from the model corresponding to the changed
154 * bookmarks stored by the bookmark-manager. Is called whenever the bookmarks
155 * have been changed by another application.
156 */
157 void updateBookmarks();
158
159 /**
160 * Saves the bookmarks and indicates to other applications that the
161 * state of the bookmarks has been changed. Is only called by the
162 * timeout of m_saveBookmarksTimer to prevent unnecessary savings.
163 */
164 void saveBookmarks();
165
166 private:
167 struct SystemBookmarkData;
168
169 /**
170 * Loads the bookmarks from the bookmark-manager and creates items for
171 * the model or moves hidden items to m_bookmarkedItems.
172 */
173 void loadBookmarks();
174
175 /**
176 * @return True, if the bookmark can be accepted in the context of the
177 * current application (e.g. bookmarks from other applications
178 * will be ignored).
179 */
180 bool acceptBookmark(const KBookmark& bookmark,
181 const QSet<QString>& availableDevices) const;
182
183 /**
184 * Creates a PlacesItem for a system-bookmark:
185 * - PlacesItem::isSystemItem() will return true
186 * - Default view-properties will be created for "Search For" items
187 * The item is not inserted to the model yet.
188 */
189 PlacesItem* createSystemPlacesItem(const SystemBookmarkData& data);
190
191 /**
192 * Creates system bookmarks that are shown per default and can
193 * only be hidden but not removed. The result will be stored
194 * in m_systemBookmarks.
195 */
196 void createSystemBookmarks();
197
198 void initializeAvailableDevices();
199
200 /**
201 * @param index Item index related to the model.
202 * @return Corresponding index related to m_bookmarkedItems.
203 */
204 int bookmarkIndex(int index) const;
205
206 /**
207 * Marks the item with the index \a index as hidden and
208 * removes it from the model so that it gets invisible.
209 */
210 void hideItem(int index);
211
212 /**
213 * Triggers a delayed saving of bookmarks by starting
214 * m_saveBookmarksTimer.
215 */
216 void triggerBookmarksSaving();
217
218 QString internalMimeType() const;
219
220 /**
221 * @return Adjusted drop index which assures that the item is aligned
222 * into the same group as specified by PlacesItem::groupType().
223 */
224 int groupedDropIndex(int index, const PlacesItem* item) const;
225
226 /**
227 * @return True if the bookmarks have the same identifiers. The identifier
228 * is the unique "ID"-property in case if no UDI is set, otherwise
229 * the UDI is used as identifier.
230 */
231 static bool equalBookmarkIdentifiers(const KBookmark& b1, const KBookmark& b2);
232
233 /**
234 * @return URL using the timeline-protocol for searching (see convertedUrl()).
235 */
236 static KUrl createTimelineUrl(const KUrl& url);
237
238 /**
239 * Helper method for createTimelineUrl().
240 * @return String that represents a date-path in the format that
241 * the timeline-protocol expects.
242 */
243 static QString timelineDateString(int year, int month, int day = 0);
244
245 /**
246 * @return URL that can be listed by KIO and results in searching
247 * for a given term. The URL \a url represents a places-internal
248 * URL like e.g. "search:/documents" (see convertedUrl()).
249 */
250 static KUrl createSearchUrl(const KUrl& url);
251
252 #ifdef HAVE_NEPOMUK
253 /**
254 * Helper method for createSearchUrl().
255 * @return URL that can be listed by KIO and results in searching
256 * for the given term.
257 */
258 static KUrl searchUrlForTerm(const Nepomuk2::Query::Term& term);
259 #endif
260
261 #ifdef PLACESITEMMODEL_DEBUG
262 void showModelState();
263 #endif
264
265 private:
266 bool m_fileIndexingEnabled;
267 bool m_hiddenItemsShown;
268
269 QSet<QString> m_availableDevices;
270 Solid::Predicate m_predicate;
271 KBookmarkManager* m_bookmarkManager;
272
273 struct SystemBookmarkData
274 {
275 SystemBookmarkData(const KUrl& url,
276 const QString& icon,
277 const QString& text) :
278 url(url), icon(icon), text(text) {}
279 KUrl url;
280 QString icon;
281 QString text;
282 };
283
284 QList<SystemBookmarkData> m_systemBookmarks;
285 QHash<KUrl, int> m_systemBookmarksIndexes;
286
287 // Contains hidden and unhidden items that are stored as
288 // bookmark (the model itself only contains items that
289 // are shown in the view). If an entry is 0, then the
290 // places-item is part of the model. If an entry is not
291 // 0, the item is hidden and not part of the model.
292 QList<PlacesItem*> m_bookmarkedItems;
293
294 // Index of the hidden item that should be removed in
295 // removeHiddenItem(). The removing must be done
296 // asynchronously as in the scope of onItemChanged()
297 // removing an item is not allowed.
298 int m_hiddenItemToRemove;
299
300 QTimer* m_saveBookmarksTimer;
301 QTimer* m_updateBookmarksTimer;
302
303 QHash<QObject*, int> m_storageSetupInProgress;
304 };
305
306 #endif
307
308