]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/kitemviews/kfileitemmodel.cpp
Re-organize the code that compares expanded items
[dolphin.git] / src / kitemviews / kfileitemmodel.cpp
index efc7b326e61e3510f1df929796ceab5e35d35d7e..60fc27546bb435e5d73f7378be41334cd50eec49 100644 (file)
@@ -1,5 +1,6 @@
 /***************************************************************************
  *   Copyright (C) 2011 by Peter Penz <peter.penz19@gmail.com>             *
+ *   Copyright (C) 2013 by Frank Reininghaus <frank78ac@googlemail.com>    *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
  *   it under the terms of the GNU General Public License as published by  *
 
 #include "kfileitemmodel.h"
 
-#include <KDirLister>
 #include <KDirModel>
 #include <KGlobalSettings>
 #include <KLocale>
 #include <KStringHandler>
 #include <KDebug>
 
+#include "private/kfileitemmodelsortalgorithm.h"
+#include "private/kfileitemmodeldirlister.h"
+
+#include <QApplication>
 #include <QMimeData>
 #include <QTimer>
+#include <QWidget>
 
 // #define KFILEITEMMODEL_DEBUG
 
-KFileItemModel::KFileItemModel(KDirLister* dirLister, QObject* parent) :
-    KItemModelBase("name", parent),
-    m_dirLister(dirLister),
+KFileItemModel::KFileItemModel(QObject* parent) :
+    KItemModelBase("text", parent),
+    m_dirLister(0),
     m_naturalSorting(KGlobalSettings::naturalSorting()),
-    m_sortFoldersFirst(true),
+    m_sortDirsFirst(true),
     m_sortRole(NameRole),
+    m_sortingProgressPercent(-1),
     m_roles(),
     m_caseSensitivity(Qt::CaseInsensitive),
     m_itemData(),
@@ -49,25 +55,38 @@ KFileItemModel::KFileItemModel(KDirLister* dirLister, QObject* parent) :
     m_pendingItemsToInsert(),
     m_groups(),
     m_expandedParentsCountRoot(UninitializedExpandedParentsCountRoot),
-    m_expandedUrls(),
+    m_expandedDirs(),
     m_urlsToExpand()
 {
+    m_dirLister = new KFileItemModelDirLister(this);
+    m_dirLister->setDelayedMimeTypes(true);
+
+    const QWidget* parentWidget = qobject_cast<QWidget*>(parent);
+    if (parentWidget) {
+        m_dirLister->setMainWindow(parentWidget->window());
+    }
+
+    connect(m_dirLister, SIGNAL(started(KUrl)), this, SIGNAL(directoryLoadingStarted()));
+    connect(m_dirLister, SIGNAL(canceled()), this, SLOT(slotCanceled()));
+    connect(m_dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted()));
+    connect(m_dirLister, SIGNAL(newItems(KFileItemList)), this, SLOT(slotNewItems(KFileItemList)));
+    connect(m_dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SLOT(slotItemsDeleted(KFileItemList)));
+    connect(m_dirLister, SIGNAL(refreshItems(QList<QPair<KFileItem,KFileItem> >)), this, SLOT(slotRefreshItems(QList<QPair<KFileItem,KFileItem> >)));
+    connect(m_dirLister, SIGNAL(clear()), this, SLOT(slotClear()));
+    connect(m_dirLister, SIGNAL(clear(KUrl)), this, SLOT(slotClear(KUrl)));
+    connect(m_dirLister, SIGNAL(infoMessage(QString)), this, SIGNAL(infoMessage(QString)));
+    connect(m_dirLister, SIGNAL(errorMessage(QString)), this, SIGNAL(errorMessage(QString)));
+    connect(m_dirLister, SIGNAL(redirection(KUrl,KUrl)), this, SIGNAL(directoryRedirection(KUrl,KUrl)));
+    connect(m_dirLister, SIGNAL(urlIsFileError(KUrl)), this, SIGNAL(urlIsFileError(KUrl)));
+
     // Apply default roles that should be determined
     resetRoles();
     m_requestRole[NameRole] = true;
     m_requestRole[IsDirRole] = true;
-    m_roles.insert("name");
+    m_requestRole[IsLinkRole] = true;
+    m_roles.insert("text");
     m_roles.insert("isDir");
-
-    Q_ASSERT(dirLister);
-
-    connect(dirLister, SIGNAL(canceled()), this, SLOT(slotCanceled()));
-    connect(dirLister, SIGNAL(completed(KUrl)), this, SLOT(slotCompleted()));
-    connect(dirLister, SIGNAL(newItems(KFileItemList)), this, SLOT(slotNewItems(KFileItemList)));
-    connect(dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SLOT(slotItemsDeleted(KFileItemList)));
-    connect(dirLister, SIGNAL(refreshItems(QList<QPair<KFileItem,KFileItem> >)), this, SLOT(slotRefreshItems(QList<QPair<KFileItem,KFileItem> >)));
-    connect(dirLister, SIGNAL(clear()), this, SLOT(slotClear()));
-    connect(dirLister, SIGNAL(clear(KUrl)), this, SLOT(slotClear(KUrl)));
+    m_roles.insert("isLink");
 
     // For slow KIO-slaves like used for searching it makes sense to show results periodically even
     // before the completed() or canceled() signal has been emitted.
@@ -94,6 +113,26 @@ KFileItemModel::~KFileItemModel()
     m_itemData.clear();
 }
 
+void KFileItemModel::loadDirectory(const KUrl& url)
+{
+    m_dirLister->openUrl(url);
+}
+
+void KFileItemModel::refreshDirectory(const KUrl& url)
+{
+    m_dirLister->openUrl(url, KDirLister::Reload);
+}
+
+KUrl KFileItemModel::directory() const
+{
+    return m_dirLister->url();
+}
+
+void KFileItemModel::cancelDirectoryLoading()
+{
+    m_dirLister->stop();
+}
+
 int KFileItemModel::count() const
 {
     return m_itemData.count();
@@ -134,6 +173,12 @@ bool KFileItemModel::setData(int index, const QHash<QByteArray, QVariant>& value
     }
 
     m_itemData[index]->values = currentValues;
+    if (changedRoles.contains("text")) {
+        KUrl url = m_itemData[index]->item.url();
+        url.setFileName(currentValues["text"].toString());
+        m_itemData[index]->item.setUrl(url);
+    }
+
     emit itemsChanged(KItemRangeList() << KItemRange(index, 1), changedRoles);
 
     if (changedRoles.contains(sortRole())) {
@@ -143,49 +188,41 @@ bool KFileItemModel::setData(int index, const QHash<QByteArray, QVariant>& value
     return true;
 }
 
-void KFileItemModel::setSortFoldersFirst(bool foldersFirst)
+void KFileItemModel::setSortDirectoriesFirst(bool dirsFirst)
 {
-    if (foldersFirst != m_sortFoldersFirst) {
-        m_sortFoldersFirst = foldersFirst;
+    if (dirsFirst != m_sortDirsFirst) {
+        m_sortDirsFirst = dirsFirst;
         resortAllItems();
     }
 }
 
-bool KFileItemModel::sortFoldersFirst() const
+bool KFileItemModel::sortDirectoriesFirst() const
 {
-    return m_sortFoldersFirst;
+    return m_sortDirsFirst;
 }
 
 void KFileItemModel::setShowHiddenFiles(bool show)
 {
-    KDirLister* dirLister = m_dirLister.data();
-    if (dirLister) {
-        dirLister->setShowingDotFiles(show);
-        dirLister->emitChanges();
-        if (show) {
-            slotCompleted();
-        }
+    m_dirLister->setShowingDotFiles(show);
+    m_dirLister->emitChanges();
+    if (show) {
+        slotCompleted();
     }
 }
 
 bool KFileItemModel::showHiddenFiles() const
 {
-    const KDirLister* dirLister = m_dirLister.data();
-    return dirLister ? dirLister->showingDotFiles() : false;
+    return m_dirLister->showingDotFiles();
 }
 
-void KFileItemModel::setShowFoldersOnly(bool enabled)
+void KFileItemModel::setShowDirectoriesOnly(bool enabled)
 {
-    KDirLister* dirLister = m_dirLister.data();
-    if (dirLister) {
-        dirLister->setDirOnlyMode(enabled);
-    }
+    m_dirLister->setDirOnlyMode(enabled);
 }
 
-bool KFileItemModel::showFoldersOnly() const
+bool KFileItemModel::showDirectoriesOnly() const
 {
-    KDirLister* dirLister = m_dirLister.data();
-    return dirLister ? dirLister->dirOnlyMode() : false;
+    return m_dirLister->dirOnlyMode();
 }
 
 QMimeData* KFileItemModel::createMimeData(const QSet<int>& indexes) const
@@ -230,12 +267,12 @@ int KFileItemModel::indexForKeyboardSearch(const QString& text, int startFromInd
 {
     startFromIndex = qMax(0, startFromIndex);
     for (int i = startFromIndex; i < count(); ++i) {
-        if (data(i)["name"].toString().startsWith(text, Qt::CaseInsensitive)) {
+        if (data(i)["text"].toString().startsWith(text, Qt::CaseInsensitive)) {
             return i;
         }
     }
     for (int i = 0; i < startFromIndex; ++i) {
-        if (data(i)["name"].toString().startsWith(text, Qt::CaseInsensitive)) {
+        if (data(i)["text"].toString().startsWith(text, Qt::CaseInsensitive)) {
             return i;
         }
     }
@@ -255,7 +292,7 @@ QString KFileItemModel::roleDescription(const QByteArray& role) const
         int count = 0;
         const RoleInfoMap* map = rolesInfoMap(count);
         for (int i = 0; i < count; ++i) {
-            description.insert(map[i].role, map[i].roleTranslation);
+            description.insert(map[i].role, i18nc(map[i].roleTranslationContext, map[i].roleTranslation));
         }
     }
 
@@ -274,19 +311,8 @@ QList<QPair<int, QVariant> > KFileItemModel::groups() const
         case SizeRole:        m_groups = sizeRoleGroups(); break;
         case DateRole:        m_groups = dateRoleGroups(); break;
         case PermissionsRole: m_groups = permissionRoleGroups(); break;
-        case OwnerRole:       m_groups = genericStringRoleGroups("owner"); break;
-        case GroupRole:       m_groups = genericStringRoleGroups("group"); break;
-        case TypeRole:        m_groups = genericStringRoleGroups("type"); break;
-        case DestinationRole: m_groups = genericStringRoleGroups("destination"); break;
-        case PathRole:        m_groups = genericStringRoleGroups("path"); break;
-        case CommentRole:     m_groups = genericStringRoleGroups("comment"); break;
-        case TagsRole:        m_groups = genericStringRoleGroups("tags"); break;
         case RatingRole:      m_groups = ratingRoleGroups(); break;
-        case NoRole:          break;
-        case IsDirRole:       break;
-        case IsExpandedRole:  break;
-        case ExpandedParentsCountRole: break;
-        default:                 Q_ASSERT(false); break;
+        default:              m_groups = genericStringRoleGroups(sortRole()); break;
         }
 
 #ifdef KFILEITEMMODEL_DEBUG
@@ -333,11 +359,7 @@ int KFileItemModel::index(const KUrl& url) const
 
 KFileItem KFileItemModel::rootItem() const
 {
-    const KDirLister* dirLister = m_dirLister.data();
-    if (dirLister) {
-        return dirLister->rootItem();
-    }
-    return KFileItem();
+    return m_dirLister->rootItem();
 }
 
 void KFileItemModel::clear()
@@ -400,21 +422,14 @@ bool KFileItemModel::setExpanded(int index, bool expanded)
         return false;
     }
 
-    KDirLister* dirLister = m_dirLister.data();
     const KUrl url = m_itemData.at(index)->item.url();
     if (expanded) {
-        m_expandedUrls.insert(url);
-
-        if (dirLister) {
-            dirLister->openUrl(url, KDirLister::Keep);
-            return true;
-        }
+        m_expandedDirs.insert(url);
+        m_dirLister->openUrl(url, KDirLister::Keep);
     } else {
-        m_expandedUrls.remove(url);
+        m_expandedDirs.remove(url);
+        m_dirLister->stop(url);
 
-        if (dirLister) {
-            dirLister->stop(url);
-        }
 
         KFileItemList itemsToRemove;
         const int expandedParentsCount = data(index)["expandedParentsCount"].toInt();
@@ -424,10 +439,9 @@ bool KFileItemModel::setExpanded(int index, bool expanded)
             ++index;
         }
         removeItems(itemsToRemove);
-        return true;
     }
 
-    return false;
+    return true;
 }
 
 bool KFileItemModel::isExpanded(int index) const
@@ -457,30 +471,25 @@ int KFileItemModel::expandedParentsCount(int index) const
     return 0;
 }
 
-QSet<KUrl> KFileItemModel::expandedUrls() const
+QSet<KUrl> KFileItemModel::expandedDirectories() const
 {
-    return m_expandedUrls;
+    return m_expandedDirs;
 }
 
-void KFileItemModel::restoreExpandedUrls(const QSet<KUrl>& urls)
+void KFileItemModel::restoreExpandedDirectories(const QSet<KUrl>& urls)
 {
     m_urlsToExpand = urls;
 }
 
-void KFileItemModel::expandParentItems(const KUrl& url)
+void KFileItemModel::expandParentDirectories(const KUrl& url)
 {
-    const KDirLister* dirLister = m_dirLister.data();
-    if (!dirLister) {
-        return;
-    }
-
-    const int pos = dirLister->url().path().length();
+    const int pos = m_dirLister->url().path().length();
 
     // Assure that each sub-path of the URL that should be
     // expanded is added to m_urlsToExpand. KDirLister
     // does not care whether the parent-URL has already been
     // expanded.
-    KUrl urlToExpand = dirLister->url();
+    KUrl urlToExpand = m_dirLister->url();
     const QStringList subDirs = url.path().mid(pos).split(QDir::separator());
     for (int i = 0; i < subDirs.count() - 1; ++i) {
         urlToExpand.addPath(subDirs.at(i));
@@ -504,46 +513,64 @@ void KFileItemModel::setNameFilter(const QString& nameFilter)
 {
     if (m_filter.pattern() != nameFilter) {
         dispatchPendingItemsToInsert();
-
         m_filter.setPattern(nameFilter);
+        applyFilters();
+    }
+}
+
+QString KFileItemModel::nameFilter() const
+{
+    return m_filter.pattern();
+}
 
-        // Check which shown items from m_itemData must get
-        // hidden and hence moved to m_filteredItems.
-        KFileItemList newFilteredItems;
+void KFileItemModel::setMimeTypeFilters(const QStringList& filters)
+{
+    if (m_filter.mimeTypes() != filters) {
+        dispatchPendingItemsToInsert();
+        m_filter.setMimeTypes(filters);
+        applyFilters();
+    }
+}
 
-        foreach (ItemData* itemData, m_itemData) {
+QStringList KFileItemModel::mimeTypeFilters() const
+{
+    return m_filter.mimeTypes();
+}
+
+
+void KFileItemModel::applyFilters()
+{
+    // Check which shown items from m_itemData must get
+    // hidden and hence moved to m_filteredItems.
+    KFileItemList newFilteredItems;
+
+    foreach (ItemData* itemData, m_itemData) {
+        // Only filter non-expanded items as child items may never
+        // exist without a parent item
+        if (!itemData->values.value("isExpanded").toBool()) {
             if (!m_filter.matches(itemData->item)) {
-                // Only filter non-expanded items as child items may never
-                // exist without a parent item
-                if (!itemData->values.value("isExpanded").toBool()) {
-                    newFilteredItems.append(itemData->item);
-                    m_filteredItems.insert(itemData->item);
-                }
+                newFilteredItems.append(itemData->item);
+                m_filteredItems.insert(itemData->item);
             }
         }
+    }
 
-        removeItems(newFilteredItems);
+    removeItems(newFilteredItems);
 
-        // Check which hidden items from m_filteredItems should
-        // get visible again and hence removed from m_filteredItems.
-        KFileItemList newVisibleItems;
+    // Check which hidden items from m_filteredItems should
+    // get visible again and hence removed from m_filteredItems.
+    KFileItemList newVisibleItems;
 
-        QMutableSetIterator<KFileItem> it(m_filteredItems);
-        while (it.hasNext()) {
-            const KFileItem item = it.next();
-            if (m_filter.matches(item)) {
-                newVisibleItems.append(item);
-                m_filteredItems.remove(item);
-            }
+    QMutableSetIterator<KFileItem> it(m_filteredItems);
+    while (it.hasNext()) {
+        const KFileItem item = it.next();
+        if (m_filter.matches(item)) {
+            newVisibleItems.append(item);
+            it.remove();
         }
-
-        insertItems(newVisibleItems);
     }
-}
 
-QString KFileItemModel::nameFilter() const
-{
-    return m_filter.pattern();
+    insertItems(newVisibleItems);
 }
 
 QList<KFileItemModel::RoleInfo> KFileItemModel::rolesInformation()
@@ -556,8 +583,17 @@ QList<KFileItemModel::RoleInfo> KFileItemModel::rolesInformation()
             if (map[i].roleType != NoRole) {
                 RoleInfo info;
                 info.role = map[i].role;
-                info.translation = map[i].roleTranslation;
-                info.group = map[i].groupTranslation;
+                info.translation = i18nc(map[i].roleTranslationContext, map[i].roleTranslation);
+                if (map[i].groupTranslation) {
+                    info.group = i18nc(map[i].groupTranslationContext, map[i].groupTranslation);
+                } else {
+                    // For top level roles, groupTranslation is 0. We must make sure that
+                    // info.group is an empty string then because the code that generates
+                    // menus tries to put the actions into sub menus otherwise.
+                    info.group = QString();
+                }
+                info.requiresNepomuk = map[i].requiresNepomuk;
+                info.requiresIndexer = map[i].requiresIndexer;
                 rolesInfo.append(info);
             }
         }
@@ -656,7 +692,7 @@ void KFileItemModel::slotCompleted()
         // Note that the parent folder must be expanded before any of its subfolders become visible.
         // Therefore, some URLs in m_restoredExpandedUrls might not be visible yet
         // -> we expand the first visible URL we find in m_restoredExpandedUrls.
-        foreach(const KUrl& url, m_urlsToExpand) {
+        foreach (const KUrl& url, m_urlsToExpand) {
             const int index = m_items.value(url, -1);
             if (index >= 0) {
                 m_urlsToExpand.remove(url);
@@ -673,13 +709,15 @@ void KFileItemModel::slotCompleted()
         m_urlsToExpand.clear();
     }
 
-    emit loadingCompleted();
+    emit directoryLoadingCompleted();
 }
 
 void KFileItemModel::slotCanceled()
 {
     m_maximumUpdateIntervalTimer->stop();
     dispatchPendingItemsToInsert();
+
+    emit directoryLoadingCanceled();
 }
 
 void KFileItemModel::slotNewItems(const KFileItemList& items)
@@ -716,10 +754,10 @@ void KFileItemModel::slotNewItems(const KFileItemList& items)
         }
     }
 
-    if (m_filter.pattern().isEmpty()) {
+    if (!m_filter.hasSetFilters()) {
         m_pendingItemsToInsert.append(items);
     } else {
-        // The name-filter is active. Hide filtered items
+        // The name or type filter is active. Hide filtered items
         // before inserting them into the model and remember
         // the filtered items in m_filteredItems.
         KFileItemList filteredItems;
@@ -858,7 +896,7 @@ void KFileItemModel::slotClear()
         emit itemsRemoved(KItemRangeList() << KItemRange(0, removedCount));
     }
 
-    m_expandedUrls.clear();
+    m_expandedDirs.clear();
 }
 
 void KFileItemModel::slotClear(const KUrl& url)
@@ -886,6 +924,13 @@ void KFileItemModel::insertItems(const KFileItemList& items)
         return;
     }
 
+    if (m_sortRole == TypeRole) {
+        // Try to resolve the MIME-types synchronously to prevent a reordering of
+        // the items when sorting by type (per default MIME-types are resolved
+        // asynchronously by KFileItemModelRolesUpdater).
+        determineMimeTypes(items, 200);
+    }
+
 #ifdef KFILEITEMMODEL_DEBUG
     QElapsedTimer timer;
     timer.start();
@@ -926,7 +971,7 @@ void KFileItemModel::insertItems(const KFileItemList& items)
             insertedCount = 0;
         }
 
-        // Insert item at the position targetIndex by transfering
+        // Insert item at the position targetIndex by transferring
         // the ownership of the item-data from sortedItems to m_itemData.
         // m_items will be inserted after the loop (see comment below)
         m_itemData.insert(targetIndex, sortedItems.at(sourceIndex));
@@ -943,6 +988,7 @@ void KFileItemModel::insertItems(const KFileItemList& items)
     // The indexes of all m_items must be adjusted, not only the index
     // of the new items
     const int itemDataCount = m_itemData.count();
+    m_items.reserve(itemDataCount);
     for (int i = 0; i < itemDataCount; ++i) {
         m_items.insert(m_itemData.at(i)->item.url(), i);
     }
@@ -955,80 +1001,96 @@ void KFileItemModel::insertItems(const KFileItemList& items)
 #endif
 }
 
-void KFileItemModel::removeItems(const KFileItemList& items)
+static KItemRangeList sortedIndexesToKItemRangeList(const QList<int>& sortedNumbers)
 {
-    if (items.isEmpty()) {
-        return;
+    if (sortedNumbers.empty()) {
+        return KItemRangeList();
+    }
+
+    KItemRangeList result;
+
+    QList<int>::const_iterator it = sortedNumbers.begin();
+    int index = *it;
+    int count = 1;
+
+    ++it;
+
+    QList<int>::const_iterator end = sortedNumbers.end();
+    while (it != end) {
+        if (*it == index + count) {
+            ++count;
+        } else {
+            result << KItemRange(index, count);
+            index = *it;
+            count = 1;
+        }
+        ++it;
     }
 
+    result << KItemRange(index, count);
+    return result;
+}
+
+void KFileItemModel::removeItems(const KFileItemList& items)
+{
 #ifdef KFILEITEMMODEL_DEBUG
     kDebug() << "Removing " << items.count() << "items";
 #endif
 
     m_groups.clear();
 
-    QList<ItemData*> sortedItems;
-    sortedItems.reserve(items.count());
+    // Step 1: Determine the indexes of the removed items, remove them from
+    //         the hash m_items, and free the ItemData.
+    QList<int> indexesToRemove;
+    indexesToRemove.reserve(items.count());
     foreach (const KFileItem& item, items) {
-        const int index = m_items.value(item.url(), -1);
+        const KUrl url = item.url();
+        const int index = m_items.value(url, -1);
         if (index >= 0) {
-            sortedItems.append(m_itemData.at(index));
+            indexesToRemove.append(index);
+
+            // Prevent repeated expensive rehashing by using QHash::erase(),
+            // rather than QHash::remove().
+            QHash<KUrl, int>::iterator it = m_items.find(url);
+            m_items.erase(it);
+
+            ItemData* data = m_itemData.at(index);
+            delete data;
+            m_itemData[index] = 0;
         }
     }
-    sort(sortedItems.begin(), sortedItems.end());
 
-    QList<int> indexesToRemove;
-    indexesToRemove.reserve(items.count());
+    if (indexesToRemove.isEmpty()) {
+        return;
+    }
 
-    // Calculate the item ranges that will get deleted
-    KItemRangeList itemRanges;
-    int removedAtIndex = -1;
-    int removedCount = 0;
-    int targetIndex = 0;
-    foreach (const ItemData* itemData, sortedItems) {
-        const KFileItem& itemToRemove = itemData->item;
+    std::sort(indexesToRemove.begin(), indexesToRemove.end());
 
-        const int previousTargetIndex = targetIndex;
-        while (targetIndex < m_itemData.count()) {
-            if (m_itemData.at(targetIndex)->item.url() == itemToRemove.url()) {
-                break;
-            }
-            ++targetIndex;
-        }
-        if (targetIndex >= m_itemData.count()) {
-            kWarning() << "Item that should be deleted has not been found!";
-            return;
-        }
+    // Step 2: Remove the ItemData pointers from the list m_itemData.
+    const KItemRangeList itemRanges = sortedIndexesToKItemRangeList(indexesToRemove);
+    int target = itemRanges.at(0).index;
+    int source = itemRanges.at(0).index + itemRanges.at(0).count;
+    int nextRange = 1;
 
-        if (targetIndex - previousTargetIndex > 0 && removedAtIndex >= 0) {
-            itemRanges << KItemRange(removedAtIndex, removedCount);
-            removedAtIndex = targetIndex;
-            removedCount = 0;
-        }
+    const int oldItemDataCount = m_itemData.count();
+    while (source < oldItemDataCount) {
+        m_itemData[target] = m_itemData[source];
+        ++target;
+        ++source;
 
-        indexesToRemove.append(targetIndex);
-        if (removedAtIndex < 0) {
-            removedAtIndex = targetIndex;
+        if (nextRange < itemRanges.count() && source == itemRanges.at(nextRange).index) {
+            // Skip the items in the next removed range.
+            source += itemRanges.at(nextRange).count;
+            ++nextRange;
         }
-        ++removedCount;
-        ++targetIndex;
     }
 
-    // Delete the items
-    for (int i = indexesToRemove.count() - 1; i >= 0; --i) {
-        const int indexToRemove = indexesToRemove.at(i);
-        ItemData* data = m_itemData.at(indexToRemove);
+    m_itemData.erase(m_itemData.end() - indexesToRemove.count(), m_itemData.end());
 
-        m_items.remove(data->item.url());
-
-        delete data;
-        m_itemData.removeAt(indexToRemove);
-    }
-
-    // The indexes of all m_items must be adjusted, not only the index
-    // of the removed items
-    const int itemDataCount = m_itemData.count();
-    for (int i = 0; i < itemDataCount; ++i) {
+    // Step 3: Adjust indexes in the hash m_items. Note that all indexes
+    //         might have been changed by the removal of the items.
+    const int newItemDataCount = m_itemData.count();
+    for (int i = 0; i < newItemDataCount; ++i) {
         m_items.insert(m_itemData.at(i)->item.url(), i);
     }
 
@@ -1036,7 +1098,6 @@ void KFileItemModel::removeItems(const KFileItemList& items)
         m_expandedParentsCountRoot = UninitializedExpandedParentsCountRoot;
     }
 
-    itemRanges << KItemRange(removedAtIndex, removedCount);
     emit itemsRemoved(itemRanges);
 }
 
@@ -1087,7 +1148,7 @@ void KFileItemModel::removeExpandedItems()
     removeItems(expandedItems);
 
     m_expandedParentsCountRoot = UninitializedExpandedParentsCountRoot;
-    m_expandedUrls.clear();
+    m_expandedDirs.clear();
 }
 
 void KFileItemModel::resetRoles()
@@ -1112,6 +1173,7 @@ KFileItemModel::RoleType KFileItemModel::typeForRole(const QByteArray& role) con
         // Insert internal roles (take care to synchronize the implementation
         // with KFileItemModel::roleForType() in case if a change is done).
         roles.insert("isDir", IsDirRole);
+        roles.insert("isLink", IsLinkRole);
         roles.insert("isExpanded", IsExpandedRole);
         roles.insert("isExpandable", IsExpandableRole);
         roles.insert("expandedParentsCount", ExpandedParentsCountRole);
@@ -1137,6 +1199,7 @@ QByteArray KFileItemModel::roleForType(RoleType roleType) const
         // Insert internal roles (take care to synchronize the implementation
         // with KFileItemModel::typeForRole() in case if a change is done).
         roles.insert(IsDirRole, "isDir");
+        roles.insert(IsLinkRole, "isLink");
         roles.insert(IsExpandedRole, "isExpanded");
         roles.insert(IsExpandableRole, "isExpandable");
         roles.insert(ExpandedParentsCountRole, "expandedParentsCount");
@@ -1160,8 +1223,13 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item)
         data.insert("isDir", isDir);
     }
 
+    if (m_requestRole[IsLinkRole]) {
+        const bool isLink = item.isLink();
+        data.insert("isLink", isLink);
+    }
+
     if (m_requestRole[NameRole]) {
-        data.insert("name", item.text());
+        data.insert("text", item.text());
     }
 
     if (m_requestRole[SizeRole]) {
@@ -1195,7 +1263,7 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item)
     if (m_requestRole[DestinationRole]) {
         QString destination = item.linkDest();
         if (destination.isEmpty()) {
-            destination = i18nc("@item:intable", "No destination");
+            destination = QLatin1String("-");
         }
         data.insert("destination", destination);
     }
@@ -1205,7 +1273,17 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item)
         if (item.url().protocol() == QLatin1String("trash")) {
             path = item.entry().stringValue(KIO::UDSEntry::UDS_EXTRA);
         } else {
+            // For performance reasons cache the home-path in a static QString
+            // (see QDir::homePath() for more details)
+            static QString homePath;
+            if (homePath.isEmpty()) {
+                homePath = QDir::homePath();
+            }
+
             path = item.localPath();
+            if (path.startsWith(homePath)) {
+                path.replace(0, homePath.length(), QLatin1Char('~'));
+            }
         }
 
         const int index = path.lastIndexOf(item.text());
@@ -1222,8 +1300,8 @@ QHash<QByteArray, QVariant> KFileItemModel::retrieveData(const KFileItem& item)
     }
 
     if (m_requestRole[ExpandedParentsCountRole]) {
-        if (m_expandedParentsCountRoot == UninitializedExpandedParentsCountRoot && m_dirLister.data()) {
-            const KUrl rootUrl = m_dirLister.data()->url();
+        if (m_expandedParentsCountRoot == UninitializedExpandedParentsCountRoot) {
+            const KUrl rootUrl = m_dirLister->url();
             const QString protocol = rootUrl.protocol();
             const bool forceExpandedParentsCountRoot = (protocol == QLatin1String("trash") ||
                                                         protocol == QLatin1String("nepomuk") ||
@@ -1261,15 +1339,38 @@ bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const
 {
     int result = 0;
 
-    if (m_expandedParentsCountRoot >= 0) {
-        result = expandedParentsCountCompare(a, b);
-        if (result != 0) {
-            // The items have parents with different expansion levels
-            return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0;
+    if (m_expandedParentsCountRoot >= 0 && a->parent != b->parent) {
+        const int expansionLevelA = a->values.value("expandedParentsCount").toInt();
+        const int expansionLevelB = b->values.value("expandedParentsCount").toInt();
+
+        // If b has a higher expansion level than a, check if a is a parent
+        // of b, and make sure that both expansion levels are equal otherwise.
+        for (int i = expansionLevelB; i > expansionLevelA; --i) {
+            if (b->parent == a) {
+                return true;
+            }
+            b = b->parent;
+        }
+
+        // If a has a higher expansion level than a, check if b is a parent
+        // of a, and make sure that both expansion levels are equal otherwise.
+        for (int i = expansionLevelA; i > expansionLevelB; --i) {
+            if (a->parent == b) {
+                return false;
+            }
+            a = a->parent;
+        }
+
+        Q_ASSERT(a->values.value("expandedParentsCount").toInt() == b->values.value("expandedParentsCount").toInt());
+
+        // Compare the last parents of a and b which are different.
+        while (a->parent != b->parent) {
+            a = a->parent;
+            b = b->parent;
         }
     }
 
-    if (m_sortFoldersFirst || m_sortRole == SizeRole) {
+    if (m_sortDirsFirst || m_sortRole == SizeRole) {
         const bool isDirA = a->item.isDir();
         const bool isDirB = b->item.isDir();
         if (isDirA && !isDirB) {
@@ -1284,6 +1385,44 @@ bool KFileItemModel::lessThan(const ItemData* a, const ItemData* b) const
     return (sortOrder() == Qt::AscendingOrder) ? result < 0 : result > 0;
 }
 
+/**
+ * Helper class for KFileItemModel::sort().
+ */
+class KFileItemModelLessThan
+{
+public:
+    KFileItemModelLessThan(const KFileItemModel* model) :
+        m_model(model)
+    {
+    }
+
+    bool operator()(const KFileItemModel::ItemData* a, const KFileItemModel::ItemData* b) const
+    {
+        return m_model->lessThan(a, b);
+    }
+
+private:
+    const KFileItemModel* m_model;
+};
+
+void KFileItemModel::sort(QList<KFileItemModel::ItemData*>::iterator begin,
+                          QList<KFileItemModel::ItemData*>::iterator end) const
+{
+    KFileItemModelLessThan lessThan(this);
+
+    if (m_sortRole == NameRole) {
+        // Sorting by name can be expensive, in particular if natural sorting is
+        // enabled. Use all CPU cores to speed up the sorting process.
+        static const int numberOfThreads = QThread::idealThreadCount();
+        parallelMergeSort(begin, end, lessThan, numberOfThreads);
+    } else {
+        // Sorting by other roles is quite fast. Use only one thread to prevent
+        // problems caused by non-reentrant comparison functions, see
+        // https://bugs.kde.org/show_bug.cgi?id=312679
+        mergeSort(begin, end, lessThan);
+    }
+}
+
 int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const
 {
     const KFileItem& itemA = a->item;
@@ -1344,22 +1483,22 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const
         break;
     }
 
