KIO

dropintonewfolderPlugin.cpp
1/*
2 This file is part of the KDE project
3 SPDX-FileCopyrightText: 2025 Méven Car <meven@kde.org>
4
5 SPDX-License-Identifier: GPL-2.0-or-later
6*/
7
8#include "dropintonewfolderPlugin.h"
9#include "copyjob.h"
10#include "openfilemanagerwindowjob.h"
11#include <KIO/FileUndoManager>
12#include <KIO/OpenFileManagerWindowJob>
13#include <KIO/StatJob>
14#include <kfileitem.h>
15#include <knewfilemenu.h>
16
17#include <QAction>
18
19#include <KFileItemListProperties>
20#include <KLocalizedString>
21#include <KPluginFactory>
22
23K_PLUGIN_CLASS_WITH_JSON(DropIntoNewFolderPlugin, "dropnewintonewfolder.json")
24
25DropIntoNewFolderPlugin::DropIntoNewFolderPlugin(QObject *parent, const QVariantList &)
26 : KIO::DndPopupMenuPlugin(parent)
27{
28}
29
30QList<QAction *> DropIntoNewFolderPlugin::setup(const KFileItemListProperties &fileItemProps, const QUrl &destination)
31{
32 QList<QAction *> actionList;
33
34 if (!destination.isLocalFile()) {
35 return actionList;
36 }
37 // need to check write acccess to dest and m_urls
38 bool allowed = fileItemProps.supportsMoving();
39
40 if (allowed) {
41 auto statJob = KIO::stat(destination, KIO::StatJob::StatSide::SourceSide, KIO::StatDetail::StatBasic);
42
43 if (!statJob->exec()) {
44 qWarning() << "Could not stat destination" << destination;
45 allowed = false;
46 } else {
47 KFileItem item(statJob->statResult(), destination);
48
49 allowed = item.isWritable();
50 }
51 }
52
53 const QString dropIntoNewFolderMessage = i18nc("@action:inmenu Context menu shown when files are dragged", "Move Into New Folder");
54
55 QAction *action = new QAction(QIcon::fromTheme(QStringLiteral("folder-new")), dropIntoNewFolderMessage, this);
56 connect(action, &QAction::triggered, this, &DropIntoNewFolderPlugin::slotTriggered);
57 action->setEnabled(allowed);
58
59 actionList.append(action);
60 m_dest = destination;
61 m_urls = fileItemProps.urlList();
62
63 return actionList;
64}
65
66void DropIntoNewFolderPlugin::slotTriggered()
67{
68 auto menu = new KNewFileMenu(this);
69 menu->setWorkingDirectory(m_dest);
70 menu->setWindowTitle(i18nc("@title:window", "Create New Folder for These Items"));
71
72 connect(menu, &KNewFileMenu::directoryCreated, this, [this](const QUrl &newFolder) {
73 auto job = KIO::move(m_urls, newFolder);
75 connect(job, &KJob::result, this, [newFolder, this](KJob *job) {
76 if (job->error() != KJob::NoError) {
77 return;
78 }
79 auto openFileManagerJob = new KIO::OpenFileManagerWindowJob{this};
80 openFileManagerJob->setHighlightUrls({newFolder});
81 openFileManagerJob->start();
82 });
83 job->start();
84 });
85
86 menu->createDirectory();
87}
88
89#include "dropnewintonewfolderPlugin.moc"
90#include "moc_dropintonewfolderPlugin.cpp"
Provides information about the common properties of a group of KFileItem objects.
bool supportsMoving() const
Check if moving capability is supported.
QList< QUrl > urlList() const
List of urls, gathered from the fileitems.
void recordCopyJob(KIO::CopyJob *copyJob)
Record this CopyJob while it's happening and add a command for it so that the user can undo it.
static FileUndoManager * self()
int error() const
void result(KJob *job)
virtual Q_SCRIPTABLE void start()=0
void directoryCreated(const QUrl &url)
Emitted once the directory url has been successfully created.
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
A namespace for KIO globals.
KIOCORE_EXPORT CopyJob * move(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
Moves a file or directory src to the given destination dest.
Definition copyjob.cpp:2657
KIOCORE_EXPORT StatJob * stat(const QUrl &url, JobFlags flags=DefaultFlags)
Find all details for one file or directory.
Definition statjob.cpp:203
@ StatBasic
Filename, access, type, size, linkdest.
Definition global.h:255
void setEnabled(bool)
void triggered(bool checked)
QIcon fromTheme(const QString &name)
void append(QList< T > &&value)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isLocalFile() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:50:10 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.