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 #ifndef DRAGANDDROPHELPER_H
9 #define DRAGANDDROPHELPER_H
11 #include "dolphin_export.h"
14 #include <KIO/StatJob>
29 class DOLPHIN_EXPORT DragAndDropHelper
: public QObject
33 * Handles the dropping of URLs to the given destination. A context menu
34 * with the options 'Move Here', 'Copy Here', 'Link Here' and 'Cancel' is
35 * offered to the user. The drag destination must represent a directory or
36 * a desktop-file, otherwise the dropping gets ignored.
38 * @param destUrl URL of the item destination. Is used only if destItem::isNull()
40 * @param event Drop event.
41 * @param window Widget where the drop happened, will be used as parent of the drop menu.
42 * @return KIO::DropJob pointer or null in case the destUrl is contained
43 * in the mimeData url list.
45 static KIO::DropJob
*dropUrls(const QUrl
&destUrl
, QDropEvent
*event
, QWidget
*window
);
48 * Checks if the destination supports dropping.
50 * @param destItem The item destination.
51 * @return True if the destination is a directory and is writable, or it's a desktop file.
54 static bool supportsDropping(const KFileItem
&destItem
);
57 * @return True if destUrl is contained in the urls parameter.
59 static bool urlListMatchesUrl(const QList
<QUrl
> &urls
, const QUrl
&destUrl
);
62 * @return True if mimeData contains Ark's drag and drop mime types.
64 static bool isArkDndMimeType(const QMimeData
*mimeData
);
65 static QString
arkDndServiceMimeType()
67 return QStringLiteral("application/x-kde-ark-dndextract-service");
69 static QString
arkDndPathMimeType()
71 return QStringLiteral("application/x-kde-ark-dndextract-path");
75 * clear the internal cache.
77 static void clearUrlListMatchesUrlCache();
79 DragAndDropHelper(QObject
*parent
);
82 * Updates the drop action according to whether the destination supports dropping.
83 * If supportsDropping(destUrl), set dropAction = proposedAction. Otherwise, set
84 * dropAction = Qt::IgnoreAction.
86 * @param event Drop event.
87 * @param destUrl Destination URL.
89 void updateDropAction(QDropEvent
*event
, const QUrl
&destUrl
);
93 * Stores the results of the expensive checks made in urlListMatchesUrl.
95 static QHash
<QUrl
, bool> m_urlListMatchesUrlCache
;
98 * When updateDropAction() is called with a remote URL, we create a StatJob to
99 * check if the destination is a directory or a desktop file. We cache the result
100 * here to avoid doing the stat again on subsequent calls to updateDropAction().
102 KFileItem m_destItemCache
;
105 * Only keep the cache for 30 seconds, because the stat of the destUrl might change.
107 QTimer m_destItemCacheInvalidationTimer
;
110 * A StatJob on-fly to fill the cache for a remote URL. We shouldn't create more
111 * than one StatJob at a time, so we keep a pointer to the current one.
113 KIO::StatJob
*m_statJob
= nullptr;
116 * The URL for which the StatJob is running.
117 * Note: We can't use m_statJob->url() because StatJob might resolve the URL to be
118 * different from what we passed into stat(). E.g. "mtp:<bus-name>" is resolved
119 * to "mtp:<phone name>"
124 * The last event we received in updateDropAction(), but can't react to yet,
125 * because a StatJob is on-fly.
127 QDropEvent
*m_lastUndecidedEvent
= nullptr;