-    case PermissionsRole:
-    case OwnerRole:
-    case GroupRole:
-    case TypeRole:
-    case DestinationRole:
-    case PathRole:
-    case CommentRole:
-    case TagsRole: {
+    case ImageSizeRole: {
+        // Alway use a natural comparing to interpret the numbers of a string like
+        // "1600 x 1200" for having a correct sorting.
+        result = KStringHandler::naturalCompare(a->values.value("imageSize").toString(),
+                                                b->values.value("imageSize").toString(),
+                                                Qt::CaseSensitive);
+        break;
+    }
+
+    default: {
         const QByteArray role = roleForType(m_sortRole);
         result = QString::compare(a->values.value(role).toString(),
                                   b->values.value(role).toString());
         break;
     }
 
-    default:
-        break;
     }
 
     if (result != 0) {
@@ -1386,128 +1525,6 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const
     return QString::compare(itemA.url().url(), itemB.url().url(), Qt::CaseSensitive);
 }
 
-void KFileItemModel::sort(QList<ItemData*>::iterator begin,
-                          QList<ItemData*>::iterator end)
-{
-    // The implementation is based on qStableSortHelper() from qalgorithms.h
-    // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-    // In opposite to qStableSort() it allows to use a member-function for the comparison of elements.
-
-    const int span = end - begin;
-    if (span < 2) {
-        return;
-    }
-
-    const QList<ItemData*>::iterator middle = begin + span / 2;
-    sort(begin, middle);
-    sort(middle, end);
-    merge(begin, middle, end);
-}
-
-void KFileItemModel::merge(QList<ItemData*>::iterator begin,
-                           QList<ItemData*>::iterator pivot,
-                           QList<ItemData*>::iterator end)
-{
-    // The implementation is based on qMerge() from qalgorithms.h
-    // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-
-    const int len1 = pivot - begin;
-    const int len2 = end - pivot;
-
-    if (len1 == 0 || len2 == 0) {
-        return;
-    }
-
-    if (len1 + len2 == 2) {
-        if (lessThan(*(begin + 1), *(begin))) {
-            qSwap(*begin, *(begin + 1));
-        }
-        return;
-    }
-
-    QList<ItemData*>::iterator firstCut;
-    QList<ItemData*>::iterator secondCut;
-    int len2Half;
-    if (len1 > len2) {
-        const int len1Half = len1 / 2;
-        firstCut = begin + len1Half;
-        secondCut = lowerBound(pivot, end, *firstCut);
-        len2Half = secondCut - pivot;
-    } else {
-        len2Half = len2 / 2;
-        secondCut = pivot + len2Half;
-        firstCut = upperBound(begin, pivot, *secondCut);
-    }
-
-    reverse(firstCut, pivot);
-    reverse(pivot, secondCut);
-    reverse(firstCut, secondCut);
-
-    const QList<ItemData*>::iterator newPivot = firstCut + len2Half;
-    merge(begin, firstCut, newPivot);
-    merge(newPivot, secondCut, end);
-}
-
-QList<KFileItemModel::ItemData*>::iterator KFileItemModel::lowerBound(QList<ItemData*>::iterator begin,
-                                                                      QList<ItemData*>::iterator end,
-                                                                      const ItemData* value)
-{
-    // The implementation is based on qLowerBound() from qalgorithms.h
-    // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-
-    QList<ItemData*>::iterator middle;
-    int n = int(end - begin);
-    int half;
-
-    while (n > 0) {
-        half = n >> 1;
-        middle = begin + half;
-        if (lessThan(*middle, value)) {
-            begin = middle + 1;
-            n -= half + 1;
-        } else {
-            n = half;
-        }
-    }
-    return begin;
-}
-
-QList<KFileItemModel::ItemData*>::iterator KFileItemModel::upperBound(QList<ItemData*>::iterator begin,
-                                                                      QList<ItemData*>::iterator end,
-                                                                      const ItemData* value)
-{
-    // The implementation is based on qUpperBound() from qalgorithms.h
-    // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-
-    QList<ItemData*>::iterator middle;
-    int n = end - begin;
-    int half;
-
-    while (n > 0) {
-        half = n >> 1;
-        middle = begin + half;
-        if (lessThan(value, *middle)) {
-            n = half;
-        } else {
-            begin = middle + 1;
-            n -= half + 1;
-        }
-    }
-    return begin;
-}
-
-void KFileItemModel::reverse(QList<ItemData*>::iterator begin,
-                             QList<ItemData*>::iterator end)
-{
-    // The implementation is based on qReverse() from qalgorithms.h
-    // Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-
-    --end;
-    while (begin < end) {
-        qSwap(*begin++, *end--);
-    }
-}
-
 int KFileItemModel::stringCompare(const QString& a, const QString& b) const
 {
     // Taken from KDirSortFilterProxyModel (kdelibs/kfile/kdirsortfilterproxymodel.*)
@@ -1530,92 +1547,9 @@ int KFileItemModel::stringCompare(const QString& a, const QString& b) const
                             : QString::compare(a, b, Qt::CaseSensitive);
 }
 
