]>
cloud.milkyroute.net Git - dolphin.git/blob - src/views/draganddrophelper.cpp
2 * SPDX-FileCopyrightText: 2007-2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
5 * SPDX-License-Identifier: GPL-2.0-or-later
8 #include "draganddrophelper.h"
10 #include <KIO/DropJob>
11 #include <KJobWidgets>
13 #include <QDBusConnection>
14 #include <QDBusMessage>
18 QHash
<QUrl
, bool> DragAndDropHelper::m_urlListMatchesUrlCache
;
20 bool DragAndDropHelper::urlListMatchesUrl(const QList
<QUrl
> &urls
, const QUrl
&destUrl
)
22 auto iteratorResult
= m_urlListMatchesUrlCache
.constFind(destUrl
);
23 if (iteratorResult
!= m_urlListMatchesUrlCache
.constEnd()) {
24 return *iteratorResult
;
27 const bool destUrlMatches
= std::find_if(urls
.constBegin(),
29 [destUrl
](const QUrl
&url
) {
30 return url
.matches(destUrl
, QUrl::StripTrailingSlash
);
34 return *m_urlListMatchesUrlCache
.insert(destUrl
, destUrlMatches
);
37 KIO::DropJob
*DragAndDropHelper::dropUrls(const QUrl
&destUrl
, QDropEvent
*event
, QWidget
*window
)
39 const QMimeData
*mimeData
= event
->mimeData();
40 if (isArkDndMimeType(mimeData
)) {
41 const QString remoteDBusClient
= QString::fromUtf8(mimeData
->data(arkDndServiceMimeType()));
42 const QString remoteDBusPath
= QString::fromUtf8(mimeData
->data(arkDndPathMimeType()));
44 QDBusMessage message
= QDBusMessage::createMethodCall(remoteDBusClient
,
46 QStringLiteral("org.kde.ark.DndExtract"),
47 QStringLiteral("extractSelectedFilesTo"));
48 message
.setArguments({destUrl
.toDisplayString(QUrl::PreferLocalFile
)});
49 QDBusConnection::sessionBus().call(message
);
51 if (urlListMatchesUrl(event
->mimeData()->urls(), destUrl
)) {
55 // TODO: remove this check once Qt is fixed so that it doesn't emit a QDropEvent on Wayland
56 // when we called QDragMoveEvent::ignore()
57 // https://codereview.qt-project.org/c/qt/qtwayland/+/541750
58 KFileItem
item(destUrl
);
59 // KFileItem(QUrl) only stat local URLs, so we always allow dropping on non-local URLs
60 if (!item
.isLocalFile() || supportsDropping(item
)) {
61 // Drop into a directory or a desktop-file
62 KIO::DropJob
*job
= KIO::drop(event
, destUrl
);
63 KJobWidgets::setWindow(job
, window
);
71 bool DragAndDropHelper::supportsDropping(const KFileItem
&destItem
)
73 return (destItem
.isDir() && destItem
.isWritable()) || destItem
.isDesktopFile();
76 void DragAndDropHelper::updateDropAction(QDropEvent
*event
, const QUrl
&destUrl
)
78 if (urlListMatchesUrl(event
->mimeData()->urls(), destUrl
)) {
79 event
->setDropAction(Qt::IgnoreAction
);
82 KFileItem
item(destUrl
);
83 if (!item
.isLocalFile() || supportsDropping(item
)) {
84 event
->setDropAction(event
->proposedAction());
87 event
->setDropAction(Qt::IgnoreAction
);
92 void DragAndDropHelper::clearUrlListMatchesUrlCache()
94 DragAndDropHelper::m_urlListMatchesUrlCache
.clear();
97 bool DragAndDropHelper::isArkDndMimeType(const QMimeData
*mimeData
)
99 return mimeData
->hasFormat(arkDndServiceMimeType()) && mimeData
->hasFormat(arkDndPathMimeType());