]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.h
KFileItemModel::insertItems(): guarantee O(N) run time complexity
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.h
1 /***************************************************************************
2 * Copyright (C) 2011 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 KFILEITEMMODELROLESUPDATER_H
21 #define KFILEITEMMODELROLESUPDATER_H
22
23 #include <config-nepomuk.h>
24
25 #include <KFileItem>
26 #include <kitemviews/kitemmodelbase.h>
27
28 #include <libdolphin_export.h>
29
30 #include <QObject>
31 #include <QSet>
32 #include <QSize>
33 #include <QStringList>
34
35 class KDirWatch;
36 class KFileItemModel;
37 class KJob;
38 class QPixmap;
39 class QTimer;
40
41 #ifdef HAVE_NEPOMUK
42 namespace Nepomuk2
43 {
44 class ResourceWatcher;
45 class Resource;
46 }
47 #else
48 // Required for the slot applyChangedNepomukRoles() that
49 // cannot be ifdefined due to moc.
50 namespace Nepomuk2
51 {
52 class Resource;
53 }
54 #endif
55
56 /**
57 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
58 *
59 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
60 * the permissions. Creating previews or determining the MIME-type can be quite expensive
61 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
62 */
63 class LIBDOLPHINPRIVATE_EXPORT KFileItemModelRolesUpdater : public QObject
64 {
65 Q_OBJECT
66
67 public:
68 explicit KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent = 0);
69 virtual ~KFileItemModelRolesUpdater();
70
71 void setIconSize(const QSize& size);
72 QSize iconSize() const;
73
74 /**
75 * Sets the range of items that are visible currently. The roles
76 * of visible items are resolved first.
77 */
78 void setVisibleIndexRange(int index, int count);
79
80 void setMaximumVisibleItems(int count);
81
82 /**
83 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
84 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
85 * role.
86 */
87 void setPreviewsShown(bool show);
88 bool previewsShown() const;
89
90 /**
91 * If enabled a small preview gets upscaled to the icon size in case where
92 * the icon size is larger than the preview. Per default enlarging is
93 * enabled.
94 */
95 void setEnlargeSmallPreviews(bool enlarge);
96 bool enlargeSmallPreviews() const;
97
98 /**
99 * If \a paused is set to true the asynchronous resolving of roles will be paused.
100 * State changes during pauses like changing the icon size or the preview-shown
101 * will be remembered and handled after unpausing.
102 */
103 void setPaused(bool paused);
104 bool isPaused() const;
105
106 /**
107 * Sets the roles that should be resolved asynchronously.
108 */
109 void setRoles(const QSet<QByteArray>& roles);
110 QSet<QByteArray> roles() const;
111
112 /**
113 * Sets the list of enabled thumbnail plugins that are used for previews.
114 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
115 * are used.
116 *
117 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
118 *
119 * @see enabledPlugins
120 */
121 void setEnabledPlugins(const QStringList& list);
122
123 /**
124 * Returns the list of enabled thumbnail plugins.
125 * @see setEnabledPlugins
126 */
127 QStringList enabledPlugins() const;
128
129 private slots:
130 void slotItemsInserted(const KItemRangeList& itemRanges);
131 void slotItemsRemoved(const KItemRangeList& itemRanges);
132 void slotItemsChanged(const KItemRangeList& itemRanges,
133 const QSet<QByteArray>& roles);
134 void slotSortRoleChanged(const QByteArray& current,
135 const QByteArray& previous);
136
137 /**
138 * Is invoked after a preview has been received successfully.
139 * @see startPreviewJob()
140 */
141 void slotGotPreview(const KFileItem& item, const QPixmap& pixmap);
142
143 /**
144 * Is invoked after generating a preview has failed.
145 * @see startPreviewJob()
146 */
147 void slotPreviewFailed(const KFileItem& item);
148
149 /**
150 * Is invoked when the preview job has been finished and
151 * removes the job from the m_previewJobs list.
152 * @see startPreviewJob()
153 */
154 void slotPreviewJobFinished(KJob* job);
155
156 void resolveNextPendingRoles();
157
158 /**
159 * Resolves items that have not been resolved yet after the change has been
160 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
161 * exceeds.
162 */
163 void resolveChangedItems();
164
165 void applyChangedNepomukRoles(const Nepomuk2::Resource& resource);
166
167 /**
168 * Is invoked if a directory watched by KDirWatch got dirty. Updates
169 * the "isExpandable"- and "size"-roles of the item that matches to
170 * the given path.
171 */
172 void slotDirWatchDirty(const QString& path);
173
174 private:
175 /**
176 * Updates the roles for the given item ranges. The roles for the currently
177 * visible items will get updated first.
178 */
179 void startUpdating(const KItemRangeList& itemRanges);
180
181 /**
182 * Creates previews for the items starting from the first item of the
183 * given list.
184 * @see slotGotPreview()
185 * @see slotPreviewFailed()
186 * @see slotPreviewJobFinished()
187 */
188 void startPreviewJob(const KFileItemList& items);
189
190 bool hasPendingRoles() const;
191 void resolvePendingRoles();
192 void resetPendingRoles();
193 void sortAndResolveAllRoles();
194 void sortAndResolvePendingRoles();
195 void applySortProgressToModel();
196
197 /**
198 * Updates m_sortProgress to be 0 if the sort-role
199 * needs to get resolved asynchronously and hence a
200 * progress is required. Otherwise m_sortProgress
201 * will be set to -1 which means that no progress
202 * will be provided.
203 */
204 void updateSortProgress();
205
206 /**
207 * @return True, if at least one item from the model
208 * has an unknown MIME-type.
209 */
210 bool hasUnknownMimeTypes() const;
211
212 enum ResolveHint {
213 ResolveFast,
214 ResolveAll
215 };
216 bool applyResolvedRoles(const KFileItem& item, ResolveHint hint);
217 QHash<QByteArray, QVariant> rolesData(const KFileItem& item) const;
218
219 KFileItemList sortedItems(const QSet<KFileItem>& items) const;
220
221 /**
222 * @return The number of items of the path \a path.
223 */
224 int subItemsCount(const QString& path) const;
225
226 /**
227 * Must be invoked if a property has been changed that affects
228 * the look of the preview. Takes care to update all previews.
229 */
230 void updateAllPreviews();
231
232 private:
233 // Property for setPaused()/isPaused().
234 bool m_paused;
235
236 // Property changes during pausing must be remembered to be able
237 // to react when unpausing again:
238 bool m_previewChangedDuringPausing;
239 bool m_iconSizeChangedDuringPausing;
240 bool m_rolesChangedDuringPausing;
241
242 // Property for setPreviewsShown()/previewsShown().
243 bool m_previewShown;
244
245 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
246 bool m_enlargeSmallPreviews;
247
248 // True if the role "iconPixmap" should be cleared when resolving the next
249 // role with resolveRole(). Is necessary if the preview gets disabled
250 // during the roles-updater has been paused by setPaused().
251 bool m_clearPreviews;
252
253 int m_sortingProgress;
254
255 KFileItemModel* m_model;
256 QSize m_iconSize;
257 int m_firstVisibleIndex;
258 int m_lastVisibleIndex;
259 int m_maximumVisibleItems;
260 QSet<QByteArray> m_roles;
261 QSet<QByteArray> m_resolvableRoles;
262 QStringList m_enabledPlugins;
263
264 QSet<KFileItem> m_pendingVisibleItems;
265 QSet<KFileItem> m_pendingInvisibleItems;
266 QList<KJob*> m_previewJobs;
267
268 // When downloading or copying large files, the slot slotItemsChanged()
269 // will be called periodically within a quite short delay. To prevent
270 // a high CPU-load by generating e.g. previews for each notification, the update
271 // will be postponed until no file change has been done within a longer period
272 // of time.
273 QTimer* m_changedItemsTimer;
274 QSet<KFileItem> m_changedItems;
275
276 KDirWatch* m_dirWatcher;
277 mutable QSet<QString> m_watchedDirs; // Required as sadly KDirWatch does not offer a getter method
278 // to get all watched directories.
279 #ifdef HAVE_NEPOMUK
280 Nepomuk2::ResourceWatcher* m_nepomukResourceWatcher;
281 mutable QHash<QUrl, KUrl> m_nepomukUriItems;
282 #endif
283 };
284
285 #endif
286
287