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 <config-nepomuk.h>
26 #include <kitemviews/kitemmodelbase.h>
28 #include <libdolphin_export.h>
33 #include <QStringList>
44 class ResourceWatcher
;
48 // Required for the slot applyChangedNepomukRoles() that
49 // cannot be ifdefined due to moc.
57 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
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.
63 class LIBDOLPHINPRIVATE_EXPORT KFileItemModelRolesUpdater
: public QObject
68 explicit KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
= 0);
69 virtual ~KFileItemModelRolesUpdater();
71 void setIconSize(const QSize
& size
);
72 QSize
iconSize() const;
75 * Sets the range of items that are visible currently. The roles
76 * of visible items are resolved first.
78 void setVisibleIndexRange(int index
, int count
);
80 void setMaximumVisibleItems(int count
);
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"
87 void setPreviewsShown(bool show
);
88 bool previewsShown() const;
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
95 void setEnlargeSmallPreviews(bool enlarge
);
96 bool enlargeSmallPreviews() const;
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.
103 void setPaused(bool paused
);
104 bool isPaused() const;
107 * Sets the roles that should be resolved asynchronously.
109 void setRoles(const QSet
<QByteArray
>& roles
);
110 QSet
<QByteArray
> roles() const;
113 * Sets the list of enabled thumbnail plugins that are used for previews.
114 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
117 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
119 * @see enabledPlugins
121 void setEnabledPlugins(const QStringList
& list
);
124 * Returns the list of enabled thumbnail plugins.
125 * @see setEnabledPlugins
127 QStringList
enabledPlugins() const;
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
);
138 * Is invoked after a preview has been received successfully.
139 * @see startPreviewJob()
141 void slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
);
144 * Is invoked after generating a preview has failed.
145 * @see startPreviewJob()
147 void slotPreviewFailed(const KFileItem
& item
);
150 * Is invoked when the preview job has been finished and
151 * removes the job from the m_previewJobs list.
152 * @see startPreviewJob()
154 void slotPreviewJobFinished(KJob
* job
);
156 void resolveNextPendingRoles();
159 * Resolves items that have not been resolved yet after the change has been
160 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
163 void resolveChangedItems();
165 void applyChangedNepomukRoles(const Nepomuk2::Resource
& resource
);
168 * Is invoked if a directory watched by KDirWatch got dirty. Updates
169 * the "isExpandable"- and "size"-roles of the item that matches to
172 void slotDirWatchDirty(const QString
& path
);
176 * Updates the roles for the given item ranges. The roles for the currently
177 * visible items will get updated first.
179 void startUpdating(const KItemRangeList
& itemRanges
);
182 * Creates previews for the items starting from the first item of the
184 * @see slotGotPreview()
185 * @see slotPreviewFailed()
186 * @see slotPreviewJobFinished()
188 void startPreviewJob(const KFileItemList
& items
);
190 bool hasPendingRoles() const;
191 void resolvePendingRoles();
192 void resetPendingRoles();
193 void sortAndResolveAllRoles();
194 void sortAndResolvePendingRoles();
195 void applySortProgressToModel();
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
204 void updateSortProgress();
207 * @return True, if at least one item from the model
208 * has an unknown MIME-type.
210 bool hasUnknownMimeTypes() const;
216 bool applyResolvedRoles(const KFileItem
& item
, ResolveHint hint
);
217 QHash
<QByteArray
, QVariant
> rolesData(const KFileItem
& item
) const;
219 KFileItemList
sortedItems(const QSet
<KFileItem
>& items
) const;
222 * @return The number of items of the path \a path.
224 int subItemsCount(const QString
& path
) const;
227 * Must be invoked if a property has been changed that affects
228 * the look of the preview. Takes care to update all previews.
230 void updateAllPreviews();
233 // Property for setPaused()/isPaused().
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
;
242 // Property for setPreviewsShown()/previewsShown().
245 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
246 bool m_enlargeSmallPreviews
;
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
;
253 int m_sortingProgress
;
255 KFileItemModel
* m_model
;
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
;
264 QSet
<KFileItem
> m_pendingVisibleItems
;
265 QSet
<KFileItem
> m_pendingInvisibleItems
;
266 QList
<KJob
*> m_previewJobs
;
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
273 QTimer
* m_changedItemsTimer
;
274 QSet
<KFileItem
> m_changedItems
;
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.
280 Nepomuk2::ResourceWatcher
* m_nepomukResourceWatcher
;
281 mutable QHash
<QUrl
, KUrl
> m_nepomukUriItems
;