]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.h
Merge remote-tracking branch 'origin/KDE/4.11'
[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 KDirectoryContentsCounter;
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 namespace Types
47 {
48 class Property;
49 }
50 }
51 #else
52 // Required for the slot applyChangedNepomukRoles() that
53 // cannot be ifdefined due to moc.
54 namespace Nepomuk2
55 {
56 class Resource;
57 namespace Types
58 {
59 class Property;
60 }
61 }
62 #endif
63
64 /**
65 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
66 *
67 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
68 * the permissions. Creating previews or determining the MIME-type can be quite expensive
69 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
70 *
71 * To prevent a huge CPU and I/O load, these roles are not updated for all
72 * items, but only for the visible items, some items around the visible area,
73 * and the items on the first and last pages of the view. This is a compromise
74 * that aims to minimize the risk that the user sees items with unknown icons
75 * in the view when scrolling or pressing Home or End.
76 *
77 * Determining the roles is done in several phases:
78 *
79 * 1. If the sort role is "slow", it is determined for all items. If this
80 * cannot be finished synchronously in 200 ms, the remaining items are
81 * handled asynchronously by \a resolveNextSortRole().
82 *
83 * 2. The function startUpdating(), which is called if either the sort role
84 * has been successfully determined for all items, or items are inserted
85 * in the view, or the visible items might have changed because items
86 * were removed or moved, tries to determine the icons for all visible
87 * items synchronously for 200 ms. Then:
88 *
89 * (a) If previews are disabled, icons and all other roles are determined
90 * asynchronously for the interesting items. This is done by the
91 * function \a resolveNextPendingRoles().
92 *
93 * (b) If previews are enabled, a \a KIO::PreviewJob is started that loads
94 * the previews for the interesting items. At the same time, the icons
95 * for these items are determined asynchronously as fast as possible
96 * by \a resolveNextPendingRoles(). This minimizes the risk that the
97 * user sees "unknown" icons when scrolling before the previews have
98 * arrived.
99 *
100 * 3. Finally, the entire process is repeated for any items that might have
101 * changed in the mean time.
102 */
103 class LIBDOLPHINPRIVATE_EXPORT KFileItemModelRolesUpdater : public QObject
104 {
105 Q_OBJECT
106
107 public:
108 explicit KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent = 0);
109 virtual ~KFileItemModelRolesUpdater();
110
111 void setIconSize(const QSize& size);
112 QSize iconSize() const;
113
114 /**
115 * Sets the range of items that are visible currently. The roles
116 * of visible items are resolved first.
117 */
118 void setVisibleIndexRange(int index, int count);
119
120 void setMaximumVisibleItems(int count);
121
122 /**
123 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
124 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
125 * role.
126 */
127 void setPreviewsShown(bool show);
128 bool previewsShown() const;
129
130 /**
131 * If enabled a small preview gets upscaled to the icon size in case where
132 * the icon size is larger than the preview. Per default enlarging is
133 * enabled.
134 */
135 void setEnlargeSmallPreviews(bool enlarge);
136 bool enlargeSmallPreviews() const;
137
138 /**
139 * If \a paused is set to true the asynchronous resolving of roles will be paused.
140 * State changes during pauses like changing the icon size or the preview-shown
141 * will be remembered and handled after unpausing.
142 */
143 void setPaused(bool paused);
144 bool isPaused() const;
145
146 /**
147 * Sets the roles that should be resolved asynchronously.
148 */
149 void setRoles(const QSet<QByteArray>& roles);
150 QSet<QByteArray> roles() const;
151
152 /**
153 * Sets the list of enabled thumbnail plugins that are used for previews.
154 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
155 * are used.
156 *
157 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
158 *
159 * @see enabledPlugins
160 */
161 void setEnabledPlugins(const QStringList& list);
162
163 /**
164 * Returns the list of enabled thumbnail plugins.
165 * @see setEnabledPlugins
166 */
167 QStringList enabledPlugins() const;
168
169 private slots:
170 void slotItemsInserted(const KItemRangeList& itemRanges);
171 void slotItemsRemoved(const KItemRangeList& itemRanges);
172 void slotItemsMoved(const KItemRange& itemRange, QList<int> movedToIndexes);
173 void slotItemsChanged(const KItemRangeList& itemRanges,
174 const QSet<QByteArray>& roles);
175 void slotSortRoleChanged(const QByteArray& current,
176 const QByteArray& previous);
177
178 /**
179 * Is invoked after a preview has been received successfully.
180 * @see startPreviewJob()
181 */
182 void slotGotPreview(const KFileItem& item, const QPixmap& pixmap);
183
184 /**
185 * Is invoked after generating a preview has failed.
186 * @see startPreviewJob()
187 */
188 void slotPreviewFailed(const KFileItem& item);
189
190 /**
191 * Is invoked when the preview job has been finished. Starts a new preview
192 * job if there are any interesting items without previews left, or updates
193 * the changed items otherwise. *
194 * @see startPreviewJob()
195 */
196 void slotPreviewJobFinished();
197
198 /**
199 * Resolves the sort role of the next item in m_pendingSortRole, applies it
200 * to the model, and invokes itself if there are any pending items left. If
201 * that is not the case, \a startUpdating() is called.
202 */
203 void resolveNextSortRole();
204
205 /**
206 * Resolves the icon name and (if previews are disabled) all other roles
207 * for the next interesting item. If there are no pending items left, any
208 * changed items are updated.
209 */
210 void resolveNextPendingRoles();
211
212 /**
213 * Resolves items that have not been resolved yet after the change has been
214 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
215 * expires.
216 */
217 void resolveRecentlyChangedItems();
218
219 void applyChangedNepomukRoles(const Nepomuk2::Resource& resource, const Nepomuk2::Types::Property& property);
220
221 void slotDirectoryContentsCountReceived(const QString& path, int count);
222
223 private:
224 /**
225 * Starts the updating of all roles. The visible items are handled first.
226 */
227 void startUpdating();
228
229 /**
230 * Loads the icons for the visible items. After 200 ms, the function
231 * stops determining mime types and only loads preliminary icons.
232 * This is a compromise that prevents that
233 * (a) the GUI is blocked for more than 200 ms, and
234 * (b) "unknown" icons could be shown in the view.
235 */
236 void updateVisibleIcons();
237
238 /**
239 * Creates previews for the items starting from the first item in
240 * m_pendingPreviewItems.
241 * @see slotGotPreview()
242 * @see slotPreviewFailed()
243 * @see slotPreviewJobFinished()
244 */
245 void startPreviewJob();
246
247 /**
248 * Ensures that icons, previews, and other roles are determined for any
249 * items that have been changed.
250 */
251 void updateChangedItems();
252
253 /**
254 * Resolves the sort role of the item and applies it to the model.
255 */
256 void applySortRole(int index);
257
258 void applySortProgressToModel();
259
260 enum ResolveHint {
261 ResolveFast,
262 ResolveAll
263 };
264 bool applyResolvedRoles(const KFileItem& item, ResolveHint hint);
265 QHash<QByteArray, QVariant> rolesData(const KFileItem& item);
266
267 /**
268 * @return The number of items of the path \a path.
269 */
270 int subItemsCount(const QString& path) const;
271
272 /**
273 * Must be invoked if a property has been changed that affects
274 * the look of the preview. Takes care to update all previews.
275 */
276 void updateAllPreviews();
277
278 void killPreviewJob();
279
280 QList<int> indexesToResolve() const;
281
282 private:
283 enum State {
284 Idle,
285 Paused,
286 ResolvingSortRole,
287 ResolvingAllRoles,
288 PreviewJobRunning
289 };
290
291 State m_state;
292
293 // Property changes during pausing must be remembered to be able
294 // to react when unpausing again:
295 bool m_previewChangedDuringPausing;
296 bool m_iconSizeChangedDuringPausing;
297 bool m_rolesChangedDuringPausing;
298
299 // Property for setPreviewsShown()/previewsShown().
300 bool m_previewShown;
301
302 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
303 bool m_enlargeSmallPreviews;
304
305 // True if the role "iconPixmap" should be cleared when resolving the next
306 // role with resolveRole(). Is necessary if the preview gets disabled
307 // during the roles-updater has been paused by setPaused().
308 bool m_clearPreviews;
309
310 // Remembers which items have been handled already, to prevent that
311 // previews and other expensive roles are determined again.
312 QSet<KFileItem> m_finishedItems;
313
314 KFileItemModel* m_model;
315 QSize m_iconSize;
316 int m_firstVisibleIndex;
317 int m_lastVisibleIndex;
318 int m_maximumVisibleItems;
319 QSet<QByteArray> m_roles;
320 QSet<QByteArray> m_resolvableRoles;
321 QStringList m_enabledPlugins;
322
323 // Items for which the sort role still has to be determined.
324 QSet<KFileItem> m_pendingSortRoleItems;
325
326 // Indexes of items which still have to be handled by
327 // resolveNextPendingRoles().
328 QList<int> m_pendingIndexes;
329
330 // Items which have been left over from the last call of startPreviewJob().
331 // A new preview job will be started from them once the first one finishes.
332 KFileItemList m_pendingPreviewItems;
333
334 KJob* m_previewJob;
335
336 // When downloading or copying large files, the slot slotItemsChanged()
337 // will be called periodically within a quite short delay. To prevent
338 // a high CPU-load by generating e.g. previews for each notification, the update
339 // will be postponed until no file change has been done within a longer period
340 // of time.
341 QTimer* m_recentlyChangedItemsTimer;
342 QSet<KFileItem> m_recentlyChangedItems;
343
344 // Items which have not been changed repeatedly recently.
345 QSet<KFileItem> m_changedItems;
346
347 KDirectoryContentsCounter* m_directoryContentsCounter;
348
349 #ifdef HAVE_NEPOMUK
350 Nepomuk2::ResourceWatcher* m_nepomukResourceWatcher;
351 mutable QHash<QUrl, KUrl> m_nepomukUriItems;
352 #endif
353 };
354
355 #endif
356
357