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>
26 #include <KSharedConfig>
28 #include <KIconLoader>
29 #include <KJobWidgets>
30 #include <KIO/JobUiDelegate>
31 #include <KIO/PreviewJob>
32 #include <KPluginLoader>
33 #include <KOverlayIconPlugin>
35 #include "private/kpixmapmodifier.h"
36 #include "private/kdirectorycontentscounter.h"
38 #include <QApplication>
41 #include <QElapsedTimer>
47 #include "private/kbaloorolesprovider.h"
49 #include <Baloo/FileMonitor>
53 // #define KFILEITEMMODELROLESUPDATER_DEBUG
56 // Maximum time in ms that the KFileItemModelRolesUpdater
57 // may perform a blocking operation
58 const int MaxBlockTimeout
= 200;
60 // If the number of items is smaller than ResolveAllItemsLimit,
61 // the roles of all items will be resolved.
62 const int ResolveAllItemsLimit
= 500;
64 // Not only the visible area, but up to ReadAheadPages before and after
65 // this area will be resolved.
66 const int ReadAheadPages
= 5;
69 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
) :
72 m_previewChangedDuringPausing(false),
73 m_iconSizeChangedDuringPausing(false),
74 m_rolesChangedDuringPausing(false),
75 m_previewShown(false),
76 m_enlargeSmallPreviews(true),
77 m_clearPreviews(false),
81 m_firstVisibleIndex(0),
82 m_lastVisibleIndex(-1),
83 m_maximumVisibleItems(50),
87 m_pendingSortRoleItems(),
89 m_pendingPreviewItems(),
91 m_recentlyChangedItemsTimer(0),
92 m_recentlyChangedItems(),
94 m_directoryContentsCounter(0)
96 , m_balooFileMonitor(0)
101 const KConfigGroup
globalConfig(KSharedConfig::openConfig(), "PreviewSettings");
102 m_enabledPlugins
= globalConfig
.readEntry("Plugins", QStringList()
103 << "directorythumbnail"
107 connect(m_model
, &KFileItemModel::itemsInserted
,
108 this, &KFileItemModelRolesUpdater::slotItemsInserted
);
109 connect(m_model
, &KFileItemModel::itemsRemoved
,
110 this, &KFileItemModelRolesUpdater::slotItemsRemoved
);
111 connect(m_model
, &KFileItemModel::itemsChanged
,
112 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
113 connect(m_model
, &KFileItemModel::itemsMoved
,
114 this, &KFileItemModelRolesUpdater::slotItemsMoved
);
115 connect(m_model
, &KFileItemModel::sortRoleChanged
,
116 this, &KFileItemModelRolesUpdater::slotSortRoleChanged
);
118 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
119 // resolving of the roles. Postpone the resolving until no update has been done for 1 second.
120 m_recentlyChangedItemsTimer
= new QTimer(this);
121 m_recentlyChangedItemsTimer
->setInterval(1000);
122 m_recentlyChangedItemsTimer
->setSingleShot(true);
123 connect(m_recentlyChangedItemsTimer
, &QTimer::timeout
, this, &KFileItemModelRolesUpdater::resolveRecentlyChangedItems
);
125 m_resolvableRoles
.insert("size");
126 m_resolvableRoles
.insert("type");
127 m_resolvableRoles
.insert("isExpandable");
129 m_resolvableRoles
+= KBalooRolesProvider::instance().roles();
132 m_directoryContentsCounter
= new KDirectoryContentsCounter(m_model
, this);
133 connect(m_directoryContentsCounter
, &KDirectoryContentsCounter::result
,
134 this, &KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived
);
136 auto plugins
= KPluginLoader::instantiatePlugins(QStringLiteral("kf5/overlayicon"), nullptr, this);
137 foreach (QObject
*it
, plugins
) {
138 auto plugin
= qobject_cast
<KOverlayIconPlugin
*>(it
);
140 m_overlayIconsPlugin
.append(plugin
);
141 connect(plugin
, &KOverlayIconPlugin::overlaysChanged
, this, &KFileItemModelRolesUpdater::slotOverlaysChanged
);
143 // not our/valid plugin, so delete the created object
149 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
154 void KFileItemModelRolesUpdater::setIconSize(const QSize
& size
)
156 if (size
!= m_iconSize
) {
158 if (m_state
== Paused
) {
159 m_iconSizeChangedDuringPausing
= true;
160 } else if (m_previewShown
) {
161 // An icon size change requires the regenerating of
163 m_finishedItems
.clear();
169 QSize
KFileItemModelRolesUpdater::iconSize() const
174 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index
, int count
)
183 if (index
== m_firstVisibleIndex
&& count
== m_lastVisibleIndex
- m_firstVisibleIndex
+ 1) {
184 // The range has not been changed
188 m_firstVisibleIndex
= index
;
189 m_lastVisibleIndex
= qMin(index
+ count
- 1, m_model
->count() - 1);
194 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count
)
196 m_maximumVisibleItems
= count
;
199 void KFileItemModelRolesUpdater::setPreviewsShown(bool show
)
201 if (show
== m_previewShown
) {
205 m_previewShown
= show
;
207 m_clearPreviews
= true;
213 bool KFileItemModelRolesUpdater::previewsShown() const
215 return m_previewShown
;
218 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge
)
220 if (enlarge
!= m_enlargeSmallPreviews
) {
221 m_enlargeSmallPreviews
= enlarge
;
222 if (m_previewShown
) {
228 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
230 return m_enlargeSmallPreviews
;
233 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList
& list
)
235 if (m_enabledPlugins
!= list
) {
236 m_enabledPlugins
= list
;
237 if (m_previewShown
) {
243 void KFileItemModelRolesUpdater::setPaused(bool paused
)
245 if (paused
== (m_state
== Paused
)) {
253 const bool updatePreviews
= (m_iconSizeChangedDuringPausing
&& m_previewShown
) ||
254 m_previewChangedDuringPausing
;
255 const bool resolveAll
= updatePreviews
|| m_rolesChangedDuringPausing
;
257 m_finishedItems
.clear();
260 m_iconSizeChangedDuringPausing
= false;
261 m_previewChangedDuringPausing
= false;
262 m_rolesChangedDuringPausing
= false;
264 if (!m_pendingSortRoleItems
.isEmpty()) {
265 m_state
= ResolvingSortRole
;
266 resolveNextSortRole();
275 void KFileItemModelRolesUpdater::setRoles(const QSet
<QByteArray
>& roles
)
277 if (m_roles
!= roles
) {
281 // Check whether there is at least one role that must be resolved
282 // with the help of Baloo. If this is the case, a (quite expensive)
283 // resolving will be done in KFileItemModelRolesUpdater::rolesData() and
284 // the role gets watched for changes.
285 const KBalooRolesProvider
& rolesProvider
= KBalooRolesProvider::instance();
286 bool hasBalooRole
= false;
287 QSetIterator
<QByteArray
> it(roles
);
288 while (it
.hasNext()) {
289 const QByteArray
& role
= it
.next();
290 if (rolesProvider
.roles().contains(role
)) {
296 if (hasBalooRole
&& m_balooConfig
.fileIndexingEnabled() && !m_balooFileMonitor
) {
297 m_balooFileMonitor
= new Baloo::FileMonitor(this);
298 connect(m_balooFileMonitor
, &Baloo::FileMonitor::fileMetaDataChanged
,
299 this, &KFileItemModelRolesUpdater::applyChangedBalooRoles
);
300 } else if (!hasBalooRole
&& m_balooFileMonitor
) {
301 delete m_balooFileMonitor
;
302 m_balooFileMonitor
= 0;
306 if (m_state
== Paused
) {
307 m_rolesChangedDuringPausing
= true;
314 QSet
<QByteArray
> KFileItemModelRolesUpdater::roles() const
319 bool KFileItemModelRolesUpdater::isPaused() const
321 return m_state
== Paused
;
324 QStringList
KFileItemModelRolesUpdater::enabledPlugins() const
326 return m_enabledPlugins
;
329 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList
& itemRanges
)
334 // Determine the sort role synchronously for as many items as possible.
335 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
336 int insertedCount
= 0;
337 foreach (const KItemRange
& range
, itemRanges
) {
338 const int lastIndex
= insertedCount
+ range
.index
+ range
.count
- 1;
339 for (int i
= insertedCount
+ range
.index
; i
<= lastIndex
; ++i
) {
340 if (timer
.elapsed() < MaxBlockTimeout
) {
343 m_pendingSortRoleItems
.insert(m_model
->fileItem(i
));
346 insertedCount
+= range
.count
;
349 applySortProgressToModel();
351 // If there are still items whose sort role is unknown, check if the
352 // asynchronous determination of the sort role is already in progress,
353 // and start it if that is not the case.
354 if (!m_pendingSortRoleItems
.isEmpty() && m_state
!= ResolvingSortRole
) {
356 m_state
= ResolvingSortRole
;
357 resolveNextSortRole();
364 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList
& itemRanges
)
366 Q_UNUSED(itemRanges
);
368 const bool allItemsRemoved
= (m_model
->count() == 0);
371 if (m_balooFileMonitor
) {
372 // Don't let the FileWatcher watch for removed items
373 if (allItemsRemoved
) {
374 m_balooFileMonitor
->clear();
376 QStringList newFileList
;
377 foreach (const QString
& itemUrl
, m_balooFileMonitor
->files()) {
378 if (m_model
->index(itemUrl
) >= 0) {
379 newFileList
.append(itemUrl
);
382 m_balooFileMonitor
->setFiles(newFileList
);
387 if (allItemsRemoved
) {
390 m_finishedItems
.clear();
391 m_pendingSortRoleItems
.clear();
392 m_pendingIndexes
.clear();
393 m_pendingPreviewItems
.clear();
394 m_recentlyChangedItems
.clear();
395 m_recentlyChangedItemsTimer
->stop();
396 m_changedItems
.clear();
400 // Only remove the items from m_finishedItems. They will be removed
401 // from the other sets later on.
402 QSet
<KFileItem
>::iterator it
= m_finishedItems
.begin();
403 while (it
!= m_finishedItems
.end()) {
404 if (m_model
->index(*it
) < 0) {
405 it
= m_finishedItems
.erase(it
);
411 // The visible items might have changed.
416 void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange
& itemRange
, QList
<int> movedToIndexes
)
419 Q_UNUSED(movedToIndexes
);
421 // The visible items might have changed.
425 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList
& itemRanges
,
426 const QSet
<QByteArray
>& roles
)
430 // Find out if slotItemsChanged() has been done recently. If that is the
431 // case, resolving the roles is postponed until a timer has exceeded
432 // to prevent expensive repeated updates if files are updated frequently.
433 const bool itemsChangedRecently
= m_recentlyChangedItemsTimer
->isActive();
435 QSet
<KFileItem
>& targetSet
= itemsChangedRecently
? m_recentlyChangedItems
: m_changedItems
;
437 foreach (const KItemRange
& itemRange
, itemRanges
) {
438 int index
= itemRange
.index
;
439 for (int count
= itemRange
.count
; count
> 0; --count
) {
440 const KFileItem item
= m_model
->fileItem(index
);
441 targetSet
.insert(item
);
446 m_recentlyChangedItemsTimer
->start();
448 if (!itemsChangedRecently
) {
449 updateChangedItems();
453 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray
& current
,
454 const QByteArray
& previous
)
459 if (m_resolvableRoles
.contains(current
)) {
460 m_pendingSortRoleItems
.clear();
461 m_finishedItems
.clear();
463 const int count
= m_model
->count();
467 // Determine the sort role synchronously for as many items as possible.
468 for (int index
= 0; index
< count
; ++index
) {
469 if (timer
.elapsed() < MaxBlockTimeout
) {
470 applySortRole(index
);
472 m_pendingSortRoleItems
.insert(m_model
->fileItem(index
));
476 applySortProgressToModel();
478 if (!m_pendingSortRoleItems
.isEmpty()) {
479 // Trigger the asynchronous determination of the sort role.
481 m_state
= ResolvingSortRole
;
482 resolveNextSortRole();
486 m_pendingSortRoleItems
.clear();
487 applySortProgressToModel();
491 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
493 if (m_state
!= PreviewJobRunning
) {
497 m_changedItems
.remove(item
);
499 const int index
= m_model
->index(item
);
504 QPixmap scaledPixmap
= pixmap
;
506 const QString mimeType
= item
.mimetype();
507 const int slashIndex
= mimeType
.indexOf(QLatin1Char('/'));
508 const QString mimeTypeGroup
= mimeType
.left(slashIndex
);
509 if (mimeTypeGroup
== QLatin1String("image")) {
510 if (m_enlargeSmallPreviews
) {
511 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
513 // Assure that small previews don't get enlarged. Instead they
514 // should be shown centered within the frame.
515 const QSize contentSize
= KPixmapModifier::sizeInsideFrame(m_iconSize
);
516 const bool enlargingRequired
= scaledPixmap
.width() < contentSize
.width() &&
517 scaledPixmap
.height() < contentSize
.height();
518 if (enlargingRequired
) {
519 QSize frameSize
= scaledPixmap
.size() / scaledPixmap
.devicePixelRatio();
520 frameSize
.scale(m_iconSize
, Qt::KeepAspectRatio
);
522 QPixmap
largeFrame(frameSize
);
523 largeFrame
.fill(Qt::transparent
);
525 KPixmapModifier::applyFrame(largeFrame
, frameSize
);
527 QPainter
painter(&largeFrame
);
528 painter
.drawPixmap((largeFrame
.width() - scaledPixmap
.width() / scaledPixmap
.devicePixelRatio()) / 2,
529 (largeFrame
.height() - scaledPixmap
.height() / scaledPixmap
.devicePixelRatio()) / 2,
531 scaledPixmap
= largeFrame
;
533 // The image must be shrinked as it is too large to fit into
534 // the available icon size
535 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
539 KPixmapModifier::scale(scaledPixmap
, m_iconSize
);
542 QHash
<QByteArray
, QVariant
> data
= rolesData(item
);
544 const QStringList overlays
= data
["iconOverlays"].toStringList();
545 // Strangely KFileItem::overlays() returns empty string-values, so
546 // we need to check first whether an overlay must be drawn at all.
547 // It is more efficient to do it here, as KIconLoader::drawOverlays()
548 // assumes that an overlay will be drawn and has some additional
550 foreach (const QString
& overlay
, overlays
) {
551 if (!overlay
.isEmpty()) {
552 // There is at least one overlay, draw all overlays above m_pixmap
553 // and cancel the check
554 KIconLoader::global()->drawOverlays(overlays
, scaledPixmap
, KIconLoader::Desktop
);
559 data
.insert("iconPixmap", scaledPixmap
);
561 disconnect(m_model
, &KFileItemModel::itemsChanged
,
562 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
563 m_model
->setData(index
, data
);
564 connect(m_model
, &KFileItemModel::itemsChanged
,
565 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
567 m_finishedItems
.insert(item
);
570 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem
& item
)
572 if (m_state
!= PreviewJobRunning
) {
576 m_changedItems
.remove(item
);
578 const int index
= m_model
->index(item
);
580 QHash
<QByteArray
, QVariant
> data
;
581 data
.insert("iconPixmap", QPixmap());
583 disconnect(m_model
, &KFileItemModel::itemsChanged
,
584 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
585 m_model
->setData(index
, data
);
586 connect(m_model
, &KFileItemModel::itemsChanged
,
587 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
589 applyResolvedRoles(index
, ResolveAll
);
590 m_finishedItems
.insert(item
);
594 void KFileItemModelRolesUpdater::slotPreviewJobFinished()
598 if (m_state
!= PreviewJobRunning
) {
604 if (!m_pendingPreviewItems
.isEmpty()) {
607 if (!m_changedItems
.isEmpty()) {
608 updateChangedItems();
613 void KFileItemModelRolesUpdater::resolveNextSortRole()
615 if (m_state
!= ResolvingSortRole
) {
619 QSet
<KFileItem
>::iterator it
= m_pendingSortRoleItems
.begin();
620 while (it
!= m_pendingSortRoleItems
.end()) {
621 const KFileItem item
= *it
;
622 const int index
= m_model
->index(item
);
624 // Continue if the sort role has already been determined for the
625 // item, and the item has not been changed recently.
626 if (!m_changedItems
.contains(item
) && m_model
->data(index
).contains(m_model
->sortRole())) {
627 it
= m_pendingSortRoleItems
.erase(it
);
631 applySortRole(index
);
632 m_pendingSortRoleItems
.erase(it
);
636 if (!m_pendingSortRoleItems
.isEmpty()) {
637 applySortProgressToModel();
638 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
642 // Prevent that we try to update the items twice.
643 disconnect(m_model
, &KFileItemModel::itemsMoved
,
644 this, &KFileItemModelRolesUpdater::slotItemsMoved
);
645 applySortProgressToModel();
646 connect(m_model
, &KFileItemModel::itemsMoved
,
647 this, &KFileItemModelRolesUpdater::slotItemsMoved
);
652 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
654 if (m_state
!= ResolvingAllRoles
) {
658 while (!m_pendingIndexes
.isEmpty()) {
659 const int index
= m_pendingIndexes
.takeFirst();
660 const KFileItem item
= m_model
->fileItem(index
);
662 if (m_finishedItems
.contains(item
)) {
666 applyResolvedRoles(index
, ResolveAll
);
667 m_finishedItems
.insert(item
);
668 m_changedItems
.remove(item
);
672 if (!m_pendingIndexes
.isEmpty()) {
673 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
677 if (m_clearPreviews
) {
678 // Only go through the list if there are items which might still have previews.
679 if (m_finishedItems
.count() != m_model
->count()) {
680 QHash
<QByteArray
, QVariant
> data
;
681 data
.insert("iconPixmap", QPixmap());
683 disconnect(m_model
, &KFileItemModel::itemsChanged
,
684 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
685 for (int index
= 0; index
<= m_model
->count(); ++index
) {
686 if (m_model
->data(index
).contains("iconPixmap")) {
687 m_model
->setData(index
, data
);
690 connect(m_model
, &KFileItemModel::itemsChanged
,
691 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
694 m_clearPreviews
= false;
697 if (!m_changedItems
.isEmpty()) {
698 updateChangedItems();
703 void KFileItemModelRolesUpdater::resolveRecentlyChangedItems()
705 m_changedItems
+= m_recentlyChangedItems
;
706 m_recentlyChangedItems
.clear();
707 updateChangedItems();
710 void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString
& itemUrl
)
713 const KFileItem item
= m_model
->fileItem(itemUrl
);
716 // itemUrl is not in the model anymore, probably because
717 // the corresponding file has been deleted in the meantime.
721 Baloo::File
file(item
.localPath());
724 const KBalooRolesProvider
& rolesProvider
= KBalooRolesProvider::instance();
725 QHash
<QByteArray
, QVariant
> data
;
727 foreach (const QByteArray
& role
, rolesProvider
.roles()) {
728 // Overwrite all the role values with an empty QVariant, because the roles
729 // provider doesn't overwrite it when the property value list is empty.
731 data
.insert(role
, QVariant());
734 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(file
, m_roles
));
735 while (it
.hasNext()) {
737 data
.insert(it
.key(), it
.value());
740 disconnect(m_model
, &KFileItemModel::itemsChanged
,
741 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
742 const int index
= m_model
->index(item
);
743 m_model
->setData(index
, data
);
744 connect(m_model
, &KFileItemModel::itemsChanged
,
745 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
753 void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString
& path
, int count
)
755 const bool getSizeRole
= m_roles
.contains("size");
756 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
758 if (getSizeRole
|| getIsExpandableRole
) {
759 const int index
= m_model
->index(QUrl::fromLocalFile(path
));
761 QHash
<QByteArray
, QVariant
> data
;
764 data
.insert("size", count
);
766 if (getIsExpandableRole
) {
767 data
.insert("isExpandable", count
> 0);
770 disconnect(m_model
, &KFileItemModel::itemsChanged
,
771 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
772 m_model
->setData(index
, data
);
773 connect(m_model
, &KFileItemModel::itemsChanged
,
774 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
779 void KFileItemModelRolesUpdater::startUpdating()
781 if (m_state
== Paused
) {
785 if (m_finishedItems
.count() == m_model
->count()) {
786 // All roles have been resolved already.
791 // Terminate all updates that are currently active.
793 m_pendingIndexes
.clear();
798 // Determine the icons for the visible items synchronously.
799 updateVisibleIcons();
801 // A detailed update of the items in and near the visible area
802 // only makes sense if sorting is finished.
803 if (m_state
== ResolvingSortRole
) {
807 // Start the preview job or the asynchronous resolving of all roles.
808 QList
<int> indexes
= indexesToResolve();
810 if (m_previewShown
) {
811 m_pendingPreviewItems
.clear();
812 m_pendingPreviewItems
.reserve(indexes
.count());
814 foreach (int index
, indexes
) {
815 const KFileItem item
= m_model
->fileItem(index
);
816 if (!m_finishedItems
.contains(item
)) {
817 m_pendingPreviewItems
.append(item
);
823 m_pendingIndexes
= indexes
;
824 // Trigger the asynchronous resolving of all roles.
825 m_state
= ResolvingAllRoles
;
826 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
830 void KFileItemModelRolesUpdater::updateVisibleIcons()
832 int lastVisibleIndex
= m_lastVisibleIndex
;
833 if (lastVisibleIndex
<= 0) {
834 // Guess a reasonable value for the last visible index if the view
835 // has not told us about the real value yet.
836 lastVisibleIndex
= qMin(m_firstVisibleIndex
+ m_maximumVisibleItems
, m_model
->count() - 1);
837 if (lastVisibleIndex
<= 0) {
838 lastVisibleIndex
= qMin(200, m_model
->count() - 1);
845 // Try to determine the final icons for all visible items.
847 for (index
= m_firstVisibleIndex
; index
<= lastVisibleIndex
&& timer
.elapsed() < MaxBlockTimeout
; ++index
) {
848 applyResolvedRoles(index
, ResolveFast
);
851 // KFileItemListView::initializeItemListWidget(KItemListWidget*) will load
852 // preliminary icons (i.e., without mime type determination) for the
856 void KFileItemModelRolesUpdater::startPreviewJob()
858 m_state
= PreviewJobRunning
;
860 if (m_pendingPreviewItems
.isEmpty()) {
861 QTimer::singleShot(0, this, SLOT(slotPreviewJobFinished()));
865 // PreviewJob internally caches items always with the size of
866 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
867 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
868 // do a downscaling anyhow because of the frame, so in this case only the provided
869 // cache sizes are requested.
870 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128)
871 ? QSize(256, 256) : QSize(128, 128);
873 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
874 // worst case) might block the application for several seconds. To prevent such
875 // a blocking, we only pass items with known mime type to the preview job.
876 const int count
= m_pendingPreviewItems
.count();
877 KFileItemList itemSubSet
;
878 itemSubSet
.reserve(count
);
880 if (m_pendingPreviewItems
.first().isMimeTypeKnown()) {
881 // Some mime types are known already, probably because they were
882 // determined when loading the icons for the visible items. Start
883 // a preview job for all items at the beginning of the list which
884 // have a known mime type.
886 itemSubSet
.append(m_pendingPreviewItems
.takeFirst());
887 } while (!m_pendingPreviewItems
.isEmpty() && m_pendingPreviewItems
.first().isMimeTypeKnown());
889 // Determine mime types for MaxBlockTimeout ms, and start a preview
890 // job for the corresponding items.
895 const KFileItem item
= m_pendingPreviewItems
.takeFirst();
896 item
.determineMimeType();
897 itemSubSet
.append(item
);
898 } while (!m_pendingPreviewItems
.isEmpty() && timer
.elapsed() < MaxBlockTimeout
);
901 KIO::PreviewJob
* job
= new KIO::PreviewJob(itemSubSet
, cacheSize
, &m_enabledPlugins
);
903 job
->setIgnoreMaximumSize(itemSubSet
.first().isLocalFile());
905 KJobWidgets::setWindow(job
, qApp
->activeWindow());
908 connect(job
, &KIO::PreviewJob::gotPreview
,
909 this, &KFileItemModelRolesUpdater::slotGotPreview
);
910 connect(job
, &KIO::PreviewJob::failed
,
911 this, &KFileItemModelRolesUpdater::slotPreviewFailed
);
912 connect(job
, &KIO::PreviewJob::finished
,
913 this, &KFileItemModelRolesUpdater::slotPreviewJobFinished
);
918 void KFileItemModelRolesUpdater::updateChangedItems()
920 if (m_state
== Paused
) {
924 if (m_changedItems
.isEmpty()) {
928 m_finishedItems
-= m_changedItems
;
930 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
931 m_pendingSortRoleItems
+= m_changedItems
;
933 if (m_state
!= ResolvingSortRole
) {
934 // Stop the preview job if necessary, and trigger the
935 // asynchronous determination of the sort role.
937 m_state
= ResolvingSortRole
;
938 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
944 QList
<int> visibleChangedIndexes
;
945 QList
<int> invisibleChangedIndexes
;
947 foreach (const KFileItem
& item
, m_changedItems
) {
948 const int index
= m_model
->index(item
);
951 m_changedItems
.remove(item
);
955 if (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
) {
956 visibleChangedIndexes
.append(index
);
958 invisibleChangedIndexes
.append(index
);
962 std::sort(visibleChangedIndexes
.begin(), visibleChangedIndexes
.end());
964 if (m_previewShown
) {
965 foreach (int index
, visibleChangedIndexes
) {
966 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
969 foreach (int index
, invisibleChangedIndexes
) {
970 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
977 const bool resolvingInProgress
= !m_pendingIndexes
.isEmpty();
978 m_pendingIndexes
= visibleChangedIndexes
+ m_pendingIndexes
+ invisibleChangedIndexes
;
979 if (!resolvingInProgress
) {
980 // Trigger the asynchronous resolving of the changed roles.
981 m_state
= ResolvingAllRoles
;
982 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
987 void KFileItemModelRolesUpdater::applySortRole(int index
)
989 QHash
<QByteArray
, QVariant
> data
;
990 const KFileItem item
= m_model
->fileItem(index
);
992 if (m_model
->sortRole() == "type") {
993 if (!item
.isMimeTypeKnown()) {
994 item
.determineMimeType();
997 data
.insert("type", item
.mimeComment());
998 } else if (m_model
->sortRole() == "size" && item
.isLocalFile() && item
.isDir()) {
999 const QString path
= item
.localPath();
1000 data
.insert("size", m_directoryContentsCounter
->countDirectoryContentsSynchronously(path
));
1002 // Probably the sort role is a baloo role - just determine all roles.
1003 data
= rolesData(item
);
1006 disconnect(m_model
, &KFileItemModel::itemsChanged
,
1007 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1008 m_model
->setData(index
, data
);
1009 connect(m_model
, &KFileItemModel::itemsChanged
,
1010 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1013 void KFileItemModelRolesUpdater::applySortProgressToModel()
1015 // Inform the model about the progress of the resolved items,
1016 // so that it can give an indication when the sorting has been finished.
1017 const int resolvedCount
= m_model
->count() - m_pendingSortRoleItems
.count();
1018 m_model
->emitSortProgress(resolvedCount
);
1021 bool KFileItemModelRolesUpdater::applyResolvedRoles(int index
, ResolveHint hint
)
1023 const KFileItem item
= m_model
->fileItem(index
);
1024 const bool resolveAll
= (hint
== ResolveAll
);
1026 bool iconChanged
= false;
1027 if (!item
.isMimeTypeKnown() || !item
.isFinalIconKnown()) {
1028 item
.determineMimeType();
1030 } else if (!m_model
->data(index
).contains("iconName")) {
1034 if (iconChanged
|| resolveAll
|| m_clearPreviews
) {
1039 QHash
<QByteArray
, QVariant
> data
;
1041 data
= rolesData(item
);
1044 data
.insert("iconName", item
.iconName());
1046 if (m_clearPreviews
) {
1047 data
.insert("iconPixmap", QPixmap());
1050 disconnect(m_model
, &KFileItemModel::itemsChanged
,
1051 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1052 m_model
->setData(index
, data
);
1053 connect(m_model
, &KFileItemModel::itemsChanged
,
1054 this, &KFileItemModelRolesUpdater::slotItemsChanged
);
1061 QHash
<QByteArray
, QVariant
> KFileItemModelRolesUpdater::rolesData(const KFileItem
& item
)
1063 QHash
<QByteArray
, QVariant
> data
;
1065 const bool getSizeRole
= m_roles
.contains("size");
1066 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
1068 if ((getSizeRole
|| getIsExpandableRole
) && item
.isDir()) {
1069 if (item
.isLocalFile()) {
1070 // Tell m_directoryContentsCounter that we want to count the items
1071 // inside the directory. The result will be received in slotDirectoryContentsCountReceived.
1072 const QString path
= item
.localPath();
1073 m_directoryContentsCounter
->addDirectory(path
);
1074 } else if (getSizeRole
) {
1075 data
.insert("size", -1); // -1 indicates an unknown number of items
1079 if (m_roles
.contains("type")) {
1080 data
.insert("type", item
.mimeComment());
1083 QStringList overlays
= item
.overlays();
1084 foreach(KOverlayIconPlugin
*it
, m_overlayIconsPlugin
) {
1085 overlays
.append(it
->getOverlays(item
.url()));
1087 data
.insert("iconOverlays", overlays
);
1090 if (m_balooFileMonitor
) {
1091 m_balooFileMonitor
->addFile(item
.localPath());
1092 applyChangedBalooRoles(item
.localPath());
1098 void KFileItemModelRolesUpdater::slotOverlaysChanged(const QUrl
& url
, const QStringList
&)
1100 const KFileItem item
= m_model
->fileItem(url
);
1101 if (item
.isNull()) {
1104 const int index
= m_model
->index(item
);
1105 QHash
<QByteArray
, QVariant
> data
= m_model
->data(index
);
1106 QStringList overlays
= item
.overlays();
1107 foreach (KOverlayIconPlugin
*it
, m_overlayIconsPlugin
) {
1108 overlays
.append(it
->getOverlays(url
));
1110 data
.insert("iconOverlays", overlays
);
1111 m_model
->setData(index
, data
);
1114 void KFileItemModelRolesUpdater::updateAllPreviews()
1116 if (m_state
== Paused
) {
1117 m_previewChangedDuringPausing
= true;
1119 m_finishedItems
.clear();
1124 void KFileItemModelRolesUpdater::killPreviewJob()
1127 disconnect(m_previewJob
, &KIO::PreviewJob::gotPreview
,
1128 this, &KFileItemModelRolesUpdater::slotGotPreview
);
1129 disconnect(m_previewJob
, &KIO::PreviewJob::failed
,
1130 this, &KFileItemModelRolesUpdater::slotPreviewFailed
);
1131 disconnect(m_previewJob
, &KIO::PreviewJob::finished
,
1132 this, &KFileItemModelRolesUpdater::slotPreviewJobFinished
);
1133 m_previewJob
->kill();
1135 m_pendingPreviewItems
.clear();
1139 QList
<int> KFileItemModelRolesUpdater::indexesToResolve() const
1141 const int count
= m_model
->count();
1144 result
.reserve(ResolveAllItemsLimit
);
1146 // Add visible items.
1147 for (int i
= m_firstVisibleIndex
; i
<= m_lastVisibleIndex
; ++i
) {
1151 // We need a reasonable upper limit for number of items to resolve after
1152 // and before the visible range. m_maximumVisibleItems can be quite large
1153 // when using Compace View.
1154 const int readAheadItems
= qMin(ReadAheadPages
* m_maximumVisibleItems
, ResolveAllItemsLimit
/ 2);
1156 // Add items after the visible range.
1157 const int endExtendedVisibleRange
= qMin(m_lastVisibleIndex
+ readAheadItems
, count
- 1);
1158 for (int i
= m_lastVisibleIndex
+ 1; i
<= endExtendedVisibleRange
; ++i
) {
1162 // Add items before the visible range in reverse order.
1163 const int beginExtendedVisibleRange
= qMax(0, m_firstVisibleIndex
- readAheadItems
);
1164 for (int i
= m_firstVisibleIndex
- 1; i
>= beginExtendedVisibleRange
; --i
) {
1168 // Add items on the last page.
1169 const int beginLastPage
= qMax(qMin(endExtendedVisibleRange
+ 1, count
- 1), count
- m_maximumVisibleItems
);
1170 for (int i
= beginLastPage
; i
< count
; ++i
) {
1174 // Add items on the first page.
1175 const int endFirstPage
= qMin(qMax(beginExtendedVisibleRange
- 1, 0), m_maximumVisibleItems
);
1176 for (int i
= 0; i
<= endFirstPage
; ++i
) {
1180 // Continue adding items until ResolveAllItemsLimit is reached.
1181 int remainingItems
= ResolveAllItemsLimit
- result
.count();
1183 for (int i
= endExtendedVisibleRange
+ 1; i
< beginLastPage
&& remainingItems
> 0; ++i
) {
1188 for (int i
= beginExtendedVisibleRange
- 1; i
> endFirstPage
&& remainingItems
> 0; --i
) {