]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemmodel.h
Places Panel: Implement eject and teardown actions
[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 KBookmarkManager;
35 class PlacesItem;
36 class QAction;
37
38 #ifdef HAVE_NEPOMUK
39 namespace Nepomuk
40 {
41 namespace Query
42 {
43 class Term;
44 }
45 }
46 #endif
47
48 #define PLACESITEMMODEL_DEBUG
49
50 /**
51 * @brief Model for maintaining the bookmarks of the places panel.
52 *
53 * It is compatible to the KFilePlacesModel from kdelibs but adds
54 * the ability to have groups for places.
55 */
56 class PlacesItemModel: public KStandardItemModel
57 {
58 Q_OBJECT
59
60 public:
61 explicit PlacesItemModel(QObject* parent = 0);
62 virtual ~PlacesItemModel();
63
64 PlacesItem* placesItem(int index) const;
65
66 void setHiddenItemsShown(bool show);
67 bool hiddenItemsShown() const;
68
69 int hiddenCount() const;
70
71 void setItemHidden(int index, bool hide);
72 bool isItemHidden(int index) const;
73
74 /**
75 * @return True if the item is a default item created by
76 * the system (e.g. the places for home, root, trash etc.)
77 */
78 bool isSystemItem(int index) const;
79
80 /**
81 * Search the item which is equal to the URL or at least
82 * is a parent URL. If there are more than one possible
83 * candidates, return the item which covers the biggest
84 * range of the URL. -1 is returned if no closest item
85 * could be found.
86 */
87 int closestItem(const KUrl& url) const;
88
89 /**
90 * @return Name of the group where the item with the URL
91 * \a URL belongs to.
92 */
93 QString groupName(const KUrl& url) const;
94
95 QAction* ejectAction(int index) const;
96 QAction* teardownAction(int index) const;
97
98 void requestEject(int index);
99 void requestTeardown(int index);
100
101 signals:
102 void errorMessage(const QString& message);
103
104 protected:
105 virtual void onItemInserted(int index);
106 virtual void onItemRemoved(int index);
107
108 private slots:
109 void slotDeviceAdded(const QString& udi);
110 void slotDeviceRemoved(const QString& udi);
111 void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData);
112
113 private:
114 void loadBookmarks();
115
116 /**
117 * Helper method for loadBookmarks(): Adds the items
118 * to the model if the "isHidden"-property is false,
119 * otherwise the items get added to m_hiddenItems.
120 */
121 void addItems(const QList<PlacesItem*>& items);
122
123 /**
124 * Creates system bookmarks that are shown per default and can
125 * only be hidden but not removed. The result will be stored
126 * in m_systemBookmarks.
127 */
128 void createSystemBookmarks();
129
130 void initializeAvailableDevices();
131
132 /**
133 * @param index Item index related to the model.
134 * @return Corresponding item index related to m_hiddenItems.
135 */
136 int hiddenIndex(int index) const;
137
138 static QString placesGroupName();
139 static QString recentlyAccessedGroupName();
140 static QString searchForGroupName();
141
142 static KUrl translatedSystemBookmarkUrl(const KUrl& url);
143
144 /**
145 * @return URL using the timeline-protocol for searching.
146 */
147 static KUrl createTimelineUrl(const KUrl& url);
148
149 /**
150 * Helper method for createTimelineUrl().
151 * @return String that represents a date-path in the format that
152 * the timeline-protocol expects.
153 */
154 static QString timelineDateString(int year, int month, int day = 0);
155
156 /**
157 * @return URL that can be listed by KIO and results in searching
158 * for a given term. The URL \a url represents a places-internal
159 * URL like e.g. "search:/documents"
160 */
161 static KUrl createSearchUrl(const KUrl& url);
162
163 #ifdef HAVE_NEPOMUK
164 /**
165 * Helper method for createSearchUrl().
166 * @return URL that can be listed by KIO and results in searching
167 * for the given term.
168 */
169 static KUrl searchUrlForTerm(const Nepomuk::Query::Term& term);
170 #endif
171
172 #ifdef PLACESITEMMODEL_DEBUG
173 void showModelState();
174 #endif
175
176 private:
177 bool m_nepomukRunning;
178 bool m_hiddenItemsShown;
179
180 QSet<QString> m_availableDevices;
181 Solid::Predicate m_predicate;
182 KBookmarkManager* m_bookmarkManager;
183
184 struct SystemBookmarkData
185 {
186 SystemBookmarkData(const KUrl& url,
187 const QString& icon,
188 const QString& text,
189 const QString& group) :
190 url(url), icon(icon), text(text), group(group) {}
191 KUrl url;
192 QString icon;
193 QString text;
194 QString group;
195 };
196
197 QList<SystemBookmarkData> m_systemBookmarks;
198 QHash<KUrl, int> m_systemBookmarksIndexes;
199
200 QList<PlacesItem*> m_hiddenItems;
201 };
202
203 #endif
204
205