]> cloud.milkyroute.net Git - dolphin.git/commitdiff
[Places Panel] Reject drops on unwritable locations
authorKai Uwe Broulik <kde@privat.broulik.de>
Mon, 7 Feb 2022 12:10:53 +0000 (13:10 +0100)
committerKai Uwe Broulik <kde@privat.broulik.de>
Fri, 4 Mar 2022 18:25:38 +0000 (18:25 +0000)
Indicate that you cannot drop here.

Avoids a "Cannot drop file" or "not supported" error when
dropping files ontop of a search or timeline URL.

It is done in Dolphin rather than the library as there we cannot
assume what a consumer might be doing with the drop.

src/panels/places/placespanel.cpp
src/panels/places/placespanel.h

index 81056fb9cca863bc55067b3b9a780f46aede9068..38dc4dc3a65d53c3168fffa3fd2a811999a5f65e 100644 (file)
 #include <KIO/Job>
 #include <KListOpenFilesJob>
 #include <KLocalizedString>
+#include <KProtocolManager>
 
 #include <QIcon>
 #include <QMenu>
+#include <QMimeData>
 #include <QShowEvent>
 #include <QTimer>
 
@@ -127,6 +129,36 @@ void PlacesPanel::showEvent(QShowEvent* event)
     KFilePlacesView::showEvent(event);
 }
 
+static bool isInternalDrag(const QMimeData *mimeData)
+{
+    const auto formats = mimeData->formats();
+    for (const auto &format : formats) {
+        // from KFilePlacesModel::_k_internalMimetype
+        if (format.startsWith(QLatin1String("application/x-kfileplacesmodel-"))) {
+            return true;
+        }
+    }
+    return false;
+}
+
+void PlacesPanel::dragMoveEvent(QDragMoveEvent *event)
+{
+    const QModelIndex index = indexAt(event->pos());
+    if (index.isValid()) {
+        auto *placesModel = static_cast<KFilePlacesModel *>(model());
+
+        // Reject drag ontop of a non-writable protocol
+        // We don't know whether we're dropping inbetween or ontop of a place
+        // so still allow internal drag events so that re-arranging still works.
+        const QUrl url = placesModel->url(index);
+        if (url.isValid() && !isInternalDrag(event->mimeData()) && !KProtocolManager::supportsWriting(url)) {
+            event->setDropAction(Qt::IgnoreAction);
+        }
+    }
+
+    KFilePlacesView::dragMoveEvent(event);
+}
+
 void PlacesPanel::slotConfigureTrash()
 {
     const QUrl url = currentIndex().data(KFilePlacesModel::UrlRole).toUrl();
index 97be1f735016ccd05bd1523e01b941ab4caf8601..6c37301ad6afb19fe7e7899495e89924291b50d4 100644 (file)
@@ -54,6 +54,7 @@ Q_SIGNALS:
 
 protected:
     void showEvent(QShowEvent* event) override;
+    void dragMoveEvent(QDragMoveEvent *event) override;
 
 private Q_SLOTS:
     void slotConfigureTrash();