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