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>
30 #include <KIO/JobUiDelegate>
31 #include <KIO/PreviewJob>
33 #include "private/kpixmapmodifier.h"
35 #include <QApplication>
38 #include <QElapsedTimer>
42 #include "private/knepomukrolesprovider.h"
43 #include <Nepomuk2/ResourceWatcher>
46 // Required includes for subItemsCount():
54 // #define KFILEITEMMODELROLESUPDATER_DEBUG
57 // Maximum time in ms that the KFileItemModelRolesUpdater
58 // may perform a blocking operation
59 const int MaxBlockTimeout
= 200;
61 // Maximum number of items that will get resolved synchronously.
62 // The value should roughly represent the number of maximum visible
63 // items, as it does not make sense to resolve more items synchronously
64 // and probably reach the MaxBlockTimeout because of invisible items.
65 const int MaxResolveItemsCount
= 100;
68 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
) :
71 m_previewChangedDuringPausing(false),
72 m_iconSizeChangedDuringPausing(false),
73 m_rolesChangedDuringPausing(false),
74 m_previewShown(false),
75 m_enlargeSmallPreviews(true),
76 m_clearPreviews(false),
77 m_sortingProgress(-1),
80 m_firstVisibleIndex(0),
81 m_lastVisibleIndex(-1),
82 m_maximumVisibleItems(100),
85 m_pendingVisibleItems(),
86 m_pendingInvisibleItems(),
88 m_changedItemsTimer(0),
93 , m_nepomukResourceWatcher(0),
99 const KConfigGroup
globalConfig(KGlobal::config(), "PreviewSettings");
100 m_enabledPlugins
= globalConfig
.readEntry("Plugins", QStringList()
101 << "directorythumbnail"
105 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
106 this, SLOT(slotItemsInserted(KItemRangeList
)));
107 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
108 this, SLOT(slotItemsRemoved(KItemRangeList
)));
109 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
110 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
111 connect(m_model
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
112 this, SLOT(slotSortRoleChanged(QByteArray
,QByteArray
)));
114 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
115 // resolving of the roles. Postpone the resolving until no update has been done for 1 second.
116 m_changedItemsTimer
= new QTimer(this);
117 m_changedItemsTimer
->setInterval(1000);
118 m_changedItemsTimer
->setSingleShot(true);
119 connect(m_changedItemsTimer
, SIGNAL(timeout()), this, SLOT(resolveChangedItems()));
121 m_resolvableRoles
.insert("size");
122 m_resolvableRoles
.insert("type");
123 m_resolvableRoles
.insert("isExpandable");
125 m_resolvableRoles
+= KNepomukRolesProvider::instance().roles();
128 // When folders are expandable or the item-count is shown for folders, it is necessary
129 // to watch the number of items of the sub-folder to be able to react on changes.
130 m_dirWatcher
= new KDirWatch(this);
131 connect(m_dirWatcher
, SIGNAL(dirty(QString
)), this, SLOT(slotDirWatchDirty(QString
)));
134 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
139 void KFileItemModelRolesUpdater::setIconSize(const QSize
& size
)
141 if (size
!= m_iconSize
) {
144 m_iconSizeChangedDuringPausing
= true;
145 } else if (m_previewShown
) {
146 // An icon size change requires the regenerating of
148 sortAndResolveAllRoles();
150 sortAndResolvePendingRoles();
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);
177 if (hasPendingRoles() && !m_paused
) {
178 sortAndResolvePendingRoles();
182 void KFileItemModelRolesUpdater::setMaximumVisibleItems(int count
)
184 m_maximumVisibleItems
= count
;
187 void KFileItemModelRolesUpdater::setPreviewsShown(bool show
)
189 if (show
== m_previewShown
) {
193 m_previewShown
= show
;
195 m_clearPreviews
= true;
201 bool KFileItemModelRolesUpdater::previewsShown() const
203 return m_previewShown
;
206 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge
)
208 if (enlarge
!= m_enlargeSmallPreviews
) {
209 m_enlargeSmallPreviews
= enlarge
;
210 if (m_previewShown
) {
216 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
218 return m_enlargeSmallPreviews
;
221 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList
& list
)
223 if (m_enabledPlugins
!= list
) {
224 m_enabledPlugins
= list
;
225 if (m_previewShown
) {
231 void KFileItemModelRolesUpdater::setPaused(bool paused
)
233 if (paused
== m_paused
) {
239 if (hasPendingRoles()) {
240 foreach (KJob
* job
, m_previewJobs
) {
243 Q_ASSERT(m_previewJobs
.isEmpty());
246 const bool resolveAll
= (m_iconSizeChangedDuringPausing
&& m_previewShown
) ||
247 m_previewChangedDuringPausing
||
248 m_rolesChangedDuringPausing
;
250 sortAndResolveAllRoles();
252 sortAndResolvePendingRoles();
255 m_iconSizeChangedDuringPausing
= false;
256 m_previewChangedDuringPausing
= false;
257 m_rolesChangedDuringPausing
= false;
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 Nepomuk. 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 KNepomukRolesProvider
& rolesProvider
= KNepomukRolesProvider::instance();
272 bool hasNepomukRole
= false;
273 QSetIterator
<QByteArray
> it(roles
);
274 while (it
.hasNext()) {
275 const QByteArray
& role
= it
.next();
276 if (rolesProvider
.roles().contains(role
)) {
277 hasNepomukRole
= true;
282 if (hasNepomukRole
&& !m_nepomukResourceWatcher
) {
283 Q_ASSERT(m_nepomukUriItems
.isEmpty());
285 m_nepomukResourceWatcher
= new Nepomuk2::ResourceWatcher(this);
286 connect(m_nepomukResourceWatcher
, SIGNAL(propertyChanged(Nepomuk2::Resource
,Nepomuk2::Types::Property
,QVariantList
,QVariantList
)),
287 this, SLOT(applyChangedNepomukRoles(Nepomuk2::Resource
)));
288 } else if (!hasNepomukRole
&& m_nepomukResourceWatcher
) {
289 delete m_nepomukResourceWatcher
;
290 m_nepomukResourceWatcher
= 0;
291 m_nepomukUriItems
.clear();
295 updateSortProgress();
298 m_rolesChangedDuringPausing
= true;
300 sortAndResolveAllRoles();
305 QSet
<QByteArray
> KFileItemModelRolesUpdater::roles() const
310 bool KFileItemModelRolesUpdater::isPaused() const
315 QStringList
KFileItemModelRolesUpdater::enabledPlugins() const
317 return m_enabledPlugins
;
320 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList
& itemRanges
)
322 startUpdating(itemRanges
);
325 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList
& itemRanges
)
327 Q_UNUSED(itemRanges
);
329 const bool allItemsRemoved
= (m_model
->count() == 0);
331 if (!m_watchedDirs
.isEmpty()) {
332 // Don't let KDirWatch watch for removed items
333 if (allItemsRemoved
) {
334 foreach (const QString
& path
, m_watchedDirs
) {
335 m_dirWatcher
->removeDir(path
);
337 m_watchedDirs
.clear();
339 QMutableSetIterator
<QString
> it(m_watchedDirs
);
340 while (it
.hasNext()) {
341 const QString
& path
= it
.next();
342 if (m_model
->index(KUrl(path
)) < 0) {
343 m_dirWatcher
->removeDir(path
);
351 if (m_nepomukResourceWatcher
) {
352 // Don't let the ResourceWatcher watch for removed items
353 if (allItemsRemoved
) {
354 m_nepomukResourceWatcher
->setResources(QList
<Nepomuk2::Resource
>());
355 m_nepomukResourceWatcher
->stop();
356 m_nepomukUriItems
.clear();
358 QList
<Nepomuk2::Resource
> newResources
;
359 const QList
<Nepomuk2::Resource
> oldResources
= m_nepomukResourceWatcher
->resources();
360 foreach (const Nepomuk2::Resource
& resource
, oldResources
) {
361 const QUrl uri
= resource
.uri();
362 const KUrl itemUrl
= m_nepomukUriItems
.value(uri
);
363 if (m_model
->index(itemUrl
) >= 0) {
364 newResources
.append(resource
);
366 m_nepomukUriItems
.remove(uri
);
369 m_nepomukResourceWatcher
->setResources(newResources
);
370 if (newResources
.isEmpty()) {
371 Q_ASSERT(m_nepomukUriItems
.isEmpty());
372 m_nepomukResourceWatcher
->stop();
378 m_firstVisibleIndex
= 0;
379 m_lastVisibleIndex
= -1;
380 if (!hasPendingRoles()) {
384 if (allItemsRemoved
) {
385 // Most probably a directory change is done. Clear all pending items
386 // and also kill all ongoing preview-jobs.
389 m_changedItems
.clear();
390 m_changedItemsTimer
->stop();
392 // Remove all items from m_pendingVisibleItems and m_pendingInvisibleItems
393 // that are not part of the model anymore. The items from m_changedItems
394 // don't need to be handled here, removed items are just skipped in
395 // resolveChangedItems().
396 for (int i
= 0; i
<= 1; ++i
) {
397 QSet
<KFileItem
>& pendingItems
= (i
== 0) ? m_pendingVisibleItems
: m_pendingInvisibleItems
;
398 QMutableSetIterator
<KFileItem
> it(pendingItems
);
399 while (it
.hasNext()) {
400 const KFileItem item
= it
.next();
401 if (m_model
->index(item
) < 0) {
402 pendingItems
.remove(item
);
409 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList
& itemRanges
,
410 const QSet
<QByteArray
>& roles
)
414 if (m_changedItemsTimer
->isActive()) {
415 // A call of slotItemsChanged() has been done recently. Postpone the resolving
416 // of the roles until the timer has exceeded.
417 foreach (const KItemRange
& itemRange
, itemRanges
) {
418 int index
= itemRange
.index
;
419 for (int count
= itemRange
.count
; count
> 0; --count
) {
420 m_changedItems
.insert(m_model
->fileItem(index
));
425 // No call of slotItemsChanged() has been done recently, resolve the roles now.
426 startUpdating(itemRanges
);
428 m_changedItemsTimer
->start();
431 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray
& current
,
432 const QByteArray
& previous
)
436 updateSortProgress();
439 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
441 m_pendingVisibleItems
.remove(item
);
442 m_pendingInvisibleItems
.remove(item
);
444 const int index
= m_model
->index(item
);
449 QPixmap scaledPixmap
= pixmap
;
451 const QString mimeType
= item
.mimetype();
452 const int slashIndex
= mimeType
.indexOf(QLatin1Char('/'));
453 const QString mimeTypeGroup
= mimeType
.left(slashIndex
);
454 if (mimeTypeGroup
== QLatin1String("image")) {
455 if (m_enlargeSmallPreviews
) {
456 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
458 // Assure that small previews don't get enlarged. Instead they
459 // should be shown centered within the frame.
460 const QSize contentSize
= KPixmapModifier::sizeInsideFrame(m_iconSize
);
461 const bool enlargingRequired
= scaledPixmap
.width() < contentSize
.width() &&
462 scaledPixmap
.height() < contentSize
.height();
463 if (enlargingRequired
) {
464 QSize frameSize
= scaledPixmap
.size();
465 frameSize
.scale(m_iconSize
, Qt::KeepAspectRatio
);
467 QPixmap
largeFrame(frameSize
);
468 largeFrame
.fill(Qt::transparent
);
470 KPixmapModifier::applyFrame(largeFrame
, frameSize
);
472 QPainter
painter(&largeFrame
);
473 painter
.drawPixmap((largeFrame
.width() - scaledPixmap
.width()) / 2,
474 (largeFrame
.height() - scaledPixmap
.height()) / 2,
476 scaledPixmap
= largeFrame
;
478 // The image must be shrinked as it is too large to fit into
479 // the available icon size
480 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
484 KPixmapModifier::scale(scaledPixmap
, m_iconSize
);
487 QHash
<QByteArray
, QVariant
> data
= rolesData(item
);
488 data
.insert("iconPixmap", scaledPixmap
);
490 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
491 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
492 m_model
->setData(index
, data
);
493 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
494 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
496 applySortProgressToModel();
499 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem
& item
)
501 m_pendingVisibleItems
.remove(item
);
502 m_pendingInvisibleItems
.remove(item
);
504 const bool clearPreviews
= m_clearPreviews
;
505 m_clearPreviews
= true;
506 applyResolvedRoles(item
, ResolveAll
);
507 m_clearPreviews
= clearPreviews
;
509 applySortProgressToModel();
512 void KFileItemModelRolesUpdater::slotPreviewJobFinished(KJob
* job
)
514 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
515 kDebug() << "Preview job finished. Pending visible:" << m_pendingVisibleItems
.count() << "invisible:" << m_pendingInvisibleItems
.count();
518 m_previewJobs
.removeOne(job
);
519 if (!m_previewJobs
.isEmpty() || !hasPendingRoles()) {
523 const KFileItemList visibleItems
= sortedItems(m_pendingVisibleItems
);
524 startPreviewJob(visibleItems
+ m_pendingInvisibleItems
.toList());
527 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
533 if (m_previewShown
) {
534 // The preview has been turned on since the last run. Skip
535 // resolving further pending roles as this is done as soon
536 // as a preview has been received.
540 int resolvedCount
= 0;
541 bool changed
= false;
542 for (int i
= 0; i
<= 1; ++i
) {
543 QSet
<KFileItem
>& pendingItems
= (i
== 0) ? m_pendingVisibleItems
: m_pendingInvisibleItems
;
544 QSet
<KFileItem
>::iterator it
= pendingItems
.begin();
545 while (it
!= pendingItems
.end() && !changed
&& resolvedCount
< MaxResolveItemsCount
) {
546 changed
= applyResolvedRoles(*it
, ResolveAll
);
547 it
= pendingItems
.erase(it
);
552 if (hasPendingRoles()) {
553 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
555 m_clearPreviews
= false;
558 applySortProgressToModel();
560 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
561 static int callCount
= 0;
563 if (callCount
% 100 == 0) {
564 kDebug() << "Remaining visible roles to resolve:" << m_pendingVisibleItems
.count()
565 << "invisible:" << m_pendingInvisibleItems
.count();
570 void KFileItemModelRolesUpdater::resolveChangedItems()
572 if (m_changedItems
.isEmpty()) {
576 KItemRangeList itemRanges
;
578 QSetIterator
<KFileItem
> it(m_changedItems
);
579 while (it
.hasNext()) {
580 const KFileItem
& item
= it
.next();
581 const int index
= m_model
->index(item
);
583 itemRanges
.append(KItemRange(index
, 1));
586 m_changedItems
.clear();
588 startUpdating(itemRanges
);
591 void KFileItemModelRolesUpdater::applyChangedNepomukRoles(const Nepomuk2::Resource
& resource
)
594 const KUrl itemUrl
= m_nepomukUriItems
.value(resource
.uri());
595 const KFileItem item
= m_model
->fileItem(itemUrl
);
598 // itemUrl is not in the model anymore, probably because
599 // the corresponding file has been deleted in the meantime.
603 QHash
<QByteArray
, QVariant
> data
= rolesData(item
);
605 const KNepomukRolesProvider
& rolesProvider
= KNepomukRolesProvider::instance();
606 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(resource
, m_roles
));
607 while (it
.hasNext()) {
609 data
.insert(it
.key(), it
.value());
612 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
613 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
614 const int index
= m_model
->index(item
);
615 m_model
->setData(index
, data
);
616 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
617 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
625 void KFileItemModelRolesUpdater::slotDirWatchDirty(const QString
& path
)
627 const bool getSizeRole
= m_roles
.contains("size");
628 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
630 if (getSizeRole
|| getIsExpandableRole
) {
631 const int index
= m_model
->index(KUrl(path
));
633 if (!m_model
->fileItem(index
).isDir()) {
634 // If INotify is used, KDirWatch issues the dirty() signal
635 // also for changed files inside the directory, even if we
636 // don't enable this behavior explicitly (see bug 309740).
640 QHash
<QByteArray
, QVariant
> data
;
642 const int count
= subItemsCount(path
);
644 data
.insert("size", count
);
646 if (getIsExpandableRole
) {
647 data
.insert("isExpandable", count
> 0);
650 m_model
->setData(index
, data
);
655 void KFileItemModelRolesUpdater::startUpdating(const KItemRangeList
& itemRanges
)
657 // If no valid index range is given assume that all items are visible.
658 // A cleanup will be done later as soon as the index range has been set.
659 const bool hasValidIndexRange
= (m_lastVisibleIndex
>= 0);
661 if (hasValidIndexRange
) {
662 // Move all current pending visible items that are not visible anymore
663 // to the pending invisible items.
664 QSet
<KFileItem
>::iterator it
= m_pendingVisibleItems
.begin();
665 while (it
!= m_pendingVisibleItems
.end()) {
666 const KFileItem item
= *it
;
667 const int index
= m_model
->index(item
);
668 if (index
< m_firstVisibleIndex
|| index
> m_lastVisibleIndex
) {
669 it
= m_pendingVisibleItems
.erase(it
);
670 m_pendingInvisibleItems
.insert(item
);
679 foreach (const KItemRange
& range
, itemRanges
) {
680 rangesCount
+= range
.count
;
682 // Add the inserted items to the pending visible and invisible items
683 const int lastIndex
= range
.index
+ range
.count
- 1;
684 for (int i
= range
.index
; i
<= lastIndex
; ++i
) {
685 const KFileItem item
= m_model
->fileItem(i
);
687 if (hasValidIndexRange
) {
688 visible
= (i
>= m_firstVisibleIndex
&& i
<= m_lastVisibleIndex
);
690 // If the view has not informed us about the visible range yet,
691 // just assume that the first items are visible.
692 visible
= (i
< m_maximumVisibleItems
);
696 m_pendingVisibleItems
.insert(item
);
698 m_pendingInvisibleItems
.insert(item
);
703 resolvePendingRoles();
706 void KFileItemModelRolesUpdater::startPreviewJob(const KFileItemList
& items
)
708 if (items
.isEmpty() || m_paused
) {
712 // PreviewJob internally caches items always with the size of
713 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
714 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
715 // do a downscaling anyhow because of the frame, so in this case only the provided
716 // cache sizes are requested.
717 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128)
718 ? QSize(256, 256) : QSize(128, 128);
720 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
721 // worst case) might block the application for several seconds. To prevent such
722 // a blocking the MIME-type of the items will determined until the MaxBlockTimeout
723 // has been reached and only those items will get passed. As soon as the MIME-type
724 // has been resolved once KIO::PreviewJob() can already access the resolved
725 // MIME-type in a fast way.
729 KFileItemList itemSubSet
;
730 const int count
= items
.count();
731 itemSubSet
.reserve(count
);
732 for (int i
= 0; i
< count
; ++i
) {
733 KFileItem item
= items
.at(i
);
734 item
.determineMimeType();
735 itemSubSet
.append(item
);
736 if (timer
.elapsed() > MaxBlockTimeout
) {
737 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
738 kDebug() << "Maximum time of" << MaxBlockTimeout
<< "ms exceeded, creating only previews for"
739 << (i
+ 1) << "items," << (items
.count() - (i
+ 1)) << "will be resolved later";
744 KIO::PreviewJob
* job
= new KIO::PreviewJob(itemSubSet
, cacheSize
, &m_enabledPlugins
);
745 job
->setIgnoreMaximumSize(items
.first().isLocalFile());
747 job
->ui()->setWindow(qApp
->activeWindow());
750 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
751 this, SLOT(slotGotPreview(KFileItem
,QPixmap
)));
752 connect(job
, SIGNAL(failed(KFileItem
)),
753 this, SLOT(slotPreviewFailed(KFileItem
)));
754 connect(job
, SIGNAL(finished(KJob
*)),
755 this, SLOT(slotPreviewJobFinished(KJob
*)));
757 m_previewJobs
.append(job
);
761 bool KFileItemModelRolesUpdater::hasPendingRoles() const
763 return !m_pendingVisibleItems
.isEmpty() || !m_pendingInvisibleItems
.isEmpty();
766 void KFileItemModelRolesUpdater::resolvePendingRoles()
768 int resolvedCount
= 0;
770 bool hasSlowRoles
= m_previewShown
;
772 QSetIterator
<QByteArray
> it(m_roles
);
773 while (it
.hasNext()) {
774 if (m_resolvableRoles
.contains(it
.next())) {
781 const ResolveHint resolveHint
= hasSlowRoles
? ResolveFast
: ResolveAll
;
783 // Resolving the MIME type can be expensive. Assure that not more than MaxBlockTimeout ms are
784 // spend for resolving them synchronously. Usually this is more than enough to determine
785 // all visible items, but there are corner cases where this limit gets easily exceeded.
789 // Resolve the MIME type of all visible items
790 QSet
<KFileItem
>::iterator visibleIt
= m_pendingVisibleItems
.begin();
791 while (visibleIt
!= m_pendingVisibleItems
.end()) {
792 const KFileItem item
= *visibleIt
;
794 Q_ASSERT(!m_pendingInvisibleItems
.contains(item
));
795 // All roles will be resolved by applyResolvedRoles()
796 visibleIt
= m_pendingVisibleItems
.erase(visibleIt
);
800 applyResolvedRoles(item
, resolveHint
);
803 if (timer
.elapsed() > MaxBlockTimeout
) {
808 // Resolve the MIME type of the invisible items at least until the timeout
809 // has been exceeded or the maximum number of items has been reached
810 KFileItemList invisibleItems
;
811 if (m_lastVisibleIndex
>= 0) {
812 // The visible range is valid, don't care about the order how the MIME
813 // type of invisible items get resolved
814 invisibleItems
= m_pendingInvisibleItems
.toList();
816 // The visible range is temporary invalid (e.g. happens when loading
817 // a directory) so take care to sort the currently invisible items where
818 // a part will get visible later
819 invisibleItems
= sortedItems(m_pendingInvisibleItems
);
823 while (resolvedCount
< MaxResolveItemsCount
&& index
< invisibleItems
.count() && timer
.elapsed() <= MaxBlockTimeout
) {
824 const KFileItem item
= invisibleItems
.at(index
);
825 applyResolvedRoles(item
, resolveHint
);
828 // All roles have been resolved already by applyResolvedRoles()
829 m_pendingInvisibleItems
.remove(item
);
835 if (m_previewShown
) {
836 KFileItemList items
= sortedItems(m_pendingVisibleItems
);
837 items
+= invisibleItems
;
838 startPreviewJob(items
);
840 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
843 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
844 if (timer
.elapsed() > MaxBlockTimeout
) {
845 kDebug() << "Maximum time of" << MaxBlockTimeout
846 << "ms exceeded, skipping items... Remaining visible:" << m_pendingVisibleItems
.count()
847 << "invisible:" << m_pendingInvisibleItems
.count();
849 kDebug() << "[TIME] Resolved pending roles:" << timer
.elapsed();
852 applySortProgressToModel();
855 void KFileItemModelRolesUpdater::resetPendingRoles()
857 m_pendingVisibleItems
.clear();
858 m_pendingInvisibleItems
.clear();
860 foreach (KJob
* job
, m_previewJobs
) {
863 Q_ASSERT(m_previewJobs
.isEmpty());
866 void KFileItemModelRolesUpdater::sortAndResolveAllRoles()
873 Q_ASSERT(m_pendingVisibleItems
.isEmpty());
874 Q_ASSERT(m_pendingInvisibleItems
.isEmpty());
876 if (m_model
->count() == 0) {
880 // Determine all visible items
881 Q_ASSERT(m_firstVisibleIndex
>= 0);
882 for (int i
= m_firstVisibleIndex
; i
<= m_lastVisibleIndex
; ++i
) {
883 const KFileItem item
= m_model
->fileItem(i
);
884 if (!item
.isNull()) {
885 m_pendingVisibleItems
.insert(item
);
889 // Determine all invisible items
890 for (int i
= 0; i
< m_firstVisibleIndex
; ++i
) {
891 const KFileItem item
= m_model
->fileItem(i
);
892 if (!item
.isNull()) {
893 m_pendingInvisibleItems
.insert(item
);
896 const int count
= m_model
->count();
897 for (int i
= m_lastVisibleIndex
+ 1; i
< count
; ++i
) {
898 const KFileItem item
= m_model
->fileItem(i
);
899 if (!item
.isNull()) {
900 m_pendingInvisibleItems
.insert(item
);
904 resolvePendingRoles();
907 void KFileItemModelRolesUpdater::sortAndResolvePendingRoles()
910 if (m_model
->count() == 0) {
914 // If no valid index range is given assume that all items are visible.
915 // A cleanup will be done later as soon as the index range has been set.
916 const bool hasValidIndexRange
= (m_lastVisibleIndex
>= 0);
918 // Trigger a preview generation of all pending items. Assure that the visible
919 // pending items get generated first.
921 // Step 1: Check if any items in m_pendingVisibleItems are not visible any more
922 // and move them to m_pendingInvisibleItems.
923 QSet
<KFileItem
>::iterator itVisible
= m_pendingVisibleItems
.begin();
924 while (itVisible
!= m_pendingVisibleItems
.end()) {
925 const KFileItem item
= *itVisible
;
927 itVisible
= m_pendingVisibleItems
.erase(itVisible
);
931 const int index
= m_model
->index(item
);
932 if (!hasValidIndexRange
|| (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
)) {
935 itVisible
= m_pendingVisibleItems
.erase(itVisible
);
936 m_pendingInvisibleItems
.insert(item
);
940 // Step 2: Check if any items in m_pendingInvisibleItems have become visible
941 // and move them to m_pendingVisibleItems.
942 QSet
<KFileItem
>::iterator itInvisible
= m_pendingInvisibleItems
.begin();
943 while (itInvisible
!= m_pendingInvisibleItems
.end()) {
944 const KFileItem item
= *itInvisible
;
946 itInvisible
= m_pendingInvisibleItems
.erase(itInvisible
);
950 const int index
= m_model
->index(item
);
951 if (!hasValidIndexRange
|| (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
)) {
952 itInvisible
= m_pendingInvisibleItems
.erase(itInvisible
);
953 m_pendingVisibleItems
.insert(item
);
959 resolvePendingRoles();
962 void KFileItemModelRolesUpdater::applySortProgressToModel()
964 if (m_sortingProgress
< 0) {
968 // Inform the model about the progress of the resolved items,
969 // so that it can give an indication when the sorting has been finished.
970 const int resolvedCount
= m_model
->count()
971 - m_pendingVisibleItems
.count()
972 - m_pendingInvisibleItems
.count();
973 if (resolvedCount
> 0) {
974 m_model
->emitSortProgress(resolvedCount
);
975 if (resolvedCount
== m_model
->count()) {
976 m_sortingProgress
= -1;
981 void KFileItemModelRolesUpdater::updateSortProgress()
983 const QByteArray sortRole
= m_model
->sortRole();
985 // Optimization if the sorting is done by type: In case if all MIME-types
986 // are known, the types have been resolved already by KFileItemModel and
987 // no sort-progress feedback is required.
988 const bool showProgress
= (sortRole
== "type")
989 ? hasUnknownMimeTypes()
990 : m_resolvableRoles
.contains(sortRole
);
992 if (m_sortingProgress
>= 0) {
993 // Mark the current sorting as finished
994 m_model
->emitSortProgress(m_model
->count());
996 m_sortingProgress
= showProgress
? 0 : -1;
999 bool KFileItemModelRolesUpdater::hasUnknownMimeTypes() const
1001 const int count
= m_model
->count();
1002 for (int i
= 0; i
< count
; ++i
) {
1003 const KFileItem item
= m_model
->fileItem(i
);
1004 if (!item
.isMimeTypeKnown()) {
1012 bool KFileItemModelRolesUpdater::applyResolvedRoles(const KFileItem
& item
, ResolveHint hint
)
1014 if (item
.isNull()) {
1018 const bool resolveAll
= (hint
== ResolveAll
);
1020 bool mimeTypeChanged
= false;
1021 if (!item
.isMimeTypeKnown() || !item
.isFinalIconKnown()) {
1022 item
.determineMimeType();
1023 mimeTypeChanged
= true;
1026 if (mimeTypeChanged
|| resolveAll
|| m_clearPreviews
) {
1027 const int index
= m_model
->index(item
);
1032 QHash
<QByteArray
, QVariant
> data
;
1034 data
= rolesData(item
);
1037 data
.insert("iconName", item
.iconName());
1039 if (m_clearPreviews
) {
1040 data
.insert("iconPixmap", QPixmap());
1043 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1044 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1045 m_model
->setData(index
, data
);
1046 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1047 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1054 QHash
<QByteArray
, QVariant
> KFileItemModelRolesUpdater::rolesData(const KFileItem
& item
) const
1056 QHash
<QByteArray
, QVariant
> data
;
1058 const bool getSizeRole
= m_roles
.contains("size");
1059 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
1061 if ((getSizeRole
|| getIsExpandableRole
) && item
.isDir()) {
1062 if (item
.isLocalFile()) {
1063 const QString path
= item
.localPath();
1064 const int count
= subItemsCount(path
);
1066 data
.insert("size", count
);
1068 if (getIsExpandableRole
) {
1069 data
.insert("isExpandable", count
> 0);
1072 if (!m_dirWatcher
->contains(path
)) {
1073 m_dirWatcher
->addDir(path
);
1074 m_watchedDirs
.insert(path
);
1076 } else if (getSizeRole
) {
1077 data
.insert("size", -1); // -1 indicates an unknown number of items
1081 if (m_roles
.contains("type")) {
1082 data
.insert("type", item
.mimeComment());
1085 data
.insert("iconOverlays", item
.overlays());
1088 if (m_nepomukResourceWatcher
) {
1089 const KNepomukRolesProvider
& rolesProvider
= KNepomukRolesProvider::instance();
1090 Nepomuk2::Resource
resource(item
.nepomukUri());
1091 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(resource
, m_roles
));
1092 while (it
.hasNext()) {
1094 data
.insert(it
.key(), it
.value());
1097 QUrl uri
= resource
.uri();
1098 if (uri
.isEmpty()) {
1099 // TODO: Is there another way to explicitly create a resource?
1100 // We need a resource to be able to track it for changes.
1101 resource
.setRating(0);
1102 uri
= resource
.uri();
1104 if (!uri
.isEmpty() && !m_nepomukUriItems
.contains(uri
)) {
1105 m_nepomukResourceWatcher
->addResource(resource
);
1107 if (m_nepomukUriItems
.isEmpty()) {
1108 m_nepomukResourceWatcher
->start();
1111 m_nepomukUriItems
.insert(uri
, item
.url());
1119 KFileItemList
KFileItemModelRolesUpdater::sortedItems(const QSet
<KFileItem
>& items
) const
1121 KFileItemList itemList
;
1122 if (items
.isEmpty()) {
1126 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
1127 QElapsedTimer timer
;
1132 indexes
.reserve(items
.count());
1134 QSetIterator
<KFileItem
> it(items
);
1135 while (it
.hasNext()) {
1136 const KFileItem item
= it
.next();
1137 const int index
= m_model
->index(item
);
1139 indexes
.append(index
);
1144 itemList
.reserve(items
.count());
1145 foreach (int index
, indexes
) {
1146 itemList
.append(m_model
->fileItem(index
));
1149 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
1150 kDebug() << "[TIME] Sorting of items:" << timer
.elapsed();
1155 int KFileItemModelRolesUpdater::subItemsCount(const QString
& path
) const
1157 const bool countHiddenFiles
= m_model
->showHiddenFiles();
1158 const bool showFoldersOnly
= m_model
->showDirectoriesOnly();
1162 QDir::Filters filters
= QDir::NoDotAndDotDot
| QDir::System
;
1163 if (countHiddenFiles
) {
1164 filters
|= QDir::Hidden
;
1166 if (showFoldersOnly
) {
1167 filters
|= QDir::Dirs
;
1169 filters
|= QDir::AllEntries
;
1171 return dir
.entryList(filters
).count();
1173 // Taken from kdelibs/kio/kio/kdirmodel.cpp
1174 // Copyright (C) 2006 David Faure <faure@kde.org>
1177 DIR* dir
= ::opendir(QFile::encodeName(path
));
1178 if (dir
) { // krazy:exclude=syscalls
1180 struct dirent
*dirEntry
= 0;
1181 while ((dirEntry
= ::readdir(dir
))) {
1182 if (dirEntry
->d_name
[0] == '.') {
1183 if (dirEntry
->d_name
[1] == '\0' || !countHiddenFiles
) {
1184 // Skip "." or hidden files
1187 if (dirEntry
->d_name
[1] == '.' && dirEntry
->d_name
[2] == '\0') {
1193 // If only directories are counted, consider an unknown file type and links also
1194 // as directory instead of trying to do an expensive stat()
1195 // (see bugs 292642 and 299997).
1196 const bool countEntry
= !showFoldersOnly
||
1197 dirEntry
->d_type
== DT_DIR
||
1198 dirEntry
->d_type
== DT_LNK
||
1199 dirEntry
->d_type
== DT_UNKNOWN
;
1210 void KFileItemModelRolesUpdater::updateAllPreviews()
1213 m_previewChangedDuringPausing
= true;
1215 sortAndResolveAllRoles();
1219 #include "kfileitemmodelrolesupdater.moc"