]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphinviewcontainer.cpp
Exit the deleted directory when it is removed
[dolphin.git] / src / dolphinviewcontainer.cpp
index 0d2dcdafe53feac583c98dc9d620a583901816ce..a388334819c9b40d11f13fba33cffd075fc71c29 100644 (file)
@@ -172,6 +172,8 @@ DolphinViewContainer::DolphinViewContainer(const QUrl& url, QWidget* parent) :
             this, &DolphinViewContainer::slotHiddenFilesShownChanged);
     connect(m_view, &DolphinView::sortHiddenLastChanged,
             this, &DolphinViewContainer::slotSortHiddenLastChanged);
             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);
 
     // Initialize status bar
     m_statusBar = new DolphinStatusBar(this);
@@ -939,6 +941,19 @@ void DolphinViewContainer::slotSortHiddenLastChanged(bool hiddenLast)
     }
 }
 
     }
 }
 
+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) {
 void DolphinViewContainer::slotOpenUrlFinished(KJob *job)
 {
     if (job->error() && job->error() != KIO::ERR_USER_CANCELED) {
@@ -967,3 +982,14 @@ void DolphinViewContainer::tryRestoreViewState()
         m_view->restoreState(stream);
     }
 }
         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{};
+}