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 "kfileitemmodel.h"
24 #include <KStringHandler>
29 #define KFILEITEMMODEL_DEBUG
31 KFileItemModel::KFileItemModel(KDirLister
* dirLister
, QObject
* parent
) :
32 KItemModelBase(QByteArray(), "name", parent
),
33 m_dirLister(dirLister
),
34 m_naturalSorting(true),
35 m_sortFoldersFirst(true),
38 m_caseSensitivity(Qt::CaseInsensitive
),
43 m_minimumUpdateIntervalTimer(0),
44 m_maximumUpdateIntervalTimer(0),
45 m_pendingItemsToInsert(),
46 m_pendingItemsToDelete(),
47 m_rootExpansionLevel(-1)
50 m_requestRole
[NameRole
] = true;
51 m_requestRole
[IsDirRole
] = true;
55 connect(dirLister
, SIGNAL(canceled()), this, SLOT(slotCanceled()));
56 connect(dirLister
, SIGNAL(completed()), this, SLOT(slotCompleted()));
57 connect(dirLister
, SIGNAL(newItems(KFileItemList
)), this, SLOT(slotNewItems(KFileItemList
)));
58 connect(dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SLOT(slotItemsDeleted(KFileItemList
)));
59 connect(dirLister
, SIGNAL(clear()), this, SLOT(slotClear()));
60 connect(dirLister
, SIGNAL(clear(KUrl
)), this, SLOT(slotClear(KUrl
)));
62 // Although the layout engine of KItemListView is fast it is very inefficient to e.g.
63 // emit 50 itemsInserted()-signals each 100 ms. m_minimumUpdateIntervalTimer assures that updates
64 // are done in 1 second intervals for equal operations.
65 m_minimumUpdateIntervalTimer
= new QTimer(this);
66 m_minimumUpdateIntervalTimer
->setInterval(1000);
67 m_minimumUpdateIntervalTimer
->setSingleShot(true);
68 connect(m_minimumUpdateIntervalTimer
, SIGNAL(timeout()), this, SLOT(dispatchPendingItems()));
70 // For slow KIO-slaves like used for searching it makes sense to show results periodically even
71 // before the completed() or canceled() signal has been emitted.
72 m_maximumUpdateIntervalTimer
= new QTimer(this);
73 m_maximumUpdateIntervalTimer
->setInterval(2000);
74 m_maximumUpdateIntervalTimer
->setSingleShot(true);
75 connect(m_maximumUpdateIntervalTimer
, SIGNAL(timeout()), this, SLOT(dispatchPendingItems()));
77 Q_ASSERT(m_minimumUpdateIntervalTimer
->interval() <= m_maximumUpdateIntervalTimer
->interval());
80 KFileItemModel::~KFileItemModel()
84 int KFileItemModel::count() const
86 return m_data
.count();
89 QHash
<QByteArray
, QVariant
> KFileItemModel::data(int index
) const
91 if (index
>= 0 && index
< count()) {
92 return m_data
.at(index
);
94 return QHash
<QByteArray
, QVariant
>();
97 bool KFileItemModel::setData(int index
, const QHash
<QByteArray
, QVariant
>& values
)
99 if (index
>= 0 && index
< count()) {
100 QHash
<QByteArray
, QVariant
> currentValue
= m_data
.at(index
);
102 QSet
<QByteArray
> changedRoles
;
103 QHashIterator
<QByteArray
, QVariant
> it(values
);
104 while (it
.hasNext()) {
106 const QByteArray role
= it
.key();
107 const QVariant value
= it
.value();
109 if (currentValue
[role
] != value
) {
110 currentValue
[role
] = value
;
111 changedRoles
.insert(role
);
115 if (!changedRoles
.isEmpty()) {
116 m_data
[index
] = currentValue
;
117 emit
itemsChanged(KItemRangeList() << KItemRange(index
, 1), changedRoles
);
125 bool KFileItemModel::supportsGrouping() const
130 bool KFileItemModel::supportsSorting() const
135 KFileItem
KFileItemModel::fileItem(int index
) const
137 if (index
>= 0 && index
< count()) {
138 return m_sortedItems
.at(index
);
144 int KFileItemModel::index(const KFileItem
& item
) const
150 return m_items
.value(item
, -1);
153 void KFileItemModel::clear()
158 void KFileItemModel::setRoles(const QSet
<QByteArray
>& roles
)
161 const bool supportedExpanding
= m_requestRole
[IsExpandedRole
] && m_requestRole
[ExpansionLevelRole
];
162 const bool willSupportExpanding
= roles
.contains("isExpanded") && roles
.contains("expansionLevel");
163 if (supportedExpanding
&& !willSupportExpanding
) {
164 // No expanding is supported anymore. Take care to delete all items that have an expansion level
165 // that is not 0 (and hence are part of an expanded item).
166 removeExpandedItems();
171 QSetIterator
<QByteArray
> it(roles
);
172 while (it
.hasNext()) {
173 const QByteArray
& role
= it
.next();
174 m_requestRole
[roleIndex(role
)] = true;
178 // Update m_data with the changed requested roles
179 const int maxIndex
= count() - 1;
180 for (int i
= 0; i
<= maxIndex
; ++i
) {
181 m_data
[i
] = retrieveData(m_sortedItems
.at(i
));
184 kWarning() << "TODO: Emitting itemsChanged() with no information what has changed!";
185 emit
itemsChanged(KItemRangeList() << KItemRange(0, count()), QSet
<QByteArray
>());
189 QSet
<QByteArray
> KFileItemModel::roles() const
191 QSet
<QByteArray
> roles
;
192 for (int i
= 0; i
< RolesCount
; ++i
) {
193 if (m_requestRole
[i
]) {
196 case NameRole
: roles
.insert("name"); break;
197 case SizeRole
: roles
.insert("size"); break;
198 case DateRole
: roles
.insert("date"); break;
199 case PermissionsRole
: roles
.insert("permissions"); break;
200 case OwnerRole
: roles
.insert("owner"); break;
201 case GroupRole
: roles
.insert("group"); break;
202 case TypeRole
: roles
.insert("type"); break;
203 case DestinationRole
: roles
.insert("destination"); break;
204 case PathRole
: roles
.insert("path"); break;
205 case IsDirRole
: roles
.insert("isDir"); break;
206 case IsExpandedRole
: roles
.insert("isExpanded"); break;
207 case ExpansionLevelRole
: roles
.insert("expansionLevel"); break;
208 default: Q_ASSERT(false); break;
215 bool KFileItemModel::setExpanded(int index
, bool expanded
)
217 if (isExpanded(index
) == expanded
|| index
< 0 || index
>= count()) {
221 QHash
<QByteArray
, QVariant
> values
;
222 values
.insert("isExpanded", expanded
);
223 if (!setData(index
, values
)) {
228 const KUrl url
= m_sortedItems
.at(index
).url();
229 KDirLister
* dirLister
= m_dirLister
.data();
231 dirLister
->openUrl(url
, KDirLister::Keep
);
235 KFileItemList itemsToRemove
;
236 const int expansionLevel
= data(index
)["expansionLevel"].toInt();
238 while (index
< count() && data(index
)["expansionLevel"].toInt() > expansionLevel
) {
239 itemsToRemove
.append(m_sortedItems
.at(index
));
242 removeItems(itemsToRemove
);
249 bool KFileItemModel::isExpanded(int index
) const
251 if (index
>= 0 && index
< count()) {
252 return m_data
.at(index
).value("isExpanded").toBool();
257 bool KFileItemModel::isExpandable(int index
) const
259 if (index
>= 0 && index
< count()) {
260 return m_sortedItems
.at(index
).isDir();
265 void KFileItemModel::onGroupRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
268 m_groupRole
= roleIndex(current
);
271 void KFileItemModel::onSortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
274 const int itemCount
= count();
275 if (itemCount
<= 0) {
279 m_sortRole
= roleIndex(current
);
281 KFileItemList sortedItems
= m_sortedItems
;
282 m_sortedItems
.clear();
285 emit
itemsRemoved(KItemRangeList() << KItemRange(0, itemCount
));
287 sort(sortedItems
.begin(), sortedItems
.end());
289 foreach (const KFileItem
& item
, sortedItems
) {
290 m_sortedItems
.append(item
);
291 m_items
.insert(item
, index
);
292 m_data
.append(retrieveData(item
));
297 emit
itemsInserted(KItemRangeList() << KItemRange(0, itemCount
));
300 void KFileItemModel::slotCompleted()
302 if (m_minimumUpdateIntervalTimer
->isActive()) {
303 // dispatchPendingItems() will be called when the timer
308 dispatchPendingItems();
309 m_minimumUpdateIntervalTimer
->start();
312 void KFileItemModel::slotCanceled()
314 m_minimumUpdateIntervalTimer
->stop();
315 m_maximumUpdateIntervalTimer
->stop();
316 dispatchPendingItems();
319 void KFileItemModel::slotNewItems(const KFileItemList
& items
)
321 if (!m_pendingItemsToDelete
.isEmpty()) {
322 removeItems(m_pendingItemsToDelete
);
323 m_pendingItemsToDelete
.clear();
325 m_pendingItemsToInsert
.append(items
);
327 if (useMaximumUpdateInterval() && !m_maximumUpdateIntervalTimer
->isActive()) {
328 // Assure that items get dispatched if no completed() or canceled() signal is
329 // emitted during the maximum update interval.
330 m_maximumUpdateIntervalTimer
->start();
334 void KFileItemModel::slotItemsDeleted(const KFileItemList
& items
)
336 if (!m_pendingItemsToInsert
.isEmpty()) {
337 insertItems(m_pendingItemsToInsert
);
338 m_pendingItemsToInsert
.clear();
340 m_pendingItemsToDelete
.append(items
);
343 void KFileItemModel::slotClear()
345 #ifdef KFILEITEMMODEL_DEBUG
346 kDebug() << "Clearing all items";
349 m_minimumUpdateIntervalTimer
->stop();
350 m_maximumUpdateIntervalTimer
->stop();
351 m_pendingItemsToInsert
.clear();
352 m_pendingItemsToDelete
.clear();
354 m_rootExpansionLevel
= -1;
356 const int removedCount
= m_data
.count();
357 if (removedCount
> 0) {
358 m_sortedItems
.clear();
361 emit
itemsRemoved(KItemRangeList() << KItemRange(0, removedCount
));
365 void KFileItemModel::slotClear(const KUrl
& url
)
370 void KFileItemModel::dispatchPendingItems()
372 if (!m_pendingItemsToInsert
.isEmpty()) {
373 Q_ASSERT(m_pendingItemsToDelete
.isEmpty());
374 insertItems(m_pendingItemsToInsert
);
375 m_pendingItemsToInsert
.clear();
376 } else if (!m_pendingItemsToDelete
.isEmpty()) {
377 Q_ASSERT(m_pendingItemsToInsert
.isEmpty());
378 removeItems(m_pendingItemsToDelete
);
379 m_pendingItemsToDelete
.clear();
383 void KFileItemModel::insertItems(const KFileItemList
& items
)
385 if (items
.isEmpty()) {
389 #ifdef KFILEITEMMODEL_DEBUG
392 kDebug() << "===========================================================";
393 kDebug() << "Inserting" << items
.count() << "items";
396 KFileItemList sortedItems
= items
;
397 sort(sortedItems
.begin(), sortedItems
.end());
399 #ifdef KFILEITEMMODEL_DEBUG
400 kDebug() << "[TIME] Sorting:" << timer
.elapsed();
403 KItemRangeList itemRanges
;
406 int insertedAtIndex
= -1; // Index for the current item-range
407 int insertedCount
= 0; // Count for the current item-range
408 int previouslyInsertedCount
= 0; // Sum of previously inserted items for all ranges
409 while (sourceIndex
< sortedItems
.count()) {
410 // Find target index from m_items to insert the current item
412 const int previousTargetIndex
= targetIndex
;
413 while (targetIndex
< m_sortedItems
.count()) {
414 if (!lessThan(m_sortedItems
.at(targetIndex
), sortedItems
.at(sourceIndex
))) {
420 if (targetIndex
- previousTargetIndex
> 0 && insertedAtIndex
>= 0) {
421 itemRanges
<< KItemRange(insertedAtIndex
, insertedCount
);
422 previouslyInsertedCount
+= insertedCount
;
423 insertedAtIndex
= targetIndex
- previouslyInsertedCount
;
427 // Insert item at the position targetIndex
428 const KFileItem item
= sortedItems
.at(sourceIndex
);
429 m_sortedItems
.insert(targetIndex
, item
);
430 m_data
.insert(targetIndex
, retrieveData(item
));
431 // m_items will be inserted after the loop (see comment below)
434 if (insertedAtIndex
< 0) {
435 insertedAtIndex
= targetIndex
;
436 Q_ASSERT(previouslyInsertedCount
== 0);
442 // The indexes of all m_items must be adjusted, not only the index
444 for (int i
= 0; i
< m_sortedItems
.count(); ++i
) {
445 m_items
.insert(m_sortedItems
.at(i
), i
);
448 itemRanges
<< KItemRange(insertedAtIndex
, insertedCount
);
449 emit
itemsInserted(itemRanges
);
451 #ifdef KFILEITEMMODEL_DEBUG
452 kDebug() << "[TIME] Inserting of" << items
.count() << "items:" << timer
.elapsed();
456 void KFileItemModel::removeItems(const KFileItemList
& items
)
458 if (items
.isEmpty()) {
462 #ifdef KFILEITEMMODEL_DEBUG
463 kDebug() << "Removing " << items
.count() << "items";
466 KFileItemList sortedItems
= items
;
467 sort(sortedItems
.begin(), sortedItems
.end());
469 QList
<int> indexesToRemove
;
470 indexesToRemove
.reserve(items
.count());
472 // Calculate the item ranges that will get deleted
473 KItemRangeList itemRanges
;
474 int removedAtIndex
= -1;
475 int removedCount
= 0;
477 foreach (const KFileItem
& itemToRemove
, sortedItems
) {
478 const int previousTargetIndex
= targetIndex
;
479 while (targetIndex
< m_sortedItems
.count()) {
480 if (m_sortedItems
.at(targetIndex
) == itemToRemove
) {
485 if (targetIndex
>= m_sortedItems
.count()) {
486 kWarning() << "Item that should be deleted has not been found!";
490 if (targetIndex
- previousTargetIndex
> 0 && removedAtIndex
>= 0) {
491 itemRanges
<< KItemRange(removedAtIndex
, removedCount
);
492 removedAtIndex
= targetIndex
;
496 indexesToRemove
.append(targetIndex
);
497 if (removedAtIndex
< 0) {
498 removedAtIndex
= targetIndex
;
505 for (int i
= indexesToRemove
.count() - 1; i
>= 0; --i
) {
506 const int indexToRemove
= indexesToRemove
.at(i
);
507 m_items
.remove(m_sortedItems
.at(indexToRemove
));
508 m_sortedItems
.removeAt(indexToRemove
);
509 m_data
.removeAt(indexToRemove
);
512 // The indexes of all m_items must be adjusted, not only the index
513 // of the removed items
514 for (int i
= 0; i
< m_sortedItems
.count(); ++i
) {
515 m_items
.insert(m_sortedItems
.at(i
), i
);
519 m_rootExpansionLevel
= -1;
522 itemRanges
<< KItemRange(removedAtIndex
, removedCount
);
523 emit
itemsRemoved(itemRanges
);
526 void KFileItemModel::removeExpandedItems()
529 KFileItemList expandedItems
;
531 const int maxIndex
= m_data
.count() - 1;
532 for (int i
= 0; i
<= maxIndex
; ++i
) {
533 if (m_data
.at(i
).value("expansionLevel").toInt() > 0) {
534 const KFileItem fileItem
= m_sortedItems
.at(i
);
535 expandedItems
.append(fileItem
);
539 // The m_rootExpansionLevel may not get reset before all items with
540 // a bigger expansionLevel have been removed.
541 Q_ASSERT(m_rootExpansionLevel
>= 0);
542 removeItems(expandedItems
);
544 m_rootExpansionLevel
= -1;
547 void KFileItemModel::resetRoles()
549 for (int i
= 0; i
< RolesCount
; ++i
) {
550 m_requestRole
[i
] = false;
554 KFileItemModel::Role
KFileItemModel::roleIndex(const QByteArray
& role
) const
556 static QHash
<QByteArray
, Role
> rolesHash
;
557 if (rolesHash
.isEmpty()) {
558 rolesHash
.insert("name", NameRole
);
559 rolesHash
.insert("size", SizeRole
);
560 rolesHash
.insert("date", DateRole
);
561 rolesHash
.insert("permissions", PermissionsRole
);
562 rolesHash
.insert("owner", OwnerRole
);
563 rolesHash
.insert("group", GroupRole
);
564 rolesHash
.insert("type", TypeRole
);
565 rolesHash
.insert("destination", DestinationRole
);
566 rolesHash
.insert("path", PathRole
);
567 rolesHash
.insert("isDir", IsDirRole
);
568 rolesHash
.insert("isExpanded", IsExpandedRole
);
569 rolesHash
.insert("expansionLevel", ExpansionLevelRole
);
571 return rolesHash
.value(role
, NoRole
);
574 QHash
<QByteArray
, QVariant
> KFileItemModel::retrieveData(const KFileItem
& item
) const
576 // It is important to insert only roles that are fast to retrieve. E.g.
577 // KFileItem::iconName() can be very expensive if the MIME-type is unknown
578 // and hence will be retrieved asynchronously by KFileItemModelRolesUpdater.
579 QHash
<QByteArray
, QVariant
> data
;
580 data
.insert("iconPixmap", QPixmap());
582 const bool isDir
= item
.isDir();
583 if (m_requestRole
[IsDirRole
]) {
584 data
.insert("isDir", isDir
);
587 if (m_requestRole
[NameRole
]) {
588 data
.insert("name", item
.name());
591 if (m_requestRole
[SizeRole
]) {
593 data
.insert("size", QVariant());
595 data
.insert("size", item
.size());
599 if (m_requestRole
[DateRole
]) {
600 // Don't use KFileItem::timeString() as this is too expensive when
601 // having several thousands of items. Instead the formatting of the
602 // date-time will be done on-demand by the view when the date will be shown.
603 const KDateTime dateTime
= item
.time(KFileItem::ModificationTime
);
604 data
.insert("date", dateTime
.dateTime());
607 if (m_requestRole
[PermissionsRole
]) {
608 data
.insert("permissions", item
.permissionsString());
611 if (m_requestRole
[OwnerRole
]) {
612 data
.insert("owner", item
.user());
615 if (m_requestRole
[GroupRole
]) {
616 data
.insert("group", item
.group());
619 if (m_requestRole
[DestinationRole
]) {
620 QString destination
= item
.linkDest();
621 if (destination
.isEmpty()) {
622 destination
= i18nc("@item:intable", "No destination");
624 data
.insert("destination", destination
);
627 if (m_requestRole
[PathRole
]) {
628 data
.insert("path", item
.localPath());
631 if (m_requestRole
[IsExpandedRole
]) {
632 data
.insert("isExpanded", false);
635 if (m_requestRole
[ExpansionLevelRole
]) {
636 if (m_rootExpansionLevel
< 0) {
637 KDirLister
* dirLister
= m_dirLister
.data();
639 const QString rootDir
= dirLister
->url().directory(KUrl::AppendTrailingSlash
);
640 m_rootExpansionLevel
= rootDir
.count('/');
643 const QString dir
= item
.url().directory(KUrl::AppendTrailingSlash
);
644 const int level
= dir
.count('/') - m_rootExpansionLevel
- 1;
645 data
.insert("expansionLevel", level
);
648 if (item
.isMimeTypeKnown()) {
649 data
.insert("iconName", item
.iconName());
651 if (m_requestRole
[TypeRole
]) {
652 data
.insert("type", item
.mimeComment());
659 bool KFileItemModel::lessThan(const KFileItem
& a
, const KFileItem
& b
) const
663 if (m_rootExpansionLevel
>= 0) {
664 result
= expansionLevelsCompare(a
, b
);
666 // The items have parents with different expansion levels
671 if (m_sortFoldersFirst
) {
672 const bool isDirA
= a
.isDir();
673 const bool isDirB
= b
.isDir();
674 if (isDirA
&& !isDirB
) {
676 } else if (!isDirA
&& isDirB
) {
681 switch (m_sortRole
) {
683 result
= stringCompare(a
.text(), b
.text());
685 // KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used
686 result
= stringCompare(a
.name(m_caseSensitivity
== Qt::CaseInsensitive
),
687 b
.name(m_caseSensitivity
== Qt::CaseInsensitive
));
693 const KDateTime dateTimeA
= a
.time(KFileItem::ModificationTime
);
694 const KDateTime dateTimeB
= b
.time(KFileItem::ModificationTime
);
695 if (dateTimeA
< dateTimeB
) {
697 } else if (dateTimeA
> dateTimeB
) {
708 // It must be assured that the sort order is always unique even if two values have been
709 // equal. In this case a comparison of the URL is done which is unique in all cases
710 // within KDirLister.
711 result
= QString::compare(a
.url().url(), b
.url().url(), Qt::CaseSensitive
);
717 void KFileItemModel::sort(const KFileItemList::iterator
& startIterator
, const KFileItemList::iterator
& endIterator
)
719 KFileItemList::iterator start
= startIterator
;
720 KFileItemList::iterator end
= endIterator
;
722 // The implementation is based on qSortHelper() from qalgorithms.h
723 // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
724 // In opposite to qSort() it allows to use a member-function for the comparison of elements.
726 int span
= int(end
- start
);
732 KFileItemList::iterator low
= start
, high
= end
- 1;
733 KFileItemList::iterator pivot
= start
+ span
/ 2;
735 if (lessThan(*end
, *start
)) {
742 if (lessThan(*pivot
, *start
)) {
743 qSwap(*pivot
, *start
);
745 if (lessThan(*end
, *pivot
)) {
755 while (low
< high
&& lessThan(*low
, *end
)) {
759 while (high
> low
&& lessThan(*end
, *high
)) {
771 if (lessThan(*low
, *end
)) {
783 int KFileItemModel::stringCompare(const QString
& a
, const QString
& b
) const
785 // Taken from KDirSortFilterProxyModel (kdelibs/kfile/kdirsortfilterproxymodel.*)
786 // Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>
787 // Copyright (C) 2006 by Dominic Battre <dominic@battre.de>
788 // Copyright (C) 2006 by Martin Pool <mbp@canonical.com>
790 if (m_caseSensitivity
== Qt::CaseInsensitive
) {
791 const int result
= m_naturalSorting
? KStringHandler::naturalCompare(a
, b
, Qt::CaseInsensitive
)
792 : QString::compare(a
, b
, Qt::CaseInsensitive
);
794 // Only return the result, if the strings are not equal. If they are equal by a case insensitive
795 // comparison, still a deterministic sort order is required. A case sensitive
796 // comparison is done as fallback.
801 return m_naturalSorting
? KStringHandler::naturalCompare(a
, b
, Qt::CaseSensitive
)
802 : QString::compare(a
, b
, Qt::CaseSensitive
);
805 int KFileItemModel::expansionLevelsCompare(const KFileItem
& a
, const KFileItem
& b
) const
807 const KUrl urlA
= a
.url();
808 const KUrl urlB
= b
.url();
809 if (urlA
.directory() == urlB
.directory()) {
810 // Both items have the same directory as parent
814 // Check whether one item is the parent of the other item
815 if (urlA
.isParentOf(urlB
)) {
817 } else if (urlB
.isParentOf(urlA
)) {
821 // Determine the maximum common path of both items and
822 // remember the index in 'index'
823 const QString pathA
= urlA
.path();
824 const QString pathB
= urlB
.path();
826 const int maxIndex
= qMin(pathA
.length(), pathB
.length()) - 1;
828 while (index
<= maxIndex
&& pathA
.at(index
) == pathB
.at(index
)) {
831 if (index
> maxIndex
) {
834 while ((pathA
.at(index
) != QLatin1Char('/') || pathB
.at(index
) != QLatin1Char('/')) && index
> 0) {
838 // Determine the first sub-path after the common path and
839 // check whether it represents a directory or already a file
841 const QString subPathA
= subPath(a
, pathA
, index
, &isDirA
);
843 const QString subPathB
= subPath(b
, pathB
, index
, &isDirB
);
845 if (isDirA
&& !isDirB
) {
847 } else if (!isDirA
&& isDirB
) {
851 return stringCompare(subPathA
, subPathB
);
854 QString
KFileItemModel::subPath(const KFileItem
& item
,
855 const QString
& itemPath
,
860 const int pathIndex
= itemPath
.indexOf('/', start
+ 1);
861 *isDir
= (pathIndex
> 0) || item
.isDir();
862 return itemPath
.mid(start
, pathIndex
- start
);
865 bool KFileItemModel::useMaximumUpdateInterval() const
867 const KDirLister
* dirLister
= m_dirLister
.data();
868 return dirLister
&& !dirLister
->url().isLocalFile();
871 #include "kfileitemmodel.moc"