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