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
);
81 * If \a show is set to true, the "iconPixmap" role will be filled with a preview
82 * of the file. If \a show is false the MIME type icon will be used for the "iconPixmap"
85 void setPreviewsShown(bool show
);
86 bool previewsShown() const;
89 * If enabled a small preview gets upscaled to the icon size in case where
90 * the icon size is larger than the preview. Per default enlarging is
93 void setEnlargeSmallPreviews(bool enlarge
);
94 bool enlargeSmallPreviews() const;
97 * If \a paused is set to true the asynchronous resolving of roles will be paused.
98 * State changes during pauses like changing the icon size or the preview-shown
99 * will be remembered and handled after unpausing.
101 void setPaused(bool paused
);
102 bool isPaused() const;
105 * Sets the roles that should be resolved asynchronously.
107 void setRoles(const QSet
<QByteArray
>& roles
);
108 QSet
<QByteArray
> roles() const;
111 * Sets the list of enabled thumbnail plugins that are used for previews.
112 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
115 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
117 * @see enabledPlugins
119 void setEnabledPlugins(const QStringList
& list
);
122 * Returns the list of enabled thumbnail plugins.
123 * @see setEnabledPlugins
125 QStringList
enabledPlugins() const;
128 void slotItemsInserted(const KItemRangeList
& itemRanges
);
129 void slotItemsRemoved(const KItemRangeList
& itemRanges
);
130 void slotItemsChanged(const KItemRangeList
& itemRanges
,
131 const QSet
<QByteArray
>& roles
);
132 void slotSortRoleChanged(const QByteArray
& current
,
133 const QByteArray
& previous
);
136 * Is invoked after a preview has been received successfully.
137 * @see startPreviewJob()
139 void slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
);
142 * Is invoked after generating a preview has failed.
143 * @see startPreviewJob()
145 void slotPreviewFailed(const KFileItem
& item
);
148 * Is invoked when the preview job has been finished and
149 * removes the job from the m_previewJobs list.
150 * @see startPreviewJob()
152 void slotPreviewJobFinished(KJob
* job
);
154 void resolveNextPendingRoles();
157 * Resolves items that have not been resolved yet after the change has been
158 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
161 void resolveChangedItems();
163 void applyChangedNepomukRoles(const Nepomuk2::Resource
& resource
);
166 * Is invoked if a directory watched by KDirWatch got dirty. Updates
167 * the "isExpandable"- and "size"-roles of the item that matches to
170 void slotDirWatchDirty(const QString
& path
);
174 * Updates the roles for the given item ranges. The roles for the currently
175 * visible items will get updated first.
177 void startUpdating(const KItemRangeList
& itemRanges
);
180 * Creates previews for the items starting from the first item of the
182 * @see slotGotPreview()
183 * @see slotPreviewFailed()
184 * @see slotPreviewJobFinished()
186 void startPreviewJob(const KFileItemList
& items
);
188 bool hasPendingRoles() const;
189 void resolvePendingRoles();
190 void resetPendingRoles();
191 void sortAndResolveAllRoles();
192 void sortAndResolvePendingRoles();
193 void applySortProgressToModel();
196 * Updates m_sortProgress to be 0 if the sort-role
197 * needs to get resolved asynchronously and hence a
198 * progress is required. Otherwise m_sortProgress
199 * will be set to -1 which means that no progress
202 void updateSortProgress();
205 * @return True, if at least one item from the model
206 * has an unknown MIME-type.
208 bool hasUnknownMimeTypes() const;
214 bool applyResolvedRoles(const KFileItem
& item
, ResolveHint hint
);
215 QHash
<QByteArray
, QVariant
> rolesData(const KFileItem
& item
) const;
217 KFileItemList
sortedItems(const QSet
<KFileItem
>& items
) const;
220 * @return The number of items of the path \a path.
222 int subItemsCount(const QString
& path
) const;
225 * Must be invoked if a property has been changed that affects
226 * the look of the preview. Takes care to update all previews.
228 void updateAllPreviews();
231 // Property for setPaused()/isPaused().
234 // Property changes during pausing must be remembered to be able
235 // to react when unpausing again:
236 bool m_previewChangedDuringPausing
;
237 bool m_iconSizeChangedDuringPausing
;
238 bool m_rolesChangedDuringPausing
;
240 // Property for setPreviewsShown()/previewsShown().
243 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
244 bool m_enlargeSmallPreviews
;
246 // True if the role "iconPixmap" should be cleared when resolving the next
247 // role with resolveRole(). Is necessary if the preview gets disabled
248 // during the roles-updater has been paused by setPaused().
249 bool m_clearPreviews
;
251 int m_sortingProgress
;
253 KFileItemModel
* m_model
;
255 int m_firstVisibleIndex
;
256 int m_lastVisibleIndex
;
257 QSet
<QByteArray
> m_roles
;
258 QSet
<QByteArray
> m_resolvableRoles
;
259 QStringList m_enabledPlugins
;
261 QSet
<KFileItem
> m_pendingVisibleItems
;
262 QSet
<KFileItem
> m_pendingInvisibleItems
;
263 QList
<KJob
*> m_previewJobs
;
265 // When downloading or copying large files, the slot slotItemsChanged()
266 // will be called periodically within a quite short delay. To prevent
267 // a high CPU-load by generating e.g. previews for each notification, the update
268 // will be postponed until no file change has been done within a longer period
270 QTimer
* m_changedItemsTimer
;
271 QSet
<KFileItem
> m_changedItems
;
273 KDirWatch
* m_dirWatcher
;
274 mutable QSet
<QString
> m_watchedDirs
; // Required as sadly KDirWatch does not offer a getter method
275 // to get all watched directories.
277 Nepomuk2::ResourceWatcher
* m_nepomukResourceWatcher
;
278 mutable QHash
<QUrl
, KUrl
> m_nepomukUriItems
;