1 /***************************************************************************
2 * Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com> *
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. *
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. *
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 ***************************************************************************/
20 #ifndef KFILEITEMMODELROLESUPDATER_H
21 #define KFILEITEMMODELROLESUPDATER_H
23 #include "dolphin_export.h"
24 #include "kitemviews/kitemmodelbase.h"
27 #include <config-baloo.h>
32 #include <QStringList>
34 class KDirectoryContentsCounter
;
38 class KOverlayIconPlugin
;
49 #include <Baloo/IndexerConfig>
53 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
55 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
56 * the permissions. Creating previews or determining the MIME-type can be quite expensive
57 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
59 * To prevent a huge CPU and I/O load, these roles are not updated for all
60 * items, but only for the visible items, some items around the visible area,
61 * and the items on the first and last pages of the view. This is a compromise
62 * that aims to minimize the risk that the user sees items with unknown icons
63 * in the view when scrolling or pressing Home or End.
65 * Determining the roles is done in several phases:
67 * 1. If the sort role is "slow", it is determined for all items. If this
68 * cannot be finished synchronously in 200 ms, the remaining items are
69 * handled asynchronously by \a resolveNextSortRole().
71 * 2. The function startUpdating(), which is called if either the sort role
72 * has been successfully determined for all items, or items are inserted
73 * in the view, or the visible items might have changed because items
74 * were removed or moved, tries to determine the icons for all visible
75 * items synchronously for 200 ms. Then:
77 * (a) If previews are disabled, icons and all other roles are determined
78 * asynchronously for the interesting items. This is done by the
79 * function \a resolveNextPendingRoles().
81 * (b) If previews are enabled, a \a KIO::PreviewJob is started that loads
82 * the previews for the interesting items. At the same time, the icons
83 * for these items are determined asynchronously as fast as possible
84 * by \a resolveNextPendingRoles(). This minimizes the risk that the
85 * user sees "unknown" icons when scrolling before the previews have
88 * 3. Finally, the entire process is repeated for any items that might have
89 * changed in the mean time.
91 class DOLPHIN_EXPORT KFileItemModelRolesUpdater
: public QObject
96 explicit KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
= nullptr);
97 ~KFileItemModelRolesUpdater() override
;
99 void setIconSize(const QSize
& size
);
100 QSize
iconSize() const;
103 * Sets the range of items that are visible currently. The roles
104 * of visible items are resolved first.
106 void setVisibleIndexRange(int index
, int count
);
108 void setMaximumVisibleItems(int count
);
111 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
112 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
115 void setPreviewsShown(bool show
);
116 bool previewsShown() const;
119 * If enabled a small preview gets upscaled to the icon size in case where
120 * the icon size is larger than the preview. Per default enlarging is
123 void setEnlargeSmallPreviews(bool enlarge
);
124 bool enlargeSmallPreviews() const;
127 * If \a paused is set to true the asynchronous resolving of roles will be paused.
128 * State changes during pauses like changing the icon size or the preview-shown
129 * will be remembered and handled after unpausing.
131 void setPaused(bool paused
);
132 bool isPaused() const;
135 * Sets the roles that should be resolved asynchronously.
137 void setRoles(const QSet
<QByteArray
>& roles
);
138 QSet
<QByteArray
> roles() const;
141 * Sets the list of enabled thumbnail plugins that are used for previews.
142 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
145 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
147 * @see enabledPlugins
149 void setEnabledPlugins(const QStringList
& list
);
152 * Returns the list of enabled thumbnail plugins.
153 * @see setEnabledPlugins
155 QStringList
enabledPlugins() const;
158 * Sets the maximum file size of local files for which
159 * previews will be generated (if enabled). A value of 0
160 * indicates no file size limit.
161 * Per default the value from KConfigGroup "PreviewSettings"
162 * MaximumSize is used, 0 otherwise.
165 void setLocalFileSizePreviewLimit(qlonglong size
);
166 qlonglong
localFileSizePreviewLimit() const;
169 void slotItemsInserted(const KItemRangeList
& itemRanges
);
170 void slotItemsRemoved(const KItemRangeList
& itemRanges
);
171 void slotItemsMoved(const KItemRange
& itemRange
, const QList
<int> &movedToIndexes
);
172 void slotItemsChanged(const KItemRangeList
& itemRanges
,
173 const QSet
<QByteArray
>& roles
);
174 void slotSortRoleChanged(const QByteArray
& current
,
175 const QByteArray
& previous
);
178 * Is invoked after a preview has been received successfully.
179 * @see startPreviewJob()
181 void slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
);
184 * Is invoked after generating a preview has failed.
185 * @see startPreviewJob()
187 void slotPreviewFailed(const KFileItem
& item
);
190 * Is invoked when the preview job has been finished. Starts a new preview
191 * job if there are any interesting items without previews left, or updates
192 * the changed items otherwise. *
193 * @see startPreviewJob()
195 void slotPreviewJobFinished();
198 * Is invoked when one of the KOverlayIconPlugin emit the signal that an overlay has changed
200 void slotOverlaysChanged(const QUrl
& url
, const QStringList
&);
203 * Resolves the sort role of the next item in m_pendingSortRole, applies it
204 * to the model, and invokes itself if there are any pending items left. If
205 * that is not the case, \a startUpdating() is called.
207 void resolveNextSortRole();
210 * Resolves the icon name and (if previews are disabled) all other roles
211 * for the next interesting item. If there are no pending items left, any
212 * changed items are updated.
214 void resolveNextPendingRoles();
217 * Resolves items that have not been resolved yet after the change has been
218 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
221 void resolveRecentlyChangedItems();
223 void applyChangedBalooRoles(const QString
& file
);
224 void applyChangedBalooRolesForItem(const KFileItem
& file
);
226 void slotDirectoryContentsCountReceived(const QString
& path
, int count
, long size
);
230 * Starts the updating of all roles. The visible items are handled first.
232 void startUpdating();
235 * Loads the icons for the visible items. After 200 ms, the function
236 * stops determining mime types and only loads preliminary icons.
237 * This is a compromise that prevents that
238 * (a) the GUI is blocked for more than 200 ms, and
239 * (b) "unknown" icons could be shown in the view.
241 void updateVisibleIcons();
244 * Creates previews for the items starting from the first item in
245 * m_pendingPreviewItems.
246 * @see slotGotPreview()
247 * @see slotPreviewFailed()
248 * @see slotPreviewJobFinished()
250 void startPreviewJob();
253 * Ensures that icons, previews, and other roles are determined for any
254 * items that have been changed.
256 void updateChangedItems();
259 * Resolves the sort role of the item and applies it to the model.
261 void applySortRole(int index
);
263 void applySortProgressToModel();
269 bool applyResolvedRoles(int index
, ResolveHint hint
);
270 QHash
<QByteArray
, QVariant
> rolesData(const KFileItem
& item
);
273 * @return The number of items of the path \a path.
275 int subItemsCount(const QString
& path
) const;
278 * Must be invoked if a property has been changed that affects
279 * the look of the preview. Takes care to update all previews.
281 void updateAllPreviews();
283 void killPreviewJob();
285 QList
<int> indexesToResolve() const;
298 // Property changes during pausing must be remembered to be able
299 // to react when unpausing again:
300 bool m_previewChangedDuringPausing
;
301 bool m_iconSizeChangedDuringPausing
;
302 bool m_rolesChangedDuringPausing
;
304 // Property for setPreviewsShown()/previewsShown().
307 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
308 bool m_enlargeSmallPreviews
;
310 // True if the role "iconPixmap" should be cleared when resolving the next
311 // role with resolveRole(). Is necessary if the preview gets disabled
312 // during the roles-updater has been paused by setPaused().
313 bool m_clearPreviews
;
315 // Remembers which items have been handled already, to prevent that
316 // previews and other expensive roles are determined again.
317 QSet
<KFileItem
> m_finishedItems
;
319 KFileItemModel
* m_model
;
321 int m_firstVisibleIndex
;
322 int m_lastVisibleIndex
;
323 int m_maximumVisibleItems
;
324 QSet
<QByteArray
> m_roles
;
325 QSet
<QByteArray
> m_resolvableRoles
;
326 QStringList m_enabledPlugins
;
327 qulonglong m_localFileSizePreviewLimit
;
329 // Items for which the sort role still has to be determined.
330 QSet
<KFileItem
> m_pendingSortRoleItems
;
332 // Indexes of items which still have to be handled by
333 // resolveNextPendingRoles().
334 QList
<int> m_pendingIndexes
;
336 // Items which have been left over from the last call of startPreviewJob().
337 // A new preview job will be started from them once the first one finishes.
338 KFileItemList m_pendingPreviewItems
;
340 KIO::PreviewJob
* m_previewJob
;
342 // When downloading or copying large files, the slot slotItemsChanged()
343 // will be called periodically within a quite short delay. To prevent
344 // a high CPU-load by generating e.g. previews for each notification, the update
345 // will be postponed until no file change has been done within a longer period
347 QTimer
* m_recentlyChangedItemsTimer
;
348 QSet
<KFileItem
> m_recentlyChangedItems
;
350 // Items which have not been changed repeatedly recently.
351 QSet
<KFileItem
> m_changedItems
;
353 KDirectoryContentsCounter
* m_directoryContentsCounter
;
355 QList
<KOverlayIconPlugin
*> m_overlayIconsPlugin
;
358 Baloo::FileMonitor
* m_balooFileMonitor
;
359 Baloo::IndexerConfig m_balooConfig
;