X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/dd18909eecce59ae23b1e921b96937fd547c9558..a7692611315065b308cd8243563cc50d40bd0f71:/src/kitemviews/kfileitemmodel.cpp diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index fd773e1e9..13057cbbd 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -25,6 +25,7 @@ #include #include #include +#include //TODO: port to QCollator #include "private/kfileitemmodelsortalgorithm.h" #include "private/kfileitemmodeldirlister.h" @@ -68,17 +69,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 +95,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 +104,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() @@ -216,7 +218,7 @@ void KFileItemModel::setShowHiddenFiles(bool show) m_dirLister->setShowingDotFiles(show); m_dirLister->emitChanges(); if (show) { - slotCompleted(); + dispatchPendingItemsToInsert(); } } @@ -363,7 +365,7 @@ KFileItem KFileItemModel::fileItem(const KUrl& url) const int KFileItemModel::index(const KFileItem& item) const { - return index(item.url()); + return index(KUrl(item.url())); } int KFileItemModel::index(const KUrl& url) const @@ -395,7 +397,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 KUrl& 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; } @@ -486,6 +522,18 @@ bool KFileItemModel::setExpanded(int index, bool expanded) m_urlsToExpand.insert(url); } } 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); @@ -500,7 +548,9 @@ bool KFileItemModel::setExpanded(int index, bool expanded) ItemData* itemData = m_itemData.at(childIndex); if (itemData->values.value("isExpanded").toBool()) { const KUrl targetUrl = itemData->item.targetUrl(); + const KUrl 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; @@ -866,7 +916,7 @@ void KFileItemModel::slotItemsAdded(const KUrl& directoryUrl, const KFileItemLis // might result in emitting the same items twice due to the Keep-parameter. // This case happens if an item gets expanded, collapsed and expanded again // before the items could be loaded for the first expansion. - if (index(items.first().url()) >= 0) { + if (index(KUrl(items.first().url())) >= 0) { // The items are already part of the model. return; } @@ -1461,8 +1511,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 +1537,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 +1716,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 +1909,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 +1918,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 +2005,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; @@ -2196,4 +2246,3 @@ bool KFileItemModel::isConsistent() const return true; } -#include "kfileitemmodel.moc"