]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.h
Merge remote-tracking branch 'origin/KDE/4.9'
[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-nepomuk.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 KDirWatch;
36 class KFileItemModel;
37 class KJob;
38 class QPixmap;
39 class QTimer;
40
41 #ifdef HAVE_NEPOMUK
42 namespace Nepomuk2
43 {
44 class ResourceWatcher;
45 class Resource;
46 }
47 #else
48 // Required for the slot applyChangedNepomukRoles() that
49 // cannot be ifdefined due to moc.
50 namespace Nepomuk2
51 {
52 class Resource;
53 }
54 #endif
55
56 /**
57 * @brief Resolves expensive roles asynchronously and applies them to the KFileItemModel.
58 *
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.
62 */
63 class LIBDOLPHINPRIVATE_EXPORT KFileItemModelRolesUpdater : public QObject
64 {
65 Q_OBJECT
66
67 public:
68 explicit KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent = 0);
69 virtual ~KFileItemModelRolesUpdater();
70
71 void setIconSize(const QSize& size);
72 QSize iconSize() const;
73
74 /**
75 * Sets the range of items that are visible currently. The roles
76 * of visible items are resolved first.
77 */
78 void setVisibleIndexRange(int index, int count);
79
80 /**
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"
83 * role.
84 */
85 void setPreviewsShown(bool show);
86 bool previewsShown() const;
87
88 /**
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
91 * enabled.
92 */
93 void setEnlargeSmallPreviews(bool enlarge);
94 bool enlargeSmallPreviews() const;
95
96 /**
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.
100 */
101 void setPaused(bool paused);
102 bool isPaused() const;
103
104 /**
105 * Sets the roles that should be resolved asynchronously.
106 */
107 void setRoles(const QSet<QByteArray>& roles);
108 QSet<QByteArray> roles() const;
109
110 /**
111 * Sets the list of enabled thumbnail plugins that are used for previews.
112 * Per default all plugins enabled in the KConfigGroup "PreviewSettings"
113 * are used.
114 *
115 * For a list of available plugins, call KServiceTypeTrader::self()->query("ThumbCreator").
116 *
117 * @see enabledPlugins
118 */
119 void setEnabledPlugins(const QStringList& list);
120
121 /**
122 * Returns the list of enabled thumbnail plugins.
123 * @see setEnabledPlugins
124 */
125 QStringList enabledPlugins() const;
126
127 private slots:
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);
134
135 /**
136 * Is invoked after a preview has been received successfully.
137 * @see startPreviewJob()
138 */
139 void slotGotPreview(const KFileItem& item, const QPixmap& pixmap);
140
141 /**
142 * Is invoked after generating a preview has failed.
143 * @see startPreviewJob()
144 */
145 void slotPreviewFailed(const KFileItem& item);
146
147 /**
148 * Is invoked when the preview job has been finished and
149 * removes the job from the m_previewJobs list.
150 * @see startPreviewJob()
151 */
152 void slotPreviewJobFinished(KJob* job);
153
154 void resolveNextPendingRoles();
155
156 /**
157 * Resolves items that have not been resolved yet after the change has been
158 * notified by slotItemsChanged(). Is invoked if the m_changedItemsTimer
159 * exceeds.
160 */
161 void resolveChangedItems();
162
163 void applyChangedNepomukRoles(const Nepomuk2::Resource& resource);
164
165 /**
166 * Is invoked if a directory watched by KDirWatch got dirty. Updates
167 * the "isExpandable"- and "size"-roles of the item that matches to
168 * the given path.
169 */
170 void slotDirWatchDirty(const QString& path);
171
172 private:
173 /**
174 * Updates the roles for the given item ranges. The roles for the currently
175 * visible items will get updated first.
176 */
177 void startUpdating(const KItemRangeList& itemRanges);
178
179 /**
180 * Creates previews for the items starting from the first item of the
181 * given list.
182 * @see slotGotPreview()
183 * @see slotPreviewFailed()
184 * @see slotPreviewJobFinished()
185 */
186 void startPreviewJob(const KFileItemList& items);
187
188 bool hasPendingRoles() const;
189 void resolvePendingRoles();
190 void resetPendingRoles();
191 void sortAndResolveAllRoles();
192 void sortAndResolvePendingRoles();
193 void applySortProgressToModel();
194
195 /**
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
200 * will be provided.
201 */
202 void updateSortProgress();
203
204 /**
205 * @return True, if at least one item from the model
206 * has an unknown MIME-type.
207 */
208 bool hasUnknownMimeTypes() const;
209
210 enum ResolveHint {
211 ResolveFast,
212 ResolveAll
213 };
214 bool applyResolvedRoles(const KFileItem& item, ResolveHint hint);
215 QHash<QByteArray, QVariant> rolesData(const KFileItem& item) const;
216
217 KFileItemList sortedItems(const QSet<KFileItem>& items) const;
218
219 /**
220 * @return The number of items of the path \a path.
221 */
222 int subItemsCount(const QString& path) const;
223
224 /**
225 * Must be invoked if a property has been changed that affects
226 * the look of the preview. Takes care to update all previews.
227 */
228 void updateAllPreviews();
229
230 private:
231 // Property for setPaused()/isPaused().
232 bool m_paused;
233
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;
239
240 // Property for setPreviewsShown()/previewsShown().
241 bool m_previewShown;
242
243 // Property for setEnlargeSmallPreviews()/enlargeSmallPreviews()
244 bool m_enlargeSmallPreviews;
245
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;
250
251 int m_sortingProgress;
252
253 KFileItemModel* m_model;
254 QSize m_iconSize;
255 int m_firstVisibleIndex;
256 int m_lastVisibleIndex;
257 QSet<QByteArray> m_roles;
258 QSet<QByteArray> m_resolvableRoles;
259 QStringList m_enabledPlugins;
260
261 QSet<KFileItem> m_pendingVisibleItems;
262 QSet<KFileItem> m_pendingInvisibleItems;
263 QList<KJob*> m_previewJobs;
264
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
269 // of time.
270 QTimer* m_changedItemsTimer;
271 QSet<KFileItem> m_changedItems;
272
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.
276 #ifdef HAVE_NEPOMUK
277 Nepomuk2::ResourceWatcher* m_nepomukResourceWatcher;
278 mutable QHash<QUrl, KUrl> m_nepomukUriItems;
279 #endif
280 };
281
282 #endif
283
284