this, &DolphinViewContainer::slotHiddenFilesShownChanged);
connect(m_view, &DolphinView::sortHiddenLastChanged,
this, &DolphinViewContainer::slotSortHiddenLastChanged);
+ connect(m_view, &DolphinView::currentDirectoryRemoved,
+ this, &DolphinViewContainer::slotCurrentDirectoryRemoved);
// Initialize status bar
m_statusBar = new DolphinStatusBar(this);
}
}
+void DolphinViewContainer::slotCurrentDirectoryRemoved()
+{
+ const QString location(url().toDisplayString(QUrl::PreferLocalFile));
+ if (url().isLocalFile()) {
+ const QString dirPath = url().toLocalFile();
+ const QString newPath = getNearestExistingAncestorOfPath(dirPath);
+ const QUrl newUrl = QUrl::fromLocalFile(newPath);
+ setUrl(newUrl);
+ }
+
+ showMessage(xi18n("Current location changed, <filename>%1</filename> is no longer accessible.", location), Warning);
+}
+
void DolphinViewContainer::slotOpenUrlFinished(KJob *job)
{
if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
m_view->restoreState(stream);
}
}
+
+QString DolphinViewContainer::getNearestExistingAncestorOfPath(const QString& path) const
+{
+ QDir dir(path);
+ do {
+ dir.setPath(QDir::cleanPath(dir.filePath(QStringLiteral(".."))));
+ }
+ while (!dir.exists() && !dir.isRoot());
+
+ return dir.exists() ? dir.path() : QString{};
+}
void slotHiddenFilesShownChanged(bool showHiddenFiles);
void slotSortHiddenLastChanged(bool hiddenLast);
+ void slotCurrentDirectoryRemoved();
void slotOpenUrlFinished(KJob* job);
*/
void tryRestoreViewState();
+ /**
+ * @return Path of nearest existing ancestor directory.
+ */
+ QString getNearestExistingAncestorOfPath(const QString& path) const;
+
private:
QGridLayout *m_topLayout;
indexesToRemove.reserve(items.count());
KFileItemList dirsChanged;
+ const auto currentDir = directory();
+
for (const KFileItem& item : items) {
+ if (item.url() == currentDir) {
+ Q_EMIT currentDirectoryRemoved();
+ return;
+ }
+
const int indexForItem = index(item);
if (indexForItem >= 0) {
indexesToRemove.append(indexForItem);
*/
void fileItemsChanged(const KFileItemList &changedFileItems);
+ /**
+ * It is emitted when the parent directory was removed.
+ */
+ void currentDirectoryRemoved();
+
protected:
void onGroupedSortingChanged(bool current) override;
void onSortRoleChanged(const QByteArray& current, const QByteArray& previous, bool resortItems = true) override;
void testCreateMimeData();
void testDeleteFileMoreThanOnce();
void testInsertAfterExpand();
+ void testCurrentDirRemoved();
private:
QStringList itemsInModel() const;
}
+void KFileItemModelTest::testCurrentDirRemoved()
+{
+ m_model->m_dirLister->setAutoUpdate(true);
+ QSignalSpy currentDirectoryRemovedSpy(m_model, &KFileItemModel::currentDirectoryRemoved);
+ QVERIFY(currentDirectoryRemovedSpy.isValid());
+ QSignalSpy loadingCompletedSpy(m_model, &KFileItemModel::directoryLoadingCompleted);
+ QVERIFY(loadingCompletedSpy.isValid());
+ QSignalSpy dirListerClearSpy(m_model->m_dirLister, &KCoreDirLister::clear);
+ QVERIFY(dirListerClearSpy.isValid());
+
+ m_testDir->createFiles({"dir/a.txt", "dir/b.txt"});
+ m_model->loadDirectory(QUrl::fromLocalFile(m_testDir->path() + "/dir/"));
+ QVERIFY(loadingCompletedSpy.wait());
+ QCOMPARE(m_model->count(), 2);
+ QVERIFY(m_model->isConsistent());
+
+ m_testDir->removeDir("dir");
+ QVERIFY(currentDirectoryRemovedSpy.wait());
+
+ // dirLister calls clear
+ QCOMPARE(dirListerClearSpy.count(), 2);
+ QVERIFY(m_model->isConsistent());
+ QVERIFY(m_model->m_itemData.isEmpty());
+ QCOMPARE(m_model->count(), 0);
+}
+
QStringList KFileItemModelTest::itemsInModel() const
{
QStringList items;
QFile::remove(absolutePath);
}
+void TestDir::removeDir(const QString& path)
+{
+ QString absolutePath = path;
+ QFileInfo fileInfo(absolutePath);
+ if (!fileInfo.isAbsolute()) {
+ absolutePath = TestDir::path() + QLatin1Char('/') + path;
+ }
+ QDir dirToRemove = QDir(absolutePath);
+ dirToRemove.removeRecursively();
+}
+
void TestDir::makePathAbsoluteAndCreateParents(QString& path)
{
QFileInfo fileInfo(path);
void removeFile(const QString& path);
void removeFiles(const QStringList& files);
+ void removeDir(const QString& path);
private:
void makePathAbsoluteAndCreateParents(QString& path);
connect(m_model, &KFileItemModel::directoryRedirection, this, &DolphinView::slotDirectoryRedirection);
connect(m_model, &KFileItemModel::urlIsFileError, this, &DolphinView::urlIsFileError);
connect(m_model, &KFileItemModel::fileItemsChanged, this, &DolphinView::fileItemsChanged);
+ connect(m_model, &KFileItemModel::currentDirectoryRemoved, this, &DolphinView::currentDirectoryRemoved);
connect(this, &DolphinView::itemCountChanged,
this, &DolphinView::updatePlaceholderLabel);
void fileItemsChanged(const KFileItemList &changedFileItems);
+ /**
+ * Emitted when the current directory of the model was removed.
+ */
+ void currentDirectoryRemoved();
+
protected:
/** Changes the zoom level if Control is pressed during a wheel event. */
void wheelEvent(QWheelEvent* event) override;