-int KFileItemModel::expandedParentsCountCompare(const ItemData* a, const ItemData* b) const
-{
-    const KUrl urlA = a->item.url();
-    const KUrl urlB = b->item.url();
-    if (urlA.directory() == urlB.directory()) {
-        // Both items have the same directory as parent
-        return 0;
-    }
-
-    // Check whether one item is the parent of the other item
-    if (urlA.isParentOf(urlB)) {
-        return (sortOrder() == Qt::AscendingOrder) ? -1 : +1;
-    } else if (urlB.isParentOf(urlA)) {
-        return (sortOrder() == Qt::AscendingOrder) ? +1 : -1;
-    }
-
-    // Determine the maximum common path of both items and
-    // remember the index in 'index'
-    const QString pathA = urlA.path();
-    const QString pathB = urlB.path();
-
-    const int maxIndex = qMin(pathA.length(), pathB.length()) - 1;
-    int index = 0;
-    while (index <= maxIndex && pathA.at(index) == pathB.at(index)) {
-        ++index;
-    }
-    if (index > maxIndex) {
-        index = maxIndex;
-    }
-    while ((pathA.at(index) != QLatin1Char('/') || pathB.at(index) != QLatin1Char('/')) && index > 0) {
-        --index;
-    }
-
-    // Determine the first sub-path after the common path and
-    // check whether it represents a directory or already a file
-    bool isDirA = true;
-    const QString subPathA = subPath(a->item, pathA, index, &isDirA);
-    bool isDirB = true;
-    const QString subPathB = subPath(b->item, pathB, index, &isDirB);
-
-    if (m_sortFoldersFirst || m_sortRole == SizeRole) {
-        if (isDirA && !isDirB) {
-            return (sortOrder() == Qt::AscendingOrder) ? -1 : +1;
-        } else if (!isDirA && isDirB) {
-            return (sortOrder() == Qt::AscendingOrder) ? +1 : -1;
-        }
-    }
-
-    // Compare the items of the parents that represent the first
-    // different path after the common path.
-    const QString parentPathA = pathA.left(index) + subPathA;
-    const QString parentPathB = pathB.left(index) + subPathB;
-
-    const ItemData* parentA = a;
-    while (parentA && parentA->item.url().path() != parentPathA) {
-        parentA = parentA->parent;
-    }
-
-    const ItemData* parentB = b;
-    while (parentB && parentB->item.url().path() != parentPathB) {
-        parentB = parentB->parent;
-    }
-
-    if (parentA && parentB) {
-        return sortRoleCompare(parentA, parentB);
-    }
-
-    kWarning() << "Child items without parent detected:" << a->item.url() << b->item.url();
-    return QString::compare(urlA.url(), urlB.url(), Qt::CaseSensitive);
-}
-
-QString KFileItemModel::subPath(const KFileItem& item,
-                                const QString& itemPath,
-                                int start,
-                                bool* isDir) const
-{
-    Q_ASSERT(isDir);
-    const int pathIndex = itemPath.indexOf('/', start + 1);
-    *isDir = (pathIndex > 0) || item.isDir();
-    return itemPath.mid(start, pathIndex - start);
-}
-
 bool KFileItemModel::useMaximumUpdateInterval() const
 {
-    const KDirLister* dirLister = m_dirLister.data();
-    return dirLister && !dirLister->url().isLocalFile();
+    return !m_dirLister->url().isLocalFile();
 }
 
 QList<QPair<int, QVariant> > KFileItemModel::nameRoleGroups() const
