]> cloud.milkyroute.net Git - dolphin.git/blob - src/panels/places/placesitemmodel.h
[PlacesItemModel] Drop unused forward declaration
[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 "kitemviews/kstandarditemmodel.h"
24
25 #include <KFilePlacesModel>
26 #include <Solid/Predicate>
27 #include <Solid/StorageAccess>
28
29 #include <QHash>
30 #include <QList>
31 #include <QSet>
32 #include <QUrl>
33
34 class KBookmark;
35 class PlacesItem;
36 class QAction;
37
38 /**
39 * @brief Model for maintaining the bookmarks of the places panel.
40 *
41 * It is compatible to the KFilePlacesModel from kdelibs but adds
42 * the ability to have groups for places.
43 */
44 class PlacesItemModel: public KStandardItemModel
45 {
46 Q_OBJECT
47
48 public:
49 explicit PlacesItemModel(QObject* parent = nullptr);
50 ~PlacesItemModel() override;
51
52 /**
53 * @brief Create a new place entry in the bookmark file
54 * and add it to the model
55 */
56 void createPlacesItem(const QString& text,
57 const QUrl& url,
58 const QString& iconName = QString(),
59 int after = -1);
60
61 PlacesItem* placesItem(int index) const;
62
63 /**
64 * @brief Mark an item as hiden
65 * @param index of the item to be hidden
66 */
67 void hideItem(int index);
68
69 /**
70 * If set to true, all items that are marked as hidden
71 * will be shown in the view. The items will
72 * stay marked as hidden, which is visually indicated
73 * by the view by desaturating the icon and the text.
74 */
75 void setHiddenItemsShown(bool show);
76 bool hiddenItemsShown() const;
77
78 /**
79 * @return Number of items that are marked as hidden.
80 * Note that this does not mean that the items
81 * are really hidden
82 * (see PlacesItemModel::setHiddenItemsShown()).
83 */
84 int hiddenCount() const;
85
86 /**
87 * Search the item which is equal to the URL or at least
88 * is a parent URL. If there are more than one possible
89 * candidates, return the item which covers the biggest
90 * range of the URL. -1 is returned if no closest item
91 * could be found.
92 */
93 int closestItem(const QUrl& 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 bool storageSetupNeeded(int index) const;
102 void requestStorageSetup(int index);
103
104 QMimeData* createMimeData(const KItemSet& indexes) const override;
105
106 bool supportsDropping(int index) const override;
107
108 void dropMimeDataBefore(int index, const QMimeData* mimeData);
109
110 /**
111 * @return Converts the URL, which contains "virtual" URLs for system-items like
112 * "search:/documents" into a Query-URL that will be handled by
113 * the corresponding IO-slave. Virtual URLs for bookmarks are used to
114 * be independent from internal format changes.
115 */
116 static QUrl convertedUrl(const QUrl& url);
117
118 void clear() override;
119
120 void proceedWithTearDown();
121
122 /**
123 * @brief Remove item from bookmark
124 *
125 * This function remove the index from bookmark file permanently
126 *
127 * @param index - the item to be removed
128 */
129 void deleteItem(int index);
130
131 /**
132 * Force a sync on the bookmarks and indicates to other applications that the
133 * state of the bookmarks has been changed.
134 */
135 void refresh();
136
137 bool isDir(int index) const override;
138
139
140 KFilePlacesModel::GroupType groupType(int row) const;
141 bool isGroupHidden(KFilePlacesModel::GroupType type) const;
142 void setGroupHidden(KFilePlacesModel::GroupType type, bool hidden);
143
144 signals:
145 void errorMessage(const QString& message);
146 void storageSetupDone(int index, bool success);
147 void storageTearDownRequested(const QString& mountPath);
148 void storageTearDownExternallyRequested(const QString& mountPath);
149
150 protected:
151 void onItemInserted(int index) override;
152 void onItemRemoved(int index, KStandardItem* removedItem) override;
153 void onItemChanged(int index, const QSet<QByteArray>& changedRoles) override;
154
155 private slots:
156 void slotStorageTearDownDone(Solid::ErrorType error, const QVariant& errorData);
157 void slotStorageSetupDone(Solid::ErrorType error, const QVariant& errorData, const QString& udi);
158
159 // source model control
160 void onSourceModelRowsInserted(const QModelIndex &parent, int first, int last);
161 void onSourceModelRowsAboutToBeRemoved(const QModelIndex &parent, int first, int last);
162 void onSourceModelRowsAboutToBeMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row);
163 void onSourceModelRowsMoved(const QModelIndex &parent, int start, int end, const QModelIndex &destination, int row);
164 void onSourceModelDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles);
165 void onSourceModelGroupHiddenChanged(KFilePlacesModel::GroupType group, bool hidden);
166
167 private:
168 /**
169 * Remove bookmarks created by the previous version of dolphin that are
170 * not valid anymore
171 */
172 void cleanupBookmarks();
173
174 /**
175 * Loads the bookmarks from the bookmark-manager and creates items for
176 * the model or moves hidden items to m_bookmarkedItems.
177 */
178 void loadBookmarks();
179
180 QString internalMimeType() const;
181
182 /**
183 * @return Adjusted drop index which assures that the item is aligned
184 * into the same group as specified by PlacesItem::groupType().
185 */
186 int groupedDropIndex(int index, const PlacesItem* item) const;
187
188 /**
189 * @return True if the bookmarks have the same identifiers. The identifier
190 * is the unique "ID"-property in case if no UDI is set, otherwise
191 * the UDI is used as identifier.
192 */
193 static bool equalBookmarkIdentifiers(const KBookmark& b1, const KBookmark& b2);
194
195 /**
196 * Appends the item \a item as last element of the group
197 * the item belongs to. If no item with the same group is
198 * present, the item gets appended as last element of the
199 * model. PlacesItemModel takes the ownership
200 * of the item.
201 */
202 void insertSortedItem(PlacesItem* item);
203
204 PlacesItem *itemFromBookmark(const KBookmark &bookmark) const;
205
206 void addItemFromSourceModel(const QModelIndex &index);
207 void removeItemByIndex(const QModelIndex &mapToSource);
208
209 QString bookmarkId(const KBookmark &bookmark) const;
210 void initializeDefaultViewProperties() const;
211
212 int mapFromSource(const QModelIndex &index) const;
213 QModelIndex mapToSource(int row) const;
214
215 static void updateItem(PlacesItem *item, const QModelIndex &index);
216
217 private:
218 bool m_hiddenItemsShown;
219
220 Solid::StorageAccess *m_deviceToTearDown;
221
222 QHash<QObject*, int> m_storageSetupInProgress;
223
224 KFilePlacesModel *m_sourceModel;
225
226 QVector<QPersistentModelIndex> m_indexMap;
227 };
228
229 #endif
230
231