]> cloud.milkyroute.net Git - dolphin.git/blob - src/kitemviews/kfileitemmodelrolesupdater.cpp
Provide scaled pixmaps in the main file model
[dolphin.git] / src / kitemviews / kfileitemmodelrolesupdater.cpp
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 #include "kfileitemmodelrolesupdater.h"
21
22 #include "kfileitemmodel.h"
23
24 #include <KConfig>
25 #include <KConfigGroup>
26 #include <KSharedConfig>
27 #include <KFileItem>
28 #include <KIconLoader>
29 #include <KJobWidgets>
30 #include <KIO/JobUiDelegate>
31 #include <KIO/PreviewJob>
32
33 #include "private/kpixmapmodifier.h"
34 #include "private/kdirectorycontentscounter.h"
35
36 #include <QApplication>
37 #include <QPainter>
38 #include <QPixmap>
39 #include <QElapsedTimer>
40 #include <QTimer>
41
42 #include <algorithm>
43
44 #ifdef HAVE_BALOO
45 #include "private/kbaloorolesprovider.h"
46 #include <Baloo/File>
47 #include <Baloo/FileMonitor>
48 #endif
49
50 // #define KFILEITEMMODELROLESUPDATER_DEBUG
51
52 namespace {
53 // Maximum time in ms that the KFileItemModelRolesUpdater
54 // may perform a blocking operation
55 const int MaxBlockTimeout = 200;
56
57 // If the number of items is smaller than ResolveAllItemsLimit,
58 // the roles of all items will be resolved.
59 const int ResolveAllItemsLimit = 500;
60
61 // Not only the visible area, but up to ReadAheadPages before and after
62 // this area will be resolved.
63 const int ReadAheadPages = 5;
64 }
65
66 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel* model, QObject* parent) :
67 QObject(parent),
68 m_state(Idle),
69 m_previewChangedDuringPausing(false),
70 m_iconSizeChangedDuringPausing(false),
71 m_rolesChangedDuringPausing(false),
72 m_previewShown(false),
73 m_enlargeSmallPreviews(true),
74 m_clearPreviews(false),
75 m_finishedItems(),
76 m_model(model),
77 m_iconSize(),
78 m_firstVisibleIndex(0),
79 m_lastVisibleIndex(-1),
80 m_maximumVisibleItems(50),
81 m_roles(),
82 m_resolvableRoles(),
83 m_enabledPlugins(),
84 m_pendingSortRoleItems(),
85 m_pendingIndexes(),
86 m_pendingPreviewItems(),
87 m_previewJob(),
88 m_recentlyChangedItemsTimer(0),
89 m_recentlyChangedItems(),
90 m_changedItems(),
91 m_directoryContentsCounter(0)
92 #ifdef HAVE_BALOO
93 , m_balooFileMonitor(0)
94 #endif
95 {
96 Q_ASSERT(model);
97
98 const KConfigGroup globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
99 m_enabledPlugins = globalConfig.readEntry("Plugins", QStringList()
100 << "directorythumbnail"
101 << "imagethumbnail"
102 << "jpegthumbnail");
103
104 connect(m_model, &KFileItemModel::itemsInserted,
105 this, &KFileItemModelRolesUpdater::slotItemsInserted);
106 connect(m_model, &KFileItemModel::itemsRemoved,
107 this, &KFileItemModelRolesUpdater::slotItemsRemoved);
108 connect(m_model, &KFileItemModel::itemsChanged,
109 this, &KFileItemModelRolesUpdater::slotItemsChanged);
110 connect(m_model, &KFileItemModel::itemsMoved,
111 this, &KFileItemModelRolesUpdater::slotItemsMoved);
112 connect(m_model, &KFileItemModel::sortRoleChanged,
113 this, &KFileItemModelRolesUpdater::slotSortRoleChanged);
114
115 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
116 // resolving of the roles. Postpone the resolving until no update has been done for 1 second.
117 m_recentlyChangedItemsTimer = new QTimer(this);
118 m_recentlyChangedItemsTimer->setInterval(1000);
119 m_recentlyChangedItemsTimer->setSingleShot(true);
120 connect(m_recentlyChangedItemsTimer, &QTimer::timeout, this, &KFileItemModelRolesUpdater::resolveRecentlyChangedItems);
121
122 m_resolvableRoles.insert("size");
123 m_resolvableRoles.insert("type");
124 m_resolvableRoles.insert("isExpandable");
125 #ifdef HAVE_BALOO
126 m_resolvableRoles += KBalooRolesProvider::instance().roles();
127 #endif
128
129 m_directoryContentsCounter = new KDirectoryContentsCounter(m_model, this);
130 connect(m_directoryContentsCounter, &KDirectoryContentsCounter::result,
131 this, &KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived);
132 }
133
134 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
135 {
136 killPreviewJob();
137 }
138
139 void KFileItemModelRolesUpdater::setIconSize(const QSize& size)
140 {
141 if (size != m_iconSize) {
142 m_iconSize = size;
143 if (m_state == Paused) {
144 m_iconSizeChangedDuringPausing = true;
145 } else if (m_previewShown) {
146 // An icon size change requires the regenerating of
147 // all previews
148 m_finishedItems.clear();
149 startUpdating();
150 }
151 }
152 }
153
154 QSize KFileItemModelRolesUpdater::iconSize() const
155 {
156 return m_iconSize;
157 }
158
159 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index, int count)
160 {
161 if (index < 0) {
162 index = 0;
163 }
164 if (count < 0) {
165 count = 0;
166 }
167
168 if (index == m_firstVisibleIndex && count == m_lastVisibleIndex - m_firstVisibleIndex + 1) {
169 // The range has not been changed
170 return;
171 }
172
173 m_firstVisibleIndex = index;
174 m_lastVisibleIndex = qMin(index + count - 1, m_model->count() - 1);
175
176 startUpdating();
177 }
178
179 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count)
180 {
181 m_maximumVisibleItems = count;
182 }
183
184 void KFileItemModelRolesUpdater::setPreviewsShown(bool show)
185 {
186 if (show == m_previewShown) {
187 return;
188 }
189
190 m_previewShown = show;
191 if (!show) {
192 m_clearPreviews = true;
193 }
194
195 updateAllPreviews();
196 }
197
198 bool KFileItemModelRolesUpdater::previewsShown() const
199 {
200 return m_previewShown;
201 }
202
203 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge)
204 {
205 if (enlarge != m_enlargeSmallPreviews) {
206 m_enlargeSmallPreviews = enlarge;
207 if (m_previewShown) {
208 updateAllPreviews();
209 }
210 }
211 }
212
213 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
214 {
215 return m_enlargeSmallPreviews;
216 }
217
218 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList& list)
219 {
220 if (m_enabledPlugins != list) {
221 m_enabledPlugins = list;
222 if (m_previewShown) {
223 updateAllPreviews();
224 }
225 }
226 }
227
228 void KFileItemModelRolesUpdater::setPaused(bool paused)
229 {
230 if (paused == (m_state == Paused)) {
231 return;
232 }
233
234 if (paused) {
235 m_state = Paused;
236 killPreviewJob();
237 } else {
238 const bool updatePreviews = (m_iconSizeChangedDuringPausing && m_previewShown) ||
239 m_previewChangedDuringPausing;
240 const bool resolveAll = updatePreviews || m_rolesChangedDuringPausing;
241 if (resolveAll) {
242 m_finishedItems.clear();
243 }
244
245 m_iconSizeChangedDuringPausing = false;
246 m_previewChangedDuringPausing = false;
247 m_rolesChangedDuringPausing = false;
248
249 if (!m_pendingSortRoleItems.isEmpty()) {
250 m_state = ResolvingSortRole;
251 resolveNextSortRole();
252 } else {
253 m_state = Idle;
254 }
255
256 startUpdating();
257 }
258 }
259
260 void KFileItemModelRolesUpdater::setRoles(const QSet<QByteArray>& roles)
261 {
262 if (m_roles != roles) {
263 m_roles = roles;
264
265 #ifdef HAVE_BALOO
266 // Check whether there is at least one role that must be resolved
267 // with the help of Baloo. If this is the case, a (quite expensive)
268 // resolving will be done in KFileItemModelRolesUpdater::rolesData() and
269 // the role gets watched for changes.
270 const KBalooRolesProvider& rolesProvider = KBalooRolesProvider::instance();
271 bool hasBalooRole = false;
272 QSetIterator<QByteArray> it(roles);
273 while (it.hasNext()) {
274 const QByteArray& role = it.next();
275 if (rolesProvider.roles().contains(role)) {
276 hasBalooRole = true;
277 break;
278 }
279 }
280
281 if (hasBalooRole && !m_balooFileMonitor) {
282 m_balooFileMonitor = new Baloo::FileMonitor(this);
283 connect(m_balooFileMonitor, &Baloo::FileMonitor::fileMetaDataChanged,
284 this, &KFileItemModelRolesUpdater::applyChangedBalooRoles);
285 } else if (!hasBalooRole && m_balooFileMonitor) {
286 delete m_balooFileMonitor;
287 m_balooFileMonitor = 0;
288 }
289 #endif
290
291 if (m_state == Paused) {
292 m_rolesChangedDuringPausing = true;
293 } else {
294 startUpdating();
295 }
296 }
297 }
298
299 QSet<QByteArray> KFileItemModelRolesUpdater::roles() const
300 {
301 return m_roles;
302 }
303
304 bool KFileItemModelRolesUpdater::isPaused() const
305 {
306 return m_state == Paused;
307 }
308
309 QStringList KFileItemModelRolesUpdater::enabledPlugins() const
310 {
311 return m_enabledPlugins;
312 }
313
314 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList& itemRanges)
315 {
316 QElapsedTimer timer;
317 timer.start();
318
319 // Determine the sort role synchronously for as many items as possible.
320 if (m_resolvableRoles.contains(m_model->sortRole())) {
321 int insertedCount = 0;
322 foreach (const KItemRange& range, itemRanges) {
323 const int lastIndex = insertedCount + range.index + range.count - 1;
324 for (int i = insertedCount + range.index; i <= lastIndex; ++i) {
325 if (timer.elapsed() < MaxBlockTimeout) {
326 applySortRole(i);
327 } else {
328 m_pendingSortRoleItems.insert(m_model->fileItem(i));
329 }
330 }
331 insertedCount += range.count;
332 }
333
334 applySortProgressToModel();
335
336 // If there are still items whose sort role is unknown, check if the
337 // asynchronous determination of the sort role is already in progress,
338 // and start it if that is not the case.
339 if (!m_pendingSortRoleItems.isEmpty() && m_state != ResolvingSortRole) {
340 killPreviewJob();
341 m_state = ResolvingSortRole;
342 resolveNextSortRole();
343 }
344 }
345
346 startUpdating();
347 }
348
349 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList& itemRanges)
350 {
351 Q_UNUSED(itemRanges);
352
353 const bool allItemsRemoved = (m_model->count() == 0);
354
355 #ifdef HAVE_BALOO
356 if (m_balooFileMonitor) {
357 // Don't let the FileWatcher watch for removed items
358 if (allItemsRemoved) {
359 m_balooFileMonitor->clear();
360 } else {
361 QStringList newFileList;
362 foreach (const QString& itemUrl, m_balooFileMonitor->files()) {
363 if (m_model->index(itemUrl) >= 0) {
364 newFileList.append(itemUrl);
365 }
366 }
367 m_balooFileMonitor->setFiles(newFileList);
368 }
369 }
370 #endif
371
372 if (allItemsRemoved) {
373 m_state = Idle;
374
375 m_finishedItems.clear();
376 m_pendingSortRoleItems.clear();
377 m_pendingIndexes.clear();
378 m_pendingPreviewItems.clear();
379 m_recentlyChangedItems.clear();
380 m_recentlyChangedItemsTimer->stop();
381 m_changedItems.clear();
382
383 killPreviewJob();
384 } else {
385 // Only remove the items from m_finishedItems. They will be removed
386 // from the other sets later on.
387 QSet<KFileItem>::iterator it = m_finishedItems.begin();
388 while (it != m_finishedItems.end()) {
389 if (m_model->index(*it) < 0) {
390 it = m_finishedItems.erase(it);
391 } else {
392 ++it;
393 }
394 }
395
396 // The visible items might have changed.
397 startUpdating();
398 }
399 }
400
401 void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange& itemRange, QList<int> movedToIndexes)
402 {
403 Q_UNUSED(itemRange);
404 Q_UNUSED(movedToIndexes);
405
406 // The visible items might have changed.
407 startUpdating();
408 }
409
410 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList& itemRanges,
411 const QSet<QByteArray>& roles)
412 {
413 Q_UNUSED(roles);
414
415 // Find out if slotItemsChanged() has been done recently. If that is the
416 // case, resolving the roles is postponed until a timer has exceeded
417 // to prevent expensive repeated updates if files are updated frequently.
418 const bool itemsChangedRecently = m_recentlyChangedItemsTimer->isActive();
419
420 QSet<KFileItem>& targetSet = itemsChangedRecently ? m_recentlyChangedItems : m_changedItems;
421
422 foreach (const KItemRange& itemRange, itemRanges) {
423 int index = itemRange.index;
424 for (int count = itemRange.count; count > 0; --count) {
425 const KFileItem item = m_model->fileItem(index);
426 targetSet.insert(item);
427 ++index;
428 }
429 }
430
431 m_recentlyChangedItemsTimer->start();
432
433 if (!itemsChangedRecently) {
434 updateChangedItems();
435 }
436 }
437
438 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray& current,
439 const QByteArray& previous)
440 {
441 Q_UNUSED(current);
442 Q_UNUSED(previous);
443
444 if (m_resolvableRoles.contains(current)) {
445 m_pendingSortRoleItems.clear();
446 m_finishedItems.clear();
447
448 const int count = m_model->count();
449 QElapsedTimer timer;
450 timer.start();
451
452 // Determine the sort role synchronously for as many items as possible.
453 for (int index = 0; index < count; ++index) {
454 if (timer.elapsed() < MaxBlockTimeout) {
455 applySortRole(index);
456 } else {
457 m_pendingSortRoleItems.insert(m_model->fileItem(index));
458 }
459 }
460
461 applySortProgressToModel();
462
463 if (!m_pendingSortRoleItems.isEmpty()) {
464 // Trigger the asynchronous determination of the sort role.
465 killPreviewJob();
466 m_state = ResolvingSortRole;
467 resolveNextSortRole();
468 }
469 } else {
470 m_state = Idle;
471 m_pendingSortRoleItems.clear();
472 applySortProgressToModel();
473 }
474 }
475
476 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem& item, const QPixmap& pixmap)
477 {
478 if (m_state != PreviewJobRunning) {
479 return;
480 }
481
482 m_changedItems.remove(item);
483
484 const int index = m_model->index(item);
485 if (index < 0) {
486 return;
487 }
488
489 QPixmap scaledPixmap = pixmap;
490
491 const QString mimeType = item.mimetype();
492 const int slashIndex = mimeType.indexOf(QLatin1Char('/'));
493 const QString mimeTypeGroup = mimeType.left(slashIndex);
494 if (mimeTypeGroup == QLatin1String("image")) {
495 if (m_enlargeSmallPreviews) {
496 KPixmapModifier::applyFrame(scaledPixmap, m_iconSize * qApp->devicePixelRatio());
497 } else {
498 // Assure that small previews don't get enlarged. Instead they
499 // should be shown centered within the frame.
500 const QSize contentSize = KPixmapModifier::sizeInsideFrame(m_iconSize);
501 const bool enlargingRequired = scaledPixmap.width() < contentSize.width() &&
502 scaledPixmap.height() < contentSize.height();
503 if (enlargingRequired) {
504 QSize frameSize = scaledPixmap.size();
505 frameSize.scale(m_iconSize * qApp->devicePixelRatio(), Qt::KeepAspectRatio);
506
507 QPixmap largeFrame(frameSize);
508 largeFrame.fill(Qt::transparent);
509
510 KPixmapModifier::applyFrame(largeFrame, frameSize);
511
512 QPainter painter(&largeFrame);
513 painter.drawPixmap((largeFrame.width() - scaledPixmap.width() / scaledPixmap.devicePixelRatio()) / 2,
514 (largeFrame.height() - scaledPixmap.height() / scaledPixmap.devicePixelRatio()) / 2,
515 scaledPixmap);
516 scaledPixmap = largeFrame;
517 } else {
518 // The image must be shrinked as it is too large to fit into
519 // the available icon size
520 KPixmapModifier::applyFrame(scaledPixmap, m_iconSize * qApp->devicePixelRatio());
521 }
522 }
523 } else {
524 KPixmapModifier::scale(scaledPixmap, m_iconSize * qApp->devicePixelRatio());
525 }
526 scaledPixmap.setDevicePixelRatio(qApp->devicePixelRatio());
527
528 QHash<QByteArray, QVariant> data = rolesData(item);
529
530 const QStringList overlays = data["iconOverlays"].toStringList();
531 // Strangely KFileItem::overlays() returns empty string-values, so
532 // we need to check first whether an overlay must be drawn at all.
533 // It is more efficient to do it here, as KIconLoader::drawOverlays()
534 // assumes that an overlay will be drawn and has some additional
535 // setup time.
536 foreach (const QString& overlay, overlays) {
537 if (!overlay.isEmpty()) {
538 // There is at least one overlay, draw all overlays above m_pixmap
539 // and cancel the check
540 KIconLoader::global()->drawOverlays(overlays, scaledPixmap, KIconLoader::Desktop);
541 break;
542 }
543 }
544
545 data.insert("iconPixmap", scaledPixmap);
546
547 disconnect(m_model, &KFileItemModel::itemsChanged,
548 this, &KFileItemModelRolesUpdater::slotItemsChanged);
549 m_model->setData(index, data);
550 connect(m_model, &KFileItemModel::itemsChanged,
551 this, &KFileItemModelRolesUpdater::slotItemsChanged);
552
553 m_finishedItems.insert(item);
554 }
555
556 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem& item)
557 {
558 if (m_state != PreviewJobRunning) {
559 return;
560 }
561
562 m_changedItems.remove(item);
563
564 const int index = m_model->index(item);
565 if (index >= 0) {
566 QHash<QByteArray, QVariant> data;
567 data.insert("iconPixmap", QPixmap());
568
569 disconnect(m_model, &KFileItemModel::itemsChanged,
570 this, &KFileItemModelRolesUpdater::slotItemsChanged);
571 m_model->setData(index, data);
572 connect(m_model, &KFileItemModel::itemsChanged,
573 this, &KFileItemModelRolesUpdater::slotItemsChanged);
574
575 applyResolvedRoles(index, ResolveAll);
576 m_finishedItems.insert(item);
577 }
578 }
579
580 void KFileItemModelRolesUpdater::slotPreviewJobFinished()
581 {
582 m_previewJob = 0;
583
584 if (m_state != PreviewJobRunning) {
585 return;
586 }
587
588 m_state = Idle;
589
590 if (!m_pendingPreviewItems.isEmpty()) {
591 startPreviewJob();
592 } else {
593 if (!m_changedItems.isEmpty()) {
594 updateChangedItems();
595 }
596 }
597 }
598
599 void KFileItemModelRolesUpdater::resolveNextSortRole()
600 {
601 if (m_state != ResolvingSortRole) {
602 return;
603 }
604
605 QSet<KFileItem>::iterator it = m_pendingSortRoleItems.begin();
606 while (it != m_pendingSortRoleItems.end()) {
607 const KFileItem item = *it;
608 const int index = m_model->index(item);
609
610 // Continue if the sort role has already been determined for the
611 // item, and the item has not been changed recently.
612 if (!m_changedItems.contains(item) && m_model->data(index).contains(m_model->sortRole())) {
613 it = m_pendingSortRoleItems.erase(it);
614 continue;
615 }
616
617 applySortRole(index);
618 m_pendingSortRoleItems.erase(it);
619 break;
620 }
621
622 if (!m_pendingSortRoleItems.isEmpty()) {
623 applySortProgressToModel();
624 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
625 } else {
626 m_state = Idle;
627
628 // Prevent that we try to update the items twice.
629 disconnect(m_model, &KFileItemModel::itemsMoved,
630 this, &KFileItemModelRolesUpdater::slotItemsMoved);
631 applySortProgressToModel();
632 connect(m_model, &KFileItemModel::itemsMoved,
633 this, &KFileItemModelRolesUpdater::slotItemsMoved);
634 startUpdating();
635 }
636 }
637
638 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
639 {
640 if (m_state != ResolvingAllRoles) {
641 return;
642 }
643
644 while (!m_pendingIndexes.isEmpty()) {
645 const int index = m_pendingIndexes.takeFirst();
646 const KFileItem item = m_model->fileItem(index);
647
648 if (m_finishedItems.contains(item)) {
649 continue;
650 }
651
652 applyResolvedRoles(index, ResolveAll);
653 m_finishedItems.insert(item);
654 m_changedItems.remove(item);
655 break;
656 }
657
658 if (!m_pendingIndexes.isEmpty()) {
659 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
660 } else {
661 m_state = Idle;
662
663 if (m_clearPreviews) {
664 // Only go through the list if there are items which might still have previews.
665 if (m_finishedItems.count() != m_model->count()) {
666 QHash<QByteArray, QVariant> data;
667 data.insert("iconPixmap", QPixmap());
668
669 disconnect(m_model, &KFileItemModel::itemsChanged,
670 this, &KFileItemModelRolesUpdater::slotItemsChanged);
671 for (int index = 0; index <= m_model->count(); ++index) {
672 if (m_model->data(index).contains("iconPixmap")) {
673 m_model->setData(index, data);
674 }
675 }
676 connect(m_model, &KFileItemModel::itemsChanged,
677 this, &KFileItemModelRolesUpdater::slotItemsChanged);
678
679 }
680 m_clearPreviews = false;
681 }
682
683 if (!m_changedItems.isEmpty()) {
684 updateChangedItems();
685 }
686 }
687 }
688
689 void KFileItemModelRolesUpdater::resolveRecentlyChangedItems()
690 {
691 m_changedItems += m_recentlyChangedItems;
692 m_recentlyChangedItems.clear();
693 updateChangedItems();
694 }
695
696 void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString& itemUrl)
697 {
698 #ifdef HAVE_BALOO
699 const KFileItem item = m_model->fileItem(itemUrl);
700
701 if (item.isNull()) {
702 // itemUrl is not in the model anymore, probably because
703 // the corresponding file has been deleted in the meantime.
704 return;
705 }
706
707 Baloo::File file(item.localPath());
708 file.load();
709
710 const KBalooRolesProvider& rolesProvider = KBalooRolesProvider::instance();
711 QHash<QByteArray, QVariant> data;
712
713 foreach (const QByteArray& role, rolesProvider.roles()) {
714 // Overwrite all the role values with an empty QVariant, because the roles
715 // provider doesn't overwrite it when the property value list is empty.
716 // See bug 322348
717 data.insert(role, QVariant());
718 }
719
720 QHashIterator<QByteArray, QVariant> it(rolesProvider.roleValues(file, m_roles));
721 while (it.hasNext()) {
722 it.next();
723 data.insert(it.key(), it.value());
724 }
725
726 disconnect(m_model, &KFileItemModel::itemsChanged,
727 this, &KFileItemModelRolesUpdater::slotItemsChanged);
728 const int index = m_model->index(item);
729 m_model->setData(index, data);
730 connect(m_model, &KFileItemModel::itemsChanged,
731 this, &KFileItemModelRolesUpdater::slotItemsChanged);
732 #else
733 #ifndef Q_CC_MSVC
734 Q_UNUSED(itemUrl);
735 #endif
736 #endif
737 }
738
739 void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString& path, int count)
740 {
741 const bool getSizeRole = m_roles.contains("size");
742 const bool getIsExpandableRole = m_roles.contains("isExpandable");
743
744 if (getSizeRole || getIsExpandableRole) {
745 const int index = m_model->index(QUrl::fromLocalFile(path));
746 if (index >= 0) {
747 QHash<QByteArray, QVariant> data;
748
749 if (getSizeRole) {
750 data.insert("size", count);
751 }
752 if (getIsExpandableRole) {
753 data.insert("isExpandable", count > 0);
754 }
755
756 disconnect(m_model, &KFileItemModel::itemsChanged,
757 this, &KFileItemModelRolesUpdater::slotItemsChanged);
758 m_model->setData(index, data);
759 connect(m_model, &KFileItemModel::itemsChanged,
760 this, &KFileItemModelRolesUpdater::slotItemsChanged);
761 }
762 }
763 }
764
765 void KFileItemModelRolesUpdater::startUpdating()
766 {
767 if (m_state == Paused) {
768 return;
769 }
770
771 if (m_finishedItems.count() == m_model->count()) {
772 // All roles have been resolved already.
773 m_state = Idle;
774 return;
775 }
776
777 // Terminate all updates that are currently active.
778 killPreviewJob();
779 m_pendingIndexes.clear();
780
781 QElapsedTimer timer;
782 timer.start();
783
784 // Determine the icons for the visible items synchronously.
785 updateVisibleIcons();
786
787 // A detailed update of the items in and near the visible area
788 // only makes sense if sorting is finished.
789 if (m_state == ResolvingSortRole) {
790 return;
791 }
792
793 // Start the preview job or the asynchronous resolving of all roles.
794 QList<int> indexes = indexesToResolve();
795
796 if (m_previewShown) {
797 m_pendingPreviewItems.clear();
798 m_pendingPreviewItems.reserve(indexes.count());
799
800 foreach (int index, indexes) {
801 const KFileItem item = m_model->fileItem(index);
802 if (!m_finishedItems.contains(item)) {
803 m_pendingPreviewItems.append(item);
804 }
805 }
806
807 startPreviewJob();
808 } else {
809 m_pendingIndexes = indexes;
810 // Trigger the asynchronous resolving of all roles.
811 m_state = ResolvingAllRoles;
812 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
813 }
814 }
815
816 void KFileItemModelRolesUpdater::updateVisibleIcons()
817 {
818 int lastVisibleIndex = m_lastVisibleIndex;
819 if (lastVisibleIndex <= 0) {
820 // Guess a reasonable value for the last visible index if the view
821 // has not told us about the real value yet.
822 lastVisibleIndex = qMin(m_firstVisibleIndex + m_maximumVisibleItems, m_model->count() - 1);
823 if (lastVisibleIndex <= 0) {
824 lastVisibleIndex = qMin(200, m_model->count() - 1);
825 }
826 }
827
828 QElapsedTimer timer;
829 timer.start();
830
831 // Try to determine the final icons for all visible items.
832 int index;
833 for (index = m_firstVisibleIndex; index <= lastVisibleIndex && timer.elapsed() < MaxBlockTimeout; ++index) {
834 applyResolvedRoles(index, ResolveFast);
835 }
836
837 // KFileItemListView::initializeItemListWidget(KItemListWidget*) will load
838 // preliminary icons (i.e., without mime type determination) for the
839 // remaining items.
840 }
841
842 void KFileItemModelRolesUpdater::startPreviewJob()
843 {
844 m_state = PreviewJobRunning;
845
846 if (m_pendingPreviewItems.isEmpty()) {
847 QTimer::singleShot(0, this, SLOT(slotPreviewJobFinished()));
848 return;
849 }
850
851 // PreviewJob internally caches items always with the size of
852 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
853 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
854 // do a downscaling anyhow because of the frame, so in this case only the provided
855 // cache sizes are requested.
856 const QSize cacheSize = (m_iconSize.width() > 128) || (m_iconSize.height() > 128)
857 ? QSize(256, 256) : QSize(128, 128);
858
859 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
860 // worst case) might block the application for several seconds. To prevent such
861 // a blocking, we only pass items with known mime type to the preview job.
862 const int count = m_pendingPreviewItems.count();
863 KFileItemList itemSubSet;
864 itemSubSet.reserve(count);
865
866 if (m_pendingPreviewItems.first().isMimeTypeKnown()) {
867 // Some mime types are known already, probably because they were
868 // determined when loading the icons for the visible items. Start
869 // a preview job for all items at the beginning of the list which
870 // have a known mime type.
871 do {
872 itemSubSet.append(m_pendingPreviewItems.takeFirst());
873 } while (!m_pendingPreviewItems.isEmpty() && m_pendingPreviewItems.first().isMimeTypeKnown());
874 } else {
875 // Determine mime types for MaxBlockTimeout ms, and start a preview
876 // job for the corresponding items.
877 QElapsedTimer timer;
878 timer.start();
879
880 do {
881 const KFileItem item = m_pendingPreviewItems.takeFirst();
882 item.determineMimeType();
883 itemSubSet.append(item);
884 } while (!m_pendingPreviewItems.isEmpty() && timer.elapsed() < MaxBlockTimeout);
885 }
886
887 KIO::PreviewJob* job = new KIO::PreviewJob(itemSubSet, cacheSize, &m_enabledPlugins);
888
889 job->setIgnoreMaximumSize(itemSubSet.first().isLocalFile());
890 if (job->ui()) {
891 KJobWidgets::setWindow(job, qApp->activeWindow());
892 }
893
894 connect(job, &KIO::PreviewJob::gotPreview,
895 this, &KFileItemModelRolesUpdater::slotGotPreview);
896 connect(job, &KIO::PreviewJob::failed,
897 this, &KFileItemModelRolesUpdater::slotPreviewFailed);
898 connect(job, &KIO::PreviewJob::finished,
899 this, &KFileItemModelRolesUpdater::slotPreviewJobFinished);
900
901 m_previewJob = job;
902 }
903
904 void KFileItemModelRolesUpdater::updateChangedItems()
905 {
906 if (m_state == Paused) {
907 return;
908 }
909
910 if (m_changedItems.isEmpty()) {
911 return;
912 }
913
914 m_finishedItems -= m_changedItems;
915
916 if (m_resolvableRoles.contains(m_model->sortRole())) {
917 m_pendingSortRoleItems += m_changedItems;
918
919 if (m_state != ResolvingSortRole) {
920 // Stop the preview job if necessary, and trigger the
921 // asynchronous determination of the sort role.
922 killPreviewJob();
923 m_state = ResolvingSortRole;
924 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
925 }
926
927 return;
928 }
929
930 QList<int> visibleChangedIndexes;
931 QList<int> invisibleChangedIndexes;
932
933 foreach (const KFileItem& item, m_changedItems) {
934 const int index = m_model->index(item);
935
936 if (index < 0) {
937 m_changedItems.remove(item);
938 continue;
939 }
940
941 if (index >= m_firstVisibleIndex && index <= m_lastVisibleIndex) {
942 visibleChangedIndexes.append(index);
943 } else {
944 invisibleChangedIndexes.append(index);
945 }
946 }
947
948 std::sort(visibleChangedIndexes.begin(), visibleChangedIndexes.end());
949
950 if (m_previewShown) {
951 foreach (int index, visibleChangedIndexes) {
952 m_pendingPreviewItems.append(m_model->fileItem(index));
953 }
954
955 foreach (int index, invisibleChangedIndexes) {
956 m_pendingPreviewItems.append(m_model->fileItem(index));
957 }
958
959 if (!m_previewJob) {
960 startPreviewJob();
961 }
962 } else {
963 const bool resolvingInProgress = !m_pendingIndexes.isEmpty();
964 m_pendingIndexes = visibleChangedIndexes + m_pendingIndexes + invisibleChangedIndexes;
965 if (!resolvingInProgress) {
966 // Trigger the asynchronous resolving of the changed roles.
967 m_state = ResolvingAllRoles;
968 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
969 }
970 }
971 }
972
973 void KFileItemModelRolesUpdater::applySortRole(int index)
974 {
975 QHash<QByteArray, QVariant> data;
976 const KFileItem item = m_model->fileItem(index);
977
978 if (m_model->sortRole() == "type") {
979 if (!item.isMimeTypeKnown()) {
980 item.determineMimeType();
981 }
982
983 data.insert("type", item.mimeComment());
984 } else if (m_model->sortRole() == "size" && item.isLocalFile() && item.isDir()) {
985 const QString path = item.localPath();
986 data.insert("size", m_directoryContentsCounter->countDirectoryContentsSynchronously(path));
987 } else {
988 // Probably the sort role is a baloo role - just determine all roles.
989 data = rolesData(item);
990 }
991
992 disconnect(m_model, &KFileItemModel::itemsChanged,
993 this, &KFileItemModelRolesUpdater::slotItemsChanged);
994 m_model->setData(index, data);
995 connect(m_model, &KFileItemModel::itemsChanged,
996 this, &KFileItemModelRolesUpdater::slotItemsChanged);
997 }
998
999 void KFileItemModelRolesUpdater::applySortProgressToModel()
1000 {
1001 // Inform the model about the progress of the resolved items,
1002 // so that it can give an indication when the sorting has been finished.
1003 const int resolvedCount = m_model->count() - m_pendingSortRoleItems.count();
1004 m_model->emitSortProgress(resolvedCount);
1005 }
1006
1007 bool KFileItemModelRolesUpdater::applyResolvedRoles(int index, ResolveHint hint)
1008 {
1009 const KFileItem item = m_model->fileItem(index);
1010 const bool resolveAll = (hint == ResolveAll);
1011
1012 bool iconChanged = false;
1013 if (!item.isMimeTypeKnown() || !item.isFinalIconKnown()) {
1014 item.determineMimeType();
1015 iconChanged = true;
1016 } else if (!m_model->data(index).contains("iconName")) {
1017 iconChanged = true;
1018 }
1019
1020 if (iconChanged || resolveAll || m_clearPreviews) {
1021 if (index < 0) {
1022 return false;
1023 }
1024
1025 QHash<QByteArray, QVariant> data;
1026 if (resolveAll) {
1027 data = rolesData(item);
1028 }
1029
1030 data.insert("iconName", item.iconName());
1031
1032 if (m_clearPreviews) {
1033 data.insert("iconPixmap", QPixmap());
1034 }
1035
1036 disconnect(m_model, &KFileItemModel::itemsChanged,
1037 this, &KFileItemModelRolesUpdater::slotItemsChanged);
1038 m_model->setData(index, data);
1039 connect(m_model, &KFileItemModel::itemsChanged,
1040 this, &KFileItemModelRolesUpdater::slotItemsChanged);
1041 return true;
1042 }
1043
1044 return false;
1045 }
1046
1047 QHash<QByteArray, QVariant> KFileItemModelRolesUpdater::rolesData(const KFileItem& item)
1048 {
1049 QHash<QByteArray, QVariant> data;
1050
1051 const bool getSizeRole = m_roles.contains("size");
1052 const bool getIsExpandableRole = m_roles.contains("isExpandable");
1053
1054 if ((getSizeRole || getIsExpandableRole) && item.isDir()) {
1055 if (item.isLocalFile()) {
1056 // Tell m_directoryContentsCounter that we want to count the items
1057 // inside the directory. The result will be received in slotDirectoryContentsCountReceived.
1058 const QString path = item.localPath();
1059 m_directoryContentsCounter->addDirectory(path);
1060 } else if (getSizeRole) {
1061 data.insert("size", -1); // -1 indicates an unknown number of items
1062 }
1063 }
1064
1065 if (m_roles.contains("type")) {
1066 data.insert("type", item.mimeComment());
1067 }
1068
1069 data.insert("iconOverlays", item.overlays());
1070
1071 #ifdef HAVE_BALOO
1072 if (m_balooFileMonitor) {
1073 m_balooFileMonitor->addFile(item.localPath());
1074 applyChangedBalooRoles(item.localPath());
1075 }
1076 #endif
1077 return data;
1078 }
1079
1080 void KFileItemModelRolesUpdater::updateAllPreviews()
1081 {
1082 if (m_state == Paused) {
1083 m_previewChangedDuringPausing = true;
1084 } else {
1085 m_finishedItems.clear();
1086 startUpdating();
1087 }
1088 }
1089
1090 void KFileItemModelRolesUpdater::killPreviewJob()
1091 {
1092 if (m_previewJob) {
1093 disconnect(m_previewJob, &KIO::PreviewJob::gotPreview,
1094 this, &KFileItemModelRolesUpdater::slotGotPreview);
1095 disconnect(m_previewJob, &KIO::PreviewJob::failed,
1096 this, &KFileItemModelRolesUpdater::slotPreviewFailed);
1097 disconnect(m_previewJob, &KIO::PreviewJob::finished,
1098 this, &KFileItemModelRolesUpdater::slotPreviewJobFinished);
1099 m_previewJob->kill();
1100 m_previewJob = 0;
1101 m_pendingPreviewItems.clear();
1102 }
1103 }
1104
1105 QList<int> KFileItemModelRolesUpdater::indexesToResolve() const
1106 {
1107 const int count = m_model->count();
1108
1109 QList<int> result;
1110 result.reserve(ResolveAllItemsLimit);
1111
1112 // Add visible items.
1113 for (int i = m_firstVisibleIndex; i <= m_lastVisibleIndex; ++i) {
1114 result.append(i);
1115 }
1116
1117 // We need a reasonable upper limit for number of items to resolve after
1118 // and before the visible range. m_maximumVisibleItems can be quite large
1119 // when using Compace View.
1120 const int readAheadItems = qMin(ReadAheadPages * m_maximumVisibleItems, ResolveAllItemsLimit / 2);
1121
1122 // Add items after the visible range.
1123 const int endExtendedVisibleRange = qMin(m_lastVisibleIndex + readAheadItems, count - 1);
1124 for (int i = m_lastVisibleIndex + 1; i <= endExtendedVisibleRange; ++i) {
1125 result.append(i);
1126 }
1127
1128 // Add items before the visible range in reverse order.
1129 const int beginExtendedVisibleRange = qMax(0, m_firstVisibleIndex - readAheadItems);
1130 for (int i = m_firstVisibleIndex - 1; i >= beginExtendedVisibleRange; --i) {
1131 result.append(i);
1132 }
1133
1134 // Add items on the last page.
1135 const int beginLastPage = qMax(qMin(endExtendedVisibleRange + 1, count - 1), count - m_maximumVisibleItems);
1136 for (int i = beginLastPage; i < count; ++i) {
1137 result.append(i);
1138 }
1139
1140 // Add items on the first page.
1141 const int endFirstPage = qMin(qMax(beginExtendedVisibleRange - 1, 0), m_maximumVisibleItems);
1142 for (int i = 0; i <= endFirstPage; ++i) {
1143 result.append(i);
1144 }
1145
1146 // Continue adding items until ResolveAllItemsLimit is reached.
1147 int remainingItems = ResolveAllItemsLimit - result.count();
1148
1149 for (int i = endExtendedVisibleRange + 1; i < beginLastPage && remainingItems > 0; ++i) {
1150 result.append(i);
1151 --remainingItems;
1152 }
1153
1154 for (int i = beginExtendedVisibleRange - 1; i > endFirstPage && remainingItems > 0; --i) {
1155 result.append(i);
1156 --remainingItems;
1157 }
1158
1159 return result;
1160 }
1161