X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/c2887688f102d0412ba93c74facd5ed007185e3a..1b4572dac9fb529d31b786f93e4f02c6f8aeeb21:/src/kitemviews/kfileitemmodel.cpp diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index fd773e1e9..711b0797b 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -22,14 +22,14 @@ #include "kfileitemmodel.h" #include -#include +#include #include #include +#include //TODO: port to QCollator #include "private/kfileitemmodelsortalgorithm.h" #include "private/kfileitemmodeldirlister.h" -#include #include #include #include @@ -68,17 +68,17 @@ KFileItemModel::KFileItemModel(QObject* parent) : 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(itemsAdded(KUrl,KFileItemList)), this, SLOT(slotItemsAdded(KUrl,KFileItemList))); - connect(m_dirLister, SIGNAL(itemsDeleted(KFileItemList)), this, SLOT(slotItemsDeleted(KFileItemList))); - connect(m_dirLister, SIGNAL(refreshItems(QList >)), this, SLOT(slotRefreshItems(QList >))); - connect(m_dirLister, SIGNAL(clear()), this, SLOT(slotClear())); - 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))); + connect(m_dirLister, &KFileItemModelDirLister::started, this, &KFileItemModel::directoryLoadingStarted); + connect(m_dirLister, static_cast(&KFileItemModelDirLister::canceled), this, &KFileItemModel::slotCanceled); + connect(m_dirLister, static_cast(&KFileItemModelDirLister::completed), this, &KFileItemModel::slotCompleted); + connect(m_dirLister, &KFileItemModelDirLister::itemsAdded, this, &KFileItemModel::slotItemsAdded); + connect(m_dirLister, &KFileItemModelDirLister::itemsDeleted, this, &KFileItemModel::slotItemsDeleted); + connect(m_dirLister, &KFileItemModelDirLister::refreshItems, this, &KFileItemModel::slotRefreshItems); + connect(m_dirLister, static_cast(&KFileItemModelDirLister::clear), this, &KFileItemModel::slotClear); + connect(m_dirLister, &KFileItemModelDirLister::infoMessage, this, &KFileItemModel::infoMessage); + connect(m_dirLister, &KFileItemModelDirLister::errorMessage, this, &KFileItemModel::errorMessage); + connect(m_dirLister, static_cast(&KFileItemModelDirLister::redirection), this, &KFileItemModel::directoryRedirection); + connect(m_dirLister, &KFileItemModelDirLister::urlIsFileError, this, &KFileItemModel::urlIsFileError); // Apply default roles that should be determined resetRoles(); @@ -94,7 +94,7 @@ KFileItemModel::KFileItemModel(QObject* parent) : m_maximumUpdateIntervalTimer = new QTimer(this); m_maximumUpdateIntervalTimer->setInterval(2000); m_maximumUpdateIntervalTimer->setSingleShot(true); - connect(m_maximumUpdateIntervalTimer, SIGNAL(timeout()), this, SLOT(dispatchPendingItemsToInsert())); + connect(m_maximumUpdateIntervalTimer, &QTimer::timeout, this, &KFileItemModel::dispatchPendingItemsToInsert); // When changing the value of an item which represents the sort-role a resorting must be // triggered. Especially in combination with KFileItemModelRolesUpdater this might be done @@ -103,9 +103,10 @@ KFileItemModel::KFileItemModel(QObject* parent) : m_resortAllItemsTimer = new QTimer(this); m_resortAllItemsTimer->setInterval(500); m_resortAllItemsTimer->setSingleShot(true); - connect(m_resortAllItemsTimer, SIGNAL(timeout()), this, SLOT(resortAllItems())); + connect(m_resortAllItemsTimer, &QTimer::timeout, this, &KFileItemModel::resortAllItems); - connect(KGlobalSettings::self(), SIGNAL(naturalSortingChanged()), this, SLOT(slotNaturalSortingChanged())); + connect(KGlobalSettings::self(), &KGlobalSettings::naturalSortingChanged, + this, &KFileItemModel::slotNaturalSortingChanged); } KFileItemModel::~KFileItemModel() @@ -115,15 +116,15 @@ KFileItemModel::~KFileItemModel() qDeleteAll(m_pendingItemsToInsert); } -void KFileItemModel::loadDirectory(const KUrl& url) +void KFileItemModel::loadDirectory(const QUrl &url) { m_dirLister->openUrl(url); } -void KFileItemModel::refreshDirectory(const KUrl& url) +void KFileItemModel::refreshDirectory(const QUrl &url) { // Refresh all expanded directories first (Bug 295300) - QHashIterator expandedDirs(m_expandedDirs); + QHashIterator expandedDirs(m_expandedDirs); while (expandedDirs.hasNext()) { expandedDirs.next(); m_dirLister->openUrl(expandedDirs.value(), KDirLister::Reload); @@ -132,7 +133,7 @@ void KFileItemModel::refreshDirectory(const KUrl& url) m_dirLister->openUrl(url, KDirLister::Reload); } -KUrl KFileItemModel::directory() const +QUrl KFileItemModel::directory() const { return m_dirLister->url(); } @@ -188,8 +189,9 @@ bool KFileItemModel::setData(int index, const QHash& value m_itemData[index]->values = currentValues; if (changedRoles.contains("text")) { - KUrl url = m_itemData[index]->item.url(); - url.setFileName(currentValues["text"].toString()); + QUrl url = m_itemData[index]->item.url(); + url = url.adjusted(QUrl::RemoveFilename); + url.setPath(url.path() + currentValues["text"].toString()); m_itemData[index]->item.setUrl(url); } @@ -216,7 +218,7 @@ void KFileItemModel::setShowHiddenFiles(bool show) m_dirLister->setShowingDotFiles(show); m_dirLister->emitChanges(); if (show) { - slotCompleted(); + dispatchPendingItemsToInsert(); } } @@ -242,8 +244,8 @@ QMimeData* KFileItemModel::createMimeData(const KItemSet& indexes) const // The following code has been taken from KDirModel::mimeData() // (kdelibs/kio/kio/kdirmodel.cpp) // Copyright (C) 2006 David Faure - KUrl::List urls; - KUrl::List mostLocalUrls; + QList urls; + QList mostLocalUrls; bool canUseMostLocalUrls = true; const ItemData* lastAddedItem = 0; @@ -275,9 +277,9 @@ QMimeData* KFileItemModel::createMimeData(const KItemSet& indexes) const const bool different = canUseMostLocalUrls && mostLocalUrls != urls; if (different) { - urls.populateMimeData(mostLocalUrls, data); + data->setUrls(mostLocalUrls); } else { - urls.populateMimeData(data); + data->setUrls(urls); } return data; @@ -352,7 +354,7 @@ KFileItem KFileItemModel::fileItem(int index) const return KFileItem(); } -KFileItem KFileItemModel::fileItem(const KUrl& url) const +KFileItem KFileItemModel::fileItem(const QUrl &url) const { const int indexForUrl = index(url); if (indexForUrl >= 0) { @@ -366,10 +368,9 @@ int KFileItemModel::index(const KFileItem& item) const return index(item.url()); } -int KFileItemModel::index(const KUrl& url) const +int KFileItemModel::index(const QUrl& url) const { - KUrl urlToFind = url; - urlToFind.adjustPath(KUrl::RemoveTrailingSlash); + const QUrl urlToFind = url.adjusted(QUrl::StripTrailingSlash); const int itemCount = m_itemData.count(); int itemsInHash = m_items.count(); @@ -387,7 +388,7 @@ int KFileItemModel::index(const KUrl& url) const const int blockSize = 1000; const int currentBlockEnd = qMin(itemsInHash + blockSize, itemCount); for (int i = itemsInHash; i < currentBlockEnd; ++i) { - const KUrl nextUrl = m_itemData.at(i)->item.url(); + const QUrl nextUrl = m_itemData.at(i)->item.url(); m_items.insert(nextUrl, i); } @@ -395,7 +396,41 @@ int KFileItemModel::index(const KUrl& url) const index = m_items.value(urlToFind, -1); } - Q_ASSERT(index >= 0 || m_items.count() == m_itemData.count()); + if (index < 0) { + // The item could not be found, even though all items from m_itemData + // should be in m_items now. We print some diagnostic information which + // might help to find the cause of the problem, but only once. This + // prevents that obtaining and printing the debugging information + // wastes CPU cycles and floods the shell or .xsession-errors. + static bool printDebugInfo = true; + + if (m_items.count() != m_itemData.count() && printDebugInfo) { + printDebugInfo = false; + + kWarning() << "The model is in an inconsistent state."; + kWarning() << "m_items.count() ==" << m_items.count(); + kWarning() << "m_itemData.count() ==" << m_itemData.count(); + + // Check if there are multiple items with the same URL. + QMultiHash indexesForUrl; + for (int i = 0; i < m_itemData.count(); ++i) { + indexesForUrl.insert(m_itemData.at(i)->item.url(), i); + } + + foreach (const QUrl& url, indexesForUrl.uniqueKeys()) { + if (indexesForUrl.count(url) > 1) { + kWarning() << "Multiple items found with the URL" << url; + foreach (int index, indexesForUrl.values(url)) { + const ItemData* data = m_itemData.at(index); + kWarning() << "index" << index << ":" << data->item; + if (data->parent) { + kWarning() << "parent" << data->parent->item; + } + } + } + } + } + } return index; } @@ -415,6 +450,8 @@ void KFileItemModel::setRoles(const QSet& roles) if (m_roles == roles) { return; } + + const QSet changedRoles = (roles - m_roles) + (m_roles - roles); m_roles = roles; if (count() > 0) { @@ -443,8 +480,7 @@ void KFileItemModel::setRoles(const QSet& roles) m_itemData[i]->values = retrieveData(m_itemData.at(i)->item, m_itemData.at(i)->parent); } - kWarning() << "TODO: Emitting itemsChanged() with no information what has changed!"; - emit itemsChanged(KItemRangeList() << KItemRange(0, count()), QSet()); + emit itemsChanged(KItemRangeList() << KItemRange(0, count()), changedRoles); } // Clear the 'values' of all filtered items. They will be re-populated with the @@ -475,17 +511,29 @@ bool KFileItemModel::setExpanded(int index, bool expanded) } const KFileItem item = m_itemData.at(index)->item; - const KUrl url = item.url(); - const KUrl targetUrl = item.targetUrl(); + const QUrl url = item.url(); + const QUrl targetUrl = item.targetUrl(); if (expanded) { m_expandedDirs.insert(targetUrl, url); m_dirLister->openUrl(url, KDirLister::Keep); - const KUrl::List previouslyExpandedChildren = m_itemData.at(index)->values.value("previouslyExpandedChildren").value(); - foreach (const KUrl& url, previouslyExpandedChildren) { - m_urlsToExpand.insert(url); + const QVariantList previouslyExpandedChildren = m_itemData.at(index)->values.value("previouslyExpandedChildren").value(); + foreach (const QVariant& var, previouslyExpandedChildren) { + m_urlsToExpand.insert(var.toUrl()); } } else { + // Note that there might be (indirect) children of the folder which is to be collapsed in + // m_pendingItemsToInsert. To prevent that they will be inserted into the model later, + // possibly without a parent, which might result in a crash, we insert all pending items + // right now. All new items which would be without a parent will then be removed. + dispatchPendingItemsToInsert(); + + // Check if the index of the collapsed folder has changed. If that is the case, then items + // were inserted before the collapsed folder, and its index needs to be updated. + if (m_itemData.at(index)->item != item) { + index = this->index(item); + } + m_expandedDirs.remove(targetUrl); m_dirLister->stop(url); @@ -493,14 +541,16 @@ bool KFileItemModel::setExpanded(int index, bool expanded) const int itemCount = m_itemData.count(); const int firstChildIndex = index + 1; - KUrl::List expandedChildren; + QVariantList expandedChildren; int childIndex = firstChildIndex; while (childIndex < itemCount && expandedParentsCount(childIndex) > parentLevel) { ItemData* itemData = m_itemData.at(childIndex); if (itemData->values.value("isExpanded").toBool()) { - const KUrl targetUrl = itemData->item.targetUrl(); + const QUrl targetUrl = itemData->item.targetUrl(); + const QUrl url = itemData->item.url(); m_expandedDirs.remove(targetUrl); + m_dirLister->stop(url); // TODO: try to unit-test this, see https://bugs.kde.org/show_bug.cgi?id=332102#c11 expandedChildren.append(targetUrl); } ++childIndex; @@ -542,17 +592,17 @@ int KFileItemModel::expandedParentsCount(int index) const return 0; } -QSet KFileItemModel::expandedDirectories() const +QSet KFileItemModel::expandedDirectories() const { return m_expandedDirs.values().toSet(); } -void KFileItemModel::restoreExpandedDirectories(const QSet& urls) +void KFileItemModel::restoreExpandedDirectories(const QSet &urls) { m_urlsToExpand = urls; } -void KFileItemModel::expandParentDirectories(const KUrl& url) +void KFileItemModel::expandParentDirectories(const QUrl &url) { const int pos = m_dirLister->url().path().length(); @@ -560,17 +610,17 @@ void KFileItemModel::expandParentDirectories(const KUrl& url) // expanded is added to m_urlsToExpand. KDirLister // does not care whether the parent-URL has already been // expanded. - KUrl urlToExpand = m_dirLister->url(); + QUrl 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)); + urlToExpand.setPath(urlToExpand.path() + '/' + subDirs.at(i)); m_urlsToExpand.insert(urlToExpand); } // KDirLister::open() must called at least once to trigger an initial // loading. The pending URLs that must be restored are handled // in slotCompleted(). - QSetIterator it2(m_urlsToExpand); + QSetIterator it2(m_urlsToExpand); while (it2.hasNext()) { const int idx = index(it2.next()); if (idx >= 0 && !isExpanded(idx)) { @@ -751,7 +801,7 @@ void KFileItemModel::resortAllItems() // Remember the order of the current URLs so // that it can be determined which indexes have // been moved because of the resorting. - QList oldUrls; + QList oldUrls; oldUrls.reserve(itemCount); foreach (const ItemData* itemData, m_itemData) { oldUrls.append(itemData->item.url()); @@ -820,7 +870,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 QUrl& url, m_urlsToExpand) { const int indexForUrl = index(url); if (indexForUrl >= 0) { m_urlsToExpand.remove(url); @@ -848,16 +898,15 @@ void KFileItemModel::slotCanceled() emit directoryLoadingCanceled(); } -void KFileItemModel::slotItemsAdded(const KUrl& directoryUrl, const KFileItemList& items) +void KFileItemModel::slotItemsAdded(const QUrl &directoryUrl, const KFileItemList& items) { Q_ASSERT(!items.isEmpty()); - KUrl parentUrl; + QUrl parentUrl; if (m_expandedDirs.contains(directoryUrl)) { parentUrl = m_expandedDirs.value(directoryUrl); } else { - parentUrl = directoryUrl; - parentUrl.adjustPath(KUrl::RemoveTrailingSlash); + parentUrl = directoryUrl.adjusted(QUrl::StripTrailingSlash); } if (m_requestRole[ExpandedParentsCountRole]) { @@ -1152,7 +1201,7 @@ void KFileItemModel::insertItems(QList& newItems) } // The indexes in m_items are not correct anymore. Therefore, we clear m_items. - // It will be re-populated with the updated indices if index(const KUrl&) is called. + // It will be re-populated with the updated indices if index(const QUrl&) is called. m_items.clear(); emit itemsInserted(itemRanges); @@ -1205,13 +1254,13 @@ void KFileItemModel::removeItems(const KItemRangeList& itemRanges, RemoveItemsBe m_itemData.erase(m_itemData.end() - removedItemsCount, m_itemData.end()); // The indexes in m_items are not correct anymore. Therefore, we clear m_items. - // It will be re-populated with the updated indices if index(const KUrl&) is called. + // It will be re-populated with the updated indices if index(const QUrl&) is called. m_items.clear(); emit itemsRemoved(itemRanges); } -QList KFileItemModel::createItemDataList(const KUrl& parentUrl, const KFileItemList& items) const +QList KFileItemModel::createItemDataList(const QUrl& parentUrl, const KFileItemList& items) const { if (m_sortRole == TypeRole) { // Try to resolve the MIME-types synchronously to prevent a reordering of @@ -1461,8 +1510,8 @@ QHash KFileItemModel::retrieveData(const KFileItem& item, // Don't use KFileItem::timeString() as this is too expensive when // having several thousands of items. Instead the formatting of the // date-time will be done on-demand by the view when the date will be shown. - const KDateTime dateTime = item.time(KFileItem::ModificationTime); - data.insert(sharedValue("date"), dateTime.dateTime()); + const QDateTime dateTime = item.time(KFileItem::ModificationTime); + data.insert(sharedValue("date"), dateTime); } if (m_requestRole[PermissionsRole]) { @@ -1487,7 +1536,7 @@ QHash KFileItemModel::retrieveData(const KFileItem& item, if (m_requestRole[PathRole]) { QString path; - if (item.url().protocol() == QLatin1String("trash")) { + if (item.url().scheme() == QLatin1String("trash")) { path = item.entry().stringValue(KIO::UDSEntry::UDS_EXTRA); } else { // For performance reasons cache the home-path in a static QString @@ -1666,8 +1715,8 @@ int KFileItemModel::sortRoleCompare(const ItemData* a, const ItemData* b) const } case DateRole: { - const KDateTime dateTimeA = itemA.time(KFileItem::ModificationTime); - const KDateTime dateTimeB = itemB.time(KFileItem::ModificationTime); + const QDateTime dateTimeA = itemA.time(KFileItem::ModificationTime); + const QDateTime dateTimeB = itemB.time(KFileItem::ModificationTime); if (dateTimeA < dateTimeB) { result = -1; } else if (dateTimeA > dateTimeB) { @@ -1859,7 +1908,7 @@ QList > KFileItemModel::dateRoleGroups() const const int maxIndex = count() - 1; QList > groups; - const QDate currentDate = KDateTime::currentLocalDateTime().date(); + const QDate currentDate = QDate::currentDate(); QDate previousModifiedDate; QString groupValue; @@ -1868,7 +1917,7 @@ QList > KFileItemModel::dateRoleGroups() const continue; } - const KDateTime modifiedTime = m_itemData.at(i)->item.time(KFileItem::ModificationTime); + const QDateTime modifiedTime = m_itemData.at(i)->item.time(KFileItem::ModificationTime); const QDate modifiedDate = modifiedTime.date(); if (modifiedDate == previousModifiedDate) { // The current item is in the same group as the previous item @@ -1955,7 +2004,7 @@ QList > KFileItemModel::permissionRoleGroups() const } permissionsString = newPermissionsString; - const QFileInfo info(itemData->item.url().pathOrUrl()); + const QFileInfo info(itemData->item.url().toLocalFile()); // Set user string QString user; @@ -2150,7 +2199,7 @@ QByteArray KFileItemModel::sharedValue(const QByteArray& value) bool KFileItemModel::isConsistent() const { // m_items may contain less items than m_itemData because m_items - // is populated lazily, see KFileItemModel::index(const KUrl& url). + // is populated lazily, see KFileItemModel::index(const QUrl& url). if (m_items.count() > m_itemData.count()) { return false; } @@ -2196,4 +2245,3 @@ bool KFileItemModel::isConsistent() const return true; } -#include "kfileitemmodel.moc"