From 7bd5bec21977c733dd3e1fc70f5afd66dda3ab97 Mon Sep 17 00:00:00 2001 From: David Faure Date: Fri, 26 Dec 2014 08:57:42 +0100 Subject: [PATCH] Dolphin: port from KonqOperations::doDrop to the new KIO::DropJob REVIEW: 121678 --- src/dolphintabwidget.cpp | 10 ++----- src/dolphinviewcontainer.cpp | 46 ++--------------------------- src/dolphinviewcontainer.h | 21 ------------- src/panels/folders/folderspanel.cpp | 8 ++--- src/panels/places/placespanel.cpp | 23 ++++----------- src/views/dolphinview.cpp | 25 +++++++++------- src/views/dolphinview.h | 5 ++++ src/views/draganddrophelper.cpp | 39 ++++++++---------------- src/views/draganddrophelper.h | 21 +++++-------- 9 files changed, 54 insertions(+), 144 deletions(-) diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp index a0c9b9d81..5b26359e6 100644 --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -23,7 +23,6 @@ #include "dolphintabpage.h" #include "dolphinviewcontainer.h" #include "dolphin_generalsettings.h" -#include "views/draganddrophelper.h" #include #include @@ -286,13 +285,8 @@ void DolphinTabWidget::openNewActivatedTab(int index) void DolphinTabWidget::tabDropEvent(int index, QDropEvent* event) { if (index >= 0) { - const DolphinView* view = tabPageAt(index)->activeViewContainer()->view(); - - QString error; - DragAndDropHelper::dropUrls(view->rootItem(), view->url(), event, error); - if (!error.isEmpty()) { - currentTabPage()->activeViewContainer()->showMessage(error, DolphinViewContainer::Error); - } + DolphinView* view = tabPageAt(index)->activeViewContainer()->view(); + view->dropUrls(view->url(), event); } } diff --git a/src/dolphinviewcontainer.cpp b/src/dolphinviewcontainer.cpp index 3954a12e6..8fea3ba9d 100644 --- a/src/dolphinviewcontainer.cpp +++ b/src/dolphinviewcontainer.cpp @@ -28,10 +28,8 @@ #include #include #include -#include #include #include -#include #include #include #include @@ -45,7 +43,6 @@ #include "filterbar/filterbar.h" #include "search/dolphinsearchbox.h" #include "statusbar/dolphinstatusbar.h" -#include "views/draganddrophelper.h" #include "views/viewmodecontroller.h" #include "views/viewproperties.h" @@ -60,9 +57,7 @@ DolphinViewContainer::DolphinViewContainer(const QUrl& url, QWidget* parent) : m_statusBar(0), m_statusBarTimer(0), m_statusBarTimestamp(), - m_autoGrabFocus(true), - m_dropDestination(), - m_dropEvent(0) + m_autoGrabFocus(true) #ifdef KActivities_FOUND , m_activityResourceInstance(0) #endif @@ -74,8 +69,6 @@ DolphinViewContainer::DolphinViewContainer(const QUrl& url, QWidget* parent) : m_topLayout->setMargin(0); m_urlNavigator = new KUrlNavigator(new KFilePlacesModel(this), url, this); - connect(m_urlNavigator, &KUrlNavigator::urlsDropped, - this, &DolphinViewContainer::dropUrls); connect(m_urlNavigator, &KUrlNavigator::activated, this, &DolphinViewContainer::activate); connect(m_urlNavigator->editor(), &KUrlComboBox::completionModeChanged, @@ -147,6 +140,8 @@ DolphinViewContainer::DolphinViewContainer(const QUrl& url, QWidget* parent) : this, &DolphinViewContainer::slotHistoryChanged); connect(m_urlNavigator, &KUrlNavigator::returnPressed, this, &DolphinViewContainer::slotReturnPressed); + connect(m_urlNavigator, &KUrlNavigator::urlsDropped, + m_view, &DolphinView::dropUrls); // Initialize status bar m_statusBar = new DolphinStatusBar(this); @@ -612,41 +607,6 @@ void DolphinViewContainer::slotUrlNavigatorLocationChanged(const QUrl& url) } } -void DolphinViewContainer::dropUrls(const QUrl& destination, QDropEvent* event) -{ - m_dropDestination = destination; - - const QMimeData* mimeData = event->mimeData(); - QMimeData* mimeDataCopy = new QMimeData; - foreach (const QString& format, mimeData->formats()) { - mimeDataCopy->setData(format, mimeData->data(format)); - } - - m_dropEvent.reset(new QDropEvent(event->pos(), - event->possibleActions(), - mimeDataCopy, - event->mouseButtons(), - event->keyboardModifiers())); - - QTimer::singleShot(0, this, SLOT(dropUrlsDelayed())); -} - -void DolphinViewContainer::dropUrlsDelayed() -{ - if (m_dropEvent.isNull()) { - return; - } - - QString error; - DragAndDropHelper::dropUrls(KFileItem(), m_dropDestination, m_dropEvent.data(), error); - if (!error.isEmpty()) { - showMessage(error, Error); - } - - delete m_dropEvent->mimeData(); - m_dropEvent.reset(); -} - void DolphinViewContainer::redirect(const QUrl& oldUrl, const QUrl& newUrl) { Q_UNUSED(oldUrl); diff --git a/src/dolphinviewcontainer.h b/src/dolphinviewcontainer.h index bd4141db5..62f91100e 100644 --- a/src/dolphinviewcontainer.h +++ b/src/dolphinviewcontainer.h @@ -257,24 +257,6 @@ private slots: */ void slotUrlNavigatorLocationChanged(const QUrl& url); - /** - * Is connected with the URL navigator and drops the URLs - * above the destination \a destination. - * - * Creates a copy of \a event and invokes \a dropUrlsDelayed with a - * queued connection. - */ - void dropUrls(const QUrl& destination, QDropEvent* event); - - /** - * Is invoked with a queued connection by \a dropUrls to prevent that the - * drop actions are executed in the URL navigator menu's nested event loop, - * which might cause a crash. Simply using a queued connection from the URL - * navigator to \a dropUrls would not work because the \a event pointer - * would be dangling then. - */ - void dropUrlsDelayed(); - /** * Is invoked when a redirection is done and changes the * URL of the URL navigator to \a newUrl without triggering @@ -341,9 +323,6 @@ private: QElapsedTimer m_statusBarTimestamp; // Time in ms since last update bool m_autoGrabFocus; - QUrl m_dropDestination; - QScopedPointer m_dropEvent; - #ifdef KF5Activities_FOUND private: KActivities::ResourceInstance * m_activityResourceInstance; diff --git a/src/panels/folders/folderspanel.cpp b/src/panels/folders/folderspanel.cpp index f56f5a139..b83e950e5 100644 --- a/src/panels/folders/folderspanel.cpp +++ b/src/panels/folders/folderspanel.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include #include @@ -239,10 +240,9 @@ void FoldersPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* eve event->buttons(), event->modifiers()); - QString error; - DragAndDropHelper::dropUrls(destItem, destItem.url(), &dropEvent, error); - if (!error.isEmpty()) { - emit errorMessage(error); + KIO::DropJob *job = DragAndDropHelper::dropUrls(destItem.url(), &dropEvent, this); + if (job) { + connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); }); } } } diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp index b04191f1c..631b6b69d 100644 --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -375,11 +376,7 @@ void PlacesPanel::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* even event->buttons(), event->modifiers()); - QString error; - DragAndDropHelper::dropUrls(KFileItem(), destUrl, &dropEvent, error); - if (!error.isEmpty()) { - emit errorMessage(error); - } + slotUrlsDropped(destUrl, &dropEvent, this); } void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success) @@ -390,12 +387,7 @@ void PlacesPanel::slotItemDropEventStorageSetupDone(int index, bool success) if ((index == m_itemDropEventIndex) && m_itemDropEvent && m_itemDropEventMimeData) { if (success) { QUrl destUrl = m_model->placesItem(index)->url(); - - QString error; - DragAndDropHelper::dropUrls(KFileItem(), destUrl, m_itemDropEvent, error); - if (!error.isEmpty()) { - emit errorMessage(error); - } + slotUrlsDropped(destUrl, m_itemDropEvent, this); } delete m_itemDropEventMimeData; @@ -414,13 +406,10 @@ void PlacesPanel::slotAboveItemDropEvent(int index, QGraphicsSceneDragDropEvent* void PlacesPanel::slotUrlsDropped(const QUrl& dest, QDropEvent* event, QWidget* parent) { - Q_UNUSED(parent); - QString error; - DragAndDropHelper::dropUrls(KFileItem(), dest, event, error); - if (!error.isEmpty()) { - emit errorMessage(error); + KIO::DropJob *job = DragAndDropHelper::dropUrls(dest, event, parent); + if (job) { + connect(job, &KIO::DropJob::result, this, [this](KJob *job) { if (job->error()) emit errorMessage(job->errorString()); }); } - } void PlacesPanel::slotTrashUpdated(KJob* job) diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index cb25c6555..32e182ce7 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -51,13 +51,13 @@ #include #include #include +#include #include #include #include #include #include #include -#include #include #include "dolphinnewfilemenuobserver.h" @@ -1039,22 +1039,22 @@ void DolphinView::slotItemDropEvent(int index, QGraphicsSceneDragDropEvent* even event->mimeData(), event->buttons(), event->modifiers()); + dropUrls(destUrl, &dropEvent); - QString error; - KonqOperations* job = DragAndDropHelper::dropUrls(destItem, destUrl, &dropEvent, error); - if (!error.isEmpty()) { - emit infoMessage(error); - } + setActive(true); +} + +void DolphinView::dropUrls(const QUrl &destUrl, QDropEvent *dropEvent) +{ + KIO::DropJob* job = DragAndDropHelper::dropUrls(destUrl, dropEvent, this); if (job && destUrl == url()) { // Mark the dropped urls as selected. m_clearSelectionBeforeSelectingNewItems = true; m_markFirstNewlySelectedItemAsCurrent = true; - connect(job, &KonqOperations::itemCreated, this, &DolphinView::slotItemCreated); - //connect(job, &KIO::InteractiveDropJob::result, this, &DolphinView::slotPasteJobResult); + connect(job, &KIO::DropJob::itemCreated, this, &DolphinView::slotItemCreated); + connect(job, &KIO::DropJob::result, this, &DolphinView::slotPasteJobResult); } - - setActive(true); } void DolphinView::slotModelChanged(KItemModelBase* current, KItemModelBase* previous) @@ -1096,8 +1096,11 @@ void DolphinView::slotItemCreated(const QUrl& url) m_selectedUrls << url; } -void DolphinView::slotPasteJobResult(KJob *) +void DolphinView::slotPasteJobResult(KJob *job) { + if (job->error()) { + emit errorMessage(job->errorString()); + } if (!m_selectedUrls.isEmpty()) { m_selectedUrls << KDirModel::simplifiedUrlList(m_selectedUrls); } diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h index c054c311a..aa4492bc3 100644 --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -362,6 +362,11 @@ public slots: */ void pasteIntoFolder(); + /** + * Handles a drop of @p dropEvent onto @p destUrl + */ + void dropUrls(const QUrl &destUrl, QDropEvent *dropEvent); + void stopLoading(); /** Activates the view if the item list container gets focus. */ diff --git a/src/views/draganddrophelper.cpp b/src/views/draganddrophelper.cpp index a09faa345..f740fd520 100644 --- a/src/views/draganddrophelper.cpp +++ b/src/views/draganddrophelper.cpp @@ -20,25 +20,20 @@ #include "draganddrophelper.h" +#include +#include +#include +#include +#include + #include #include -#include -#include -#include -#include -#include #include +#include +#include -KonqOperations* DragAndDropHelper::dropUrls(const KFileItem& destItem, const QUrl& destUrl, QDropEvent* event, QString& error) +KIO::DropJob* DragAndDropHelper::dropUrls(const QUrl& destUrl, QDropEvent* event, QWidget* window) { - error.clear(); - - if (!destItem.isNull() && !destItem.isWritable()) { - error = xi18nc("@info:status", "Access denied. Could not write to %1", - destUrl.toDisplayString(QUrl::PreferLocalFile)); - return 0; - } - const QMimeData* mimeData = event->mimeData(); if (mimeData->hasFormat("application/x-kde-ark-dndextract-service") && mimeData->hasFormat("application/x-kde-ark-dndextract-path")) { @@ -49,19 +44,11 @@ KonqOperations* DragAndDropHelper::dropUrls(const KFileItem& destItem, const QUr "org.kde.ark.DndExtract", "extractSelectedFilesTo"); message.setArguments({destUrl.toDisplayString(QUrl::PreferLocalFile)}); QDBusConnection::sessionBus().call(message); - } else if (!destItem.isNull() && (destItem.isDir() || destItem.isDesktopFile())) { - // Drop into a directory or a desktop-file - const QList urls = KUrlMimeData::urlsFromMimeData(mimeData); - foreach (const QUrl& url, urls) { - if (url == destUrl) { - error = i18nc("@info:status", "A folder cannot be dropped into itself"); - return 0; - } - } - - return KonqOperations::doDrop(destItem, destUrl, event, QApplication::activeWindow(), QList()); } else { - return KonqOperations::doDrop(KFileItem(), destUrl, event, QApplication::activeWindow(), QList()); + // Drop into a directory or a desktop-file + KIO::DropJob *job = KIO::drop(event, destUrl); + KJobWidgets::setWindow(job, window); + return job; } return 0; diff --git a/src/views/draganddrophelper.h b/src/views/draganddrophelper.h index c4ae974b5..85268eb4f 100644 --- a/src/views/draganddrophelper.h +++ b/src/views/draganddrophelper.h @@ -25,10 +25,10 @@ #include -class KFileItem; class QUrl; class QDropEvent; -class KonqOperations; +class QWidget; +namespace KIO { class DropJob; } class LIBDOLPHINPRIVATE_EXPORT DragAndDropHelper { @@ -39,22 +39,15 @@ public: * offered to the user. The drag destination must represent a directory or * a desktop-file, otherwise the dropping gets ignored. * - * @param destItem Item of the destination. Can be 0 (KFileItem::isNull()) if - * no file-item is available for the destination. In this case - * destUrl is used as fallback. For performance reasons it is - * recommended to pass a file-item if available. * @param destUrl URL of the item destination. Is used only if destItem::isNull() * is true. * @param event Drop event. - * @param error Error message intended to be shown for users if dropping is not - * possible. If an empty string is returned, the dropping has been - * successful. - * @return KonqOperations pointer + * @param window Associated widget. + * @return KIO::DropJob pointer */ - static KonqOperations* dropUrls(const KFileItem& destItem, - const QUrl& destUrl, - QDropEvent* event, - QString& error); + static KIO::DropJob* dropUrls(const QUrl& destUrl, + QDropEvent* event, + QWidget *window); }; #endif