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 <KIO/JobUiDelegate>
30 #include <KIO/PreviewJob>
32 #include "private/kpixmapmodifier.h"
33 #include "private/kdirectorycontentscounter.h"
35 #include <QApplication>
38 #include <QElapsedTimer>
44 #include "private/kbaloorolesprovider.h"
45 #include <baloo/file.h>
46 #include <baloo/filefetchjob.h>
47 #include <baloo/filemonitor.h>
50 // #define KFILEITEMMODELROLESUPDATER_DEBUG
53 // Maximum time in ms that the KFileItemModelRolesUpdater
54 // may perform a blocking operation
55 const int MaxBlockTimeout
= 200;
57 // If the number of items is smaller than ResolveAllItemsLimit,
58 // the roles of all items will be resolved.
59 const int ResolveAllItemsLimit
= 500;
61 // Not only the visible area, but up to ReadAheadPages before and after
62 // this area will be resolved.
63 const int ReadAheadPages
= 5;
66 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
) :
69 m_previewChangedDuringPausing(false),
70 m_iconSizeChangedDuringPausing(false),
71 m_rolesChangedDuringPausing(false),
72 m_previewShown(false),
73 m_enlargeSmallPreviews(true),
74 m_clearPreviews(false),
78 m_firstVisibleIndex(0),
79 m_lastVisibleIndex(-1),
80 m_maximumVisibleItems(50),
84 m_pendingSortRoleItems(),
86 m_pendingPreviewItems(),
88 m_recentlyChangedItemsTimer(0),
89 m_recentlyChangedItems(),
91 m_directoryContentsCounter(0)
93 , m_balooFileMonitor(0)
98 const KConfigGroup
globalConfig(KGlobal::config(), "PreviewSettings");
99 m_enabledPlugins
= globalConfig
.readEntry("Plugins", QStringList()
100 << "directorythumbnail"
104 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
105 this, SLOT(slotItemsInserted(KItemRangeList
)));
106 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
107 this, SLOT(slotItemsRemoved(KItemRangeList
)));
108 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
109 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
110 connect(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)),
111 this, SLOT(slotItemsMoved(KItemRange
,QList
<int>)));
112 connect(m_model
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
113 this, SLOT(slotSortRoleChanged(QByteArray
,QByteArray
)));
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
, SIGNAL(timeout()), this, SLOT(resolveRecentlyChangedItems()));
122 m_resolvableRoles
.insert("size");
123 m_resolvableRoles
.insert("type");
124 m_resolvableRoles
.insert("isExpandable");
126 m_resolvableRoles
+= KBalooRolesProvider::instance().roles();
129 m_directoryContentsCounter
= new KDirectoryContentsCounter(m_model
, this);
130 connect(m_directoryContentsCounter
, SIGNAL(result(QString
,int)),
131 this, SLOT(slotDirectoryContentsCountReceived(QString
,int)));
134 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
139 void KFileItemModelRolesUpdater::setIconSize(const QSize
& size
)
141 if (size
!= m_iconSize
) {
143 if (m_state
== Paused
) {
144 m_iconSizeChangedDuringPausing
= true;
145 } else if (m_previewShown
) {
146 // An icon size change requires the regenerating of
148 m_finishedItems
.clear();
154 QSize
KFileItemModelRolesUpdater::iconSize() const
159 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index
, int count
)
168 if (index
== m_firstVisibleIndex
&& count
== m_lastVisibleIndex
- m_firstVisibleIndex
+ 1) {
169 // The range has not been changed
173 m_firstVisibleIndex
= index
;
174 m_lastVisibleIndex
= qMin(index
+ count
- 1, m_model
->count() - 1);
179 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count
)
181 m_maximumVisibleItems
= count
;
184 void KFileItemModelRolesUpdater::setPreviewsShown(bool show
)
186 if (show
== m_previewShown
) {
190 m_previewShown
= show
;
192 m_clearPreviews
= true;
198 bool KFileItemModelRolesUpdater::previewsShown() const
200 return m_previewShown
;
203 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge
)
205 if (enlarge
!= m_enlargeSmallPreviews
) {
206 m_enlargeSmallPreviews
= enlarge
;
207 if (m_previewShown
) {
213 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
215 return m_enlargeSmallPreviews
;
218 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList
& list
)
220 if (m_enabledPlugins
!= list
) {
221 m_enabledPlugins
= list
;
222 if (m_previewShown
) {
228 void KFileItemModelRolesUpdater::setPaused(bool paused
)
230 if (paused
== (m_state
== Paused
)) {
238 const bool updatePreviews
= (m_iconSizeChangedDuringPausing
&& m_previewShown
) ||
239 m_previewChangedDuringPausing
;
240 const bool resolveAll
= updatePreviews
|| m_rolesChangedDuringPausing
;
242 m_finishedItems
.clear();
245 m_iconSizeChangedDuringPausing
= false;
246 m_previewChangedDuringPausing
= false;
247 m_rolesChangedDuringPausing
= false;
249 if (!m_pendingSortRoleItems
.isEmpty()) {
250 m_state
= ResolvingSortRole
;
251 resolveNextSortRole();
260 void KFileItemModelRolesUpdater::setRoles(const QSet
<QByteArray
>& roles
)
262 if (m_roles
!= roles
) {
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
)) {
281 if (hasBalooRole
&& !m_balooFileMonitor
) {
282 m_balooFileMonitor
= new Baloo::FileMonitor(this);
283 connect(m_balooFileMonitor
, SIGNAL(fileMetaDataChanged(QString
)),
284 this, SLOT(applyChangedBalooRoles(QString
)));
285 } else if (!hasBalooRole
&& m_balooFileMonitor
) {
286 delete m_balooFileMonitor
;
287 m_balooFileMonitor
= 0;
291 if (m_state
== Paused
) {
292 m_rolesChangedDuringPausing
= true;
299 QSet
<QByteArray
> KFileItemModelRolesUpdater::roles() const
304 bool KFileItemModelRolesUpdater::isPaused() const
306 return m_state
== Paused
;
309 QStringList
KFileItemModelRolesUpdater::enabledPlugins() const
311 return m_enabledPlugins
;
314 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList
& itemRanges
)
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
) {
328 m_pendingSortRoleItems
.insert(m_model
->fileItem(i
));
331 insertedCount
+= range
.count
;
334 applySortProgressToModel();
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
) {
341 m_state
= ResolvingSortRole
;
342 resolveNextSortRole();
349 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList
& itemRanges
)
351 Q_UNUSED(itemRanges
);
353 const bool allItemsRemoved
= (m_model
->count() == 0);
356 if (m_balooFileMonitor
) {
357 // Don't let the FileWatcher watch for removed items
358 if (allItemsRemoved
) {
359 m_balooFileMonitor
->clear();
361 QStringList newFileList
;
362 foreach (const QString
& itemUrl
, m_balooFileMonitor
->files()) {
363 if (m_model
->index(itemUrl
) >= 0) {
364 newFileList
.append(itemUrl
);
367 m_balooFileMonitor
->setFiles(newFileList
);
372 if (allItemsRemoved
) {
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();
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
);
396 // The visible items might have changed.
401 void KFileItemModelRolesUpdater::slotItemsMoved(const KItemRange
& itemRange
, QList
<int> movedToIndexes
)
404 Q_UNUSED(movedToIndexes
);
406 // The visible items might have changed.
410 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList
& itemRanges
,
411 const QSet
<QByteArray
>& roles
)
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();
420 QSet
<KFileItem
>& targetSet
= itemsChangedRecently
? m_recentlyChangedItems
: m_changedItems
;
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
);
431 m_recentlyChangedItemsTimer
->start();
433 if (!itemsChangedRecently
) {
434 updateChangedItems();
438 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray
& current
,
439 const QByteArray
& previous
)
444 if (m_resolvableRoles
.contains(current
)) {
445 m_pendingSortRoleItems
.clear();
446 m_finishedItems
.clear();
448 const int count
= m_model
->count();
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
);
457 m_pendingSortRoleItems
.insert(m_model
->fileItem(index
));
461 applySortProgressToModel();
463 if (!m_pendingSortRoleItems
.isEmpty()) {
464 // Trigger the asynchronous determination of the sort role.
466 m_state
= ResolvingSortRole
;
467 resolveNextSortRole();
471 m_pendingSortRoleItems
.clear();
472 applySortProgressToModel();
476 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
478 if (m_state
!= PreviewJobRunning
) {
482 m_changedItems
.remove(item
);
484 const int index
= m_model
->index(item
);
489 QPixmap scaledPixmap
= pixmap
;
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
);
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
, Qt::KeepAspectRatio
);
507 QPixmap
largeFrame(frameSize
);
508 largeFrame
.fill(Qt::transparent
);
510 KPixmapModifier::applyFrame(largeFrame
, frameSize
);
512 QPainter
painter(&largeFrame
);
513 painter
.drawPixmap((largeFrame
.width() - scaledPixmap
.width()) / 2,
514 (largeFrame
.height() - scaledPixmap
.height()) / 2,
516 scaledPixmap
= largeFrame
;
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
);
524 KPixmapModifier::scale(scaledPixmap
, m_iconSize
);
527 QHash
<QByteArray
, QVariant
> data
= rolesData(item
);
529 const QStringList overlays
= data
["iconOverlays"].toStringList();
530 // Strangely KFileItem::overlays() returns empty string-values, so
531 // we need to check first whether an overlay must be drawn at all.
532 // It is more efficient to do it here, as KIconLoader::drawOverlays()
533 // assumes that an overlay will be drawn and has some additional
535 foreach (const QString
& overlay
, overlays
) {
536 if (!overlay
.isEmpty()) {
537 // There is at least one overlay, draw all overlays above m_pixmap
538 // and cancel the check
539 KIconLoader::global()->drawOverlays(overlays
, scaledPixmap
, KIconLoader::Desktop
);
544 data
.insert("iconPixmap", scaledPixmap
);
546 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
547 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
548 m_model
->setData(index
, data
);
549 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
550 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
552 m_finishedItems
.insert(item
);
555 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem
& item
)
557 if (m_state
!= PreviewJobRunning
) {
561 m_changedItems
.remove(item
);
563 const int index
= m_model
->index(item
);
565 QHash
<QByteArray
, QVariant
> data
;
566 data
.insert("iconPixmap", QPixmap());
568 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
569 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
570 m_model
->setData(index
, data
);
571 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
572 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
574 applyResolvedRoles(index
, ResolveAll
);
575 m_finishedItems
.insert(item
);
579 void KFileItemModelRolesUpdater::slotPreviewJobFinished()
583 if (m_state
!= PreviewJobRunning
) {
589 if (!m_pendingPreviewItems
.isEmpty()) {
592 if (!m_changedItems
.isEmpty()) {
593 updateChangedItems();
598 void KFileItemModelRolesUpdater::resolveNextSortRole()
600 if (m_state
!= ResolvingSortRole
) {
604 QSet
<KFileItem
>::iterator it
= m_pendingSortRoleItems
.begin();
605 while (it
!= m_pendingSortRoleItems
.end()) {
606 const KFileItem item
= *it
;
607 const int index
= m_model
->index(item
);
609 // Continue if the sort role has already been determined for the
610 // item, and the item has not been changed recently.
611 if (!m_changedItems
.contains(item
) && m_model
->data(index
).contains(m_model
->sortRole())) {
612 it
= m_pendingSortRoleItems
.erase(it
);
616 applySortRole(index
);
617 m_pendingSortRoleItems
.erase(it
);
621 if (!m_pendingSortRoleItems
.isEmpty()) {
622 applySortProgressToModel();
623 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
627 // Prevent that we try to update the items twice.
628 disconnect(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)),
629 this, SLOT(slotItemsMoved(KItemRange
,QList
<int>)));
630 applySortProgressToModel();
631 connect(m_model
, SIGNAL(itemsMoved(KItemRange
,QList
<int>)),
632 this, SLOT(slotItemsMoved(KItemRange
,QList
<int>)));
637 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
639 if (m_state
!= ResolvingAllRoles
) {
643 while (!m_pendingIndexes
.isEmpty()) {
644 const int index
= m_pendingIndexes
.takeFirst();
645 const KFileItem item
= m_model
->fileItem(index
);
647 if (m_finishedItems
.contains(item
)) {
651 applyResolvedRoles(index
, ResolveAll
);
652 m_finishedItems
.insert(item
);
653 m_changedItems
.remove(item
);
657 if (!m_pendingIndexes
.isEmpty()) {
658 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
662 if (m_clearPreviews
) {
663 // Only go through the list if there are items which might still have previews.
664 if (m_finishedItems
.count() != m_model
->count()) {
665 QHash
<QByteArray
, QVariant
> data
;
666 data
.insert("iconPixmap", QPixmap());
668 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
669 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
670 for (int index
= 0; index
<= m_model
->count(); ++index
) {
671 if (m_model
->data(index
).contains("iconPixmap")) {
672 m_model
->setData(index
, data
);
675 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
676 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
679 m_clearPreviews
= false;
682 if (!m_changedItems
.isEmpty()) {
683 updateChangedItems();
688 void KFileItemModelRolesUpdater::resolveRecentlyChangedItems()
690 m_changedItems
+= m_recentlyChangedItems
;
691 m_recentlyChangedItems
.clear();
692 updateChangedItems();
695 void KFileItemModelRolesUpdater::applyChangedBalooRoles(const QString
& itemUrl
)
698 const KFileItem item
= m_model
->fileItem(itemUrl
);
701 // itemUrl is not in the model anymore, probably because
702 // the corresponding file has been deleted in the meantime.
706 Baloo::FileFetchJob
* job
= new Baloo::FileFetchJob(item
.localPath());
707 connect(job
, SIGNAL(finished(KJob
*)), this, SLOT(applyChangedBalooRolesJobFinished(KJob
*)));
708 job
->setProperty("item", QVariant::fromValue(item
));
717 void KFileItemModelRolesUpdater::applyChangedBalooRolesJobFinished(KJob
* kjob
)
720 const KFileItem item
= kjob
->property("item").value
<KFileItem
>();
722 const KBalooRolesProvider
& rolesProvider
= KBalooRolesProvider::instance();
723 QHash
<QByteArray
, QVariant
> data
;
725 foreach (const QByteArray
& role
, rolesProvider
.roles()) {
726 // Overwrite all the role values with an empty QVariant, because the roles
727 // provider doesn't overwrite it when the property value list is empty.
729 data
.insert(role
, QVariant());
732 Baloo::FileFetchJob
* job
= static_cast<Baloo::FileFetchJob
*>(kjob
);
733 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(job
->file(), m_roles
));
734 while (it
.hasNext()) {
736 data
.insert(it
.key(), it
.value());
739 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
740 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
741 const int index
= m_model
->index(item
);
742 m_model
->setData(index
, data
);
743 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
744 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
748 void KFileItemModelRolesUpdater::slotDirectoryContentsCountReceived(const QString
& path
, int count
)
750 const bool getSizeRole
= m_roles
.contains("size");
751 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
753 if (getSizeRole
|| getIsExpandableRole
) {
754 const int index
= m_model
->index(KUrl(path
));
756 QHash
<QByteArray
, QVariant
> data
;
759 data
.insert("size", count
);
761 if (getIsExpandableRole
) {
762 data
.insert("isExpandable", count
> 0);
765 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
766 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
767 m_model
->setData(index
, data
);
768 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
769 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
774 void KFileItemModelRolesUpdater::startUpdating()
776 if (m_state
== Paused
) {
780 if (m_finishedItems
.count() == m_model
->count()) {
781 // All roles have been resolved already.
786 // Terminate all updates that are currently active.
788 m_pendingIndexes
.clear();
793 // Determine the icons for the visible items synchronously.
794 updateVisibleIcons();
796 // A detailed update of the items in and near the visible area
797 // only makes sense if sorting is finished.
798 if (m_state
== ResolvingSortRole
) {
802 // Start the preview job or the asynchronous resolving of all roles.
803 QList
<int> indexes
= indexesToResolve();
805 if (m_previewShown
) {
806 m_pendingPreviewItems
.clear();
807 m_pendingPreviewItems
.reserve(indexes
.count());
809 foreach (int index
, indexes
) {
810 const KFileItem item
= m_model
->fileItem(index
);
811 if (!m_finishedItems
.contains(item
)) {
812 m_pendingPreviewItems
.append(item
);
818 m_pendingIndexes
= indexes
;
819 // Trigger the asynchronous resolving of all roles.
820 m_state
= ResolvingAllRoles
;
821 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
825 void KFileItemModelRolesUpdater::updateVisibleIcons()
827 int lastVisibleIndex
= m_lastVisibleIndex
;
828 if (lastVisibleIndex
<= 0) {
829 // Guess a reasonable value for the last visible index if the view
830 // has not told us about the real value yet.
831 lastVisibleIndex
= qMin(m_firstVisibleIndex
+ m_maximumVisibleItems
, m_model
->count() - 1);
832 if (lastVisibleIndex
<= 0) {
833 lastVisibleIndex
= qMin(200, m_model
->count() - 1);
840 // Try to determine the final icons for all visible items.
842 for (index
= m_firstVisibleIndex
; index
<= lastVisibleIndex
&& timer
.elapsed() < MaxBlockTimeout
; ++index
) {
843 applyResolvedRoles(index
, ResolveFast
);
846 // KFileItemListView::initializeItemListWidget(KItemListWidget*) will load
847 // preliminary icons (i.e., without mime type determination) for the
851 void KFileItemModelRolesUpdater::startPreviewJob()
853 m_state
= PreviewJobRunning
;
855 if (m_pendingPreviewItems
.isEmpty()) {
856 QTimer::singleShot(0, this, SLOT(slotPreviewJobFinished()));
860 // PreviewJob internally caches items always with the size of
861 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
862 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
863 // do a downscaling anyhow because of the frame, so in this case only the provided
864 // cache sizes are requested.
865 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128)
866 ? QSize(256, 256) : QSize(128, 128);
868 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
869 // worst case) might block the application for several seconds. To prevent such
870 // a blocking, we only pass items with known mime type to the preview job.
871 const int count
= m_pendingPreviewItems
.count();
872 KFileItemList itemSubSet
;
873 itemSubSet
.reserve(count
);
875 if (m_pendingPreviewItems
.first().isMimeTypeKnown()) {
876 // Some mime types are known already, probably because they were
877 // determined when loading the icons for the visible items. Start
878 // a preview job for all items at the beginning of the list which
879 // have a known mime type.
881 itemSubSet
.append(m_pendingPreviewItems
.takeFirst());
882 } while (!m_pendingPreviewItems
.isEmpty() && m_pendingPreviewItems
.first().isMimeTypeKnown());
884 // Determine mime types for MaxBlockTimeout ms, and start a preview
885 // job for the corresponding items.
890 const KFileItem item
= m_pendingPreviewItems
.takeFirst();
891 item
.determineMimeType();
892 itemSubSet
.append(item
);
893 } while (!m_pendingPreviewItems
.isEmpty() && timer
.elapsed() < MaxBlockTimeout
);
896 KIO::PreviewJob
* job
= new KIO::PreviewJob(itemSubSet
, cacheSize
, &m_enabledPlugins
);
898 job
->setIgnoreMaximumSize(itemSubSet
.first().isLocalFile());
900 job
->ui()->setWindow(qApp
->activeWindow());
903 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
904 this, SLOT(slotGotPreview(KFileItem
,QPixmap
)));
905 connect(job
, SIGNAL(failed(KFileItem
)),
906 this, SLOT(slotPreviewFailed(KFileItem
)));
907 connect(job
, SIGNAL(finished(KJob
*)),
908 this, SLOT(slotPreviewJobFinished()));
913 void KFileItemModelRolesUpdater::updateChangedItems()
915 if (m_state
== Paused
) {
919 if (m_changedItems
.isEmpty()) {
923 m_finishedItems
-= m_changedItems
;
925 if (m_resolvableRoles
.contains(m_model
->sortRole())) {
926 m_pendingSortRoleItems
+= m_changedItems
;
928 if (m_state
!= ResolvingSortRole
) {
929 // Stop the preview job if necessary, and trigger the
930 // asynchronous determination of the sort role.
932 m_state
= ResolvingSortRole
;
933 QTimer::singleShot(0, this, SLOT(resolveNextSortRole()));
939 QList
<int> visibleChangedIndexes
;
940 QList
<int> invisibleChangedIndexes
;
942 foreach (const KFileItem
& item
, m_changedItems
) {
943 const int index
= m_model
->index(item
);
946 m_changedItems
.remove(item
);
950 if (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
) {
951 visibleChangedIndexes
.append(index
);
953 invisibleChangedIndexes
.append(index
);
957 std::sort(visibleChangedIndexes
.begin(), visibleChangedIndexes
.end());
959 if (m_previewShown
) {
960 foreach (int index
, visibleChangedIndexes
) {
961 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
964 foreach (int index
, invisibleChangedIndexes
) {
965 m_pendingPreviewItems
.append(m_model
->fileItem(index
));
972 const bool resolvingInProgress
= !m_pendingIndexes
.isEmpty();
973 m_pendingIndexes
= visibleChangedIndexes
+ m_pendingIndexes
+ invisibleChangedIndexes
;
974 if (!resolvingInProgress
) {
975 // Trigger the asynchronous resolving of the changed roles.
976 m_state
= ResolvingAllRoles
;
977 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
982 void KFileItemModelRolesUpdater::applySortRole(int index
)
984 QHash
<QByteArray
, QVariant
> data
;
985 const KFileItem item
= m_model
->fileItem(index
);
987 if (m_model
->sortRole() == "type") {
988 if (!item
.isMimeTypeKnown()) {
989 item
.determineMimeType();
992 data
.insert("type", item
.mimeComment());
993 } else if (m_model
->sortRole() == "size" && item
.isLocalFile() && item
.isDir()) {
994 const QString path
= item
.localPath();
995 data
.insert("size", m_directoryContentsCounter
->countDirectoryContentsSynchronously(path
));
997 // Probably the sort role is a baloo role - just determine all roles.
998 data
= rolesData(item
);
1001 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1002 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1003 m_model
->setData(index
, data
);
1004 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1005 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1008 void KFileItemModelRolesUpdater::applySortProgressToModel()
1010 // Inform the model about the progress of the resolved items,
1011 // so that it can give an indication when the sorting has been finished.
1012 const int resolvedCount
= m_model
->count() - m_pendingSortRoleItems
.count();
1013 m_model
->emitSortProgress(resolvedCount
);
1016 bool KFileItemModelRolesUpdater::applyResolvedRoles(int index
, ResolveHint hint
)
1018 const KFileItem item
= m_model
->fileItem(index
);
1019 const bool resolveAll
= (hint
== ResolveAll
);
1021 bool iconChanged
= false;
1022 if (!item
.isMimeTypeKnown() || !item
.isFinalIconKnown()) {
1023 item
.determineMimeType();
1025 } else if (!m_model
->data(index
).contains("iconName")) {
1029 if (iconChanged
|| resolveAll
|| m_clearPreviews
) {
1034 QHash
<QByteArray
, QVariant
> data
;
1036 data
= rolesData(item
);
1039 data
.insert("iconName", item
.iconName());
1041 if (m_clearPreviews
) {
1042 data
.insert("iconPixmap", QPixmap());
1045 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1046 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1047 m_model
->setData(index
, data
);
1048 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1049 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1056 QHash
<QByteArray
, QVariant
> KFileItemModelRolesUpdater::rolesData(const KFileItem
& item
)
1058 QHash
<QByteArray
, QVariant
> data
;
1060 const bool getSizeRole
= m_roles
.contains("size");
1061 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
1063 if ((getSizeRole
|| getIsExpandableRole
) && item
.isDir()) {
1064 if (item
.isLocalFile()) {
1065 // Tell m_directoryContentsCounter that we want to count the items
1066 // inside the directory. The result will be received in slotDirectoryContentsCountReceived.
1067 const QString path
= item
.localPath();
1068 m_directoryContentsCounter
->addDirectory(path
);
1069 } else if (getSizeRole
) {
1070 data
.insert("size", -1); // -1 indicates an unknown number of items
1074 if (m_roles
.contains("type")) {
1075 data
.insert("type", item
.mimeComment());
1078 data
.insert("iconOverlays", item
.overlays());
1081 if (m_balooFileMonitor
) {
1082 m_balooFileMonitor
->addFile(item
.localPath());
1083 applyChangedBalooRoles(item
.localPath());
1089 void KFileItemModelRolesUpdater::updateAllPreviews()
1091 if (m_state
== Paused
) {
1092 m_previewChangedDuringPausing
= true;
1094 m_finishedItems
.clear();
1099 void KFileItemModelRolesUpdater::killPreviewJob()
1102 disconnect(m_previewJob
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
1103 this, SLOT(slotGotPreview(KFileItem
,QPixmap
)));
1104 disconnect(m_previewJob
, SIGNAL(failed(KFileItem
)),
1105 this, SLOT(slotPreviewFailed(KFileItem
)));
1106 disconnect(m_previewJob
, SIGNAL(finished(KJob
*)),
1107 this, SLOT(slotPreviewJobFinished()));
1108 m_previewJob
->kill();
1110 m_pendingPreviewItems
.clear();
1114 QList
<int> KFileItemModelRolesUpdater::indexesToResolve() const
1116 const int count
= m_model
->count();
1119 result
.reserve(ResolveAllItemsLimit
);
1121 // Add visible items.
1122 for (int i
= m_firstVisibleIndex
; i
<= m_lastVisibleIndex
; ++i
) {
1126 // We need a reasonable upper limit for number of items to resolve after
1127 // and before the visible range. m_maximumVisibleItems can be quite large
1128 // when using Compace View.
1129 const int readAheadItems
= qMin(ReadAheadPages
* m_maximumVisibleItems
, ResolveAllItemsLimit
/ 2);
1131 // Add items after the visible range.
1132 const int endExtendedVisibleRange
= qMin(m_lastVisibleIndex
+ readAheadItems
, count
- 1);
1133 for (int i
= m_lastVisibleIndex
+ 1; i
<= endExtendedVisibleRange
; ++i
) {
1137 // Add items before the visible range in reverse order.
1138 const int beginExtendedVisibleRange
= qMax(0, m_firstVisibleIndex
- readAheadItems
);
1139 for (int i
= m_firstVisibleIndex
- 1; i
>= beginExtendedVisibleRange
; --i
) {
1143 // Add items on the last page.
1144 const int beginLastPage
= qMax(qMin(endExtendedVisibleRange
+ 1, count
- 1), count
- m_maximumVisibleItems
);
1145 for (int i
= beginLastPage
; i
< count
; ++i
) {
1149 // Add items on the first page.
1150 const int endFirstPage
= qMin(qMax(beginExtendedVisibleRange
- 1, 0), m_maximumVisibleItems
);
1151 for (int i
= 0; i
<= endFirstPage
; ++i
) {
1155 // Continue adding items until ResolveAllItemsLimit is reached.
1156 int remainingItems
= ResolveAllItemsLimit
- result
.count();
1158 for (int i
= endExtendedVisibleRange
+ 1; i
< beginLastPage
&& remainingItems
> 0; ++i
) {
1163 for (int i
= beginExtendedVisibleRange
- 1; i
> endFirstPage
&& remainingItems
> 0; --i
) {
1171 #include "kfileitemmodelrolesupdater.moc"