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