]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/draganddrophelper.cpp
Apply 2 suggestion(s) to 1 file(s)
[dolphin.git] / src / views / draganddrophelper.cpp
1 /*
2 * SPDX-FileCopyrightText: 2007-2011 Peter Penz <peter.penz19@gmail.com>
3 * SPDX-FileCopyrightText: 2007 David Faure <faure@kde.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8 #include "draganddrophelper.h"
9
10 #include <KIO/DropJob>
11 #include <KJobWidgets>
12
13 #include <QDBusConnection>
14 #include <QDBusMessage>
15 #include <QDropEvent>
16 #include <QMimeData>
17
18 QHash<QUrl, bool> DragAndDropHelper::m_urlListMatchesUrlCache;
19
20 bool DragAndDropHelper::urlListMatchesUrl(const QList<QUrl> &urls, const QUrl &destUrl)
21 {
22 auto iteratorResult = m_urlListMatchesUrlCache.constFind(destUrl);
23 if (iteratorResult != m_urlListMatchesUrlCache.constEnd()) {
24 return *iteratorResult;
25 }
26
27 const bool destUrlMatches = std::find_if(urls.constBegin(),
28 urls.constEnd(),
29 [destUrl](const QUrl &url) {
30 return url.matches(destUrl, QUrl::StripTrailingSlash);
31 })
32 != urls.constEnd();
33
34 return *m_urlListMatchesUrlCache.insert(destUrl, destUrlMatches);
35 }
36
37 KIO::DropJob *DragAndDropHelper::dropUrls(const QUrl &destUrl, QDropEvent *event, QWidget *window)
38 {
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()));
43
44 QDBusMessage message = QDBusMessage::createMethodCall(remoteDBusClient,
45 remoteDBusPath,
46 QStringLiteral("org.kde.ark.DndExtract"),
47 QStringLiteral("extractSelectedFilesTo"));
48 message.setArguments({destUrl.toDisplayString(QUrl::PreferLocalFile)});
49 QDBusConnection::sessionBus().call(message);
50 } else {
51 if (urlListMatchesUrl(event->mimeData()->urls(), destUrl)) {
52 return nullptr;
53 }
54
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);
64 return job;
65 }
66 }
67
68 return nullptr;
69 }
70
71 bool DragAndDropHelper::supportsDropping(const KFileItem &destItem)
72 {
73 return (destItem.isDir() && destItem.isWritable()) || destItem.isDesktopFile();
74 }
75
76 void DragAndDropHelper::updateDropAction(QDropEvent *event, const QUrl &destUrl)
77 {
78 if (urlListMatchesUrl(event->mimeData()->urls(), destUrl)) {
79 event->setDropAction(Qt::IgnoreAction);
80 event->ignore();
81 }
82 KFileItem item(destUrl);
83 if (!item.isLocalFile() || supportsDropping(item)) {
84 event->setDropAction(event->proposedAction());
85 event->accept();
86 } else {
87 event->setDropAction(Qt::IgnoreAction);
88 event->ignore();
89 }
90 }
91
92 void DragAndDropHelper::clearUrlListMatchesUrlCache()
93 {
94 DragAndDropHelper::m_urlListMatchesUrlCache.clear();
95 }
96
97 bool DragAndDropHelper::isArkDndMimeType(const QMimeData *mimeData)
98 {
99 return mimeData->hasFormat(arkDndServiceMimeType()) && mimeData->hasFormat(arkDndPathMimeType());
100 }