From 6fa9930b4959dbae7c4769d1bddc348adf198c70 Mon Sep 17 00:00:00 2001 From: shibakaneki Date: Thu, 10 Nov 2011 14:37:06 +0100 Subject: [PATCH] Supported drag and drop in the library from Firefox and Chrome --- src/board/UBLibraryController.cpp | 22 +++++++++++++++-- src/board/UBLibraryController.h | 2 +- src/core/UBApplication.cpp | 5 ++-- src/core/UBDownloadManager.cpp | 10 ++++++++ src/core/UBDownloadManager.h | 1 + src/gui/UBLibraryWidget.cpp | 33 +++++++++++++++++++++++++- src/gui/UBLibraryWidget.h | 2 ++ src/network/UBNetworkAccessManager.cpp | 7 +++--- 8 files changed, 71 insertions(+), 11 deletions(-) diff --git a/src/board/UBLibraryController.cpp b/src/board/UBLibraryController.cpp index 3288c5bd..b1e00bd2 100644 --- a/src/board/UBLibraryController.cpp +++ b/src/board/UBLibraryController.cpp @@ -23,6 +23,7 @@ #include "core/UBSettings.h" #include "core/UBSetting.h" #include "core/UBApplicationController.h" +#include "core/UBDownloadManager.h" #include "domain/UBAbstractWidget.h" #include "domain/UBGraphicsScene.h" @@ -110,7 +111,6 @@ void UBLibraryController::createDirectory(QUrl& pDirPath) void UBLibraryController::routeItem(QString& pItem, QString pMiddleDirectory) { - qDebug() << "routeItem: " << pItem; QFileInfo itemToRoute(pItem); QString mimetype = UBFileSystemUtils::mimeTypeFromFileName(itemToRoute.fileName()); QString destination(""); @@ -138,6 +138,7 @@ void UBLibraryController::routeItem(QString& pItem, QString pMiddleDirectory) createDirectory(url); } destination = UBFileSystemUtils::normalizeFilePath(destination + "/" + itemToRoute.fileName()); + QFile::copy(pItem, destination); } } @@ -187,7 +188,24 @@ void UBLibraryController::importItemOnLibrary(QString& pItemString) } } else{ - routeItem(pItemString); + if(pItemString.startsWith("uniboardTool://") || pItemString.startsWith("file://") || pItemString.startsWith("/")) + { + // The user dropped a local file + routeItem(pItemString); + } + else + { + // The user dropped a file from the web. We must download it. + sDownloadFileDesc desc; + desc.currentSize = 0; + desc.id = 0; + desc.isBackground = false; + desc.modal = false; + desc.name = QFileInfo(pItemString).fileName(); + desc.totalSize = 0; + desc.url = pItemString; + UBDownloadManager::downloadManager()->addFileToDownload(desc); + } } } diff --git a/src/board/UBLibraryController.h b/src/board/UBLibraryController.h index 9903260d..bb39e610 100644 --- a/src/board/UBLibraryController.h +++ b/src/board/UBLibraryController.h @@ -120,6 +120,7 @@ class UBLibraryController : public QObject void createNewFolder(QString name, UBLibElement* parentElem); bool canItemsOnElementBeDeleted(UBLibElement *pElement); + void routeItem(QString& pItem, QString pMiddleDirectory = QString()); signals: void dialogClosed(int state); @@ -153,7 +154,6 @@ class UBLibraryController : public QObject QList addVirtualElementsForItemPath(const QString& pPath); void createInternalWidgetItems(); - void routeItem(QString& pItem, QString pMiddleDirectory = QString()); void createDirectory(QUrl& pDirPath); QUrl mAudioStandardDirectoryPath; diff --git a/src/core/UBApplication.cpp b/src/core/UBApplication.cpp index c5206599..53bbebca 100644 --- a/src/core/UBApplication.cpp +++ b/src/core/UBApplication.cpp @@ -633,15 +633,14 @@ QString UBApplication::globalStyleSheet() QString UBApplication::urlFromHtml(QString html) { + qDebug() << "HTML: " << html.remove(QRegExp("[\\0]")); QString url; QDomDocument domDoc; - domDoc.setContent(html); + domDoc.setContent(html.remove(QRegExp("[\\0]"))); QDomElement rootElem = domDoc.documentElement(); url = rootElem.attribute("src"); - qDebug() << url; - return url; } diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index 36700b77..982a1cd2 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -15,6 +15,8 @@ #include "UBDownloadManager.h" #include "core/UBApplication.h" #include "gui/UBMainWindow.h" +#include "board/UBBoardController.h" +#include "board/UBBoardPaletteManager.h" /** The unique instance of the download manager */ static UBDownloadManager* pInstance = NULL; @@ -73,6 +75,10 @@ void UBDownloadManager::addFileToDownload(sDownloadFileDesc desc) updateDownloadOrder(); UBApplication::mainWindow->showDownloadWidget(); } + else + { + UBApplication::boardController->paletteManager()->startDownloads(); + } emit fileAddedToDownload(); } @@ -190,6 +196,10 @@ void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl // The downloaded file is modal so we must put it on the board emit addDownloadedFileToBoard(pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); } + else + { + emit addDownloadedFileToLibrary(pSuccess, sourceUrl, pContentTypeHeader, pData); + } break; } } diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h index dcbbcc2d..4b5ea49c 100644 --- a/src/core/UBDownloadManager.h +++ b/src/core/UBDownloadManager.h @@ -78,6 +78,7 @@ signals: void downloadFinished(int id); void downloadModalFinished(); void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void addDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData); void cancelAllDownloads(); void allDownloadsFinished(); diff --git a/src/gui/UBLibraryWidget.cpp b/src/gui/UBLibraryWidget.cpp index ab066eb9..05fe0b36 100644 --- a/src/gui/UBLibraryWidget.cpp +++ b/src/gui/UBLibraryWidget.cpp @@ -14,6 +14,7 @@ */ #include #include +#include #include "UBLibraryWidget.h" #include "core/UBSettings.h" @@ -24,6 +25,7 @@ #include "board/UBLibraryController.h" #include "core/memcheck.h" +#include "core/UBDownloadManager.h" #include "frameworks/UBFileSystemUtils.h" @@ -87,6 +89,7 @@ void UBLibraryWidget::init() connect(this, SIGNAL(mouseClick(QGraphicsItem*,int)), this, SLOT(onItemClicked(QGraphicsItem*,int))); connect(this, SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged())); + connect(UBDownloadManager::downloadManager(), SIGNAL(addDownloadedFileToLibrary(bool,QUrl,QString,QByteArray)), this, SLOT(onAddDownloadedFileToLibrary(bool,QUrl,QString,QByteArray))); } /** @@ -362,7 +365,14 @@ void UBLibraryWidget::dropEvent(QDropEvent *event) } else if (pMimeData->hasHtml()) { - qDebug() << "hasHtml Unsupported yet"; + qDebug() << "hasHtml"; + QString html = pMimeData->html(); + QString url = UBApplication::urlFromHtml(html); + if("" != url) + { + mLibraryController->importItemOnLibrary(url); + bDropAccepted = true; + } } else if (pMimeData->hasText()) { @@ -666,3 +676,24 @@ void UBNewFolderDlg::text_Edited(const QString &newText) QToolTip::showText(mpLineEdit->mapToGlobal(QPoint()), "A file name can`t contain any of the following characters:\r\n"+illegalCharList); } } + +void UBLibraryWidget::onAddDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentHeader, QByteArray pData) +{ + Q_UNUSED(pContentHeader); + if(pSuccess) + { + QDir dir; + dir.mkdir("tmp"); + QString qsFileName = QFileInfo(sourceUrl.toString()).fileName(); + QString qsFilePath = UBFileSystemUtils::normalizeFilePath(QString("tmp/%0").arg(qsFileName)); + QFile f(qsFilePath); + if(f.open(QIODevice::WriteOnly)) + { + f.write(pData); + f.close(); + } + mLibraryController->routeItem(qsFilePath); + dir.remove(qsFileName); + dir.rmdir("tmp"); // Due to Qt, the directoy will be removed only if it's empty :) + } +} diff --git a/src/gui/UBLibraryWidget.h b/src/gui/UBLibraryWidget.h index d0420c76..88bf56ee 100644 --- a/src/gui/UBLibraryWidget.h +++ b/src/gui/UBLibraryWidget.h @@ -55,6 +55,8 @@ public slots: void onSearchElement(QString elem); void onNewFolderToCreate(); void onDropMe(const QMimeData* _data); + void onAddDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentHeader, QByteArray pData); + signals: void navigBarUpdate(UBLibElement* pElem); void itemsSelected(QList elemList, bool inTrash); diff --git a/src/network/UBNetworkAccessManager.cpp b/src/network/UBNetworkAccessManager.cpp index c2b7aa10..4f32444a 100644 --- a/src/network/UBNetworkAccessManager.cpp +++ b/src/network/UBNetworkAccessManager.cpp @@ -84,11 +84,10 @@ QNetworkReply* UBNetworkAccessManager::createRequest(Operation op, const QNetwor QNetworkReply *UBNetworkAccessManager::get(const QNetworkRequest &request) { - qDebug() << "request url: " << request.url(); - QTime loadStartTime; + QTime loadStartTime; loadStartTime.start(); - QNetworkReply *networkReply = QNetworkAccessManager::get(request); - return networkReply; + QNetworkReply *networkReply = QNetworkAccessManager::get(request); + return networkReply; } void UBNetworkAccessManager::authenticationRequired(QNetworkReply *reply, QAuthenticator *auth)