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"
23 #include "kpixmapmodifier_p.h"
26 #include <KConfigGroup>
30 #include <KIO/PreviewJob>
33 #include <QElapsedTimer>
36 // Required includes for subItemsCount():
44 // #define KFILEITEMMODELROLESUPDATER_DEBUG
47 // Maximum time in ms that the KFileItemModelRolesUpdater
48 // may perform a blocking operation
49 const int MaxBlockTimeout
= 200;
51 // Maximum number of items that will get resolved synchronously.
52 // The value should roughly represent the number of maximum visible
53 // items, as it does not make sense to resolve more items synchronously
54 // and probably reach the MaxBlockTimeout because of invisible items.
55 const int MaxResolveItemsCount
= 100;
58 KFileItemModelRolesUpdater::KFileItemModelRolesUpdater(KFileItemModel
* model
, QObject
* parent
) :
61 m_previewChangedDuringPausing(false),
62 m_iconSizeChangedDuringPausing(false),
63 m_rolesChangedDuringPausing(false),
64 m_previewShown(false),
65 m_clearPreviews(false),
68 m_firstVisibleIndex(0),
69 m_lastVisibleIndex(-1),
72 m_pendingVisibleItems(),
73 m_pendingInvisibleItems(),
75 m_changedItemsTimer(0),
80 const KConfigGroup
globalConfig(KGlobal::config(), "PreviewSettings");
81 m_enabledPlugins
= globalConfig
.readEntry("Plugins", QStringList()
82 << "directorythumbnail"
86 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)),
87 this, SLOT(slotItemsInserted(KItemRangeList
)));
88 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)),
89 this, SLOT(slotItemsRemoved(KItemRangeList
)));
90 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
91 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
93 // Use a timer to prevent that each call of slotItemsChanged() results in a synchronous
94 // resolving of the roles. Postpone the resolving until no update has been done for 2 seconds.
95 m_changedItemsTimer
= new QTimer(this);
96 m_changedItemsTimer
->setInterval(2000);
97 m_changedItemsTimer
->setSingleShot(true);
98 connect(m_changedItemsTimer
, SIGNAL(timeout()), this, SLOT(resolveChangedItems()));
101 KFileItemModelRolesUpdater::~KFileItemModelRolesUpdater()
105 void KFileItemModelRolesUpdater::setIconSize(const QSize
& size
)
107 if (size
!= m_iconSize
) {
110 m_iconSizeChangedDuringPausing
= true;
111 } else if (m_previewShown
) {
112 // An icon size change requires the regenerating of
114 sortAndResolveAllRoles();
116 sortAndResolvePendingRoles();
121 QSize
KFileItemModelRolesUpdater::iconSize() const
126 void KFileItemModelRolesUpdater::setVisibleIndexRange(int index
, int count
)
135 if (index
== m_firstVisibleIndex
&& count
== m_lastVisibleIndex
- m_firstVisibleIndex
+ 1) {
136 // The range has not been changed
140 m_firstVisibleIndex
= index
;
141 m_lastVisibleIndex
= qMin(index
+ count
- 1, m_model
->count() - 1);
143 if (hasPendingRoles() && !m_paused
) {
144 sortAndResolvePendingRoles();
148 void KFileItemModelRolesUpdater::setPreviewShown(bool show
)
150 if (show
== m_previewShown
) {
154 m_previewShown
= show
;
156 m_clearPreviews
= true;
160 m_previewChangedDuringPausing
= true;
162 sortAndResolveAllRoles();
166 bool KFileItemModelRolesUpdater::isPreviewShown() const
168 return m_previewShown
;
171 void KFileItemModelRolesUpdater::setEnabledPlugins(const QStringList
& list
)
173 if (m_enabledPlugins
== list
) {
177 m_enabledPlugins
= list
;
178 if (m_previewShown
) {
180 m_previewChangedDuringPausing
= true;
182 sortAndResolveAllRoles();
187 void KFileItemModelRolesUpdater::setPaused(bool paused
)
189 if (paused
== m_paused
) {
195 if (hasPendingRoles()) {
196 foreach (KJob
* job
, m_previewJobs
) {
199 Q_ASSERT(m_previewJobs
.isEmpty());
202 const bool resolveAll
= (m_iconSizeChangedDuringPausing
&& m_previewShown
) ||
203 m_previewChangedDuringPausing
||
204 m_rolesChangedDuringPausing
;
206 sortAndResolveAllRoles();
208 sortAndResolvePendingRoles();
211 m_iconSizeChangedDuringPausing
= false;
212 m_previewChangedDuringPausing
= false;
213 m_rolesChangedDuringPausing
= false;
217 void KFileItemModelRolesUpdater::setRoles(const QSet
<QByteArray
>& roles
)
219 if (roles
.count() == m_roles
.count()) {
221 foreach (const QByteArray
& role
, roles
) {
222 if (!m_roles
.contains(role
)) {
235 m_rolesChangedDuringPausing
= true;
237 sortAndResolveAllRoles();
241 QSet
<QByteArray
> KFileItemModelRolesUpdater::roles() const
246 bool KFileItemModelRolesUpdater::isPaused() const
251 QStringList
KFileItemModelRolesUpdater::enabledPlugins() const
253 return m_enabledPlugins
;
256 void KFileItemModelRolesUpdater::slotItemsInserted(const KItemRangeList
& itemRanges
)
258 startUpdating(itemRanges
);
261 void KFileItemModelRolesUpdater::slotItemsRemoved(const KItemRangeList
& itemRanges
)
263 Q_UNUSED(itemRanges
);
264 m_firstVisibleIndex
= 0;
265 m_lastVisibleIndex
= -1;
266 if (!hasPendingRoles()) {
270 if (m_model
->count() == 0) {
271 // Most probably a directory change is done. Clear all pending items
272 // and also kill all ongoing preview-jobs.
275 m_changedItems
.clear();
276 m_changedItemsTimer
->stop();
278 // Remove all items from m_pendingVisibleItems and m_pendingInvisibleItems
279 // that are not part of the model anymore. The items from m_changedItems
280 // don't need to be handled here, removed items are just skipped in
281 // resolveChangedItems().
282 for (int i
= 0; i
<= 1; ++i
) {
283 QSet
<KFileItem
>& pendingItems
= (i
== 0) ? m_pendingVisibleItems
: m_pendingInvisibleItems
;
284 QMutableSetIterator
<KFileItem
> it(pendingItems
);
285 while (it
.hasNext()) {
286 const KFileItem item
= it
.next();
287 if (m_model
->index(item
) < 0) {
288 pendingItems
.remove(item
);
295 void KFileItemModelRolesUpdater::slotItemsChanged(const KItemRangeList
& itemRanges
,
296 const QSet
<QByteArray
>& roles
)
300 if (m_changedItemsTimer
->isActive()) {
301 // A call of slotItemsChanged() has been done recently. Postpone the resolving
302 // of the roles until the timer has exceeded.
303 foreach (const KItemRange
& itemRange
, itemRanges
) {
304 int index
= itemRange
.index
;
305 for (int count
= itemRange
.count
; count
> 0; --count
) {
306 m_changedItems
.insert(m_model
->fileItem(index
));
311 // No call of slotItemsChanged() has been done recently, resolve the roles now.
312 startUpdating(itemRanges
);
314 m_changedItemsTimer
->start();
317 void KFileItemModelRolesUpdater::slotGotPreview(const KFileItem
& item
, const QPixmap
& pixmap
)
319 m_pendingVisibleItems
.remove(item
);
320 m_pendingInvisibleItems
.remove(item
);
322 const int index
= m_model
->index(item
);
327 QPixmap scaledPixmap
= pixmap
;
329 const QString mimeType
= item
.mimetype();
330 const int slashIndex
= mimeType
.indexOf(QLatin1Char('/'));
331 const QString mimeTypeGroup
= mimeType
.left(slashIndex
);
332 if (mimeTypeGroup
== QLatin1String("image")) {
333 KPixmapModifier::applyFrame(scaledPixmap
, m_iconSize
);
335 KPixmapModifier::scale(scaledPixmap
, m_iconSize
);
338 QHash
<QByteArray
, QVariant
> data
= rolesData(item
);
339 data
.insert("iconPixmap", scaledPixmap
);
341 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
342 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
343 m_model
->setData(index
, data
);
344 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
345 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
348 void KFileItemModelRolesUpdater::slotPreviewFailed(const KFileItem
& item
)
350 m_pendingVisibleItems
.remove(item
);
351 m_pendingInvisibleItems
.remove(item
);
353 const bool clearPreviews
= m_clearPreviews
;
354 m_clearPreviews
= true;
355 applyResolvedRoles(item
, ResolveAll
);
356 m_clearPreviews
= clearPreviews
;
359 void KFileItemModelRolesUpdater::slotPreviewJobFinished(KJob
* job
)
361 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
362 kDebug() << "Preview job finished. Pending visible:" << m_pendingVisibleItems
.count() << "invisible:" << m_pendingInvisibleItems
.count();
365 m_previewJobs
.removeOne(job
);
366 if (!m_previewJobs
.isEmpty() || !hasPendingRoles()) {
370 const KFileItemList visibleItems
= sortedItems(m_pendingVisibleItems
);
371 startPreviewJob(visibleItems
+ m_pendingInvisibleItems
.toList());
374 void KFileItemModelRolesUpdater::resolveNextPendingRoles()
380 if (m_previewShown
) {
381 // The preview has been turned on since the last run. Skip
382 // resolving further pending roles as this is done as soon
383 // as a preview has been received.
387 int resolvedCount
= 0;
388 bool changed
= false;
389 for (int i
= 0; i
<= 1; ++i
) {
390 QSet
<KFileItem
>& pendingItems
= (i
== 0) ? m_pendingVisibleItems
: m_pendingInvisibleItems
;
391 QSetIterator
<KFileItem
> it(pendingItems
);
392 while (it
.hasNext() && !changed
&& resolvedCount
< MaxResolveItemsCount
) {
393 const KFileItem item
= it
.next();
394 pendingItems
.remove(item
);
395 changed
= applyResolvedRoles(item
, ResolveAll
);
400 if (hasPendingRoles()) {
401 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
403 m_clearPreviews
= false;
406 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
407 static int callCount
= 0;
409 if (callCount
% 100 == 0) {
410 kDebug() << "Remaining visible roles to resolve:" << m_pendingVisibleItems
.count()
411 << "invisible:" << m_pendingInvisibleItems
.count();
416 void KFileItemModelRolesUpdater::resolveChangedItems()
418 if (m_changedItems
.isEmpty()) {
422 KItemRangeList itemRanges
;
424 QSetIterator
<KFileItem
> it(m_changedItems
);
425 while (it
.hasNext()) {
426 const KFileItem
& item
= it
.next();
427 const int index
= m_model
->index(item
);
429 itemRanges
.append(KItemRange(index
, 1));
433 startUpdating(itemRanges
);
436 void KFileItemModelRolesUpdater::startUpdating(const KItemRangeList
& itemRanges
)
438 // If no valid index range is given assume that all items are visible.
439 // A cleanup will be done later as soon as the index range has been set.
440 const bool hasValidIndexRange
= (m_lastVisibleIndex
>= 0);
442 if (hasValidIndexRange
) {
443 // Move all current pending visible items that are not visible anymore
444 // to the pending invisible items.
445 QSetIterator
<KFileItem
> it(m_pendingVisibleItems
);
446 while (it
.hasNext()) {
447 const KFileItem item
= it
.next();
448 const int index
= m_model
->index(item
);
449 if (index
< m_firstVisibleIndex
|| index
> m_lastVisibleIndex
) {
450 m_pendingVisibleItems
.remove(item
);
451 m_pendingInvisibleItems
.insert(item
);
458 foreach (const KItemRange
& range
, itemRanges
) {
459 rangesCount
+= range
.count
;
461 // Add the inserted items to the pending visible and invisible items
462 const int lastIndex
= range
.index
+ range
.count
- 1;
463 for (int i
= range
.index
; i
<= lastIndex
; ++i
) {
464 const KFileItem item
= m_model
->fileItem(i
);
465 if (!hasValidIndexRange
|| (i
>= m_firstVisibleIndex
&& i
<= m_lastVisibleIndex
)) {
466 m_pendingVisibleItems
.insert(item
);
468 m_pendingInvisibleItems
.insert(item
);
473 resolvePendingRoles();
476 void KFileItemModelRolesUpdater::startPreviewJob(const KFileItemList
& items
)
478 if (items
.isEmpty() || m_paused
) {
482 // PreviewJob internally caches items always with the size of
483 // 128 x 128 pixels or 256 x 256 pixels. A (slow) downscaling is done
484 // by PreviewJob if a smaller size is requested. For images KFileItemModelRolesUpdater must
485 // do a downscaling anyhow because of the frame, so in this case only the provided
486 // cache sizes are requested.
487 const QSize cacheSize
= (m_iconSize
.width() > 128) || (m_iconSize
.height() > 128)
488 ? QSize(256, 256) : QSize(128, 128);
490 // KIO::filePreview() will request the MIME-type of all passed items, which (in the
491 // worst case) might block the application for several seconds. To prevent such
492 // a blocking the MIME-type of the items will determined until the MaxBlockTimeout
493 // has been reached and only those items will get passed. As soon as the MIME-type
494 // has been resolved once KIO::filePreview() can already access the resolved
495 // MIME-type in a fast way.
498 KFileItemList itemSubSet
;
499 for (int i
= 0; i
< items
.count(); ++i
) {
500 KFileItem item
= items
.at(i
);
501 item
.determineMimeType();
502 itemSubSet
.append(items
.at(i
));
503 if (timer
.elapsed() > MaxBlockTimeout
) {
504 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
505 kDebug() << "Maximum time of" << MaxBlockTimeout
<< "ms exceeded, creating only previews for"
506 << (i
+ 1) << "items," << (items
.count() - (i
+ 1)) << "will be resolved later";
511 KJob
* job
= KIO::filePreview(itemSubSet
, cacheSize
, &m_enabledPlugins
);
513 connect(job
, SIGNAL(gotPreview(KFileItem
,QPixmap
)),
514 this, SLOT(slotGotPreview(KFileItem
,QPixmap
)));
515 connect(job
, SIGNAL(failed(KFileItem
)),
516 this, SLOT(slotPreviewFailed(KFileItem
)));
517 connect(job
, SIGNAL(finished(KJob
*)),
518 this, SLOT(slotPreviewJobFinished(KJob
*)));
520 m_previewJobs
.append(job
);
524 bool KFileItemModelRolesUpdater::hasPendingRoles() const
526 return !m_pendingVisibleItems
.isEmpty() || !m_pendingInvisibleItems
.isEmpty();
529 void KFileItemModelRolesUpdater::resolvePendingRoles()
531 int resolvedCount
= 0;
533 const bool hasSlowRoles
= m_previewShown
534 || m_roles
.contains("size")
535 || m_roles
.contains("type")
536 || m_roles
.contains("isExpandable");
537 const ResolveHint resolveHint
= hasSlowRoles
? ResolveFast
: ResolveAll
;
539 // Resolving the MIME type can be expensive. Assure that not more than MaxBlockTimeout ms are
540 // spend for resolving them synchronously. Usually this is more than enough to determine
541 // all visible items, but there are corner cases where this limit gets easily exceeded.
545 // Resolve the MIME type of all visible items
546 QSetIterator
<KFileItem
> visibleIt(m_pendingVisibleItems
);
547 while (visibleIt
.hasNext()) {
548 const KFileItem item
= visibleIt
.next();
549 applyResolvedRoles(item
, resolveHint
);
551 Q_ASSERT(!m_pendingInvisibleItems
.contains(item
));
552 // All roles have been resolved already by applyResolvedRoles()
553 m_pendingVisibleItems
.remove(item
);
557 if (timer
.elapsed() > MaxBlockTimeout
) {
562 // Resolve the MIME type of the invisible items at least until the timeout
563 // has been exceeded or the maximum number of items has been reached
564 KFileItemList invisibleItems
;
565 if (m_lastVisibleIndex
>= 0) {
566 // The visible range is valid, don't care about the order how the MIME
567 // type of invisible items get resolved
568 invisibleItems
= m_pendingInvisibleItems
.toList();
570 // The visible range is temporary invalid (e.g. happens when loading
571 // a directory) so take care to sort the currently invisible items where
572 // a part will get visible later
573 invisibleItems
= sortedItems(m_pendingInvisibleItems
);
577 while (resolvedCount
< MaxResolveItemsCount
&& index
< invisibleItems
.count() && timer
.elapsed() <= MaxBlockTimeout
) {
578 const KFileItem item
= invisibleItems
.at(index
);
579 applyResolvedRoles(item
, resolveHint
);
582 // All roles have been resolved already by applyResolvedRoles()
583 m_pendingInvisibleItems
.remove(item
);
589 if (m_previewShown
) {
590 KFileItemList items
= sortedItems(m_pendingVisibleItems
);
591 items
+= invisibleItems
;
592 startPreviewJob(items
);
594 QTimer::singleShot(0, this, SLOT(resolveNextPendingRoles()));
597 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
598 if (timer
.elapsed() > MaxBlockTimeout
) {
599 kDebug() << "Maximum time of" << MaxBlockTimeout
600 << "ms exceeded, skipping items... Remaining visible:" << m_pendingVisibleItems
.count()
601 << "invisible:" << m_pendingInvisibleItems
.count();
603 kDebug() << "[TIME] Resolved pending roles:" << timer
.elapsed();
607 void KFileItemModelRolesUpdater::resetPendingRoles()
609 m_pendingVisibleItems
.clear();
610 m_pendingInvisibleItems
.clear();
612 foreach (KJob
* job
, m_previewJobs
) {
615 Q_ASSERT(m_previewJobs
.isEmpty());
618 void KFileItemModelRolesUpdater::sortAndResolveAllRoles()
625 Q_ASSERT(m_pendingVisibleItems
.isEmpty());
626 Q_ASSERT(m_pendingInvisibleItems
.isEmpty());
628 if (m_model
->count() == 0) {
632 // Determine all visible items
633 Q_ASSERT(m_firstVisibleIndex
>= 0);
634 for (int i
= m_firstVisibleIndex
; i
<= m_lastVisibleIndex
; ++i
) {
635 const KFileItem item
= m_model
->fileItem(i
);
636 if (!item
.isNull()) {
637 m_pendingVisibleItems
.insert(item
);
641 // Determine all invisible items
642 for (int i
= 0; i
< m_firstVisibleIndex
; ++i
) {
643 const KFileItem item
= m_model
->fileItem(i
);
644 if (!item
.isNull()) {
645 m_pendingInvisibleItems
.insert(item
);
648 for (int i
= m_lastVisibleIndex
+ 1; i
< m_model
->count(); ++i
) {
649 const KFileItem item
= m_model
->fileItem(i
);
650 if (!item
.isNull()) {
651 m_pendingInvisibleItems
.insert(item
);
655 resolvePendingRoles();
658 void KFileItemModelRolesUpdater::sortAndResolvePendingRoles()
661 if (m_model
->count() == 0) {
665 // If no valid index range is given assume that all items are visible.
666 // A cleanup will be done later as soon as the index range has been set.
667 const bool hasValidIndexRange
= (m_lastVisibleIndex
>= 0);
669 // Trigger a preview generation of all pending items. Assure that the visible
670 // pending items get generated first.
671 QSet
<KFileItem
> pendingItems
;
672 pendingItems
+= m_pendingVisibleItems
;
673 pendingItems
+= m_pendingInvisibleItems
;
676 Q_ASSERT(m_pendingVisibleItems
.isEmpty());
677 Q_ASSERT(m_pendingInvisibleItems
.isEmpty());
679 QSetIterator
<KFileItem
> it(pendingItems
);
680 while (it
.hasNext()) {
681 const KFileItem item
= it
.next();
686 const int index
= m_model
->index(item
);
687 if (!hasValidIndexRange
|| (index
>= m_firstVisibleIndex
&& index
<= m_lastVisibleIndex
)) {
688 m_pendingVisibleItems
.insert(item
);
690 m_pendingInvisibleItems
.insert(item
);
694 resolvePendingRoles();
697 bool KFileItemModelRolesUpdater::applyResolvedRoles(const KFileItem
& item
, ResolveHint hint
)
703 const bool resolveAll
= (hint
== ResolveAll
);
705 bool mimeTypeChanged
= false;
706 if (!item
.isMimeTypeKnown()) {
707 item
.determineMimeType();
708 mimeTypeChanged
= true;
711 if (mimeTypeChanged
|| resolveAll
|| m_clearPreviews
) {
712 const int index
= m_model
->index(item
);
717 QHash
<QByteArray
, QVariant
> data
;
719 data
= rolesData(item
);
722 data
.insert("iconName", item
.iconName());
724 if (m_clearPreviews
) {
725 data
.insert("iconPixmap", QString());
728 disconnect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
729 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
730 m_model
->setData(index
, data
);
731 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
732 this, SLOT(slotItemsChanged(KItemRangeList
,QSet
<QByteArray
>)));
739 QHash
<QByteArray
, QVariant
> KFileItemModelRolesUpdater::rolesData(const KFileItem
& item
) const
741 QHash
<QByteArray
, QVariant
> data
;
743 const bool getSizeRole
= m_roles
.contains("size");
744 const bool getIsExpandableRole
= m_roles
.contains("isExpandable");
746 if ((getSizeRole
|| getIsExpandableRole
) && item
.isDir() && item
.isLocalFile()) {
747 const QString path
= item
.localPath();
748 const int count
= subItemsCount(path
);
751 data
.insert("size", KIO::filesize_t(count
));
753 if (getIsExpandableRole
) {
754 data
.insert("isExpandable", count
> 0);
759 if (m_roles
.contains("type")) {
760 data
.insert("type", item
.mimeComment());
763 data
.insert("iconOverlays", item
.overlays());
768 KFileItemList
KFileItemModelRolesUpdater::sortedItems(const QSet
<KFileItem
>& items
) const
770 KFileItemList itemList
;
771 if (items
.isEmpty()) {
775 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
781 indexes
.reserve(items
.count());
783 QSetIterator
<KFileItem
> it(items
);
784 while (it
.hasNext()) {
785 const KFileItem item
= it
.next();
786 const int index
= m_model
->index(item
);
788 indexes
.append(index
);
793 itemList
.reserve(items
.count());
794 foreach (int index
, indexes
) {
795 itemList
.append(m_model
->fileItem(index
));
798 #ifdef KFILEITEMMODELROLESUPDATER_DEBUG
799 kDebug() << "[TIME] Sorting of items:" << timer
.elapsed();
804 int KFileItemModelRolesUpdater::subItemsCount(const QString
& path
) const
806 const bool countHiddenFiles
= m_model
->showHiddenFiles();
807 const bool showFoldersOnly
= m_model
->showFoldersOnly();
811 QDir::Filters filters
= QDir::NoDotAndDotDot
| QDir::System
;
812 if (countHiddenFiles
) {
813 filters
|= QDir::Hidden
;
815 if (showFoldersOnly
) {
816 filters
|= QDir::Dirs
;
818 filters
|= QDir::AllEntries
;
820 return dir
.entryList(filters
).count();
822 // Taken from kdelibs/kio/kio/kdirmodel.cpp
823 // Copyright (C) 2006 David Faure <faure@kde.org>
826 DIR* dir
= ::opendir(QFile::encodeName(path
));
829 struct dirent
*dirEntry
= 0;
830 while ((dirEntry
= ::readdir(dir
))) { // krazy:exclude=syscalls
831 if (dirEntry
->d_name
[0] == '.') {
832 if (dirEntry
->d_name
[1] == '\0' || !countHiddenFiles
) {
833 // Skip "." or hidden files
836 if (dirEntry
->d_name
[1] == '.' && dirEntry
->d_name
[2] == '\0') {
842 // If only directories are counted, consider an unknown file type also
843 // as directory instead of trying to do an expensive stat() (see bug 292642).
844 if (!showFoldersOnly
|| dirEntry
->d_type
== DT_DIR
|| dirEntry
->d_type
== DT_UNKNOWN
) {
854 #include "kfileitemmodelrolesupdater.moc"