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