]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/draganddrophelper.cpp
DragAndDropHelper::updateDropAction: use StatJob for remote URLs
[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 DragAndDropHelper::DragAndDropHelper(QObject *parent)
77 : QObject(parent)
78 {
79 m_destItemCacheInvalidationTimer.setSingleShot(true);
80 m_destItemCacheInvalidationTimer.setInterval(30000);
81 connect(&m_destItemCacheInvalidationTimer, &QTimer::timeout, this, [this]() {
82 m_destItemCache = KFileItem();
83 });
84 }
85
86 void DragAndDropHelper::updateDropAction(QDropEvent *event, const QUrl &destUrl)
87 {
88 auto processEvent = [this](QDropEvent *event) {
89 if (supportsDropping(m_destItemCache)) {
90 event->setDropAction(event->proposedAction());
91 event->accept();
92 } else {
93 event->setDropAction(Qt::IgnoreAction);
94 event->ignore();
95 }
96 };
97
98 m_lastUndecidedEvent = nullptr;
99
100 if (urlListMatchesUrl(event->mimeData()->urls(), destUrl)) {
101 event->setDropAction(Qt::IgnoreAction);
102 event->ignore();
103 return;
104 }
105
106 if (destUrl == m_destItemCache.url()) {
107 // We already received events for this URL, and already have the
108 // stat result cached because:
109 // 1. it's a local file, and we already called KFileItem(destUrl)
110 // 2. it's a remote file, and StatJob finished
111 processEvent(event);
112 return;
113 }
114
115 if (m_statJob) {
116 if (destUrl == m_statJobUrl) {
117 // We already received events for this URL. Still waiting for
118 // the stat result. StatJob will process the event when it finishes.
119 m_lastUndecidedEvent = event;
120 return;
121 }
122
123 // We are waiting for the stat result of a different URL. Cancel.
124 m_statJob->kill();
125 m_statJob = nullptr;
126 m_statJobUrl.clear();
127 }
128
129 if (destUrl.isLocalFile()) {
130 // New local URL. KFileItem will stat on demand.
131 m_destItemCache = KFileItem(destUrl);
132 m_destItemCacheInvalidationTimer.start();
133 processEvent(event);
134 return;
135 }
136
137 // New remote URL. Start a StatJob and process the event when it finishes.
138 m_lastUndecidedEvent = event;
139 m_statJob = KIO::stat(destUrl, KIO::StatJob::SourceSide, KIO::StatDetail::StatBasic, KIO::JobFlag::HideProgressInfo);
140 m_statJobUrl = destUrl;
141 connect(m_statJob, &KIO::StatJob::result, this, [this, processEvent](KJob *job) {
142 KIO::StatJob *statJob = static_cast<KIO::StatJob *>(job);
143
144 m_destItemCache = KFileItem(statJob->statResult(), m_statJobUrl);
145 m_destItemCacheInvalidationTimer.start();
146
147 if (m_lastUndecidedEvent) {
148 processEvent(m_lastUndecidedEvent);
149 m_lastUndecidedEvent = nullptr;
150 }
151
152 m_statJob = nullptr;
153 m_statJobUrl.clear();
154 });
155 }
156
157 void DragAndDropHelper::clearUrlListMatchesUrlCache()
158 {
159 DragAndDropHelper::m_urlListMatchesUrlCache.clear();
160 }
161
162 bool DragAndDropHelper::isArkDndMimeType(const QMimeData *mimeData)
163 {
164 return mimeData->hasFormat(arkDndServiceMimeType()) && mimeData->hasFormat(arkDndPathMimeType());
165 }