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"
25 #include <KStringHandler>
31 #define KFILEITEMMODEL_DEBUG
33 KFileItemModel::KFileItemModel(KDirLister
* dirLister
, QObject
* parent
) :
34 KItemModelBase("name", parent
),
35 m_dirLister(dirLister
),
36 m_naturalSorting(true),
37 m_sortFoldersFirst(true),
39 m_caseSensitivity(Qt::CaseInsensitive
),
44 m_minimumUpdateIntervalTimer(0),
45 m_maximumUpdateIntervalTimer(0),
46 m_pendingItemsToInsert(),
47 m_pendingEmitLoadingCompleted(false),
48 m_rootExpansionLevel(-1),
50 m_restoredExpandedUrls()
53 m_requestRole
[NameRole
] = true;
54 m_requestRole
[IsDirRole
] = true;
58 connect(dirLister
, SIGNAL(canceled()), this, SLOT(slotCanceled()));
59 connect(dirLister
, SIGNAL(completed()), this, SLOT(slotCompleted()));
60 connect(dirLister
, SIGNAL(newItems(KFileItemList
)), this, SLOT(slotNewItems(KFileItemList
)));
61 connect(dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SLOT(slotItemsDeleted(KFileItemList
)));
62 connect(dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)), this, SLOT(slotRefreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)));
63 connect(dirLister
, SIGNAL(clear()), this, SLOT(slotClear()));
64 connect(dirLister
, SIGNAL(clear(KUrl
)), this, SLOT(slotClear(KUrl
)));
66 // Although the layout engine of KItemListView is fast it is very inefficient to e.g.
67 // emit 50 itemsInserted()-signals each 100 ms. m_minimumUpdateIntervalTimer assures that updates
68 // are done in 1 second intervals for equal operations.
69 m_minimumUpdateIntervalTimer
= new QTimer(this);
70 m_minimumUpdateIntervalTimer
->setInterval(1000);
71 m_minimumUpdateIntervalTimer
->setSingleShot(true);
72 connect(m_minimumUpdateIntervalTimer
, SIGNAL(timeout()), this, SLOT(dispatchPendingItemsToInsert()));
74 // For slow KIO-slaves like used for searching it makes sense to show results periodically even
75 // before the completed() or canceled() signal has been emitted.
76 m_maximumUpdateIntervalTimer
= new QTimer(this);
77 m_maximumUpdateIntervalTimer
->setInterval(2000);
78 m_maximumUpdateIntervalTimer
->setSingleShot(true);
79 connect(m_maximumUpdateIntervalTimer
, SIGNAL(timeout()), this, SLOT(dispatchPendingItemsToInsert()));
81 Q_ASSERT(m_minimumUpdateIntervalTimer
->interval() <= m_maximumUpdateIntervalTimer
->interval());
84 KFileItemModel::~KFileItemModel()
88 int KFileItemModel::count() const
90 return m_data
.count();
93 QHash
<QByteArray
, QVariant
> KFileItemModel::data(int index
) const
95 if (index
>= 0 && index
< count()) {
96 return m_data
.at(index
);
98 return QHash
<QByteArray
, QVariant
>();
101 bool KFileItemModel::setData(int index
, const QHash
<QByteArray
, QVariant
>& values
)
103 if (index
>= 0 && index
< count()) {
104 QHash
<QByteArray
, QVariant
> currentValue
= m_data
.at(index
);
106 QSet
<QByteArray
> changedRoles
;
107 QHashIterator
<QByteArray
, QVariant
> it(values
);
108 while (it
.hasNext()) {
110 const QByteArray role
= it
.key();
111 const QVariant value
= it
.value();
113 if (currentValue
[role
] != value
) {
114 currentValue
[role
] = value
;
115 changedRoles
.insert(role
);
119 if (!changedRoles
.isEmpty()) {
120 m_data
[index
] = currentValue
;
121 emit
itemsChanged(KItemRangeList() << KItemRange(index
, 1), changedRoles
);
129 void KFileItemModel::setSortFoldersFirst(bool foldersFirst
)
131 if (foldersFirst
!= m_sortFoldersFirst
) {
132 m_sortFoldersFirst
= foldersFirst
;
137 bool KFileItemModel::sortFoldersFirst() const
139 return m_sortFoldersFirst
;
142 QMimeData
* KFileItemModel::createMimeData(const QSet
<int>& indexes
) const
144 QMimeData
* data
= new QMimeData();
146 // The following code has been taken from KDirModel::mimeData()
147 // (kdelibs/kio/kio/kdirmodel.cpp)
148 // Copyright (C) 2006 David Faure <faure@kde.org>
150 KUrl::List mostLocalUrls
;
151 bool canUseMostLocalUrls
= true;
153 QSetIterator
<int> it(indexes
);
154 while (it
.hasNext()) {
155 const int index
= it
.next();
156 const KFileItem item
= fileItem(index
);
157 if (!item
.isNull()) {
161 mostLocalUrls
<< item
.mostLocalUrl(isLocal
);
163 canUseMostLocalUrls
= false;
168 const bool different
= canUseMostLocalUrls
&& mostLocalUrls
!= urls
;
169 urls
= KDirModel::simplifiedUrlList(urls
); // TODO: Check if we still need KDirModel for this in KDE 5.0
171 mostLocalUrls
= KDirModel::simplifiedUrlList(mostLocalUrls
);
172 urls
.populateMimeData(mostLocalUrls
, data
);
174 urls
.populateMimeData(data
);
180 int KFileItemModel::indexForKeyboardSearch(const QString
& text
, int startFromIndex
) const
182 startFromIndex
= qMax(0, startFromIndex
);
183 for (int i
= startFromIndex
; i
< count(); ++i
) {
184 if (data(i
)["name"].toString().startsWith(text
, Qt::CaseInsensitive
)) {
188 for (int i
= 0; i
< startFromIndex
; ++i
) {
189 if (data(i
)["name"].toString().startsWith(text
, Qt::CaseInsensitive
)) {
196 bool KFileItemModel::supportsDropping(int index
) const
198 const KFileItem item
= fileItem(index
);
199 return item
.isNull() ? false : item
.isDir();
202 QString
KFileItemModel::roleDescription(const QByteArray
& role
) const
206 switch (roleIndex(role
)) {
207 case NameRole
: descr
= i18nc("@item:intable", "Name"); break;
208 case SizeRole
: descr
= i18nc("@item:intable", "Size"); break;
209 case DateRole
: descr
= i18nc("@item:intable", "Date"); break;
210 case PermissionsRole
: descr
= i18nc("@item:intable", "Permissions"); break;
211 case OwnerRole
: descr
= i18nc("@item:intable", "Owner"); break;
212 case GroupRole
: descr
= i18nc("@item:intable", "Group"); break;
213 case TypeRole
: descr
= i18nc("@item:intable", "Type"); break;
214 case DestinationRole
: descr
= i18nc("@item:intable", "Destination"); break;
215 case PathRole
: descr
= i18nc("@item:intable", "Path"); break;
217 case IsDirRole
: break;
218 case IsExpandedRole
: break;
219 case ExpansionLevelRole
: break;
220 default: Q_ASSERT(false); break;
226 QList
<QPair
<int, QVariant
> > KFileItemModel::groups() const
229 QPair
<int, QVariant
> group1(0, "Group 1");
230 QPair
<int, QVariant
> group2(5, "Group 2");
231 QPair
<int, QVariant
> group3(10, "Group 3");
233 QList
<QPair
<int, QVariant
> > groups
;
234 groups
.append(group1
);
235 groups
.append(group2
);
236 groups
.append(group3
);
240 KFileItem
KFileItemModel::fileItem(int index
) const
242 if (index
>= 0 && index
< count()) {
243 return m_sortedItems
.at(index
);
249 KFileItem
KFileItemModel::fileItem(const KUrl
& url
) const
251 const int index
= m_items
.value(url
, -1);
253 return m_sortedItems
.at(index
);
258 int KFileItemModel::index(const KFileItem
& item
) const
264 return m_items
.value(item
.url(), -1);
267 int KFileItemModel::index(const KUrl
& url
) const
269 KUrl urlToFind
= url
;
270 urlToFind
.adjustPath(KUrl::RemoveTrailingSlash
);
271 return m_items
.value(urlToFind
, -1);
274 KFileItem
KFileItemModel::rootItem() const
276 const KDirLister
* dirLister
= m_dirLister
.data();
278 return dirLister
->rootItem();
283 void KFileItemModel::clear()
288 void KFileItemModel::setRoles(const QSet
<QByteArray
>& roles
)
291 const bool supportedExpanding
= m_requestRole
[IsExpandedRole
] && m_requestRole
[ExpansionLevelRole
];
292 const bool willSupportExpanding
= roles
.contains("isExpanded") && roles
.contains("expansionLevel");
293 if (supportedExpanding
&& !willSupportExpanding
) {
294 // No expanding is supported anymore. Take care to delete all items that have an expansion level
295 // that is not 0 (and hence are part of an expanded item).
296 removeExpandedItems();
301 QSetIterator
<QByteArray
> it(roles
);
302 while (it
.hasNext()) {
303 const QByteArray
& role
= it
.next();
304 m_requestRole
[roleIndex(role
)] = true;
308 // Update m_data with the changed requested roles
309 const int maxIndex
= count() - 1;
310 for (int i
= 0; i
<= maxIndex
; ++i
) {
311 m_data
[i
] = retrieveData(m_sortedItems
.at(i
));
314 kWarning() << "TODO: Emitting itemsChanged() with no information what has changed!";
315 emit
itemsChanged(KItemRangeList() << KItemRange(0, count()), QSet
<QByteArray
>());
319 QSet
<QByteArray
> KFileItemModel::roles() const
321 QSet
<QByteArray
> roles
;
322 for (int i
= 0; i
< RolesCount
; ++i
) {
323 if (m_requestRole
[i
]) {
326 case NameRole
: roles
.insert("name"); break;
327 case SizeRole
: roles
.insert("size"); break;
328 case DateRole
: roles
.insert("date"); break;
329 case PermissionsRole
: roles
.insert("permissions"); break;
330 case OwnerRole
: roles
.insert("owner"); break;
331 case GroupRole
: roles
.insert("group"); break;
332 case TypeRole
: roles
.insert("type"); break;
333 case DestinationRole
: roles
.insert("destination"); break;
334 case PathRole
: roles
.insert("path"); break;
335 case IsDirRole
: roles
.insert("isDir"); break;
336 case IsExpandedRole
: roles
.insert("isExpanded"); break;
337 case ExpansionLevelRole
: roles
.insert("expansionLevel"); break;
338 default: Q_ASSERT(false); break;
345 bool KFileItemModel::setExpanded(int index
, bool expanded
)
347 if (isExpanded(index
) == expanded
|| index
< 0 || index
>= count()) {
351 QHash
<QByteArray
, QVariant
> values
;
352 values
.insert("isExpanded", expanded
);
353 if (!setData(index
, values
)) {
357 const KUrl url
= m_sortedItems
.at(index
).url();
359 m_expandedUrls
.insert(url
);
361 KDirLister
* dirLister
= m_dirLister
.data();
363 dirLister
->openUrl(url
, KDirLister::Keep
);
367 m_expandedUrls
.remove(url
);
369 KFileItemList itemsToRemove
;
370 const int expansionLevel
= data(index
)["expansionLevel"].toInt();
372 while (index
< count() && data(index
)["expansionLevel"].toInt() > expansionLevel
) {
373 itemsToRemove
.append(m_sortedItems
.at(index
));
376 removeItems(itemsToRemove
);
383 bool KFileItemModel::isExpanded(int index
) const
385 if (index
>= 0 && index
< count()) {
386 return m_data
.at(index
).value("isExpanded").toBool();
391 bool KFileItemModel::isExpandable(int index
) const
393 if (index
>= 0 && index
< count()) {
394 return m_sortedItems
.at(index
).isDir();
399 QSet
<KUrl
> KFileItemModel::expandedUrls() const
401 return m_expandedUrls
;
404 void KFileItemModel::restoreExpandedUrls(const QSet
<KUrl
>& urls
)
406 m_restoredExpandedUrls
= urls
;
409 void KFileItemModel::onGroupedSortingChanged(bool current
)
414 void KFileItemModel::onSortRoleChanged(const QByteArray
& current
, const QByteArray
& previous
)
417 m_sortRole
= roleIndex(current
);
421 void KFileItemModel::onSortOrderChanged(Qt::SortOrder current
, Qt::SortOrder previous
)
428 void KFileItemModel::slotCompleted()
430 if (m_restoredExpandedUrls
.isEmpty() && m_minimumUpdateIntervalTimer
->isActive()) {
431 // dispatchPendingItems() will be called when the timer
433 m_pendingEmitLoadingCompleted
= true;
437 m_pendingEmitLoadingCompleted
= false;
438 dispatchPendingItemsToInsert();
440 if (!m_restoredExpandedUrls
.isEmpty()) {
441 // Try to find a URL that can be expanded.
442 // Note that the parent folder must be expanded before any of its subfolders become visible.
443 // Therefore, some URLs in m_restoredExpandedUrls might not be visible yet
444 // -> we expand the first visible URL we find in m_restoredExpandedUrls.
445 foreach(const KUrl
& url
, m_restoredExpandedUrls
) {
446 const int index
= m_items
.value(url
, -1);
448 // We have found an expandable URL. Expand it and return - when
449 // the dir lister has finished, this slot will be called again.
450 m_restoredExpandedUrls
.remove(url
);
451 setExpanded(index
, true);
456 // None of the URLs in m_restoredExpandedUrls could be found in the model. This can happen
457 // if these URLs have been deleted in the meantime.
458 m_restoredExpandedUrls
.clear();
461 emit
loadingCompleted();
462 m_minimumUpdateIntervalTimer
->start();
465 void KFileItemModel::slotCanceled()
467 m_minimumUpdateIntervalTimer
->stop();
468 m_maximumUpdateIntervalTimer
->stop();
469 dispatchPendingItemsToInsert();
472 void KFileItemModel::slotNewItems(const KFileItemList
& items
)
474 m_pendingItemsToInsert
.append(items
);
476 if (useMaximumUpdateInterval() && !m_maximumUpdateIntervalTimer
->isActive()) {
477 // Assure that items get dispatched if no completed() or canceled() signal is
478 // emitted during the maximum update interval.
479 m_maximumUpdateIntervalTimer
->start();
483 void KFileItemModel::slotItemsDeleted(const KFileItemList
& items
)
485 if (!m_pendingItemsToInsert
.isEmpty()) {
486 insertItems(m_pendingItemsToInsert
);
487 m_pendingItemsToInsert
.clear();
492 void KFileItemModel::slotRefreshItems(const QList
<QPair
<KFileItem
, KFileItem
> >& items
)
494 Q_ASSERT(!items
.isEmpty());
495 #ifdef KFILEITEMMODEL_DEBUG
496 kDebug() << "Refreshing" << items
.count() << "items";
499 // Get the indexes of all items that have been refreshed
501 indexes
.reserve(items
.count());
503 QListIterator
<QPair
<KFileItem
, KFileItem
> > it(items
);
504 while (it
.hasNext()) {
505 const QPair
<KFileItem
, KFileItem
>& itemPair
= it
.next();
506 const int index
= m_items
.value(itemPair
.second
.url(), -1);
508 indexes
.append(index
);
512 // If the changed items have been created recently, they might not be in m_items yet.
513 // In that case, the list 'indexes' might be empty.
514 if (indexes
.isEmpty()) {
518 // Extract the item-ranges out of the changed indexes
521 KItemRangeList itemRangeList
;
524 int previousIndex
= indexes
.at(0);
526 const int maxIndex
= indexes
.count() - 1;
527 for (int i
= 1; i
<= maxIndex
; ++i
) {
528 const int currentIndex
= indexes
.at(i
);
529 if (currentIndex
== previousIndex
+ 1) {
532 itemRangeList
.append(KItemRange(rangeIndex
, rangeCount
));
534 rangeIndex
= currentIndex
;
537 previousIndex
= currentIndex
;
540 if (rangeCount
> 0) {
541 itemRangeList
.append(KItemRange(rangeIndex
, rangeCount
));
544 emit
itemsChanged(itemRangeList
, QSet
<QByteArray
>());
547 void KFileItemModel::slotClear()
549 #ifdef KFILEITEMMODEL_DEBUG
550 kDebug() << "Clearing all items";
553 m_minimumUpdateIntervalTimer
->stop();
554 m_maximumUpdateIntervalTimer
->stop();
555 m_pendingItemsToInsert
.clear();
557 m_rootExpansionLevel
= -1;
559 const int removedCount
= m_data
.count();
560 if (removedCount
> 0) {
561 m_sortedItems
.clear();
564 emit
itemsRemoved(KItemRangeList() << KItemRange(0, removedCount
));
567 m_expandedUrls
.clear();
570 void KFileItemModel::slotClear(const KUrl
& url
)
575 void KFileItemModel::dispatchPendingItemsToInsert()
577 if (!m_pendingItemsToInsert
.isEmpty()) {
578 insertItems(m_pendingItemsToInsert
);
579 m_pendingItemsToInsert
.clear();
582 if (m_pendingEmitLoadingCompleted
) {
583 emit
loadingCompleted();
587 void KFileItemModel::insertItems(const KFileItemList
& items
)
589 if (items
.isEmpty()) {
593 #ifdef KFILEITEMMODEL_DEBUG
596 kDebug() << "===========================================================";
597 kDebug() << "Inserting" << items
.count() << "items";
600 KFileItemList sortedItems
= items
;
601 sort(sortedItems
.begin(), sortedItems
.end());
603 #ifdef KFILEITEMMODEL_DEBUG
604 kDebug() << "[TIME] Sorting:" << timer
.elapsed();
607 KItemRangeList itemRanges
;
610 int insertedAtIndex
= -1; // Index for the current item-range
611 int insertedCount
= 0; // Count for the current item-range
612 int previouslyInsertedCount
= 0; // Sum of previously inserted items for all ranges
613 while (sourceIndex
< sortedItems
.count()) {
614 // Find target index from m_items to insert the current item
616 const int previousTargetIndex
= targetIndex
;
617 while (targetIndex
< m_sortedItems
.count()) {
618 if (!lessThan(m_sortedItems
.at(targetIndex
), sortedItems
.at(sourceIndex
))) {
624 if (targetIndex
- previousTargetIndex
> 0 && insertedAtIndex
>= 0) {
625 itemRanges
<< KItemRange(insertedAtIndex
, insertedCount
);
626 previouslyInsertedCount
+= insertedCount
;
627 insertedAtIndex
= targetIndex
- previouslyInsertedCount
;
631 // Insert item at the position targetIndex
632 const KFileItem item
= sortedItems
.at(sourceIndex
);
633 m_sortedItems
.insert(targetIndex
, item
);
634 m_data
.insert(targetIndex
, retrieveData(item
));
635 // m_items will be inserted after the loop (see comment below)
638 if (insertedAtIndex
< 0) {
639 insertedAtIndex
= targetIndex
;
640 Q_ASSERT(previouslyInsertedCount
== 0);
646 // The indexes of all m_items must be adjusted, not only the index
648 for (int i
= 0; i
< m_sortedItems
.count(); ++i
) {
649 m_items
.insert(m_sortedItems
.at(i
).url(), i
);
652 itemRanges
<< KItemRange(insertedAtIndex
, insertedCount
);
653 emit
itemsInserted(itemRanges
);
655 #ifdef KFILEITEMMODEL_DEBUG
656 kDebug() << "[TIME] Inserting of" << items
.count() << "items:" << timer
.elapsed();
660 void KFileItemModel::removeItems(const KFileItemList
& items
)
662 if (items
.isEmpty()) {
666 #ifdef KFILEITEMMODEL_DEBUG
667 kDebug() << "Removing " << items
.count() << "items";
670 KFileItemList sortedItems
= items
;
671 sort(sortedItems
.begin(), sortedItems
.end());
673 QList
<int> indexesToRemove
;
674 indexesToRemove
.reserve(items
.count());
676 // Calculate the item ranges that will get deleted
677 KItemRangeList itemRanges
;
678 int removedAtIndex
= -1;
679 int removedCount
= 0;
681 foreach (const KFileItem
& itemToRemove
, sortedItems
) {
682 const int previousTargetIndex
= targetIndex
;
683 while (targetIndex
< m_sortedItems
.count()) {
684 if (m_sortedItems
.at(targetIndex
).url() == itemToRemove
.url()) {
689 if (targetIndex
>= m_sortedItems
.count()) {
690 kWarning() << "Item that should be deleted has not been found!";
694 if (targetIndex
- previousTargetIndex
> 0 && removedAtIndex
>= 0) {
695 itemRanges
<< KItemRange(removedAtIndex
, removedCount
);
696 removedAtIndex
= targetIndex
;
700 indexesToRemove
.append(targetIndex
);
701 if (removedAtIndex
< 0) {
702 removedAtIndex
= targetIndex
;
709 for (int i
= indexesToRemove
.count() - 1; i
>= 0; --i
) {
710 const int indexToRemove
= indexesToRemove
.at(i
);
711 m_items
.remove(m_sortedItems
.at(indexToRemove
).url());
712 m_sortedItems
.removeAt(indexToRemove
);
713 m_data
.removeAt(indexToRemove
);
716 // The indexes of all m_items must be adjusted, not only the index
717 // of the removed items
718 for (int i
= 0; i
< m_sortedItems
.count(); ++i
) {
719 m_items
.insert(m_sortedItems
.at(i
).url(), i
);
723 m_rootExpansionLevel
= -1;
726 itemRanges
<< KItemRange(removedAtIndex
, removedCount
);
727 emit
itemsRemoved(itemRanges
);
730 void KFileItemModel::resortAllItems()
732 const int itemCount
= count();
733 if (itemCount
<= 0) {
737 const KFileItemList oldSortedItems
= m_sortedItems
;
738 const QHash
<KUrl
, int> oldItems
= m_items
;
739 const QList
<QHash
<QByteArray
, QVariant
> > oldData
= m_data
;
744 sort(m_sortedItems
.begin(), m_sortedItems
.end());
746 foreach (const KFileItem
& item
, m_sortedItems
) {
747 m_items
.insert(item
.url(), index
);
749 const int oldItemIndex
= oldItems
.value(item
.url());
750 m_data
.append(oldData
.at(oldItemIndex
));
755 bool emitItemsMoved
= false;
756 QList
<int> movedToIndexes
;
757 movedToIndexes
.reserve(m_sortedItems
.count());
758 for (int i
= 0; i
< itemCount
; i
++) {
759 const int newIndex
= m_items
.value(oldSortedItems
.at(i
).url());
760 movedToIndexes
.append(newIndex
);
761 if (!emitItemsMoved
&& newIndex
!= i
) {
762 emitItemsMoved
= true;
766 if (emitItemsMoved
) {
767 emit
itemsMoved(KItemRange(0, itemCount
), movedToIndexes
);
771 void KFileItemModel::removeExpandedItems()
773 KFileItemList expandedItems
;
775 const int maxIndex
= m_data
.count() - 1;
776 for (int i
= 0; i
<= maxIndex
; ++i
) {
777 if (m_data
.at(i
).value("expansionLevel").toInt() > 0) {
778 const KFileItem fileItem
= m_sortedItems
.at(i
);
779 expandedItems
.append(fileItem
);
783 // The m_rootExpansionLevel may not get reset before all items with
784 // a bigger expansionLevel have been removed.
785 Q_ASSERT(m_rootExpansionLevel
>= 0);
786 removeItems(expandedItems
);
788 m_rootExpansionLevel
= -1;
789 m_expandedUrls
.clear();
792 void KFileItemModel::resetRoles()
794 for (int i
= 0; i
< RolesCount
; ++i
) {
795 m_requestRole
[i
] = false;
799 KFileItemModel::Role
KFileItemModel::roleIndex(const QByteArray
& role
) const
801 static QHash
<QByteArray
, Role
> rolesHash
;
802 if (rolesHash
.isEmpty()) {
803 rolesHash
.insert("name", NameRole
);
804 rolesHash
.insert("size", SizeRole
);
805 rolesHash
.insert("date", DateRole
);
806 rolesHash
.insert("permissions", PermissionsRole
);
807 rolesHash
.insert("owner", OwnerRole
);
808 rolesHash
.insert("group", GroupRole
);
809 rolesHash
.insert("type", TypeRole
);
810 rolesHash
.insert("destination", DestinationRole
);
811 rolesHash
.insert("path", PathRole
);
812 rolesHash
.insert("isDir", IsDirRole
);
813 rolesHash
.insert("isExpanded", IsExpandedRole
);
814 rolesHash
.insert("expansionLevel", ExpansionLevelRole
);
816 return rolesHash
.value(role
, NoRole
);
819 QHash
<QByteArray
, QVariant
> KFileItemModel::retrieveData(const KFileItem
& item
) const
821 // It is important to insert only roles that are fast to retrieve. E.g.
822 // KFileItem::iconName() can be very expensive if the MIME-type is unknown
823 // and hence will be retrieved asynchronously by KFileItemModelRolesUpdater.
824 QHash
<QByteArray
, QVariant
> data
;
825 data
.insert("iconPixmap", QPixmap());
827 const bool isDir
= item
.isDir();
828 if (m_requestRole
[IsDirRole
]) {
829 data
.insert("isDir", isDir
);
832 if (m_requestRole
[NameRole
]) {
833 data
.insert("name", item
.name());
836 if (m_requestRole
[SizeRole
]) {
838 data
.insert("size", QVariant());
840 data
.insert("size", item
.size());
844 if (m_requestRole
[DateRole
]) {
845 // Don't use KFileItem::timeString() as this is too expensive when
846 // having several thousands of items. Instead the formatting of the
847 // date-time will be done on-demand by the view when the date will be shown.
848 const KDateTime dateTime
= item
.time(KFileItem::ModificationTime
);
849 data
.insert("date", dateTime
.dateTime());
852 if (m_requestRole
[PermissionsRole
]) {
853 data
.insert("permissions", item
.permissionsString());
856 if (m_requestRole
[OwnerRole
]) {
857 data
.insert("owner", item
.user());
860 if (m_requestRole
[GroupRole
]) {
861 data
.insert("group", item
.group());
864 if (m_requestRole
[DestinationRole
]) {
865 QString destination
= item
.linkDest();
866 if (destination
.isEmpty()) {
867 destination
= i18nc("@item:intable", "No destination");
869 data
.insert("destination", destination
);
872 if (m_requestRole
[PathRole
]) {
873 data
.insert("path", item
.localPath());
876 if (m_requestRole
[IsExpandedRole
]) {
877 data
.insert("isExpanded", false);
880 if (m_requestRole
[ExpansionLevelRole
]) {
881 if (m_rootExpansionLevel
< 0) {
882 KDirLister
* dirLister
= m_dirLister
.data();
884 const QString rootDir
= dirLister
->url().directory(KUrl::AppendTrailingSlash
);
885 m_rootExpansionLevel
= rootDir
.count('/');
888 const QString dir
= item
.url().directory(KUrl::AppendTrailingSlash
);
889 const int level
= dir
.count('/') - m_rootExpansionLevel
- 1;
890 data
.insert("expansionLevel", level
);
893 if (item
.isMimeTypeKnown()) {
894 data
.insert("iconName", item
.iconName());
896 if (m_requestRole
[TypeRole
]) {
897 data
.insert("type", item
.mimeComment());
904 bool KFileItemModel::lessThan(const KFileItem
& a
, const KFileItem
& b
) const
908 if (m_rootExpansionLevel
>= 0) {
909 result
= expansionLevelsCompare(a
, b
);
911 // The items have parents with different expansion levels
912 return (sortOrder() == Qt::AscendingOrder
) ? result
< 0 : result
> 0;
916 if (m_sortFoldersFirst
|| m_sortRole
== SizeRole
) {
917 const bool isDirA
= a
.isDir();
918 const bool isDirB
= b
.isDir();
919 if (isDirA
&& !isDirB
) {
921 } else if (!isDirA
&& isDirB
) {
926 switch (m_sortRole
) {
928 result
= stringCompare(a
.text(), b
.text());
930 // KFileItem::text() may not be unique in case UDS_DISPLAY_NAME is used
931 result
= stringCompare(a
.name(m_caseSensitivity
== Qt::CaseInsensitive
),
932 b
.name(m_caseSensitivity
== Qt::CaseInsensitive
));
938 const KDateTime dateTimeA
= a
.time(KFileItem::ModificationTime
);
939 const KDateTime dateTimeB
= b
.time(KFileItem::ModificationTime
);
940 if (dateTimeA
< dateTimeB
) {
942 } else if (dateTimeA
> dateTimeB
) {
949 // TODO: Implement sorting folders by the number of items inside.
950 // This is more tricky to get right because this number is retrieved
951 // asynchronously by KFileItemModelRolesUpdater.
952 const KIO::filesize_t sizeA
= a
.size();
953 const KIO::filesize_t sizeB
= b
.size();
956 } else if (sizeA
> sizeB
) {
967 // It must be assured that the sort order is always unique even if two values have been
968 // equal. In this case a comparison of the URL is done which is unique in all cases
969 // within KDirLister.
970 result
= QString::compare(a
.url().url(), b
.url().url(), Qt::CaseSensitive
);
973 return (sortOrder() == Qt::AscendingOrder
) ? result
< 0 : result
> 0;
976 void KFileItemModel::sort(const KFileItemList::iterator
& startIterator
, const KFileItemList::iterator
& endIterator
)
978 KFileItemList::iterator start
= startIterator
;
979 KFileItemList::iterator end
= endIterator
;
981 // The implementation is based on qSortHelper() from qalgorithms.h
982 // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
983 // In opposite to qSort() it allows to use a member-function for the comparison of elements.
985 int span
= int(end
- start
);
991 KFileItemList::iterator low
= start
, high
= end
- 1;
992 KFileItemList::iterator pivot
= start
+ span
/ 2;
994 if (lessThan(*end
, *start
)) {
1001 if (lessThan(*pivot
, *start
)) {
1002 qSwap(*pivot
, *start
);
1004 if (lessThan(*end
, *pivot
)) {
1005 qSwap(*end
, *pivot
);
1011 qSwap(*pivot
, *end
);
1013 while (low
< high
) {
1014 while (low
< high
&& lessThan(*low
, *end
)) {
1018 while (high
> low
&& lessThan(*end
, *high
)) {
1030 if (lessThan(*low
, *end
)) {
1042 int KFileItemModel::stringCompare(const QString
& a
, const QString
& b
) const
1044 // Taken from KDirSortFilterProxyModel (kdelibs/kfile/kdirsortfilterproxymodel.*)
1045 // Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at>
1046 // Copyright (C) 2006 by Dominic Battre <dominic@battre.de>
1047 // Copyright (C) 2006 by Martin Pool <mbp@canonical.com>
1049 if (m_caseSensitivity
== Qt::CaseInsensitive
) {
1050 const int result
= m_naturalSorting
? KStringHandler::naturalCompare(a
, b
, Qt::CaseInsensitive
)
1051 : QString::compare(a
, b
, Qt::CaseInsensitive
);
1053 // Only return the result, if the strings are not equal. If they are equal by a case insensitive
1054 // comparison, still a deterministic sort order is required. A case sensitive
1055 // comparison is done as fallback.
1060 return m_naturalSorting
? KStringHandler::naturalCompare(a
, b
, Qt::CaseSensitive
)
1061 : QString::compare(a
, b
, Qt::CaseSensitive
);
1064 int KFileItemModel::expansionLevelsCompare(const KFileItem
& a
, const KFileItem
& b
) const
1066 const KUrl urlA
= a
.url();
1067 const KUrl urlB
= b
.url();
1068 if (urlA
.directory() == urlB
.directory()) {
1069 // Both items have the same directory as parent
1073 // Check whether one item is the parent of the other item
1074 if (urlA
.isParentOf(urlB
)) {
1076 } else if (urlB
.isParentOf(urlA
)) {
1080 // Determine the maximum common path of both items and
1081 // remember the index in 'index'
1082 const QString pathA
= urlA
.path();
1083 const QString pathB
= urlB
.path();
1085 const int maxIndex
= qMin(pathA
.length(), pathB
.length()) - 1;
1087 while (index
<= maxIndex
&& pathA
.at(index
) == pathB
.at(index
)) {
1090 if (index
> maxIndex
) {
1093 while ((pathA
.at(index
) != QLatin1Char('/') || pathB
.at(index
) != QLatin1Char('/')) && index
> 0) {
1097 // Determine the first sub-path after the common path and
1098 // check whether it represents a directory or already a file
1100 const QString subPathA
= subPath(a
, pathA
, index
, &isDirA
);
1102 const QString subPathB
= subPath(b
, pathB
, index
, &isDirB
);
1104 if (isDirA
&& !isDirB
) {
1106 } else if (!isDirA
&& isDirB
) {
1110 return stringCompare(subPathA
, subPathB
);
1113 QString
KFileItemModel::subPath(const KFileItem
& item
,
1114 const QString
& itemPath
,
1119 const int pathIndex
= itemPath
.indexOf('/', start
+ 1);
1120 *isDir
= (pathIndex
> 0) || item
.isDir();
1121 return itemPath
.mid(start
, pathIndex
- start
);
1124 bool KFileItemModel::useMaximumUpdateInterval() const
1126 const KDirLister
* dirLister
= m_dirLister
.data();
1127 return dirLister
&& !dirLister
->url().isLocalFile();
1130 #include "kfileitemmodel.moc"