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),
84 m_pendingVisibleItems(),
85 m_pendingInvisibleItems(),
87 m_changedItemsTimer(0),
92 , m_nepomukResourceWatcher(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(sortRoleChanged(QByteArray
,QByteArray
)),
111 this, SLOT(slotSortRoleChanged(QByteArray
,QByteArray
)));
113 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
114 // resolving of the roles. Postpone the resolving until no update has been done for 1 second.
115 m_changedItemsTimer
= new QTimer(this);
116 m_changedItemsTimer
->setInterval(1000);
117 m_changedItemsTimer
->setSingleShot(true);
118 connect(m_changedItemsTimer
, SIGNAL(timeout()), this, SLOT(resolveChangedItems()));
120 m_resolvableRoles
.insert("size");
121 m_resolvableRoles
.insert("type");
122 m_resolvableRoles
.insert("isExpandable");
124 m_resolvableRoles
+= KNepomukRolesProvider::instance().roles();
127 // When folders are expandable or the item-count is shown for folders, it is necessary
128 // to watch the number of items of the sub-folder to be able to react on changes.
129 m_dirWatcher
= new KDirWatch(this);
130 connect(m_dirWatcher
, SIGNAL(dirty(QString
)), this, SLOT(slotDirWatchDirty(QString
)));
133 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
138 void KFileItemModelRolesUpdater::setIconSize(const QSize
& size
)
140 if (size
!= m_iconSize
) {
143 m_iconSizeChangedDuringPausing
= true;
144 } else if (m_previewShown
) {
145 // An icon size change requires the regenerating of
147 sortAndResolveAllRoles();
149 sortAndResolvePendingRoles();
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);
176 if (hasPendingRoles() && !m_paused
) {
177 sortAndResolvePendingRoles();
181 void KFileItemModelRolesUpdater::setPreviewsShown(bool show
)
183 if (show
== m_previewShown
) {
187 m_previewShown
= show
;
189 m_clearPreviews
= true;
195 bool KFileItemModelRolesUpdater::previewsShown() const
197 return m_previewShown
;
200 void KFileItemModelRolesUpdater::setEnlargeSmallPreviews(bool enlarge
)
202 if (enlarge
!= m_enlargeSmallPreviews
) {
203 m_enlargeSmallPreviews
= enlarge
;
204 if (m_previewShown
) {
210 bool KFileItemModelRolesUpdater::enlargeSmallPreviews() const
212 return m_enlargeSmallPreviews
;
215 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList
& list
)
217 if (m_enabledPlugins
!= list
) {
218 m_enabledPlugins
= list
;
219 if (m_previewShown
) {
225 void KFileItemModelRolesUpdater::setPaused(bool paused
)
227 if (paused
== m_paused
) {
233 if (hasPendingRoles()) {
234 foreach (KJob
* job
, m_previewJobs
) {
237 Q_ASSERT(m_previewJobs
.isEmpty());
240 const bool resolveAll
= (m_iconSizeChangedDuringPausing
&& m_previewShown
) ||
241 m_previewChangedDuringPausing
||
242 m_rolesChangedDuringPausing
;
244 sortAndResolveAllRoles();
246 sortAndResolvePendingRoles();
249 m_iconSizeChangedDuringPausing
= false;
250 m_previewChangedDuringPausing
= false;
251 m_rolesChangedDuringPausing
= false;
255 void KFileItemModelRolesUpdater::setRoles(const QSet
<QByteArray
>& roles
)
257 if (m_roles
!= roles
) {
261 // Check whether there is at least one role that must be resolved
262 // with the help of Nepomuk. If this is the case, a (quite expensive)
263 // resolving will be done in KFileItemModelRolesUpdater::rolesData() and
264 // the role gets watched for changes.
265 const KNepomukRolesProvider
& rolesProvider
= KNepomukRolesProvider::instance();
266 bool hasNepomukRole
= false;
267 QSetIterator
<QByteArray
> it(roles
);
268 while (it
.hasNext()) {
269 const QByteArray
& role
= it
.next();
270 if (rolesProvider
.roles().contains(role
)) {
271 hasNepomukRole
= true;
276 if (hasNepomukRole
&& !m_nepomukResourceWatcher
) {
277 Q_ASSERT(m_nepomukUriItems
.isEmpty());
279 m_nepomukResourceWatcher
= new Nepomuk2::ResourceWatcher(this);
280 connect(m_nepomukResourceWatcher
, SIGNAL(propertyChanged(Nepomuk2::Resource
,Nepomuk2::Types::Property
,QVariantList
,QVariantList
)),
281 this, SLOT(applyChangedNepomukRoles(Nepomuk2::Resource
)));
282 } else if (!hasNepomukRole
&& m_nepomukResourceWatcher
) {
283 delete m_nepomukResourceWatcher
;
284 m_nepomukResourceWatcher
= 0;
285 m_nepomukUriItems
.clear();
289 updateSortProgress();
292 m_rolesChangedDuringPausing
= true;
294 sortAndResolveAllRoles();
299 QSet
<QByteArray
> KFileItemModelRolesUpdater::roles() const
304 bool KFileItemModelRolesUpdater::isPaused() const
309 QStringList
KFileItemModelRolesUpdater::enabledPlugins() const
311 return m_enabledPlugins
;
314 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList
& itemRanges
)
316 startUpdating(itemRanges
);
319 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList
& itemRanges
)
321 Q_UNUSED(itemRanges
);
323 const bool allItemsRemoved
= (m_model
->count() == 0);
325 if (!m_watchedDirs
.isEmpty()) {
326 // Don't let KDirWatch watch for removed items
327 if (allItemsRemoved
) {
328 foreach (const QString
& path
, m_watchedDirs
) {
329 m_dirWatcher
->removeDir(path
);
331 m_watchedDirs
.clear();
333 QMutableSetIterator
<QString
> it(m_watchedDirs
);
334 while (it
.hasNext()) {
335 const QString
& path
= it
.next();
336 if (m_model
->index(KUrl(path
)) < 0) {
337 m_dirWatcher
->removeDir(path
);
345 if (m_nepomukResourceWatcher
) {
346 // Don't let the ResourceWatcher watch for removed items
347 if (allItemsRemoved
) {
348 m_nepomukResourceWatcher
->setResources(QList
<Nepomuk2::Resource
>());
349 m_nepomukResourceWatcher
->stop();
350 m_nepomukUriItems
.clear();
352 QList
<Nepomuk2::Resource
> newResources
;
353 const QList
<Nepomuk2::Resource
> oldResources
= m_nepomukResourceWatcher
->resources();
354 foreach (const Nepomuk2::Resource
& resource
, oldResources
) {
355 const QUrl uri
= resource
.uri();
356 const KUrl itemUrl
= m_nepomukUriItems
.value(uri
);
357 if (m_model
->index(itemUrl
) >= 0) {
358 newResources
.append(resource
);
360 m_nepomukUriItems
.remove(uri
);
363 m_nepomukResourceWatcher
->setResources(newResources
);
364 if (newResources
.isEmpty()) {
365 Q_ASSERT(m_nepomukUriItems
.isEmpty());
366 m_nepomukResourceWatcher
->stop();
372 m_firstVisibleIndex
= 0;
373 m_lastVisibleIndex
= -1;
374 if (!hasPendingRoles()) {
378 if (allItemsRemoved
) {
379 // Most probably a directory change is done. Clear all pending items
380 // and also kill all ongoing preview-jobs.
383 m_changedItems
.clear();
384 m_changedItemsTimer
->stop();
386 // Remove all items from m_pendingVisibleItems and m_pendingInvisibleItems
387 // that are not part of the model anymore. The items from m_changedItems
388 // don't need to be handled here, removed items are just skipped in
389 // resolveChangedItems().
390 for (int i
= 0; i
<= 1; ++i
) {
391 QSet
<KFileItem
>& pendingItems
= (i
== 0) ? m_pendingVisibleItems
: m_pendingInvisibleItems
;
392 QMutableSetIterator
<KFileItem
> it(pendingItems
);
393 while (it
.hasNext()) {
394 const KFileItem item
= it
.next();
395 if (m_model
->index(item
) < 0) {
396 pendingItems
.remove(item
);
403 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList
& itemRanges
,
404 const QSet
<QByteArray
>& roles
)
408 if (m_changedItemsTimer
->isActive()) {
409 // A call of slotItemsChanged() has been done recently. Postpone the resolving
410 // of the roles until the timer has exceeded.
411 foreach (const KItemRange
& itemRange
, itemRanges
) {
412 int index
= itemRange
.index
;
413 for (int count
= itemRange
.count
; count
> 0; --count
) {
414 m_changedItems
.insert(m_model
->fileItem(index
));
419 // No call of slotItemsChanged() has been done recently, resolve the roles now.
420 startUpdating(itemRanges
);
422 m_changedItemsTimer
->start();
425 void KFileItemModelRolesUpdater::slotSortRoleChanged(const QByteArray
& current
,
426 const QByteArray
& previous
)
430 updateSortProgress();
433 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
435 m_pendingVisibleItems
.remove(item
);
436 m_pendingInvisibleItems
.remove(item
);
438 const int index
= m_model
->index(item
);
443 QPixmap scaledPixmap
= pixmap
;
445 const QString mimeType
= item
.mimetype();
446 const int slashIndex
= mimeType
.indexOf(QLatin1Char('/'));
447 const QString mimeTypeGroup
= mimeType
.left(slashIndex
);
448 if (mimeTypeGroup
== QLatin1String("image")) {
449 if (m_enlargeSmallPreviews
) {
450 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
452 // Assure that small previews don't get enlarged. Instead they
453 // should be shown centered within the frame.
454 const QSize contentSize
= KPixmapModifier::sizeInsideFrame(m_iconSize
);
455 const bool enlargingRequired
= scaledPixmap
.width() < contentSize
.width() &&
456 scaledPixmap
.height() < contentSize
.height();
457 if (enlargingRequired
) {
458 QSize frameSize
= scaledPixmap
.size();
459 frameSize
.scale(m_iconSize
, Qt::KeepAspectRatio
);
461 QPixmap
largeFrame(frameSize
);
462 largeFrame
.fill(Qt::transparent
);
464 KPixmapModifier::applyFrame(largeFrame
, frameSize
);
466 QPainter
painter(&largeFrame
);
467 painter
.drawPixmap((largeFrame
.width() - scaledPixmap
.width()) / 2,
468 (largeFrame
.height() - scaledPixmap
.height()) / 2,
470 scaledPixmap
= largeFrame
;
472 // The image must be shrinked as it is too large to fit into
473 // the available icon size
474 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
478 KPixmapModifier::scale(scaledPixmap
, m_iconSize
);
481 QHash
<QByteArray
, QVariant
> data
= rolesData(item
);
482 data
.insert("iconPixmap", scaledPixmap
);
484 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
485 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
486 m_model
->setData(index
, data
);
487 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
488 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
490 applySortProgressToModel();
493 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem
& item
)
495 m_pendingVisibleItems
.remove(item
);
496 m_pendingInvisibleItems
.remove(item
);
498 const bool clearPreviews
= m_clearPreviews
;
499 m_clearPreviews
= true;
500 applyResolvedRoles(item
, ResolveAll
);
501 m_clearPreviews
= clearPreviews
;
503 applySortProgressToModel();
506 void KFileItemModelRolesUpdater::slotPreviewJobFinished(KJob
* job
)
508 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
509 kDebug() << "Preview job finished. Pending visible:" << m_pendingVisibleItems
.count() << "invisible:" << m_pendingInvisibleItems
.count();
512 m_previewJobs
.removeOne(job
);
513 if (!m_previewJobs
.isEmpty() || !hasPendingRoles()) {
517 const KFileItemList visibleItems
= sortedItems(m_pendingVisibleItems
);
518 startPreviewJob(visibleItems
+ m_pendingInvisibleItems
.toList());
521 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
527 if (m_previewShown
) {
528 // The preview has been turned on since the last run. Skip
529 // resolving further pending roles as this is done as soon
530 // as a preview has been received.
534 int resolvedCount
= 0;
535 bool changed
= false;
536 for (int i
= 0; i
<= 1; ++i
) {
537 QSet
<KFileItem
>& pendingItems
= (i
== 0) ? m_pendingVisibleItems
: m_pendingInvisibleItems
;
538 QSet
<KFileItem
>::iterator it
= pendingItems
.begin();
539 while (it
!= pendingItems
.end() && !changed
&& resolvedCount
< MaxResolveItemsCount
) {
540 changed
= applyResolvedRoles(*it
, ResolveAll
);
541 it
= pendingItems
.erase(it
);
546 if (hasPendingRoles()) {
547 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
549 m_clearPreviews
= false;
552 applySortProgressToModel();
554 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
555 static int callCount
= 0;
557 if (callCount
% 100 == 0) {
558 kDebug() << "Remaining visible roles to resolve:" << m_pendingVisibleItems
.count()
559 << "invisible:" << m_pendingInvisibleItems
.count();
564 void KFileItemModelRolesUpdater::resolveChangedItems()
566 if (m_changedItems
.isEmpty()) {
570 KItemRangeList itemRanges
;
572 QSetIterator
<KFileItem
> it(m_changedItems
);
573 while (it
.hasNext()) {
574 const KFileItem
& item
= it
.next();
575 const int index
= m_model
->index(item
);
577 itemRanges
.append(KItemRange(index
, 1));
580 m_changedItems
.clear();
582 startUpdating(itemRanges
);
585 void KFileItemModelRolesUpdater::applyChangedNepomukRoles(const Nepomuk2::Resource
& resource
)
588 const KUrl itemUrl
= m_nepomukUriItems
.value(resource
.uri());
589 const KFileItem item
= m_model
->fileItem(itemUrl
);
592 // itemUrl is not in the model anymore, probably because
593 // the corresponding file has been deleted in the meantime.
597 QHash
<QByteArray
, QVariant
> data
= rolesData(item
);
599 const KNepomukRolesProvider
& rolesProvider
= KNepomukRolesProvider::instance();
600 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(resource
, m_roles
));
601 while (it
.hasNext()) {
603 data
.insert(it
.key(), it
.value());
606 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
607 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
608 const int index
= m_model
->index(item
);
609 m_model
->setData(index
, data
);
610 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
611 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
619 void KFileItemModelRolesUpdater::slotDirWatchDirty(const QString
& path
)
621 const bool getSizeRole
= m_roles
.contains("size");
622 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
624 if (getSizeRole
|| getIsExpandableRole
) {
625 const int index
= m_model
->index(KUrl(path
));
627 QHash
<QByteArray
, QVariant
> data
;
629 const int count
= subItemsCount(path
);
631 data
.insert("size", count
);
633 if (getIsExpandableRole
) {
634 data
.insert("isExpandable", count
> 0);
637 m_model
->setData(index
, data
);
642 void KFileItemModelRolesUpdater::startUpdating(const KItemRangeList
& itemRanges
)
644 // If no valid index range is given assume that all items are visible.
645 // A cleanup will be done later as soon as the index range has been set.
646 const bool hasValidIndexRange
= (m_lastVisibleIndex
>= 0);
648 if (hasValidIndexRange
) {
649 // Move all current pending visible items that are not visible anymore
650 // to the pending invisible items.
651 QSet
<KFileItem
>::iterator it
= m_pendingVisibleItems
.begin();
652 while (it
!= m_pendingVisibleItems
.end()) {
653 const KFileItem item
= *it
;
654 const int index
= m_model
->index(item
);
655 if (index
< m_firstVisibleIndex
|| index
> m_lastVisibleIndex
) {
656 it
= m_pendingVisibleItems
.erase(it
);
657 m_pendingInvisibleItems
.insert(item
);
666 foreach (const KItemRange
& range
, itemRanges
) {
667 rangesCount
+= range
.count
;
669 // Add the inserted items to the pending visible and invisible items
670 const int lastIndex
= range
.index
+ range
.count
- 1;
671 for (int i
= range
.index
; i
<= lastIndex
; ++i
) {
672 const KFileItem item
= m_model
->fileItem(i
);
673 if (!hasValidIndexRange
|| (i
>= m_firstVisibleIndex
&& i
<= m_lastVisibleIndex
)) {
674 m_pendingVisibleItems
.insert(item
);
676 m_pendingInvisibleItems
.insert(item
);
681 resolvePendingRoles();
684 void KFileItemModelRolesUpdater::startPreviewJob(const KFileItemList
& items
)
686 if (items
.isEmpty() || m_paused
) {
690 // PreviewJob internally caches items always with the size of
691 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
692 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
693 // do a downscaling anyhow because of the frame, so in this case only the provided
694 // cache sizes are requested.
695 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128)
696 ? QSize(256, 256) : QSize(128, 128);
698 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
699 // worst case) might block the application for several seconds. To prevent such
700 // a blocking the MIME-type of the items will determined until the MaxBlockTimeout
701 // has been reached and only those items will get passed. As soon as the MIME-type
702 // has been resolved once KIO::PreviewJob() can already access the resolved
703 // MIME-type in a fast way.
707 KFileItemList itemSubSet
;
708 const int count
= items
.count();
709 itemSubSet
.reserve(count
);
710 for (int i
= 0; i
< count
; ++i
) {
711 KFileItem item
= items
.at(i
);
712 item
.determineMimeType();
713 itemSubSet
.append(item
);
714 if (timer
.elapsed() > MaxBlockTimeout
) {
715 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
716 kDebug() << "Maximum time of" << MaxBlockTimeout
<< "ms exceeded, creating only previews for"
717 << (i
+ 1) << "items," << (items
.count() - (i
+ 1)) << "will be resolved later";
722 KIO::PreviewJob
* job
= new KIO::PreviewJob(itemSubSet
, cacheSize
, &m_enabledPlugins
);
723 job
->setIgnoreMaximumSize(items
.first().isLocalFile());
725 job
->ui()->setWindow(qApp
->activeWindow());
728 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
729 this, SLOT(slotGotPreview(KFileItem
,QPixmap
)));
730 connect(job
, SIGNAL(failed(KFileItem
)),
731 this, SLOT(slotPreviewFailed(KFileItem
)));
732 connect(job
, SIGNAL(finished(KJob
*)),
733 this, SLOT(slotPreviewJobFinished(KJob
*)));
735 m_previewJobs
.append(job
);
739 bool KFileItemModelRolesUpdater::hasPendingRoles() const
741 return !m_pendingVisibleItems
.isEmpty() || !m_pendingInvisibleItems
.isEmpty();
744 void KFileItemModelRolesUpdater::resolvePendingRoles()
746 int resolvedCount
= 0;
748 bool hasSlowRoles
= m_previewShown
;
750 QSetIterator
<QByteArray
> it(m_roles
);
751 while (it
.hasNext()) {
752 if (m_resolvableRoles
.contains(it
.next())) {
759 const ResolveHint resolveHint
= hasSlowRoles
? ResolveFast
: ResolveAll
;
761 // Resolving the MIME type can be expensive. Assure that not more than MaxBlockTimeout ms are
762 // spend for resolving them synchronously. Usually this is more than enough to determine
763 // all visible items, but there are corner cases where this limit gets easily exceeded.
767 // Resolve the MIME type of all visible items
768 QSet
<KFileItem
>::iterator visibleIt
= m_pendingVisibleItems
.begin();
769 while (visibleIt
!= m_pendingVisibleItems
.end()) {
770 const KFileItem item
= *visibleIt
;
772 Q_ASSERT(!m_pendingInvisibleItems
.contains(item
));
773 // All roles will be resolved by applyResolvedRoles()
774 visibleIt
= m_pendingVisibleItems
.erase(visibleIt
);
778 applyResolvedRoles(item
, resolveHint
);
781 if (timer
.elapsed() > MaxBlockTimeout
) {
786 // Resolve the MIME type of the invisible items at least until the timeout
787 // has been exceeded or the maximum number of items has been reached
788 KFileItemList invisibleItems
;
789 if (m_lastVisibleIndex
>= 0) {
790 // The visible range is valid, don't care about the order how the MIME
791 // type of invisible items get resolved
792 invisibleItems
= m_pendingInvisibleItems
.toList();
794 // The visible range is temporary invalid (e.g. happens when loading
795 // a directory) so take care to sort the currently invisible items where
796 // a part will get visible later
797 invisibleItems
= sortedItems(m_pendingInvisibleItems
);
801 while (resolvedCount
< MaxResolveItemsCount
&& index
< invisibleItems
.count() && timer
.elapsed() <= MaxBlockTimeout
) {
802 const KFileItem item
= invisibleItems
.at(index
);
803 applyResolvedRoles(item
, resolveHint
);
806 // All roles have been resolved already by applyResolvedRoles()
807 m_pendingInvisibleItems
.remove(item
);
813 if (m_previewShown
) {
814 KFileItemList items
= sortedItems(m_pendingVisibleItems
);
815 items
+= invisibleItems
;
816 startPreviewJob(items
);
818 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
821 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
822 if (timer
.elapsed() > MaxBlockTimeout
) {
823 kDebug() << "Maximum time of" << MaxBlockTimeout
824 << "ms exceeded, skipping items... Remaining visible:" << m_pendingVisibleItems
.count()
825 << "invisible:" << m_pendingInvisibleItems
.count();
827 kDebug() << "[TIME] Resolved pending roles:" << timer
.elapsed();
830 applySortProgressToModel();
833 void KFileItemModelRolesUpdater::resetPendingRoles()
835 m_pendingVisibleItems
.clear();
836 m_pendingInvisibleItems
.clear();
838 foreach (KJob
* job
, m_previewJobs
) {
841 Q_ASSERT(m_previewJobs
.isEmpty());
844 void KFileItemModelRolesUpdater::sortAndResolveAllRoles()
851 Q_ASSERT(m_pendingVisibleItems
.isEmpty());
852 Q_ASSERT(m_pendingInvisibleItems
.isEmpty());
854 if (m_model
->count() == 0) {
858 // Determine all visible items
859 Q_ASSERT(m_firstVisibleIndex
>= 0);
860 for (int i
= m_firstVisibleIndex
; i
<= m_lastVisibleIndex
; ++i
) {
861 const KFileItem item
= m_model
->fileItem(i
);
862 if (!item
.isNull()) {
863 m_pendingVisibleItems
.insert(item
);
867 // Determine all invisible items
868 for (int i
= 0; i
< m_firstVisibleIndex
; ++i
) {
869 const KFileItem item
= m_model
->fileItem(i
);
870 if (!item
.isNull()) {
871 m_pendingInvisibleItems
.insert(item
);
874 const int count
= m_model
->count();
875 for (int i
= m_lastVisibleIndex
+ 1; i
< count
; ++i
) {
876 const KFileItem item
= m_model
->fileItem(i
);
877 if (!item
.isNull()) {
878 m_pendingInvisibleItems
.insert(item
);
882 resolvePendingRoles();
885 void KFileItemModelRolesUpdater::sortAndResolvePendingRoles()
888 if (m_model
->count() == 0) {
892 // If no valid index range is given assume that all items are visible.
893 // A cleanup will be done later as soon as the index range has been set.
894 const bool hasValidIndexRange
= (m_lastVisibleIndex
>= 0);
896 // Trigger a preview generation of all pending items. Assure that the visible
897 // pending items get generated first.
899 // Step 1: Check if any items in m_pendingVisibleItems are not visible any more
900 // and move them to m_pendingInvisibleItems.
901 QSet
<KFileItem
>::iterator itVisible
= m_pendingVisibleItems
.begin();
902 while (itVisible
!= m_pendingVisibleItems
.end()) {
903 const KFileItem item
= *itVisible
;
905 itVisible
= m_pendingVisibleItems
.erase(itVisible
);
909 const int index
= m_model
->index(item
);
910 if (!hasValidIndexRange
|| (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
)) {
913 itVisible
= m_pendingVisibleItems
.erase(itVisible
);
914 m_pendingInvisibleItems
.insert(item
);
918 // Step 2: Check if any items in m_pendingInvisibleItems have become visible
919 // and move them to m_pendingVisibleItems.
920 QSet
<KFileItem
>::iterator itInvisible
= m_pendingInvisibleItems
.begin();
921 while (itInvisible
!= m_pendingInvisibleItems
.end()) {
922 const KFileItem item
= *itInvisible
;
924 itInvisible
= m_pendingInvisibleItems
.erase(itInvisible
);
928 const int index
= m_model
->index(item
);
929 if (!hasValidIndexRange
|| (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
)) {
930 itInvisible
= m_pendingInvisibleItems
.erase(itInvisible
);
931 m_pendingVisibleItems
.insert(item
);
937 resolvePendingRoles();
940 void KFileItemModelRolesUpdater::applySortProgressToModel()
942 if (m_sortingProgress
< 0) {
946 // Inform the model about the progress of the resolved items,
947 // so that it can give an indication when the sorting has been finished.
948 const int resolvedCount
= m_model
->count()
949 - m_pendingVisibleItems
.count()
950 - m_pendingInvisibleItems
.count();
951 if (resolvedCount
> 0) {
952 m_model
->emitSortProgress(resolvedCount
);
953 if (resolvedCount
== m_model
->count()) {
954 m_sortingProgress
= -1;
959 void KFileItemModelRolesUpdater::updateSortProgress()
961 const QByteArray sortRole
= m_model
->sortRole();
963 // Optimization if the sorting is done by type: In case if all MIME-types
964 // are known, the types have been resolved already by KFileItemModel and
965 // no sort-progress feedback is required.
966 const bool showProgress
= (sortRole
== "type")
967 ? hasUnknownMimeTypes()
968 : m_resolvableRoles
.contains(sortRole
);
970 if (m_sortingProgress
>= 0) {
971 // Mark the current sorting as finished
972 m_model
->emitSortProgress(m_model
->count());
974 m_sortingProgress
= showProgress
? 0 : -1;
977 bool KFileItemModelRolesUpdater::hasUnknownMimeTypes() const
979 const int count
= m_model
->count();
980 for (int i
= 0; i
< count
; ++i
) {
981 const KFileItem item
= m_model
->fileItem(i
);
982 if (!item
.isMimeTypeKnown()) {
990 bool KFileItemModelRolesUpdater::applyResolvedRoles(const KFileItem
& item
, ResolveHint hint
)
996 const bool resolveAll
= (hint
== ResolveAll
);
998 bool mimeTypeChanged
= false;
999 if (!item
.isMimeTypeKnown()) {
1000 item
.determineMimeType();
1001 mimeTypeChanged
= true;
1004 if (mimeTypeChanged
|| resolveAll
|| m_clearPreviews
) {
1005 const int index
= m_model
->index(item
);
1010 QHash
<QByteArray
, QVariant
> data
;
1012 data
= rolesData(item
);
1015 data
.insert("iconName", item
.iconName());
1017 if (m_clearPreviews
) {
1018 data
.insert("iconPixmap", QPixmap());
1021 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1022 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1023 m_model
->setData(index
, data
);
1024 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
1025 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
1032 QHash
<QByteArray
, QVariant
> KFileItemModelRolesUpdater::rolesData(const KFileItem
& item
) const
1034 QHash
<QByteArray
, QVariant
> data
;
1036 const bool getSizeRole
= m_roles
.contains("size");
1037 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
1039 if ((getSizeRole
|| getIsExpandableRole
) && item
.isDir()) {
1040 if (item
.isLocalFile()) {
1041 const QString path
= item
.localPath();
1042 const int count
= subItemsCount(path
);
1044 data
.insert("size", count
);
1046 if (getIsExpandableRole
) {
1047 data
.insert("isExpandable", count
> 0);
1050 if (!m_dirWatcher
->contains(path
)) {
1051 m_dirWatcher
->addDir(path
);
1052 m_watchedDirs
.insert(path
);
1054 } else if (getSizeRole
) {
1055 data
.insert("size", -1); // -1 indicates an unknown number of items
1059 if (m_roles
.contains("type")) {
1060 data
.insert("type", item
.mimeComment());
1063 data
.insert("iconOverlays", item
.overlays());
1066 if (m_nepomukResourceWatcher
) {
1067 const KNepomukRolesProvider
& rolesProvider
= KNepomukRolesProvider::instance();
1068 Nepomuk2::Resource
resource(item
.nepomukUri());
1069 QHashIterator
<QByteArray
, QVariant
> it(rolesProvider
.roleValues(resource
, m_roles
));
1070 while (it
.hasNext()) {
1072 data
.insert(it
.key(), it
.value());
1075 QUrl uri
= resource
.uri();
1076 if (uri
.isEmpty()) {
1077 // TODO: Is there another way to explicitly create a resource?
1078 // We need a resource to be able to track it for changes.
1079 resource
.setRating(0);
1080 uri
= resource
.uri();
1082 if (!uri
.isEmpty() && !m_nepomukUriItems
.contains(uri
)) {
1083 m_nepomukResourceWatcher
->addResource(resource
);
1085 if (m_nepomukUriItems
.isEmpty()) {
1086 m_nepomukResourceWatcher
->start();
1089 m_nepomukUriItems
.insert(uri
, item
.url());
1097 KFileItemList
KFileItemModelRolesUpdater::sortedItems(const QSet
<KFileItem
>& items
) const
1099 KFileItemList itemList
;
1100 if (items
.isEmpty()) {
1104 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
1105 QElapsedTimer timer
;
1110 indexes
.reserve(items
.count());
1112 QSetIterator
<KFileItem
> it(items
);
1113 while (it
.hasNext()) {
1114 const KFileItem item
= it
.next();
1115 const int index
= m_model
->index(item
);
1117 indexes
.append(index
);
1122 itemList
.reserve(items
.count());
1123 foreach (int index
, indexes
) {
1124 itemList
.append(m_model
->fileItem(index
));
1127 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
1128 kDebug() << "[TIME] Sorting of items:" << timer
.elapsed();
1133 int KFileItemModelRolesUpdater::subItemsCount(const QString
& path
) const
1135 const bool countHiddenFiles
= m_model
->showHiddenFiles();
1136 const bool showFoldersOnly
= m_model
->showDirectoriesOnly();
1140 QDir::Filters filters
= QDir::NoDotAndDotDot
| QDir::System
;
1141 if (countHiddenFiles
) {
1142 filters
|= QDir::Hidden
;
1144 if (showFoldersOnly
) {
1145 filters
|= QDir::Dirs
;
1147 filters
|= QDir::AllEntries
;
1149 return dir
.entryList(filters
).count();
1151 // Taken from kdelibs/kio/kio/kdirmodel.cpp
1152 // Copyright (C) 2006 David Faure <faure@kde.org>
1155 DIR* dir
= ::opendir(QFile::encodeName(path
));
1156 if (dir
) { // krazy:exclude=syscalls
1158 struct dirent
*dirEntry
= 0;
1159 while ((dirEntry
= ::readdir(dir
))) {
1160 if (dirEntry
->d_name
[0] == '.') {
1161 if (dirEntry
->d_name
[1] == '\0' || !countHiddenFiles
) {
1162 // Skip "." or hidden files
1165 if (dirEntry
->d_name
[1] == '.' && dirEntry
->d_name
[2] == '\0') {
1171 // If only directories are counted, consider an unknown file type and links also
1172 // as directory instead of trying to do an expensive stat()
1173 // (see bugs 292642 and 299997).
1174 const bool countEntry
= !showFoldersOnly
||
1175 dirEntry
->d_type
== DT_DIR
||
1176 dirEntry
->d_type
== DT_LNK
||
1177 dirEntry
->d_type
== DT_UNKNOWN
;
1188 void KFileItemModelRolesUpdater::updateAllPreviews()
1191 m_previewChangedDuringPausing
= true;
1193 sortAndResolveAllRoles();
1197 #include "kfileitemmodelrolesupdater.moc"