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 #include "kfileitemmodelrolesupdater.h"
22 #include "kfileitemmodel.h"
25 #include <KConfigGroup>
29 #include <KIconLoader>
30 #include <KJobWidgets>
31 #include <KIO/JobUiDelegate>
32 #include <KIO/PreviewJob>
34 #include "private/kpixmapmodifier.h"
35 #include "private/kdirectorycontentscounter.h"
37 #include <QApplication>
40 #include <QElapsedTimer>
46 #include "private/kbaloorolesprovider.h"
48 #include <Baloo/FileMonitor>
51 // #define KFILEITEMMODELROLESUPDATER_DEBUG
54 // Maximum time in ms that the KFileItemModelRolesUpdater
55 // may perform a blocking operation
56 const int MaxBlockTimeout
= 200;
58 // If the number of items is smaller than ResolveAllItemsLimit,
59 // the roles of all items will be resolved.
60 const int ResolveAllItemsLimit
= 500;
62 // Not only the visible area, but up to ReadAheadPages before and after
63 // this area will be resolved.
64 const int ReadAheadPages
= 5;
67 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
) :
70 m_previewChangedDuringPausing(false),
71 m_iconSizeChangedDuringPausing(false),
72 m_rolesChangedDuringPausing(false),
73 m_previewShown(false),
74 m_enlargeSmallPreviews(true),
75 m_clearPreviews(false),
79 m_firstVisibleIndex(0),
80 m_lastVisibleIndex(-1),
81 m_maximumVisibleItems(50),
85 m_pendingSortRoleItems(),
87 m_pendingPreviewItems(),
89 m_recentlyChangedItemsTimer(0),
90 m_recentlyChangedItems(),
92 m_directoryContentsCounter(0)
94 , m_balooFileMonitor(0)
99 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
100 m_enabledPlugins
= globalConfig
.readEntry("Plugins", QStringList()
101 << "directorythumbnail"
105 connect(m_model
, &KFileItemModel::itemsInserted
,
106 this, &KFileItemModelRolesUpdater::slotItemsInserted
);
107 connect(m_model
, &KFileItemModel::itemsRemoved
,
108 this, &KFileItemModelRolesUpdater::slotItemsRemoved
);
109 connect(m_model
, &KFileItemModel::itemsChanged
,
110 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
111 connect(m_model
, &KFileItemModel::itemsMoved
,
112 this, &KFileItemModelRolesUpdater::slotItemsMoved
);
113 connect(m_model
, &KFileItemModel::sortRoleChanged
,
114 this, &KFileItemModelRolesUpdater::slotSortRoleChanged
);
116 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
117 // resolving of the roles. Postpone the resolving until no update has been done for 1 second.
118 m_recentlyChangedItemsTimer
= new QTimer(this);
119 m_recentlyChangedItemsTimer
->setInterval(1000);
120 m_recentlyChangedItemsTimer
->setSingleShot(true);
121 connect(m_recentlyChangedItemsTimer
, &QTimer::timeout
, this, &KFileItemModelRolesUpdater::resolveRecentlyChangedItems
);
123 m_resolvableRoles
.insert("size");
124 m_resolvableRoles
.insert("type");
125 m_resolvableRoles
.insert("isExpandable");
127 m_resolvableRoles
+= KBalooRolesProvider::instance().roles();
130 m_directoryContentsCounter
= new KDirectoryContentsCounter(m_model
, this);
131 connect(m_directoryContentsCounter
, &KDirectoryContentsCounter::result
,
132 this, &KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived
);
135 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
140 void KFileItemModelRolesUpdater::setIconSize(const QSize
& size
)
142 if (size
!= m_iconSize
) {
144 if (m_state
== Paused
) {
145 m_iconSizeChangedDuringPausing
= true;
146 } else if (m_previewShown
) {
147 // An icon size change requires the regenerating of
149 m_finishedItems
.clear();
155 QSize
KFileItemModelRolesUpdater::iconSize() const
160 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index
, int count
)
169 if (index
== m_firstVisibleIndex
&& count
== m_lastVisibleIndex
- m_firstVisibleIndex
+ 1) {
170 // The range has not been changed
174 m_firstVisibleIndex
= index
;
175 m_lastVisibleIndex
= qMin(index
+ count
- 1, m_model
->count() - 1);
180 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count
)
182 m_maximumVisibleItems
= count
;
185 void KFileItemModelRolesUpdater::setPreviewsShown(bool show
)
187 if (show
== m_previewShown
) {
191 m_previewShown
= show
;
193 m_clearPreviews
= true;
199 bool KFileItemModelRolesUpdater::previewsShown() const
201 return m_previewShown
;
204 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge
)
206 if (enlarge
!= m_enlargeSmallPreviews
) {
207 m_enlargeSmallPreviews
= enlarge
;
208 if (m_previewShown
) {
214 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
216 return m_enlargeSmallPreviews
;
219 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList
& list
)
221 if (m_enabledPlugins
!= list
) {
222 m_enabledPlugins
= list
;
223 if (m_previewShown
) {
229 void KFileItemModelRolesUpdater::setPaused(bool paused
)
231 if (paused
== (m_state
== Paused
)) {
239 const bool updatePreviews
= (m_iconSizeChangedDuringPausing
&& m_previewShown
) ||
240 m_previewChangedDuringPausing
;
241 const bool resolveAll
= updatePreviews
|| m_rolesChangedDuringPausing
;
243 m_finishedItems
.clear();
246 m_iconSizeChangedDuringPausing
= false;
247 m_previewChangedDuringPausing
= false;
248 m_rolesChangedDuringPausing
= false;
250 if (!m_pendingSortRoleItems
.isEmpty()) {
251 m_state
= ResolvingSortRole
;
252 resolveNextSortRole();
261 void KFileItemModelRolesUpdater::setRoles(const QSet
<QByteArray
>& roles
)
263 if (m_roles
!= roles
) {
267 // Check whether there is at least one role that must be resolved
268 // with the help of Baloo. If this is the case, a (quite expensive)
269 // resolving will be done in KFileItemModelRolesUpdater::rolesData() and
270 // the role gets watched for changes.
271 const KBalooRolesProvider
& rolesProvider
= KBalooRolesProvider::instance();
272 bool hasBalooRole
= false;
273 QSetIterator
<QByteArray
> it(roles
);
274 while (it
.hasNext()) {
275 const QByteArray
& role
= it
.next();
276 if (rolesProvider
.roles().contains(role
)) {
282 if (hasBalooRole
&& !m_balooFileMonitor
) {
283 m_balooFileMonitor
= new Baloo::FileMonitor(this);
284 connect(m_balooFileMonitor
, &Baloo::FileMonitor::fileMetaDataChanged
,
285 this, &KFileItemModelRolesUpdater::applyChangedBalooRoles
);
286 } else if (!hasBalooRole
&& m_balooFileMonitor
) {
287 delete m_balooFileMonitor
;
288 m_balooFileMonitor
= 0;
292 if (m_state
== Paused
) {
293 m_rolesChangedDuringPausing
= true;
300 QSet
<QByteArray
> KFileItemModelRolesUpdater::roles() const
305 bool KFileItemModelRolesUpdater::isPaused() const
307 return m_state
== Paused
;
310 QStringList
KFileItemModelRolesUpdater::enabledPlugins() const
312 return m_enabledPlugins
;
315 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList
& itemRanges
)
320 // Determine the sort role synchronously for as many items as possible.
321 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
322 int insertedCount
= 0;
323 foreach (const KItemRange
& range
, itemRanges
) {
324 const int lastIndex
= insertedCount
+ range
.index
+ range
.count
- 1;
325 for (int i
= insertedCount
+ range
.index
; i
<= lastIndex
; ++i
) {
326 if (timer
.elapsed() < MaxBlockTimeout
) {
329 m_pendingSortRoleItems
.insert(m_model
->fileItem(i
));
332 insertedCount
+= range
.count
;
335 applySortProgressToModel();
337 // If there are still items whose sort role is unknown, check if the
338 // asynchronous determination of the sort role is already in progress,
339 // and start it if that is not the case.
340 if (!m_pendingSortRoleItems
.isEmpty() && m_state
!= ResolvingSortRole
) {
342 m_state
= ResolvingSortRole
;
343 resolveNextSortRole();
350 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList
& itemRanges
)
352 Q_UNUSED(itemRanges
);
354 const bool allItemsRemoved
= (m_model
->count() == 0);
357 if (m_balooFileMonitor
) {
358 // Don't let the FileWatcher watch for removed items
359 if (allItemsRemoved
) {
360 m_balooFileMonitor
->clear();
362 QStringList newFileList
;
363 foreach (const QString
& itemUrl
, m_balooFileMonitor
->files()) {
364 if (m_model
->index(itemUrl
) >= 0) {
365 newFileList
.append(itemUrl
);
368 m_balooFileMonitor
->setFiles(newFileList
);
373 if (allItemsRemoved
) {
376 m_finishedItems
.clear();
377 m_pendingSortRoleItems
.clear();
378 m_pendingIndexes
.clear();
379 m_pendingPreviewItems
.clear();
380 m_recentlyChangedItems
.clear();
381 m_recentlyChangedItemsTimer
->stop();
382 m_changedItems
.clear();
386 // Only remove the items from m_finishedItems. They will be removed
387 // from the other sets later on.
388 QSet
<KFileItem
>::iterator it
= m_finishedItems
.begin();
389 while (it
!= m_finishedItems
.end()) {
390 if (m_model
->index(*it
) < 0) {
391 it
= m_finishedItems
.erase(it
);
397 // The visible items might have changed.
402 void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange
& itemRange
, QList
<int> movedToIndexes
)
405 Q_UNUSED(movedToIndexes
);
407 // The visible items might have changed.
411 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList
& itemRanges
,
412 const QSet
<QByteArray
>& roles
)
416 // Find out if slotItemsChanged() has been done recently. If that is the
417 // case, resolving the roles is postponed until a timer has exceeded
418 // to prevent expensive repeated updates if files are updated frequently.
419 const bool itemsChangedRecently
= m_recentlyChangedItemsTimer
->isActive();
421 QSet
<KFileItem
>& targetSet
= itemsChangedRecently
? m_recentlyChangedItems
: m_changedItems
;
423 foreach (const KItemRange
& itemRange
, itemRanges
) {
424 int index
= itemRange
.index
;
425 for (int count
= itemRange
.count
; count
> 0; --count
) {
426 const KFileItem item
= m_model
->fileItem(index
);
427 targetSet
.insert(item
);
432 m_recentlyChangedItemsTimer
->start();
434 if (!itemsChangedRecently
) {
435 updateChangedItems();
439 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray
& current
,
440 const QByteArray
& previous
)
445 if (m_resolvableRoles
.contains(current
)) {
446 m_pendingSortRoleItems
.clear();
447 m_finishedItems
.clear();
449 const int count
= m_model
->count();
453 // Determine the sort role synchronously for as many items as possible.
454 for (int index
= 0; index
< count
; ++index
) {
455 if (timer
.elapsed() < MaxBlockTimeout
) {
456 applySortRole(index
);
458 m_pendingSortRoleItems
.insert(m_model
->fileItem(index
));
462 applySortProgressToModel();
464 if (!m_pendingSortRoleItems
.isEmpty()) {
465 // Trigger the asynchronous determination of the sort role.
467 m_state
= ResolvingSortRole
;
468 resolveNextSortRole();
472 m_pendingSortRoleItems
.clear();
473 applySortProgressToModel();
477 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
479 if (m_state
!= PreviewJobRunning
) {
483 m_changedItems
.remove(item
);
485 const int index
= m_model
->index(item
);
490 QPixmap scaledPixmap
= pixmap
;
492 const QString mimeType
= item
.mimetype();
493 const int slashIndex
= mimeType
.indexOf(QLatin1Char('/'));
494 const QString mimeTypeGroup
= mimeType
.left(slashIndex
);
495 if (mimeTypeGroup
== QLatin1String("image")) {
496 if (m_enlargeSmallPreviews
) {
497 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
499 // Assure that small previews don't get enlarged. Instead they
500 // should be shown centered within the frame.
501 const QSize contentSize
= KPixmapModifier::sizeInsideFrame(m_iconSize
);
502 const bool enlargingRequired
= scaledPixmap
.width() < contentSize
.width() &&
503 scaledPixmap
.height() < contentSize
.height();
504 if (enlargingRequired
) {
505 QSize frameSize
= scaledPixmap
.size();
506 frameSize
.scale(m_iconSize
, Qt::KeepAspectRatio
);
508 QPixmap
largeFrame(frameSize
);
509 largeFrame
.fill(Qt::transparent
);
511 KPixmapModifier::applyFrame(largeFrame
, frameSize
);
513 QPainter
painter(&largeFrame
);
514 painter
.drawPixmap((largeFrame
.width() - scaledPixmap
.width()) / 2,
515 (largeFrame
.height() - scaledPixmap
.height()) / 2,
517 scaledPixmap
= largeFrame
;
519 // The image must be shrinked as it is too large to fit into
520 // the available icon size
521 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
525 KPixmapModifier::scale(scaledPixmap
, m_iconSize
);
528 QHash
<QByteArray
, QVariant
> data
= rolesData(item
);
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
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
);
545 data
.insert("iconPixmap", scaledPixmap
);
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
);
553 m_finishedItems
.insert(item
);
556 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem
& item
)
558 if (m_state
!= PreviewJobRunning
) {
562 m_changedItems
.remove(item
);
564 const int index
= m_model
->index(item
);
566 QHash
<QByteArray
, QVariant
> data
;
567 data
.insert("iconPixmap", QPixmap());
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
);
575 applyResolvedRoles(index
, ResolveAll
);
576 m_finishedItems
.insert(item
);
580 void KFileItemModelRolesUpdater::slotPreviewJobFinished()
584 if (m_state
!= PreviewJobRunning
) {
590 if (!m_pendingPreviewItems
.isEmpty()) {
593 if (!m_changedItems
.isEmpty()) {
594 updateChangedItems();
599 void KFileItemModelRolesUpdater::resolveNextSortRole()
601 if (m_state
!= ResolvingSortRole
) {
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
);
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
);
617 applySortRole(index
);
618 m_pendingSortRoleItems
.erase(it
);
622 if (!m_pendingSortRoleItems
.isEmpty()) {
623 applySortProgressToModel();
624 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
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
);
638 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
640 if (m_state
!= ResolvingAllRoles
) {
644 while (!m_pendingIndexes
.isEmpty()) {
645 const int index
= m_pendingIndexes
.takeFirst();
646 const KFileItem item
= m_model
->fileItem(index
);
648 if (m_finishedItems
.contains(item
)) {
652 applyResolvedRoles(index
, ResolveAll
);
653 m_finishedItems
.insert(item
);
654 m_changedItems
.remove(item
);
658 if (!m_pendingIndexes
.isEmpty()) {
659 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
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());
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
);
676 connect(m_model
, &KFileItemModel::itemsChanged
,
677 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
680 m_clearPreviews
= false;
683 if (!m_changedItems
.isEmpty()) {
684 updateChangedItems();
689 void KFileItemModelRolesUpdater::resolveRecentlyChangedItems()
691 m_changedItems
+= m_recentlyChangedItems
;
692 m_recentlyChangedItems
.clear();
693 updateChangedItems();
696 void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString
& itemUrl
)
699 const KFileItem item
= m_model
->fileItem(itemUrl
);
702 // itemUrl is not in the model anymore, probably because
703 // the corresponding file has been deleted in the meantime.
707 Baloo::File
file(item
.localPath());
710 const KBalooRolesProvider
& rolesProvider
= KBalooRolesProvider::instance();
711 QHash
<QByteArray
, QVariant
> data
;
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.
717 data
.insert(role
, QVariant());
720 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(file
, m_roles
));
721 while (it
.hasNext()) {
723 data
.insert(it
.key(), it
.value());
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
);
739 void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString
& path
, int count
)
741 const bool getSizeRole
= m_roles
.contains("size");
742 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
744 if (getSizeRole
|| getIsExpandableRole
) {
745 const int index
= m_model
->index(KUrl(path
));
747 QHash
<QByteArray
, QVariant
> data
;
750 data
.insert("size", count
);
752 if (getIsExpandableRole
) {
753 data
.insert("isExpandable", count
> 0);
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
);
765 void KFileItemModelRolesUpdater::startUpdating()
767 if (m_state
== Paused
) {
771 if (m_finishedItems
.count() == m_model
->count()) {
772 // All roles have been resolved already.
777 // Terminate all updates that are currently active.
779 m_pendingIndexes
.clear();
784 // Determine the icons for the visible items synchronously.
785 updateVisibleIcons();
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
) {
793 // Start the preview job or the asynchronous resolving of all roles.
794 QList
<int> indexes
= indexesToResolve();
796 if (m_previewShown
) {
797 m_pendingPreviewItems
.clear();
798 m_pendingPreviewItems
.reserve(indexes
.count());
800 foreach (int index
, indexes
) {
801 const KFileItem item
= m_model
->fileItem(index
);
802 if (!m_finishedItems
.contains(item
)) {
803 m_pendingPreviewItems
.append(item
);
809 m_pendingIndexes
= indexes
;
810 // Trigger the asynchronous resolving of all roles.
811 m_state
= ResolvingAllRoles
;
812 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
816 void KFileItemModelRolesUpdater::updateVisibleIcons()
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);
831 // Try to determine the final icons for all visible items.
833 for (index
= m_firstVisibleIndex
; index
<= lastVisibleIndex
&& timer
.elapsed() < MaxBlockTimeout
; ++index
) {
834 applyResolvedRoles(index
, ResolveFast
);
837 // KFileItemListView::initializeItemListWidget(KItemListWidget*) will load
838 // preliminary icons (i.e., without mime type determination) for the
842 void KFileItemModelRolesUpdater::startPreviewJob()
844 m_state
= PreviewJobRunning
;
846 if (m_pendingPreviewItems
.isEmpty()) {
847 QTimer::singleShot(0, this, SLOT(slotPreviewJobFinished()));
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);
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
);
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.
872 itemSubSet
.append(m_pendingPreviewItems
.takeFirst());
873 } while (!m_pendingPreviewItems
.isEmpty() && m_pendingPreviewItems
.first().isMimeTypeKnown());
875 // Determine mime types for MaxBlockTimeout ms, and start a preview
876 // job for the corresponding items.
881 const KFileItem item
= m_pendingPreviewItems
.takeFirst();
882 item
.determineMimeType();
883 itemSubSet
.append(item
);
884 } while (!m_pendingPreviewItems
.isEmpty() && timer
.elapsed() < MaxBlockTimeout
);
887 KIO::PreviewJob
* job
= new KIO::PreviewJob(itemSubSet
, cacheSize
, &m_enabledPlugins
);
889 job
->setIgnoreMaximumSize(itemSubSet
.first().isLocalFile());
891 KJobWidgets::setWindow(job
, qApp
->activeWindow());
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
);
904 void KFileItemModelRolesUpdater::updateChangedItems()
906 if (m_state
== Paused
) {
910 if (m_changedItems
.isEmpty()) {
914 m_finishedItems
-= m_changedItems
;
916 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
917 m_pendingSortRoleItems
+= m_changedItems
;
919 if (m_state
!= ResolvingSortRole
) {
920 // Stop the preview job if necessary, and trigger the
921 // asynchronous determination of the sort role.
923 m_state
= ResolvingSortRole
;
924 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
930 QList
<int> visibleChangedIndexes
;
931 QList
<int> invisibleChangedIndexes
;
933 foreach (const KFileItem
& item
, m_changedItems
) {
934 const int index
= m_model
->index(item
);
937 m_changedItems
.remove(item
);
941 if (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
) {
942 visibleChangedIndexes
.append(index
);
944 invisibleChangedIndexes
.append(index
);
948 std::sort(visibleChangedIndexes
.begin(), visibleChangedIndexes
.end());
950 if (m_previewShown
) {
951 foreach (int index
, visibleChangedIndexes
) {
952 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
955 foreach (int index
, invisibleChangedIndexes
) {
956 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
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()));
973 void KFileItemModelRolesUpdater::applySortRole(int index
)
975 QHash
<QByteArray
, QVariant
> data
;
976 const KFileItem item
= m_model
->fileItem(index
);
978 if (m_model
->sortRole() == "type") {
979 if (!item
.isMimeTypeKnown()) {
980 item
.determineMimeType();
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
));
988 // Probably the sort role is a baloo role - just determine all roles.
989 data
= rolesData(item
);
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
);
999 void KFileItemModelRolesUpdater::applySortProgressToModel()
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
);
1007 bool KFileItemModelRolesUpdater::applyResolvedRoles(int index
, ResolveHint hint
)
1009 const KFileItem item
= m_model
->fileItem(index
);
1010 const bool resolveAll
= (hint
== ResolveAll
);
1012 bool iconChanged
= false;
1013 if (!item
.isMimeTypeKnown() || !item
.isFinalIconKnown()) {
1014 item
.determineMimeType();
1016 } else if (!m_model
->data(index
).contains("iconName")) {
1020 if (iconChanged
|| resolveAll
|| m_clearPreviews
) {
1025 QHash
<QByteArray
, QVariant
> data
;
1027 data
= rolesData(item
);
1030 data
.insert("iconName", item
.iconName());
1032 if (m_clearPreviews
) {
1033 data
.insert("iconPixmap", QPixmap());
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
);
1047 QHash
<QByteArray
, QVariant
> KFileItemModelRolesUpdater::rolesData(const KFileItem
& item
)
1049 QHash
<QByteArray
, QVariant
> data
;
1051 const bool getSizeRole
= m_roles
.contains("size");
1052 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
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
1065 if (m_roles
.contains("type")) {
1066 data
.insert("type", item
.mimeComment());
1069 data
.insert("iconOverlays", item
.overlays());
1072 if (m_balooFileMonitor
) {
1073 m_balooFileMonitor
->addFile(item
.localPath());
1074 applyChangedBalooRoles(item
.localPath());
1080 void KFileItemModelRolesUpdater::updateAllPreviews()
1082 if (m_state
== Paused
) {
1083 m_previewChangedDuringPausing
= true;
1085 m_finishedItems
.clear();
1090 void KFileItemModelRolesUpdater::killPreviewJob()
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();
1101 m_pendingPreviewItems
.clear();
1105 QList
<int> KFileItemModelRolesUpdater::indexesToResolve() const
1107 const int count
= m_model
->count();
1110 result
.reserve(ResolveAllItemsLimit
);
1112 // Add visible items.
1113 for (int i
= m_firstVisibleIndex
; i
<= m_lastVisibleIndex
; ++i
) {
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);
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
) {
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
) {
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
) {
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
) {
1146 // Continue adding items until ResolveAllItemsLimit is reached.
1147 int remainingItems
= ResolveAllItemsLimit
- result
.count();
1149 for (int i
= endExtendedVisibleRange
+ 1; i
< beginLastPage
&& remainingItems
> 0; ++i
) {
1154 for (int i
= beginExtendedVisibleRange
- 1; i
> endFirstPage
&& remainingItems
> 0; --i
) {