@@ -1633,7 +1567,7 @@ QList<QPair<int, QVariant> > KFileItemModel::nameRoleGroups() const
             continue;
         }
 
-        const QString name = m_itemData.at(i)->values.value("name").toString();
+        const QString name = m_itemData.at(i)->values.value("text").toString();
 
         // Use the first character of the name as group indication
         QChar newFirstChar = name.at(0).toUpper();
@@ -1950,36 +1884,125 @@ KFileItemList KFileItemModel::childItems(const KFileItem& item) const
     return items;
 }
 
+void KFileItemModel::emitSortProgress(int resolvedCount)
+{
+    // Be tolerant against a resolvedCount with a wrong range.
+    // Although there should not be a case where KFileItemModelRolesUpdater
+    // (= caller) provides a wrong range, it is important to emit
+    // a useful progress information even if there is an unexpected
+    // implementation issue.
+
+    const int itemCount = count();
+    if (resolvedCount >= itemCount) {
+        m_sortingProgressPercent = -1;
+        if (m_resortAllItemsTimer->isActive()) {
+            m_resortAllItemsTimer->stop();
+            resortAllItems();
+        }
+
+        emit directorySortingProgress(100);
+    } else if (itemCount > 0) {
+        resolvedCount = qBound(0, resolvedCount, itemCount);
+
+        const int progress = resolvedCount * 100 / itemCount;
+        if (m_sortingProgressPercent != progress) {
+            m_sortingProgressPercent = progress;
+            emit directorySortingProgress(progress);
+        }
+    }
+}
+
 const KFileItemModel::RoleInfoMap* KFileItemModel::rolesInfoMap(int& count)
 {
     static const RoleInfoMap rolesInfoMap[] = {
-    //    role           roleType         role translation                                  group translation
-        { 0,             NoRole,          0, 0,                                             0, 0 },
-        { "name",        NameRole,        I18N_NOOP2_NOSTRIP("@label", "Name"),             0, 0 },
-        { "size",        SizeRole,        I18N_NOOP2_NOSTRIP("@label", "Size"),             0, 0 },
-        { "date",        DateRole,        I18N_NOOP2_NOSTRIP("@label", "Date"),             0, 0 },
-        { "type",        TypeRole,        I18N_NOOP2_NOSTRIP("@label", "Type"),             0, 0 },
-        { "rating",      RatingRole,      I18N_NOOP2_NOSTRIP("@label", "Rating"),           0, 0 },
-        { "tags",        TagsRole,        I18N_NOOP2_NOSTRIP("@label", "Tags"),             0, 0 },
-        { "comment",     CommentRole,     I18N_NOOP2_NOSTRIP("@label", "Comment"),          0, 0 },
-        { "wordCount",   WordCountRole,   I18N_NOOP2_NOSTRIP("@label", "Word Count"),       I18N_NOOP2_NOSTRIP("@label", "Document") },
-        { "lineCount",   LineCountRole,   I18N_NOOP2_NOSTRIP("@label", "Line Count"),       I18N_NOOP2_NOSTRIP("@label", "Document") },
-        { "imageSize",   ImageSizeRole,   I18N_NOOP2_NOSTRIP("@label", "Image Size"),       I18N_NOOP2_NOSTRIP("@label", "Image") },
-        { "orientation", OrientationRole, I18N_NOOP2_NOSTRIP("@label", "Orientation"),      I18N_NOOP2_NOSTRIP("@label", "Image") },
-        { "artist",      ArtistRole,      I18N_NOOP2_NOSTRIP("@label", "Artist"),           I18N_NOOP2_NOSTRIP("@label", "Music") },
-        { "album",       AlbumRole,       I18N_NOOP2_NOSTRIP("@label", "Album"),            I18N_NOOP2_NOSTRIP("@label", "Music") },
-        { "duration",    DurationRole,    I18N_NOOP2_NOSTRIP("@label", "Duration"),         I18N_NOOP2_NOSTRIP("@label", "Music") },
-        { "track",       TrackRole,       I18N_NOOP2_NOSTRIP("@label", "Track"),            I18N_NOOP2_NOSTRIP("@label", "Music") },
-        { "path",        PathRole,        I18N_NOOP2_NOSTRIP("@label", "Path"),             I18N_NOOP2_NOSTRIP("@label", "Other") },
-        { "destination", DestinationRole, I18N_NOOP2_NOSTRIP("@label", "Link Destination"), I18N_NOOP2_NOSTRIP("@label", "Other") },
-        { "copiedFrom",  CopiedFromRole,  I18N_NOOP2_NOSTRIP("@label", "Copied From"),      I18N_NOOP2_NOSTRIP("@label", "Other") },
-        { "permissions", PermissionsRole, I18N_NOOP2_NOSTRIP("@label", "Permissions"),      I18N_NOOP2_NOSTRIP("@label", "Other") },
-        { "owner",       OwnerRole,       I18N_NOOP2_NOSTRIP("@label", "Owner"),            I18N_NOOP2_NOSTRIP("@label", "Other") },
-        { "group",       GroupRole,       I18N_NOOP2_NOSTRIP("@label", "Group"),            I18N_NOOP2_NOSTRIP("@label", "Other") },
+    //  | role         | roleType       | role translation                                | group translation           | requires Nepomuk | requires indexer
+        { 0,             NoRole,          0, 0,                                             0, 0,                                     false, false },
+        { "text",        NameRole,        I18N_NOOP2_NOSTRIP("@label", "Name"),             0, 0,                                     false, false },
+        { "size",        SizeRole,        I18N_NOOP2_NOSTRIP("@label", "Size"),             0, 0,                                     false, false },
+        { "date",        DateRole,        I18N_NOOP2_NOSTRIP("@label", "Date"),             0, 0,                                     false, false },
+        { "type",        TypeRole,        I18N_NOOP2_NOSTRIP("@label", "Type"),             0, 0,                                     false, false },
+        { "rating",      RatingRole,      I18N_NOOP2_NOSTRIP("@label", "Rating"),           0, 0,                                     true,  false },
+        { "tags",        TagsRole,        I18N_NOOP2_NOSTRIP("@label", "Tags"),             0, 0,                                     true,  false },
+        { "comment",     CommentRole,     I18N_NOOP2_NOSTRIP("@label", "Comment"),          0, 0,                                     true,  false },
+        { "wordCount",   WordCountRole,   I18N_NOOP2_NOSTRIP("@label", "Word Count"),       I18N_NOOP2_NOSTRIP("@label", "Document"), true,  true  },
+        { "lineCount",   LineCountRole,   I18N_NOOP2_NOSTRIP("@label", "Line Count"),       I18N_NOOP2_NOSTRIP("@label", "Document"), true,  true  },
+        { "imageSize",   ImageSizeRole,   I18N_NOOP2_NOSTRIP("@label", "Image Size"),       I18N_NOOP2_NOSTRIP("@label", "Image"),    true,  true  },
+        { "orientation", OrientationRole, I18N_NOOP2_NOSTRIP("@label", "Orientation"),      I18N_NOOP2_NOSTRIP("@label", "Image"),    true,  true  },
+        { "artist",      ArtistRole,      I18N_NOOP2_NOSTRIP("@label", "Artist"),           I18N_NOOP2_NOSTRIP("@label", "Audio"),    true,  true  },
+        { "album",       AlbumRole,       I18N_NOOP2_NOSTRIP("@label", "Album"),            I18N_NOOP2_NOSTRIP("@label", "Audio"),    true,  true  },
+        { "duration",    DurationRole,    I18N_NOOP2_NOSTRIP("@label", "Duration"),         I18N_NOOP2_NOSTRIP("@label", "Audio"),    true,  true  },
+        { "track",       TrackRole,       I18N_NOOP2_NOSTRIP("@label", "Track"),            I18N_NOOP2_NOSTRIP("@label", "Audio"),    true,  true  },
+        { "path",        PathRole,        I18N_NOOP2_NOSTRIP("@label", "Path"),             I18N_NOOP2_NOSTRIP("@label", "Other"),    false, false },
+        { "destination", DestinationRole, I18N_NOOP2_NOSTRIP("@label", "Link Destination"), I18N_NOOP2_NOSTRIP("@label", "Other"),    false, false },
+        { "copiedFrom",  CopiedFromRole,  I18N_NOOP2_NOSTRIP("@label", "Copied From"),      I18N_NOOP2_NOSTRIP("@label", "Other"),    true,  false },
+        { "permissions", PermissionsRole, I18N_NOOP2_NOSTRIP("@label", "Permissions"),      I18N_NOOP2_NOSTRIP("@label", "Other"),    false, false },
+        { "owner",       OwnerRole,       I18N_NOOP2_NOSTRIP("@label", "Owner"),            I18N_NOOP2_NOSTRIP("@label", "Other"),    false, false },
+        { "group",       GroupRole,       I18N_NOOP2_NOSTRIP("@label", "User Group"),       I18N_NOOP2_NOSTRIP("@label", "Other"),    false, false },
     };
 
     count = sizeof(rolesInfoMap) / sizeof(RoleInfoMap);
     return rolesInfoMap;
 }
 
