]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/draganddrophelper.h
versioncontrol: Prevent a use-after-free in UpdateItemStatesThread
[dolphin.git] / src / views / draganddrophelper.h
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 #ifndef DRAGANDDROPHELPER_H
9 #define DRAGANDDROPHELPER_H
10
11 #include "dolphin_export.h"
12
13 #include <KFileItem>
14 #include <KIO/StatJob>
15
16 #include <QList>
17 #include <QString>
18 #include <QTimer>
19 #include <QUrl>
20
21 class QDropEvent;
22 class QMimeData;
23 class QWidget;
24 namespace KIO
25 {
26 class DropJob;
27 }
28
29 class DOLPHIN_EXPORT DragAndDropHelper : public QObject
30 {
31 public:
32 /**
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.
37 *
38 * @param destUrl URL of the item destination. Is used only if destItem::isNull()
39 * is true.
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.
44 */
45 static KIO::DropJob *dropUrls(const QUrl &destUrl, QDropEvent *event, QWidget *window);
46
47 /**
48 * Checks if the destination supports dropping.
49 *
50 * @param destItem The item destination.
51 * @return True if the destination is a directory and is writable, or it's a desktop file.
52 * False otherwise.
53 */
54 static bool supportsDropping(const KFileItem &destItem);
55
56 /**
57 * @return True if destUrl is contained in the urls parameter.
58 */
59 static bool urlListMatchesUrl(const QList<QUrl> &urls, const QUrl &destUrl);
60
61 /**
62 * @return True if mimeData contains Ark's drag and drop mime types.
63 */
64 static bool isArkDndMimeType(const QMimeData *mimeData);
65 static QString arkDndServiceMimeType()
66 {
67 return QStringLiteral("application/x-kde-ark-dndextract-service");
68 }
69 static QString arkDndPathMimeType()
70 {
71 return QStringLiteral("application/x-kde-ark-dndextract-path");
72 }
73
74 /**
75 * clear the internal cache.
76 */
77 static void clearUrlListMatchesUrlCache();
78
79 DragAndDropHelper(QObject *parent);
80
81 /**
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.
85 *
86 * @param event Drop event.
87 * @param destUrl Destination URL.
88 */
89 void updateDropAction(QDropEvent *event, const QUrl &destUrl);
90
91 private:
92 /**
93 * Stores the results of the expensive checks made in urlListMatchesUrl.
94 */
95 static QHash<QUrl, bool> m_urlListMatchesUrlCache;
96
97 /**
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().
101 */
102 KFileItem m_destItemCache;
103
104 /**
105 * Only keep the cache for 30 seconds, because the stat of the destUrl might change.
106 */
107 QTimer m_destItemCacheInvalidationTimer;
108
109 /**
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.
112 */
113 KIO::StatJob *m_statJob = nullptr;
114
115 /**
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>"
120 */
121 QUrl m_statJobUrl;
122
123 /**
124 * The last event we received in updateDropAction(), but can't react to yet,
125 * because a StatJob is on-fly.
126 */
127 QDropEvent *m_lastUndecidedEvent = nullptr;
128 };
129
130 #endif