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