]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemmodel.h
Fix several bookmark synchronization issues
[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 class QTimer;
38
39 // #define PLACESITEMMODEL_DEBUG
40
41 /**
42 * @brief Model for maintaining the bookmarks of the places panel.
43 *
44 * It is compatible to the KFilePlacesModel from kdelibs but adds
45 * the ability to have groups for places.
46 */
47 class PlacesItemModel: public KStandardItemModel
48 {
49 Q_OBJECT
50
51 public:
52 explicit PlacesItemModel(QObject* parent = 0);
53 virtual ~PlacesItemModel();
54
55 PlacesItem* createPlacesItem(const QString& text,
56 const KUrl& url,
57 const QString& iconName);
58
59 PlacesItem* placesItem(int index) const;
60
61 void setHiddenItemsShown(bool show);
62 bool hiddenItemsShown() const;
63
64 int hiddenCount() const;
65
66 /**
67 * Search the item which is equal to the URL or at least
68 * is a parent URL. If there are more than one possible
69 * candidates, return the item which covers the biggest
70 * range of the URL. -1 is returned if no closest item
71 * could be found.
72 */
73 int closestItem(const KUrl& url) const;
74
75 /**
76 * @return Name of the group where the item with the URL
77 * \a URL belongs to.
78 */
79 QString groupName(const KUrl& url) const;
80
81 QAction* ejectAction(int index) const;
82 QAction* teardownAction(int index) const;
83
84 void requestEject(int index);
85 void requestTeardown(int index);
86
87 signals:
88 void errorMessage(const QString& message);
89
90 protected:
91 virtual void onItemInserted(int index);
92 virtual void onItemRemoved(int index, KStandardItem* removedItem);
93 virtual void onItemChanged(int index, const QSet<QByteArray>& changedRoles);
94
95 private slots:
96 void slotDeviceAdded(const QString& udi);
97 void slotDeviceRemoved(const QString& udi);
98 void slotStorageTeardownDone(Solid::ErrorType error, const QVariant& errorData);
99 void removeHiddenItem();
100 void saveBookmarks();
101
102 private:
103 void loadBookmarks();
104
105 /**
106 * Helper method for loadBookmarks(): Adds the items
107 * to the model if the "isHidden"-property is false,
108 * otherwise the items get added to m_hiddenItems.
109 */
110 void addItems(const QList<PlacesItem*>& items);
111
112 /**
113 * Creates system bookmarks that are shown per default and can
114 * only be hidden but not removed. The result will be stored
115 * in m_systemBookmarks.
116 */
117 void createSystemBookmarks();
118
119 void initializeAvailableDevices();
120
121 /**
122 * @param index Item index related to the model.
123 * @return Corresponding item index related to m_hiddenItems.
124 */
125 int hiddenIndex(int index) const;
126
127 static QString placesGroupName();
128 static QString recentlyAccessedGroupName();
129 static QString searchForGroupName();
130
131 #ifdef PLACESITEMMODEL_DEBUG
132 void showModelState();
133 #endif
134
135 private:
136 bool m_nepomukRunning;
137 bool m_hiddenItemsShown;
138
139 QSet<QString> m_availableDevices;
140 Solid::Predicate m_predicate;
141 KBookmarkManager* m_bookmarkManager;
142
143 struct SystemBookmarkData
144 {
145 SystemBookmarkData(const KUrl& url,
146 const QString& icon,
147 const QString& text,
148 const QString& group) :
149 url(url), icon(icon), text(text), group(group) {}
150 KUrl url;
151 QString icon;
152 QString text;
153 QString group;
154 };
155
156 QList<SystemBookmarkData> m_systemBookmarks;
157 QHash<KUrl, int> m_systemBookmarksIndexes;
158
159 QList<PlacesItem*> m_hiddenItems;
160
161 // Index of the hidden item that should be removed in
162 // removeHiddenItem(). The removing must be done
163 // asynchronously as in the scope of onItemChanged()
164 // removing an item is not allowed.
165 int m_hiddenItemToRemove;
166
167 QTimer* m_saveBookmarksTimer;
168 };
169
170 #endif
171
172