+void KFileItemModel::determineMimeTypes(const KFileItemList& items, int timeout)
+{
+    QElapsedTimer timer;
+    timer.start();
+    foreach (KFileItem item, items) { // krazy:exclude=foreach
+        item.determineMimeType();
+        if (timer.elapsed() > timeout) {
+            // Don't block the user interface, let the remaining items
+            // be resolved asynchronously.
+            return;
+        }
+    }
+}
+
+bool KFileItemModel::isConsistent() const
+{
+    if (m_items.count() != m_itemData.count()) {
+        return false;
+    }
+
+    for (int i = 0; i < count(); ++i) {
+        // Check if m_items and m_itemData are consistent.
+        const KFileItem item = fileItem(i);
+        if (item.isNull()) {
+            qWarning() << "Item" << i << "is null";
+            return false;
+        }
+
+        const int itemIndex = index(item);
+        if (itemIndex != i) {
+            qWarning() << "Item" << i << "has a wrong index:" << itemIndex;
+            return false;
+        }
+
+        // Check if the items are sorted correctly.
+        if (i > 0 && !lessThan(m_itemData.at(i - 1), m_itemData.at(i))) {
+            qWarning() << "The order of items" << i - 1 << "and" << i << "is wrong:"
+                << fileItem(i - 1) << fileItem(i);
+            return false;
+        }
+
+        // Check if all parent-child relationships are consistent.
+        const ItemData* data = m_itemData.at(i);
+        const ItemData* parent = data->parent;
+        if (parent) {
+            if (data->values.value("expandedParentsCount").toInt() != parent->values.value("expandedParentsCount").toInt() + 1) {
+                qWarning() << "expandedParentsCount is inconsistent for parent" << parent->item << "and child" << data->item;
+                return false;
+            }
+
+            const int parentIndex = index(parent->item);
+            if (parentIndex >= i) {
+                qWarning() << "Index" << parentIndex << "of parent" << parent->item << "is not smaller than index" << i << "of child" << data->item;
+                return false;
+            }
+        }
+    }
+
+    return true;
+}
+
 #include "kfileitemmodel.moc"