From c9c3531c0b6da07de8f90761a3d799ace62f8e89 Mon Sep 17 00:00:00 2001 From: Jin Liu Date: Sat, 17 Feb 2024 11:14:46 +0000 Subject: Improve DnD handling in read-only dirs 1. Places panel and tabbar update drag status in read-only dir 2. Don't create drop job in readonly directories --- src/views/draganddrophelper.cpp | 36 ++++++++++++++++++++++++++++++++---- src/views/draganddrophelper.h | 30 ++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 4 deletions(-) (limited to 'src/views') diff --git a/src/views/draganddrophelper.cpp b/src/views/draganddrophelper.cpp index 2953233d0..efdec5b92 100644 --- a/src/views/draganddrophelper.cpp +++ b/src/views/draganddrophelper.cpp @@ -52,15 +52,43 @@ KIO::DropJob *DragAndDropHelper::dropUrls(const QUrl &destUrl, QDropEvent *event return nullptr; } - // Drop into a directory or a desktop-file - KIO::DropJob *job = KIO::drop(event, destUrl); - KJobWidgets::setWindow(job, window); - return job; + if (supportsDropping(destUrl)) { + // Drop into a directory or a desktop-file + KIO::DropJob *job = KIO::drop(event, destUrl); + KJobWidgets::setWindow(job, window); + return job; + } } return nullptr; } +bool DragAndDropHelper::supportsDropping(const QUrl &destUrl) +{ + KFileItem item(destUrl); + return supportsDropping(item); +} + +bool DragAndDropHelper::supportsDropping(const KFileItem &destItem) +{ + return (destItem.isDir() && destItem.isWritable()) || destItem.isDesktopFile(); +} + +void DragAndDropHelper::updateDropAction(QDropEvent *event, const QUrl &destUrl) +{ + if (urlListMatchesUrl(event->mimeData()->urls(), destUrl)) { + event->setDropAction(Qt::IgnoreAction); + event->ignore(); + } + if (supportsDropping(destUrl)) { + event->setDropAction(event->proposedAction()); + event->accept(); + } else { + event->setDropAction(Qt::IgnoreAction); + event->ignore(); + } +} + void DragAndDropHelper::clearUrlListMatchesUrlCache() { DragAndDropHelper::m_urlListMatchesUrlCache.clear(); diff --git a/src/views/draganddrophelper.h b/src/views/draganddrophelper.h index 656cefe1b..0eee34a3d 100644 --- a/src/views/draganddrophelper.h +++ b/src/views/draganddrophelper.h @@ -10,6 +10,8 @@ #include "dolphin_export.h" +#include + #include #include #include @@ -40,6 +42,34 @@ public: */ static KIO::DropJob *dropUrls(const QUrl &destUrl, QDropEvent *event, QWidget *window); + /** + * Checks if the destination supports dropping. + * + * @param destUrl URL of the item destination. + * @return True if the destination is a directory and is writable, or it's a desktop file. + * False otherwise. + */ + static bool supportsDropping(const QUrl &destUrl); + + /** + * Checks if the destination supports dropping. + * + * @param destItem The item destination. + * @return True if the destination is a directory and is writable, or it's a desktop file. + * False otherwise. + */ + static bool supportsDropping(const KFileItem &destItem); + + /** + * Updates the drop action according to whether the destination supports dropping. + * If supportsDropping(destUrl), set dropAction = proposedAction. Otherwise, set + * dropAction = Qt::IgnoreAction. + * + * @param event Drop event. + * @param destUrl Destination URL. + */ + static void updateDropAction(QDropEvent *event, const QUrl &destUrl); + /** * @return True if destUrl is contained in the urls parameter. */ -- cgit v1.3