]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Offer to mkpath when user goes to a nonexistent folder
authorGleb Kasachou <gkosachov99@gmail.com>
Fri, 25 Jul 2025 09:47:47 +0000 (12:47 +0300)
committerMéven Car <meven@kde.org>
Wed, 30 Jul 2025 07:57:20 +0000 (07:57 +0000)
This commit changes Dolphin's behavior when a nonexistent local path is
entered into the location bar. Previously, an error message would pop
up. This commit adds a button to that message that allows to create the
path and navigate to it.

src/dolphinviewcontainer.cpp
src/dolphinviewcontainer.h

index ee4bb6e4c3bbdbbffac58ec15931a624401abd74..862654a6b2df9b10486249a9430b948392aca02a 100644 (file)
@@ -27,6 +27,7 @@
 #include <KFileItemActions>
 #include <KFilePlacesModel>
 #include <KIO/JobUiDelegateFactory>
+#include <KIO/MkpathJob>
 #include <KIO/OpenUrlJob>
 #include <KLocalizedString>
 #include <KMessageWidget>
@@ -76,6 +77,7 @@ DolphinViewContainer::DolphinViewContainer(const QUrl &url, QWidget *parent)
     , m_searchModeEnabled(false)
     , m_adminBar{nullptr}
     , m_authorizeToEnterFolderAction{nullptr}
+    , m_createFolderAction(nullptr)
     , m_messageWidget(nullptr)
     , m_selectionModeTopBar{nullptr}
     , m_view(nullptr)
@@ -973,6 +975,27 @@ void DolphinViewContainer::slotErrorMessageFromView(const QString &message, cons
         }
         showMessage(i18nc("@info", "Authorization required to enter this folder."), KMessageWidget::Error, {m_authorizeToEnterFolderAction});
         return;
+    } else if (kioErrorCode == KIO::ERR_DOES_NOT_EXIST && m_view->url().isLocalFile()) {
+        if (!m_createFolderAction) {
+            m_createFolderAction = new QAction(this);
+            m_createFolderAction->setText(i18nc("@action", "Create"));
+            m_createFolderAction->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
+            m_createFolderAction->setToolTip(i18nc("@info:tooltip", "Create this folder and open it"));
+            connect(m_createFolderAction, &QAction::triggered, this, [this](bool) {
+                KIO::MkpathJob *job = KIO::mkpath(m_view->url());
+                KIO::FileUndoManager::self()->recordJob(KIO::FileUndoManager::Mkpath, {}, m_view->url(), job);
+                connect(job, &KJob::result, this, [this](KJob *job) {
+                    if (job->error()) {
+                        showErrorMessage(job->errorString());
+                    } else {
+                        m_view->reload();
+                        m_messageWidget->animatedHide();
+                    }
+                });
+            });
+        }
+        showMessage(message, KMessageWidget::Error, {m_createFolderAction});
+        return;
     }
     Q_EMIT showErrorMessage(message);
 }
index d4ab0e66ec758d39ec7cbc035cc51a74fd8b4000..419b9f5b11f5356bc8f078af8eeed046d69ec19e 100644 (file)
@@ -487,6 +487,8 @@ private:
     Admin::Bar *m_adminBar;
     /// An action to switch to the admin protocol. This variable will always be nullptr unless kio-admin was installed. @see Admin::WorkerIntegration.
     QAction *m_authorizeToEnterFolderAction;
+    /// An action to create new folder in case user enters a nonexistent URL in the location bar.
+    QAction *m_createFolderAction;
 
     KMessageWidget *m_messageWidget;