2 * SPDX-FileCopyrightText: 2011 Peter Penz <peter.penz19@gmail.com>
4 * SPDX-License-Identifier: GPL-2.0-or-later
7 #ifndef KFILEITEMMODELROLESUPDATER_H
8 #define KFILEITEMMODELROLESUPDATER_H
10 #include "dolphin_export.h"
11 #include "kitemviews/kitemmodelbase.h"
14 #include <config-baloo.h>
19 #include <QStringList>
21 class KDirectoryContentsCounter
;
25 class KOverlayIconPlugin
;
36 #include <Baloo/IndexerConfig>
40 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
42 * KFileItemModel only resolves roles that are inexpensive like e.g. the file name or
43 * the permissions. Creating previews or determining the MIME-type can be quite expensive
44 * and KFileItemModelRolesUpdater takes care to update such roles asynchronously.
46 * To prevent a huge CPU and I/O load, these roles are not updated for all
47 * items, but only for the visible items, some items around the visible area,
48 * and the items on the first and last pages of the view. This is a compromise
49 * that aims to minimize the risk that the user sees items with unknown icons
50 * in the view when scrolling or pressing Home or End.
52 * Determining the roles is done in several phases:
54 * 1. If the sort role is "slow", it is determined for all items. If this
55 * cannot be finished synchronously in 200 ms, the remaining items are
56 * handled asynchronously by \a resolveNextSortRole().
58 * 2. The function startUpdating(), which is called if either the sort role
59 * has been successfully determined for all items, or items are inserted
60 * in the view, or the visible items might have changed because items
61 * were removed or moved, tries to determine the icons for all visible
62 * items synchronously for 200 ms. Then:
64 * (a) If previews are disabled, icons and all other roles are determined
65 * asynchronously for the interesting items. This is done by the
66 * function \a resolveNextPendingRoles().
68 * (b) If previews are enabled, a \a KIO::PreviewJob is started that loads
69 * the previews for the interesting items. At the same time, the icons
70 * for these items are determined asynchronously as fast as possible
71 * by \a resolveNextPendingRoles(). This minimizes the risk that the
72 * user sees "unknown" icons when scrolling before the previews have
75 * 3. Finally, the entire process is repeated for any items that might have
76 * changed in the mean time.
78 class DOLPHIN_EXPORT KFileItemModelRolesUpdater
: public QObject
83 explicit KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
= nullptr);
84 ~KFileItemModelRolesUpdater() override
;
86 void setIconSize(const QSize
& size
);
87 QSize
iconSize() const;
90 * Sets the range of items that are visible currently. The roles
91 * of visible items are resolved first.
93 void setVisibleIndexRange(int index
, int count
);
95 void setMaximumVisibleItems(int count
);
98 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
99 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
102 void setPreviewsShown(bool show
);
103 bool previewsShown() const;
106 * If enabled a small preview gets upscaled to the icon size in case where
107 * the icon size is larger than the preview. Per default enlarging is
110 void setEnlargeSmallPreviews(bool enlarge
);
111 bool enlargeSmallPreviews() const;
114 * If \a paused is set to true the asynchronous resolving of roles will be paused.
115 * State changes during pauses like changing the icon size or the preview-shown
116 * will be remembered and handled after unpausing.
118 void setPaused(bool paused
);
119 bool isPaused() const;
122 * Sets the roles that should be resolved asynchronously.
124 void setRoles(const QSet
<QByteArray
>& roles
);
125 QSet
<QByteArray
> roles() const;
128 * Sets the list of enabled thumbnail plugins that are used for previews.
129 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
132 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
134 * @see enabledPlugins
136 void setEnabledPlugins(const QStringList
& list
);
139 * Returns the list of enabled thumbnail plugins.
140 * @see setEnabledPlugins
142 QStringList
enabledPlugins() const;
145 * Sets the maximum file size of local files for which
146 * previews will be generated (if enabled). A value of 0
147 * indicates no file size limit.
148 * Per default the value from KConfigGroup "PreviewSettings"
149 * MaximumSize is used, 0 otherwise.
152 void setLocalFileSizePreviewLimit(qlonglong size
);
153 qlonglong
localFileSizePreviewLimit() const;
156 void slotItemsInserted(const KItemRangeList
& itemRanges
);
157 void slotItemsRemoved(const KItemRangeList
& itemRanges
);
158 void slotItemsMoved(const KItemRange
& itemRange
, const QList
<int> &movedToIndexes
);
159 void slotItemsChanged(const KItemRangeList
& itemRanges
,
160 const QSet
<QByteArray
>& roles
);
161 void slotSortRoleChanged(const QByteArray
& current
,
162 const QByteArray
& previous
);
165 * Is invoked after a preview has been received successfully.
166 * @see startPreviewJob()
168 void slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
);
171 * Is invoked after generating a preview has failed.
172 * @see startPreviewJob()
174 void slotPreviewFailed(const KFileItem
& item
);
177 * Is invoked when the preview job has been finished. Starts a new preview
178 * job if there are any interesting items without previews left, or updates
179 * the changed items otherwise. *
180 * @see startPreviewJob()
182 void slotPreviewJobFinished();
185 * Is invoked when one of the KOverlayIconPlugin emit the signal that an overlay has changed
187 void slotOverlaysChanged(const QUrl
& url
, const QStringList
&);
190 * Resolves the sort role of the next item in m_pendingSortRole, applies it
191 * to the model, and invokes itself if there are any pending items left. If
192 * that is not the case, \a startUpdating() is called.
194 void resolveNextSortRole();
197 * Resolves the icon name and (if previews are disabled) all other roles
198 * for the next interesting item. If there are no pending items left, any
199 * changed items are updated.
201 void resolveNextPendingRoles();
204 * Resolves items that have not been resolved yet after the change has been
205 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
208 void resolveRecentlyChangedItems();
210 void applyChangedBalooRoles(const QString
& file
);
211 void applyChangedBalooRolesForItem(const KFileItem
& file
);
213 void slotDirectoryContentsCountReceived(const QString
& path
, int count
, long size
);
217 * Starts the updating of all roles. The visible items are handled first.
219 void startUpdating();
222 * Loads the icons for the visible items. After 200 ms, the function
223 * stops determining mime types and only loads preliminary icons.
224 * This is a compromise that prevents that
225 * (a) the GUI is blocked for more than 200 ms, and
226 * (b) "unknown" icons could be shown in the view.
228 void updateVisibleIcons();
231 * Creates previews for the items starting from the first item in
232 * m_pendingPreviewItems.
233 * @see slotGotPreview()
234 * @see slotPreviewFailed()
235 * @see slotPreviewJobFinished()
237 void startPreviewJob();
240 * Ensures that icons, previews, and other roles are determined for any
241 * items that have been changed.
243 void updateChangedItems();
246 * Resolves the sort role of the item and applies it to the model.
248 void applySortRole(int index
);
250 void applySortProgressToModel();
256 bool applyResolvedRoles(int index
, ResolveHint hint
);
257 QHash
<QByteArray
, QVariant
> rolesData(const KFileItem
& item
);
260 * @return The number of items of the path \a path.
262 int subItemsCount(const QString
& path
) const;
265 * Must be invoked if a property has been changed that affects
266 * the look of the preview. Takes care to update all previews.
268 void updateAllPreviews();
270 void killPreviewJob();
272 QList
<int> indexesToResolve() const;
285 // Property changes during pausing must be remembered to be able
286 // to react when unpausing again:
287 bool m_previewChangedDuringPausing
;
288 bool m_iconSizeChangedDuringPausing
;
289 bool m_rolesChangedDuringPausing
;
291 // Property for setPreviewsShown()/previewsShown().
294 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
295 bool m_enlargeSmallPreviews
;
297 // True if the role "iconPixmap" should be cleared when resolving the next
298 // role with resolveRole(). Is necessary if the preview gets disabled
299 // during the roles-updater has been paused by setPaused().
300 bool m_clearPreviews
;
302 // Remembers which items have been handled already, to prevent that
303 // previews and other expensive roles are determined again.
304 QSet
<KFileItem
> m_finishedItems
;
306 KFileItemModel
* m_model
;
308 int m_firstVisibleIndex
;
309 int m_lastVisibleIndex
;
310 int m_maximumVisibleItems
;
311 QSet
<QByteArray
> m_roles
;
312 QSet
<QByteArray
> m_resolvableRoles
;
313 QStringList m_enabledPlugins
;
314 qulonglong m_localFileSizePreviewLimit
;
316 // Items for which the sort role still has to be determined.
317 QSet
<KFileItem
> m_pendingSortRoleItems
;
319 // Indexes of items which still have to be handled by
320 // resolveNextPendingRoles().
321 QList
<int> m_pendingIndexes
;
323 // Items which have been left over from the last call of startPreviewJob().
324 // A new preview job will be started from them once the first one finishes.
325 KFileItemList m_pendingPreviewItems
;
327 KIO::PreviewJob
* m_previewJob
;
329 // When downloading or copying large files, the slot slotItemsChanged()
330 // will be called periodically within a quite short delay. To prevent
331 // a high CPU-load by generating e.g. previews for each notification, the update
332 // will be postponed until no file change has been done within a longer period
334 QTimer
* m_recentlyChangedItemsTimer
;
335 QSet
<KFileItem
> m_recentlyChangedItems
;
337 // Items which have not been changed repeatedly recently.
338 QSet
<KFileItem
> m_changedItems
;
340 KDirectoryContentsCounter
* m_directoryContentsCounter
;
342 QList
<KOverlayIconPlugin
*> m_overlayIconsPlugin
;
345 Baloo::FileMonitor
* m_balooFileMonitor
;
346 Baloo::IndexerConfig m_balooConfig
;