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