]>
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-baloo.h>
25 #include <kitemviews/kstandarditemmodel.h>
31 #include <Solid/Predicate>
32 #include <Solid/StorageAccess>
35 class KBookmarkManager
;
40 // #define PLACESITEMMODEL_DEBUG
43 * @brief Model for maintaining the bookmarks of the places panel.
45 * It is compatible to the KFilePlacesModel from kdelibs but adds
46 * the ability to have groups for places.
48 class PlacesItemModel
: public KStandardItemModel
53 explicit PlacesItemModel(QObject
* parent
= 0);
54 virtual ~PlacesItemModel();
57 * @return A new instance of a places item with the given
60 PlacesItem
* createPlacesItem(const QString
& text
,
62 const QString
& iconName
= QString());
64 PlacesItem
* placesItem(int index
) const;
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.
72 void setHiddenItemsShown(bool show
);
73 bool hiddenItemsShown() const;
76 * @return Number of items that are marked as hidden.
77 * Note that this does not mean that the items
79 * (see PlacesItemModel::setHiddenItemsShown()).
81 int hiddenCount() const;
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
90 int closestItem(const KUrl
& url
) const;
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
99 void appendItemToGroup(PlacesItem
* item
);
101 QAction
* ejectAction(int index
) const;
102 QAction
* teardownAction(int index
) const;
104 void requestEject(int index
);
105 void requestTeardown(int index
);
107 bool storageSetupNeeded(int index
) const;
108 void requestStorageSetup(int index
);
111 virtual QMimeData
* createMimeData(const KItemSet
& indexes
) const;
114 virtual bool supportsDropping(int index
) const;
116 void dropMimeDataBefore(int index
, const QMimeData
* mimeData
);
119 * @return Converts the URL, which contains "virtual" URLs for system-items like
120 * "search:/documents" into a Query-URL that will be handled by
121 * the corresponding IO-slave. Virtual URLs for bookmarks are used to
122 * be independent from internal format changes.
124 static KUrl
convertedUrl(const KUrl
& url
);
126 virtual void clear();
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.
133 void saveBookmarks();
136 void errorMessage(const QString
& message
);
137 void storageSetupDone(int index
, bool success
);
140 virtual void onItemInserted(int index
);
141 virtual void onItemRemoved(int index
, KStandardItem
* removedItem
);
142 virtual void onItemChanged(int index
, const QSet
<QByteArray
>& changedRoles
);
145 void slotDeviceAdded(const QString
& udi
);
146 void slotDeviceRemoved(const QString
& udi
);
147 void slotStorageTeardownDone(Solid::ErrorType error
, const QVariant
& errorData
);
148 void slotStorageSetupDone(Solid::ErrorType error
, const QVariant
& errorData
, const QString
& udi
);
152 * Updates the bookmarks from the model corresponding to the changed
153 * bookmarks stored by the bookmark-manager. Is called whenever the bookmarks
154 * have been changed by another application.
156 void updateBookmarks();
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
,
173 const QSet
<QString
>& availableDevices
) const;
176 * Creates a PlacesItem for a system-bookmark:
177 * - PlacesItem::isSystemItem() will return true
178 * - Default view-properties will be created for "Search For" items
179 * The item is not inserted to the model yet.
181 PlacesItem
* createSystemPlacesItem(const SystemBookmarkData
& data
);
184 * Creates system bookmarks that are shown per default and can
185 * only be hidden but not removed. The result will be stored
186 * in m_systemBookmarks.
188 void createSystemBookmarks();
190 void initializeAvailableDevices();
193 * @param index Item index related to the model.
194 * @return Corresponding index related to m_bookmarkedItems.
196 int bookmarkIndex(int index
) const;
199 * Marks the item with the index \a index as hidden and
200 * removes it from the model so that it gets invisible.
202 void hideItem(int index
);
204 QString
internalMimeType() const;
207 * @return Adjusted drop index which assures that the item is aligned
208 * into the same group as specified by PlacesItem::groupType().
210 int groupedDropIndex(int index
, const PlacesItem
* item
) const;
213 * @return True if the bookmarks have the same identifiers. The identifier
214 * is the unique "ID"-property in case if no UDI is set, otherwise
215 * the UDI is used as identifier.
217 static bool equalBookmarkIdentifiers(const KBookmark
& b1
, const KBookmark
& b2
);
220 * @return URL using the timeline-protocol for searching (see convertedUrl()).
222 static KUrl
createTimelineUrl(const KUrl
& url
);
225 * Helper method for createTimelineUrl().
226 * @return String that represents a date-path in the format that
227 * the timeline-protocol expects.
229 static QString
timelineDateString(int year
, int month
, int day
= 0);
232 * @return URL that can be listed by KIO and results in searching
233 * for a given term. The URL \a url represents a places-internal
234 * URL like e.g. "search:/documents" (see convertedUrl()).
236 static KUrl
createSearchUrl(const KUrl
& url
);
240 * Helper method for createSearchUrl()
241 * @return URL that can be listed by KIO and results in searching
244 static KUrl
searchUrlForType(const QString
& type
);
247 #ifdef PLACESITEMMODEL_DEBUG
248 void showModelState();
252 bool m_fileIndexingEnabled
;
253 bool m_hiddenItemsShown
;
255 QSet
<QString
> m_availableDevices
;
256 Solid::Predicate m_predicate
;
257 KBookmarkManager
* m_bookmarkManager
;
259 struct SystemBookmarkData
261 SystemBookmarkData(const KUrl
& url
,
263 const QString
& text
) :
264 url(url
), icon(icon
), text(text
) {}
270 QList
<SystemBookmarkData
> m_systemBookmarks
;
271 QHash
<KUrl
, int> m_systemBookmarksIndexes
;
273 // Contains hidden and unhidden items that are stored as
274 // bookmark (the model itself only contains items that
275 // are shown in the view). If an entry is 0, then the
276 // places-item is part of the model. If an entry is not
277 // 0, the item is hidden and not part of the model.
278 QList
<PlacesItem
*> m_bookmarkedItems
;
280 // Index of the hidden item that should be removed in
281 // removeHiddenItem(). The removing must be done
282 // asynchronously as in the scope of onItemChanged()
283 // removing an item is not allowed.
284 int m_hiddenItemToRemove
;
286 QTimer
* m_updateBookmarksTimer
;
288 QHash
<QObject
*, int> m_storageSetupInProgress
;