]>
cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemmodel.h
1 /***************************************************************************
2 * Copyright (C) 2012 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #ifndef PLACESITEMMODEL_H
21 #define PLACESITEMMODEL_H
23 #include <config-nepomuk.h>
25 #include <kitemviews/kstandarditemmodel.h>
31 #include <Solid/Predicate>
32 #include <Solid/StorageAccess>
35 class KBookmarkManager
;
50 // #define PLACESITEMMODEL_DEBUG
53 * @brief Model for maintaining the bookmarks of the places panel.
55 * It is compatible to the KFilePlacesModel from kdelibs but adds
56 * the ability to have groups for places.
58 class PlacesItemModel
: public KStandardItemModel
63 explicit PlacesItemModel(QObject
* parent
= 0);
64 virtual ~PlacesItemModel();
67 * @return A new instance of a places item with the given
70 PlacesItem
* createPlacesItem(const QString
& text
,
72 const QString
& iconName
= QString());
74 PlacesItem
* placesItem(int index
) const;
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.
82 void setHiddenItemsShown(bool show
);
83 bool hiddenItemsShown() const;
86 * @return Number of items that are marked as hidden.
87 * Note that this does not mean that the items
89 * (see PlacesItemModel::setHiddenItemsShown()).
91 int hiddenCount() const;
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
100 int closestItem(const KUrl
& url
) const;
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
109 void appendItemToGroup(PlacesItem
* item
);
111 QAction
* ejectAction(int index
) const;
112 QAction
* teardownAction(int index
) const;
114 void requestEject(int index
);
115 void requestTeardown(int index
);
118 virtual QMimeData
* createMimeData(const QSet
<int>& indexes
) const;
120 void dropMimeData(int index
, const QMimeData
* mimeData
);
123 * @return Converts the URL, which contains "virtual" URLs for system-items like
124 * "search:/documents" into a Nepomuk-Query-URL that will be handled by
125 * the corresponding IO-slave. Virtual URLs for bookmarks are used to
126 * be independent from internal format changes.
128 static KUrl
convertedUrl(const KUrl
& url
);
131 void errorMessage(const QString
& message
);
134 virtual void onItemInserted(int index
);
135 virtual void onItemRemoved(int index
, KStandardItem
* removedItem
);
136 virtual void onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
);
139 void slotDeviceAdded(const QString
& udi
);
140 void slotDeviceRemoved(const QString
& udi
);
141 void slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
);
145 * Updates the bookmarks from the model corresponding to the changed
146 * bookmarks stored by the bookmark-manager. Is called whenever the bookmarks
147 * have been changed by another application.
149 void updateBookmarks();
152 * Saves the bookmarks and indicates to other applications that the
153 * state of the bookmarks has been changed. Is only called by the
154 * timeout of m_saveBookmarksTimer to prevent unnecessary savings.
156 void saveBookmarks();
159 struct SystemBookmarkData
;
162 * Loads the bookmarks from the bookmark-manager and creates items for
163 * the model or moves hidden items to m_bookmarkedItems.
165 void loadBookmarks();
168 * @return True, if the bookmark can be accepted in the context of the
169 * current application (e.g. bookmarks from other applications
172 bool acceptBookmark(const KBookmark
& bookmark
) const;
175 * Creates a PlacesItem for a system-bookmark:
176 * - PlacesItem::isSystemItem() will return true
177 * - Default view-properties will be created for "Search For" items
178 * The item is not inserted to the model yet.
180 PlacesItem
* createSystemPlacesItem(const SystemBookmarkData
& data
);
183 * Creates system bookmarks that are shown per default and can
184 * only be hidden but not removed. The result will be stored
185 * in m_systemBookmarks.
187 void createSystemBookmarks();
189 void initializeAvailableDevices();
192 * @param index Item index related to the model.
193 * @return Corresponding index related to m_bookmarkedItems.
195 int bookmarkIndex(int index
) const;
198 * Marks the item with the index \a index as hidden and
199 * removes it from the model so that it gets invisible.
201 void hideItem(int index
);
204 * Triggers a delayed saving of bookmarks by starting
205 * m_saveBookmarksTimer.
207 void triggerBookmarksSaving();
209 QString
internalMimeType() const;
212 * @return Adjusted drop index which assures that the item is aligned
213 * into the same group as specified by PlacesItem::groupType().
215 int groupedDropIndex(int index
, const PlacesItem
* item
) const;
218 * @return True if the bookmarks have the same identifiers. The identifier
219 * is the unique "ID"-property in case if no UDI is set, otherwise
220 * the UDI is used as identifier.
222 static bool equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
);
225 * @return URL using the timeline-protocol for searching (see convertedUrl()).
227 static KUrl
createTimelineUrl(const KUrl
& url
);
230 * Helper method for createTimelineUrl().
231 * @return String that represents a date-path in the format that
232 * the timeline-protocol expects.
234 static QString
timelineDateString(int year
, int month
, int day
= 0);
237 * @return URL that can be listed by KIO and results in searching
238 * for a given term. The URL \a url represents a places-internal
239 * URL like e.g. "search:/documents" (see convertedUrl()).
241 static KUrl
createSearchUrl(const KUrl
& url
);
245 * Helper method for createSearchUrl().
246 * @return URL that can be listed by KIO and results in searching
247 * for the given term.
249 static KUrl
searchUrlForTerm(const Nepomuk::Query::Term
& term
);
252 #ifdef PLACESITEMMODEL_DEBUG
253 void showModelState();
257 bool m_nepomukRunning
;
258 bool m_hiddenItemsShown
;
260 QSet
<QString
> m_availableDevices
;
261 Solid::Predicate m_predicate
;
262 KBookmarkManager
* m_bookmarkManager
;
264 struct SystemBookmarkData
266 SystemBookmarkData(const KUrl
& url
,
268 const QString
& text
) :
269 url(url
), icon(icon
), text(text
) {}
275 QList
<SystemBookmarkData
> m_systemBookmarks
;
276 QHash
<KUrl
, int> m_systemBookmarksIndexes
;
278 // Contains hidden and unhidden items that are stored as
279 // bookmark (the model itself only contains items that
280 // are shown in the view). If an entry is 0, then the
281 // places-item is part of the model. If an entry is not
282 // 0, the item is hidden and not part of the model.
283 QList
<PlacesItem
*> m_bookmarkedItems
;
285 // Index of the hidden item that should be removed in
286 // removeHiddenItem(). The removing must be done
287 // asynchronously as in the scope of onItemChanged()
288 // removing an item is not allowed.
289 int m_hiddenItemToRemove
;
291 QTimer
* m_saveBookmarksTimer
;
292 QTimer
* m_updateBookmarksTimer
;