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