]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Add "Move to New Folder…" action
authorAhmet Hakan Çelik <mail@ahakan.com>
Thu, 23 May 2024 13:25:03 +0000 (13:25 +0000)
committerFelix Ernst <felixernst@kde.org>
Thu, 23 May 2024 13:25:03 +0000 (13:25 +0000)
This commit introduces an action which creates a new folder with a name
specified by the user and moves all the currently selected items there
in one go.

This action is implemented as a KFileItemActionPlugin which means users
can disable it on Dolphin's context menu settings page.

BUG: 484555

src/CMakeLists.txt
src/itemactions/movetonewfolderitemaction.cpp [new file with mode: 0644]
src/itemactions/movetonewfolderitemaction.h [new file with mode: 0644]
src/itemactions/movetonewfolderitemaction.json [new file with mode: 0644]

index 55eda9dcd31de2b6978dc39e822a066e2d4396ab..0aa369bdb6951dc0bf1c4e1bf745afb77e591344 100644 (file)
@@ -616,3 +616,15 @@ install( FILES settings/dolphin_detailsmodesettings.upd
 if(BUILD_TESTING)
     add_subdirectory(tests)
 endif()
+
+# movetonewfolderitemaction plugin
+
+kcoreaddons_add_plugin(movetonewfolderitemaction
+    SOURCES itemactions/movetonewfolderitemaction.cpp itemactions/movetonewfolderitemaction.h
+    INSTALL_NAMESPACE "kf6/kfileitemaction")
+
+target_link_libraries(movetonewfolderitemaction
+    KF6::I18n
+    KF6::KIOCore
+    KF6::KIOWidgets
+    KF6::KIOFileWidgets)
diff --git a/src/itemactions/movetonewfolderitemaction.cpp b/src/itemactions/movetonewfolderitemaction.cpp
new file mode 100644 (file)
index 0000000..6d3d6b8
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Ahmet Hakan Çelik <mail@ahakan.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "movetonewfolderitemaction.h"
+
+#include <KFileItem>
+#include <KLocalizedString>
+#include <KPluginFactory>
+#include <KNewFileMenu>
+#include <KIO/CopyJob>
+#include <KIO/JobUiDelegate>
+#include <KIO/FileUndoManager>
+
+#include <QUrl>
+
+K_PLUGIN_CLASS_WITH_JSON(MoveToNewFolderItemAction, "movetonewfolderitemaction.json")
+
+MoveToNewFolderItemAction::MoveToNewFolderItemAction(QObject *parent)
+    : KAbstractFileItemActionPlugin(parent)
+{
+
+}
+
+QList<QAction *> MoveToNewFolderItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
+{
+    const KFileItemList &selectedItems = fileItemInfos.items();
+
+    QAction *createFolderFromSelected = new QAction(i18nc("@action:inmenu", "Move to New Folder…"), parentWidget);
+    createFolderFromSelected->setIcon(QIcon::fromTheme(QStringLiteral("folder-new")));
+    connect(createFolderFromSelected, &QAction::triggered, this, [=]() {
+        QString selectedFileDirPath = selectedItems.at(0).url().toString().remove(selectedItems.at(0).name());
+        if (selectedFileDirPath.endsWith(QStringLiteral("/"))) {
+            selectedFileDirPath.removeLast();
+        }
+        const QUrl newFolderDirUrl(selectedFileDirPath);
+
+        auto newFileMenu = new KNewFileMenu(parentWidget);
+        newFileMenu->setWorkingDirectory(newFolderDirUrl);
+        newFileMenu->createDirectory();
+
+        connect(newFileMenu, &KNewFileMenu::directoryCreated, this, [=](const QUrl &createdUrl) {
+            KIO::CopyJob *job = KIO::move(selectedItems.urlList(), createdUrl);
+            KIO::FileUndoManager::self()->recordCopyJob(job);
+            newFileMenu->deleteLater();
+        });
+    });
+
+    return {createFolderFromSelected};
+}
+
+#include "movetonewfolderitemaction.moc"
diff --git a/src/itemactions/movetonewfolderitemaction.h b/src/itemactions/movetonewfolderitemaction.h
new file mode 100644 (file)
index 0000000..e690a35
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Ahmet Hakan Çelik <mail@ahakan.com>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef MOVETONEWFOLDERITEMACTION_H
+#define MOVETONEWFOLDERITEMACTION_H
+
+#include <KAbstractFileItemActionPlugin>
+#include <KFileItemListProperties>
+
+class QAction;
+class QWidget;
+
+class KNewFileMenu;
+
+class MoveToNewFolderItemAction : public KAbstractFileItemActionPlugin
+{
+    Q_OBJECT
+
+public:
+    MoveToNewFolderItemAction(QObject *parent);
+
+    QList<QAction *> actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget) override;
+};
+
+#endif // MOVETONEWFOLDERITEMACTION_H
diff --git a/src/itemactions/movetonewfolderitemaction.json b/src/itemactions/movetonewfolderitemaction.json
new file mode 100644 (file)
index 0000000..069bad6
--- /dev/null
@@ -0,0 +1,14 @@
+{
+    "KPlugin": {
+        "Icon": "folder-new",
+        "MimeTypes": [
+            "application/octet-stream"
+        ],
+        "Name": "Move to New Folder",
+        "ServiceTypes": [
+            "KFileItemAction/Plugin"
+        ]
+    },
+    "X-KDE-Require": "Write",
+    "X-KDE-Show-In-Submenu": "true"
+}