From b86614eddeb8e507e4fc5c1fb318222862239115 Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Fri, 21 Sep 2012 16:17:55 +0300 Subject: [PATCH 1/7] Implemented new helper class for downloading local files into document in separate thread. Have a problem - should take care about saving original src path. --- src/api/UBWidgetUniboardAPI.cpp | 2 +- src/board/UBBoardController.cpp | 107 ++++++++++++++++----------- src/board/UBBoardController.h | 4 +- src/core/UBDownloadManager.cpp | 30 +++++--- src/core/UBDownloadManager.h | 3 +- src/domain/UBGraphicsMediaItem.cpp | 8 +- src/domain/UBGraphicsScene.cpp | 5 ++ src/frameworks/UBFileSystemUtils.cpp | 90 ++++++++++++++++++++++ src/frameworks/UBFileSystemUtils.h | 45 ++++++++++- src/gui/UBFeaturesWidget.cpp | 4 +- 10 files changed, 235 insertions(+), 63 deletions(-) diff --git a/src/api/UBWidgetUniboardAPI.cpp b/src/api/UBWidgetUniboardAPI.cpp index b86e7aa1..e49bfe63 100644 --- a/src/api/UBWidgetUniboardAPI.cpp +++ b/src/api/UBWidgetUniboardAPI.cpp @@ -506,7 +506,7 @@ void UBWidgetUniboardAPI::ProcessDropEvent(QGraphicsSceneDragDropEvent *event) sDownloadFileDesc desc; desc.dest = sDownloadFileDesc::graphicsWidget; desc.modal = true; - desc.url = url; + desc.srcUrl = url; desc.currentSize = 0; desc.name = QFileInfo(url).fileName(); desc.totalSize = 0; // The total size will be retrieved during the download diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index ddc30c9e..358ce4ce 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -984,22 +984,36 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const || contentType.startsWith("application/widget") || contentType.startsWith("application/vnd.apple-widget"); - QFile file(fileName); - if (shouldLoadFileData) + { + QFile file(fileName); file.open(QIODevice::ReadOnly); - - downloadFinished(true, formedUrl, contentType, file.readAll(), pPos, pSize, isBackground, internalData); - - if (shouldLoadFileData) + downloadFinished(true, formedUrl, contentType, file.readAll(), pPos, pSize, isBackground, internalData); file.close(); + } + else + { + // media items should be copyed in separate thread + + sDownloadFileDesc desc; + desc.modal = false; + desc.srcUrl = sUrl; + desc.currentSize = 0; + desc.name = QFileInfo(url.toString()).fileName(); + desc.totalSize = 0; // The total size will be retrieved during the download + desc.pos = pPos; + desc.size = pSize; + desc.isBackground = isBackground; + + UBDownloadManager::downloadManager()->addFileToDownload(desc); + } } else { // When we fall there, it means that we are dropping something from the web to the board sDownloadFileDesc desc; desc.modal = true; - desc.url = url.toString(); + desc.srcUrl = url.toString(); desc.currentSize = 0; desc.name = QFileInfo(url.toString()).fileName(); desc.totalSize = 0; // The total size will be retrieved during the download @@ -1157,11 +1171,9 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri qDebug() << "accepting mime type" << mimeType << "as video"; UBGraphicsMediaItem *mediaVideoItem = 0; - + QUuid uuid = QUuid::createUuid(); if (pData.length() > 0) { - QUuid uuid = QUuid::createUuid(); - QString destFile; bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), sourceUrl.toString(), @@ -1178,16 +1190,16 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri QUrl url = QUrl::fromLocalFile(destFile); mediaVideoItem = mActiveScene->addMedia(url, false, pPos); - - mediaVideoItem->setSourceUrl(sourceUrl); - mediaVideoItem->setUuid(uuid); } else { - mediaVideoItem = addVideo(sourceUrl, false, pPos); + qDebug() << sourceUrl.toString(); + mediaVideoItem = addVideo(sourceUrl, false, pPos, true); } if(mediaVideoItem){ + mediaVideoItem->setSourceUrl(sourceUrl); + mediaVideoItem->setUuid(uuid); connect(this, SIGNAL(activeSceneChanged()), mediaVideoItem, SLOT(activeSceneChanged())); } @@ -1201,10 +1213,9 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri UBGraphicsMediaItem *audioMediaItem = 0; + QUuid uuid = QUuid::createUuid(); if (pData.length() > 0) { - QUuid uuid = QUuid::createUuid(); - QString destFile; bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), sourceUrl.toString(), @@ -1221,16 +1232,15 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri QUrl url = QUrl::fromLocalFile(destFile); audioMediaItem = mActiveScene->addMedia(url, false, pPos); - - audioMediaItem->setSourceUrl(sourceUrl); - audioMediaItem->setUuid(uuid); } else { - audioMediaItem = addAudio(sourceUrl, false, pPos); + audioMediaItem = addAudio(sourceUrl, false, pPos, true); } if(audioMediaItem){ + audioMediaItem->setSourceUrl(sourceUrl); + audioMediaItem->setUuid(uuid); connect(this, SIGNAL(activeSceneChanged()), audioMediaItem, SLOT(activeSceneChanged())); } @@ -2023,23 +2033,28 @@ void UBBoardController::grabScene(const QRectF& pSceneRect) } } -UBGraphicsMediaItem* UBBoardController::addVideo(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos) +UBGraphicsMediaItem* UBBoardController::addVideo(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos, bool bUseSource) { QUuid uuid = QUuid::createUuid(); QUrl concreteUrl = pSourceUrl; - QString destFile; - bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), - pSourceUrl.toLocalFile(), - UBPersistenceManager::videoDirectory, - uuid, - destFile); - if (!b) + // media file is not in document folder yet + if (!bUseSource) { - showMessage(tr("Add file operation failed: file copying error")); - return NULL; - } - concreteUrl = QUrl::fromLocalFile(destFile); + QString destFile; + bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), + pSourceUrl.toLocalFile(), + UBPersistenceManager::videoDirectory, + uuid, + destFile); + if (!b) + { + showMessage(tr("Add file operation failed: file copying error")); + return NULL; + } + concreteUrl = QUrl::fromLocalFile(destFile); + }// else we just use source Url. + UBGraphicsMediaItem* vi = mActiveScene->addMedia(concreteUrl, startPlay, pos); selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); @@ -2053,23 +2068,27 @@ UBGraphicsMediaItem* UBBoardController::addVideo(const QUrl& pSourceUrl, bool st } -UBGraphicsMediaItem* UBBoardController::addAudio(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos) +UBGraphicsMediaItem* UBBoardController::addAudio(const QUrl& pSourceUrl, bool startPlay, const QPointF& pos, bool bUseSource) { QUuid uuid = QUuid::createUuid(); QUrl concreteUrl = pSourceUrl; - QString destFile; - bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), - pSourceUrl.toLocalFile(), - UBPersistenceManager::audioDirectory, - uuid, - destFile); - if (!b) + // media file is not in document folder yet + if (!bUseSource) { - showMessage(tr("Add file operation failed: file copying error")); - return NULL; - } - concreteUrl = QUrl::fromLocalFile(destFile); + QString destFile; + bool b = UBPersistenceManager::persistenceManager()->addFileToDocument(selectedDocument(), + pSourceUrl.toLocalFile(), + UBPersistenceManager::audioDirectory, + uuid, + destFile); + if (!b) + { + showMessage(tr("Add file operation failed: file copying error")); + return NULL; + } + concreteUrl = QUrl::fromLocalFile(destFile); + }// else we just use source Url. UBGraphicsMediaItem* ai = mActiveScene->addMedia(concreteUrl, startPlay, pos); selectedDocument()->setMetaData(UBSettings::documentUpdatedAt, UBStringUtils::toUtcIsoDateTime(QDateTime::currentDateTime())); diff --git a/src/board/UBBoardController.h b/src/board/UBBoardController.h index a04c1a25..68468188 100644 --- a/src/board/UBBoardController.h +++ b/src/board/UBBoardController.h @@ -210,8 +210,8 @@ class UBBoardController : public UBDocumentContainer void setRegularPageSize(bool checked); void stylusToolChanged(int tool); void grabScene(const QRectF& pSceneRect); - UBGraphicsMediaItem* addVideo(const QUrl& pUrl, bool startPlay, const QPointF& pos); - UBGraphicsMediaItem* addAudio(const QUrl& pUrl, bool startPlay, const QPointF& pos); + UBGraphicsMediaItem* addVideo(const QUrl& pUrl, bool startPlay, const QPointF& pos, bool bUseSource = false); + UBGraphicsMediaItem* addAudio(const QUrl& pUrl, bool startPlay, const QPointF& pos, bool bUseSource = false); UBGraphicsWidgetItem *addW3cWidget(const QUrl& pUrl, const QPointF& pos); void cut(); diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index 4127851f..89e975bc 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -17,6 +17,7 @@ #include "gui/UBMainWindow.h" #include "board/UBBoardController.h" #include "board/UBBoardPaletteManager.h" +#include "frameworks/UBFileSystemUtils.h" #include "core/memcheck.h" @@ -209,7 +210,7 @@ void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl desc.contentTypeHeader = pContentTypeHeader; emit downloadFinished(pSuccess, desc, pData); - } else if(desc.modal) { + } else if(desc.dest == sDownloadFileDesc::board) { // The downloaded file is modal so we must put it on the board emit addDownloadedFileToBoard(pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); } @@ -302,15 +303,24 @@ void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 to */ void UBDownloadManager::startFileDownload(sDownloadFileDesc desc) { - UBDownloadHttpFile* http = new UBDownloadHttpFile(desc.id, this); - connect(http, SIGNAL(downloadProgress(int, qint64,qint64)), this, SLOT(onDownloadProgress(int,qint64,qint64))); - connect(http, SIGNAL(downloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); - - //the desc.url is encoded. So we have to decode it before. - QUrl url; - url.setEncodedUrl(desc.url.toUtf8()); - // We send here the request and store its reply in order to be able to cancel it if needed - mReplies[desc.id] = http->get(url, desc.pos, desc.size, desc.isBackground); + if (desc.srcUrl.startsWith("file://") || desc.srcUrl.startsWith("/")) + { + UBAsyncLocalFileDownloader * cpHelper = new UBAsyncLocalFileDownloader(desc, this); + connect(cpHelper, SIGNAL(signal_asyncCopyFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); + cpHelper->copyFile(QUrl(desc.srcUrl).toLocalFile(), QUrl(desc.dstUrl).toLocalFile(), true); + } + else + { + UBDownloadHttpFile* http = new UBDownloadHttpFile(desc.id, this); + connect(http, SIGNAL(downloadProgress(int, qint64,qint64)), this, SLOT(onDownloadProgress(int,qint64,qint64))); + connect(http, SIGNAL(downloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); + + //the desc.srcUrl is encoded. So we have to decode it before. + QUrl url; + url.setEncodedUrl(desc.srcUrl.toUtf8()); + // We send here the request and store its reply in order to be able to cancel it if needed + mReplies[desc.id] = http->get(url, desc.pos, desc.size, desc.isBackground); + } } /** diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h index 2b5c52c0..9e185563 100644 --- a/src/core/UBDownloadManager.h +++ b/src/core/UBDownloadManager.h @@ -52,7 +52,8 @@ struct sDownloadFileDesc int id; int totalSize; int currentSize; - QString url; + QString srcUrl; + QString dstUrl; QString contentTypeHeader; bool modal; QPointF pos; // For board drop only diff --git a/src/domain/UBGraphicsMediaItem.cpp b/src/domain/UBGraphicsMediaItem.cpp index 1faba0ce..74b378f8 100644 --- a/src/domain/UBGraphicsMediaItem.cpp +++ b/src/domain/UBGraphicsMediaItem.cpp @@ -73,7 +73,11 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte mMediaObject = new Phonon::MediaObject(this); - if (pMediaFileUrl.toLocalFile().contains("videos")) + QString mediaPath = pMediaFileUrl.toString(); + if ("" == mediaPath) + mediaPath = pMediaFileUrl.toLocalFile(); + + if (mediaPath.toLower().contains("videos")) { mMediaType = mediaType_Video; @@ -91,7 +95,7 @@ UBGraphicsMediaItem::UBGraphicsMediaItem(const QUrl& pMediaFileUrl, QGraphicsIte haveLinkedImage = true; } else - if (pMediaFileUrl.toLocalFile().contains("audios")) + if (mediaPath.toLower().contains("audios")) { mMediaType = mediaType_Audio; mAudioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this); diff --git a/src/domain/UBGraphicsScene.cpp b/src/domain/UBGraphicsScene.cpp index cc98b307..fd5f158b 100644 --- a/src/domain/UBGraphicsScene.cpp +++ b/src/domain/UBGraphicsScene.cpp @@ -1216,6 +1216,11 @@ void UBGraphicsScene::textUndoCommandAdded(UBGraphicsTextItem *textItem) } UBGraphicsMediaItem* UBGraphicsScene::addMedia(const QUrl& pMediaFileUrl, bool shouldPlayAsap, const QPointF& pPos) { + qDebug() << pMediaFileUrl.toLocalFile(); + if (!QFile::exists(pMediaFileUrl.toLocalFile())) + if (!QFile::exists(pMediaFileUrl.toString())) + return NULL; + UBGraphicsMediaItem* mediaItem = new UBGraphicsMediaItem(pMediaFileUrl); if(mediaItem){ connect(UBApplication::boardController, SIGNAL(activeSceneChanged()), mediaItem, SLOT(activeSceneChanged())); diff --git a/src/frameworks/UBFileSystemUtils.cpp b/src/frameworks/UBFileSystemUtils.cpp index 36a61945..5fb3376d 100644 --- a/src/frameworks/UBFileSystemUtils.cpp +++ b/src/frameworks/UBFileSystemUtils.cpp @@ -22,6 +22,8 @@ #include "board/UBBoardController.h" #include "document/UBDocumentContainer.h" +#include "core/UBPersistenceManager.h" + #include "globals/UBGlobals.h" THIRD_PARTY_WARNINGS_DISABLE @@ -856,3 +858,91 @@ QString UBFileSystemUtils::readTextFile(QString path) return ""; } + +UBCopyThread::UBCopyThread(QObject *parent) + : QThread(parent) +{ +} + +void UBCopyThread::copyFile(const QString &source, const QString &destination, bool overwrite) +{ + if (!QFile::exists(source)) { + qDebug() << "file" << source << "does not present in fs"; + return; + } + + QString normalizedDestination = destination; + if (QFile::exists(normalizedDestination)) { + if (QFileInfo(normalizedDestination).isFile() && overwrite) { + QFile::remove(normalizedDestination); + } + } else { + normalizedDestination = normalizedDestination.replace(QString("\\"), QString("/")); + int pos = normalizedDestination.lastIndexOf("/"); + if (pos != -1) { + QString newpath = normalizedDestination.left(pos); + if (!QDir().mkpath(newpath)) { + qDebug() << "can't create a new path at " << newpath; + } + } + } + + mFrom = source; + mTo = normalizedDestination; + + start(); +} + +void UBCopyThread::run() +{ + + QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(mFrom); + + int position=mimeType.indexOf(";"); + if(position != -1) + mimeType=mimeType.left(position); + + UBMimeType::Enum itemMimeType = UBFileSystemUtils::mimeTypeFromString(mimeType); + + + QString destDirectory; + if (UBMimeType::Video == itemMimeType) + destDirectory = UBPersistenceManager::videoDirectory; + else + if (UBMimeType::Audio == itemMimeType) + destDirectory = UBPersistenceManager::audioDirectory; + + QString uuid = QUuid::createUuid(); + UBPersistenceManager::persistenceManager()->addFileToDocument(UBApplication::boardController->selectedDocument(), + mFrom, + destDirectory, + uuid, + mTo, + NULL); + + emit finished(mTo); +} + + +UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent) + : QObject(parent) + , mDesc(desc) +{ + +} + +void UBAsyncLocalFileDownloader::copyFile(QString &source, QString &destination, bool bOverwrite) +{ + mFrom = source; + mTo = destination; + + UBCopyThread *cpThread = new UBCopyThread(this); // possible memory leak. Delete helper at signal_asyncCopyFinished() handler + connect(cpThread, SIGNAL(finished(QString)), this, SLOT(slot_asyncCopyFinished(QString))); + cpThread->copyFile(source, destination, bOverwrite); +} + +void UBAsyncLocalFileDownloader::slot_asyncCopyFinished(QString resUrl) +{ + emit signal_asyncCopyFinished(mDesc.id, !resUrl.isEmpty(), QUrl(resUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); + +} \ No newline at end of file diff --git a/src/frameworks/UBFileSystemUtils.h b/src/frameworks/UBFileSystemUtils.h index 13c82dbf..b5c5a961 100644 --- a/src/frameworks/UBFileSystemUtils.h +++ b/src/frameworks/UBFileSystemUtils.h @@ -17,14 +17,57 @@ #define UBFILESYSTEMUTILS_H_ #include +#include #include "core/UB.h" +#include "core/UBDownloadManager.h" + +class UBCopyThread : public QThread +{ + Q_OBJECT +public: + explicit UBCopyThread(QObject *parent = 0); + + void copyFile(const QString &source, const QString &destination, bool overwrite); + void run(); + +signals: + void finished(QString resUrl); + +private: + QString mFrom; + QString mTo; +}; + +class UBAsyncLocalFileDownloader : public QObject +{ + Q_OBJECT + +public: + UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0); + + void copyFile(QString &source, QString &destination, bool bOverwrite); + +public slots: + void slot_asyncCopyFinished(QString resUrl); + +signals: + void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + +private: + QString mFrom; + QString mTo; + sDownloadFileDesc mDesc; +}; + class QuaZipFile; class UBProcessingProgressListener; -class UBFileSystemUtils +class UBFileSystemUtils : public QObject { + Q_OBJECT + public: UBFileSystemUtils(); diff --git a/src/gui/UBFeaturesWidget.cpp b/src/gui/UBFeaturesWidget.cpp index ee052027..ab965689 100644 --- a/src/gui/UBFeaturesWidget.cpp +++ b/src/gui/UBFeaturesWidget.cpp @@ -1048,8 +1048,8 @@ void UBFeatureProperties::onAddToLib() desc.modal = false; desc.name = QFileInfo( mpElement->getFullPath().toString()).fileName(); qDebug() << desc.name; - desc.url = mpElement->getFullPath().toString(); - qDebug() << desc.url; + desc.srcUrl = mpElement->getFullPath().toString(); + qDebug() << desc.srcUrl; UBDownloadManager::downloadManager()->addFileToDownload(desc); } } From afb18c1e15fe87ef6ed88c5632885967660a6d12 Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Mon, 24 Sep 2012 17:09:27 +0300 Subject: [PATCH 2/7] Handled content source url. Items duplication adapted to new ideology of adding media items on the board. --- src/api/UBLibraryAPI.cpp | 2 +- src/api/UBWidgetUniboardAPI.cpp | 2 +- src/board/UBBoardController.cpp | 38 +++++++++++++++------- src/board/UBBoardController.h | 4 +-- src/board/UBFeaturesController.cpp | 2 +- src/core/UBDownloadManager.cpp | 9 +++--- src/core/UBDownloadManager.h | 6 ++-- src/domain/UBGraphicsMediaItem.cpp | 2 +- src/frameworks/UBFileSystemUtils.cpp | 48 +++++++++------------------- src/frameworks/UBFileSystemUtils.h | 11 ++++--- src/web/UBTrapFlashController.cpp | 2 +- src/web/browser/WBWebTrapWebView.cpp | 2 +- 12 files changed, 63 insertions(+), 65 deletions(-) diff --git a/src/api/UBLibraryAPI.cpp b/src/api/UBLibraryAPI.cpp index 8d75a568..073ed404 100644 --- a/src/api/UBLibraryAPI.cpp +++ b/src/api/UBLibraryAPI.cpp @@ -40,7 +40,7 @@ UBLibraryAPI::~UBLibraryAPI() void UBLibraryAPI::addObject(QString pUrl, int width, int height, int x, int y, bool background) { if (UBApplication::boardController) - UBApplication::boardController->downloadURL(QUrl(pUrl), QPointF(x, y), QSize(width, height), background); + UBApplication::boardController->downloadURL(QUrl(pUrl), QString(), QPointF(x, y), QSize(width, height), background); } diff --git a/src/api/UBWidgetUniboardAPI.cpp b/src/api/UBWidgetUniboardAPI.cpp index e49bfe63..32c62f32 100644 --- a/src/api/UBWidgetUniboardAPI.cpp +++ b/src/api/UBWidgetUniboardAPI.cpp @@ -201,7 +201,7 @@ void UBWidgetUniboardAPI::addObject(QString pUrl, int width, int height, int x, if (UBApplication::boardController->activeScene() != mScene) return; - UBApplication::boardController->downloadURL(QUrl(pUrl), QPointF(x, y), QSize(width, height), background); + UBApplication::boardController->downloadURL(QUrl(pUrl), QString(), QPointF(x, y), QSize(width, height), background); } diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 358ce4ce..0a69f25d 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -131,7 +131,7 @@ void UBBoardController::init() , this, SLOT(lastWindowClosed())); connect(UBDownloadManager::downloadManager(), SIGNAL(downloadModalFinished()), this, SLOT(onDownloadModalFinished())); - connect(UBDownloadManager::downloadManager(), SIGNAL(addDownloadedFileToBoard(bool,QUrl,QString,QByteArray,QPointF,QSize,bool)), this, SLOT(downloadFinished(bool,QUrl,QString,QByteArray,QPointF,QSize,bool))); + connect(UBDownloadManager::downloadManager(), SIGNAL(addDownloadedFileToBoard(bool,QUrl,QUrl,QString,QByteArray,QPointF,QSize,bool)), this, SLOT(downloadFinished(bool,QUrl,QUrl,QString,QByteArray,QPointF,QSize,bool))); UBDocumentProxy* doc = UBPersistenceManager::persistenceManager()->createDocument(); @@ -562,7 +562,12 @@ UBGraphicsItem *UBBoardController::duplicateItem(UBItem *item) } UBMimeType::Enum itemMimeType; - QString contentTypeHeader = UBFileSystemUtils::mimeTypeFromFileName(item->sourceUrl().toLocalFile()); + + QString srcFile = item->sourceUrl().toLocalFile(); + if (srcFile.isEmpty()) + srcFile = item->sourceUrl().toString(); + + QString contentTypeHeader = UBFileSystemUtils::mimeTypeFromFileName(srcFile); if(NULL != qgraphicsitem_cast(commonItem)) itemMimeType = UBMimeType::Group; else @@ -587,6 +592,7 @@ UBGraphicsItem *UBBoardController::duplicateItem(UBItem *item) if (mitem) { sourceUrl = mitem->mediaFileUrl(); + downloadURL(sourceUrl, srcFile, itemPos, QSize(itemSize.width(), itemSize.height()), false, false); } }break; @@ -661,7 +667,7 @@ UBGraphicsItem *UBBoardController::duplicateItem(UBItem *item) if (retItem) return retItem; - UBItem *createdItem = downloadFinished(true, sourceUrl, contentTypeHeader, pData, itemPos, QSize(itemSize.width(), itemSize.height()), false); + UBItem *createdItem = downloadFinished(true, sourceUrl, sourceUrl, contentTypeHeader, pData, itemPos, QSize(itemSize.width(), itemSize.height()), false); if (createdItem) { createdItem->setSourceUrl(item->sourceUrl()); @@ -675,6 +681,7 @@ UBGraphicsItem *UBBoardController::duplicateItem(UBItem *item) retItem = dynamic_cast(createdItem); } + return retItem; } @@ -960,7 +967,7 @@ void UBBoardController::groupButtonClicked() } } -void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const QSize& pSize, bool isBackground, bool internalData) +void UBBoardController::downloadURL(const QUrl& url, QString contentSourceUrl, const QPointF& pPos, const QSize& pSize, bool isBackground, bool internalData) { qDebug() << "something has been dropped on the board! Url is: " << url.toString(); QString sUrl = url.toString(); @@ -971,7 +978,7 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const if(sUrl.startsWith("uniboardTool://")) { - downloadFinished(true, url, "application/vnd.mnemis-uniboard-tool", QByteArray(), pPos, pSize, isBackground); + downloadFinished(true, url, QUrl(), "application/vnd.mnemis-uniboard-tool", QByteArray(), pPos, pSize, isBackground); } else if (sUrl.startsWith("file://") || sUrl.startsWith("/")) { @@ -988,7 +995,7 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const { QFile file(fileName); file.open(QIODevice::ReadOnly); - downloadFinished(true, formedUrl, contentType, file.readAll(), pPos, pSize, isBackground, internalData); + downloadFinished(true, formedUrl, QUrl(), contentType, file.readAll(), pPos, pSize, isBackground, internalData); file.close(); } else @@ -998,6 +1005,7 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const sDownloadFileDesc desc; desc.modal = false; desc.srcUrl = sUrl; + desc.originalSrcUrl = contentSourceUrl; desc.currentSize = 0; desc.name = QFileInfo(url.toString()).fileName(); desc.totalSize = 0; // The total size will be retrieved during the download @@ -1036,7 +1044,7 @@ void UBBoardController::downloadURL(const QUrl& url, const QPointF& pPos, const } -UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, +UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground, bool internalData) { @@ -1198,7 +1206,10 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri } if(mediaVideoItem){ - mediaVideoItem->setSourceUrl(sourceUrl); + if (contentUrl.isEmpty()) + mediaVideoItem->setSourceUrl(sourceUrl); + else + mediaVideoItem->setSourceUrl(contentUrl); mediaVideoItem->setUuid(uuid); connect(this, SIGNAL(activeSceneChanged()), mediaVideoItem, SLOT(activeSceneChanged())); } @@ -1239,7 +1250,10 @@ UBItem *UBBoardController::downloadFinished(bool pSuccess, QUrl sourceUrl, QStri } if(audioMediaItem){ - audioMediaItem->setSourceUrl(sourceUrl); + if (contentUrl.isEmpty()) + audioMediaItem->setSourceUrl(sourceUrl); + else + audioMediaItem->setSourceUrl(contentUrl); audioMediaItem->setUuid(uuid); connect(this, SIGNAL(activeSceneChanged()), audioMediaItem, SLOT(activeSceneChanged())); } @@ -2262,7 +2276,7 @@ void UBBoardController::processMimeData(const QMimeData* pMimeData, const QPoint if("" != url) { - downloadURL(url, pPos); + downloadURL(url, QString(), pPos); return; } } @@ -2282,7 +2296,7 @@ void UBBoardController::processMimeData(const QMimeData* pMimeData, const QPoint foreach(const QUrl url, urls){ QPointF pos(pPos + QPointF(index * 15, index * 15)); - downloadURL(url, pos, QSize(), false, internalData); + downloadURL(url, QString(), pos, QSize(), false, internalData); index++; } @@ -2308,7 +2322,7 @@ void UBBoardController::processMimeData(const QMimeData* pMimeData, const QPoint // Sometimes, it is possible to have an URL as text. we check here if it is the case QString qsTmp = pMimeData->text().remove(QRegExp("[\\0]")); if(qsTmp.startsWith("http")){ - downloadURL(QUrl(qsTmp), pPos); + downloadURL(QUrl(qsTmp), QString(), pPos); } else{ mActiveScene->addTextHtml(pMimeData->html(), pPos); diff --git a/src/board/UBBoardController.h b/src/board/UBBoardController.h index 68468188..4f0bb231 100644 --- a/src/board/UBBoardController.h +++ b/src/board/UBBoardController.h @@ -193,8 +193,8 @@ class UBBoardController : public UBDocumentContainer void firstScene(); void lastScene(); void groupButtonClicked(); - void downloadURL(const QUrl& url, const QPointF& pPos = QPointF(0.0, 0.0), const QSize& pSize = QSize(), bool isBackground = false, bool internalData = false); - UBItem *downloadFinished(bool pSuccess, QUrl sourceUrl, QString pHeader, + void downloadURL(const QUrl& url, QString contentSourceUrl = QString(), const QPointF& pPos = QPointF(0.0, 0.0), const QSize& pSize = QSize(), bool isBackground = false, bool internalData = false); + UBItem *downloadFinished(bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground = false, bool internalData = false); void changeBackground(bool isDark, bool isCrossed); diff --git a/src/board/UBFeaturesController.cpp b/src/board/UBFeaturesController.cpp index a9bbeb08..049ee301 100644 --- a/src/board/UBFeaturesController.cpp +++ b/src/board/UBFeaturesController.cpp @@ -711,7 +711,7 @@ void UBFeaturesController::addItemToPage(const UBFeature &item) void UBFeaturesController::addItemAsBackground(const UBFeature &item) { - UBApplication::boardController->downloadURL( item.getFullPath(), QPointF(), QSize(), true ); + UBApplication::boardController->downloadURL( item.getFullPath(), QString(), QPointF(), QSize(), true ); } UBFeature UBFeaturesController::getDestinationFeatureForUrl( const QUrl &url ) diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index 89e975bc..90862470 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -194,7 +194,8 @@ void UBDownloadManager::onDownloadProgress(int id, qint64 received, qint64 total * \brief Called when the download of the given file is finished * @param desc as the current downloaded file description */ -void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground) + +void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground) { // Temporary data for dnd do not delete it please Q_UNUSED(pPos) @@ -212,7 +213,7 @@ void UBDownloadManager::onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl } else if(desc.dest == sDownloadFileDesc::board) { // The downloaded file is modal so we must put it on the board - emit addDownloadedFileToBoard(pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); + emit addDownloadedFileToBoard(pSuccess, sourceUrl, contentUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); } else { @@ -306,8 +307,8 @@ void UBDownloadManager::startFileDownload(sDownloadFileDesc desc) if (desc.srcUrl.startsWith("file://") || desc.srcUrl.startsWith("/")) { UBAsyncLocalFileDownloader * cpHelper = new UBAsyncLocalFileDownloader(desc, this); - connect(cpHelper, SIGNAL(signal_asyncCopyFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); - cpHelper->copyFile(QUrl(desc.srcUrl).toLocalFile(), QUrl(desc.dstUrl).toLocalFile(), true); + connect(cpHelper, SIGNAL(signal_asyncCopyFinished(int, bool, QUrl, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QUrl,QString, QByteArray, QPointF, QSize, bool))); + cpHelper->download(); } else { diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h index 9e185563..0a5cffc9 100644 --- a/src/core/UBDownloadManager.h +++ b/src/core/UBDownloadManager.h @@ -53,7 +53,7 @@ struct sDownloadFileDesc int totalSize; int currentSize; QString srcUrl; - QString dstUrl; + QString originalSrcUrl; QString contentTypeHeader; bool modal; QPointF pos; // For board drop only @@ -109,7 +109,7 @@ signals: void downloadFinished(bool pSuccess, int id, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData); void downloadFinished(bool pSuccess, sDownloadFileDesc desc, QByteArray pData); void downloadModalFinished(); - void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void addDownloadedFileToBoard(bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); void addDownloadedFileToLibrary(bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData); void cancelAllDownloads(); void allDownloadsFinished(); @@ -117,7 +117,7 @@ signals: private slots: void onUpdateDownloadLists(); void onDownloadProgress(int id, qint64 received, qint64 total); - void onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void onDownloadFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); void onDownloadError(int id); private: diff --git a/src/domain/UBGraphicsMediaItem.cpp b/src/domain/UBGraphicsMediaItem.cpp index 74b378f8..9d3c8705 100644 --- a/src/domain/UBGraphicsMediaItem.cpp +++ b/src/domain/UBGraphicsMediaItem.cpp @@ -195,7 +195,7 @@ void UBGraphicsMediaItem::setSourceUrl(const QUrl &pSourceUrl) UBAudioPresentationWidget* pAudioWidget = dynamic_cast(mAudioWidget); if (pAudioWidget) { - pAudioWidget->setTitle(UBFileSystemUtils::lastPathComponent(pSourceUrl.toString())); + pAudioWidget->setTitle(UBFileSystemUtils::lastPathComponent(pSourceUrl.toLocalFile())); } UBItem::setSourceUrl(pSourceUrl); diff --git a/src/frameworks/UBFileSystemUtils.cpp b/src/frameworks/UBFileSystemUtils.cpp index 5fb3376d..877c6885 100644 --- a/src/frameworks/UBFileSystemUtils.cpp +++ b/src/frameworks/UBFileSystemUtils.cpp @@ -864,31 +864,14 @@ UBCopyThread::UBCopyThread(QObject *parent) { } -void UBCopyThread::copyFile(const QString &source, const QString &destination, bool overwrite) +void UBCopyThread::download(const sDownloadFileDesc &desc) { - if (!QFile::exists(source)) { - qDebug() << "file" << source << "does not present in fs"; + if (!QFile::exists(QUrl(desc.srcUrl).toLocalFile())) { + qDebug() << "file" << desc.srcUrl << "does not present in fs"; return; } - QString normalizedDestination = destination; - if (QFile::exists(normalizedDestination)) { - if (QFileInfo(normalizedDestination).isFile() && overwrite) { - QFile::remove(normalizedDestination); - } - } else { - normalizedDestination = normalizedDestination.replace(QString("\\"), QString("/")); - int pos = normalizedDestination.lastIndexOf("/"); - if (pos != -1) { - QString newpath = normalizedDestination.left(pos); - if (!QDir().mkpath(newpath)) { - qDebug() << "can't create a new path at " << newpath; - } - } - } - - mFrom = source; - mTo = normalizedDestination; + mDesc = desc; start(); } @@ -896,7 +879,7 @@ void UBCopyThread::copyFile(const QString &source, const QString &destination, b void UBCopyThread::run() { - QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(mFrom); + QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(mDesc.srcUrl); int position=mimeType.indexOf(";"); if(position != -1) @@ -914,13 +897,16 @@ void UBCopyThread::run() QString uuid = QUuid::createUuid(); UBPersistenceManager::persistenceManager()->addFileToDocument(UBApplication::boardController->selectedDocument(), - mFrom, + QUrl(mDesc.srcUrl).toLocalFile(), destDirectory, uuid, mTo, NULL); - emit finished(mTo); + if (mDesc.originalSrcUrl.isEmpty()) + mDesc.originalSrcUrl = mDesc.srcUrl; + + emit finished(mTo, mDesc.originalSrcUrl); } @@ -931,18 +917,14 @@ UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, Q } -void UBAsyncLocalFileDownloader::copyFile(QString &source, QString &destination, bool bOverwrite) +void UBAsyncLocalFileDownloader::download() { - mFrom = source; - mTo = destination; - UBCopyThread *cpThread = new UBCopyThread(this); // possible memory leak. Delete helper at signal_asyncCopyFinished() handler - connect(cpThread, SIGNAL(finished(QString)), this, SLOT(slot_asyncCopyFinished(QString))); - cpThread->copyFile(source, destination, bOverwrite); + connect(cpThread, SIGNAL(finished(QString, QString)), this, SLOT(slot_asyncCopyFinished(QString, QString))); + cpThread->download(mDesc); } -void UBAsyncLocalFileDownloader::slot_asyncCopyFinished(QString resUrl) +void UBAsyncLocalFileDownloader::slot_asyncCopyFinished(QString srcUrl, QString contentUrl) { - emit signal_asyncCopyFinished(mDesc.id, !resUrl.isEmpty(), QUrl(resUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); - + emit signal_asyncCopyFinished(mDesc.id, !srcUrl.isEmpty(), QUrl::fromLocalFile(srcUrl), QUrl::fromLocalFile(contentUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); } \ No newline at end of file diff --git a/src/frameworks/UBFileSystemUtils.h b/src/frameworks/UBFileSystemUtils.h index b5c5a961..bb857ee4 100644 --- a/src/frameworks/UBFileSystemUtils.h +++ b/src/frameworks/UBFileSystemUtils.h @@ -29,13 +29,14 @@ class UBCopyThread : public QThread public: explicit UBCopyThread(QObject *parent = 0); - void copyFile(const QString &source, const QString &destination, bool overwrite); + void download(const sDownloadFileDesc &desc); void run(); signals: - void finished(QString resUrl); + void finished(QString srcUrl, QString resUrl); private: + sDownloadFileDesc mDesc; QString mFrom; QString mTo; }; @@ -47,13 +48,13 @@ class UBAsyncLocalFileDownloader : public QObject public: UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0); - void copyFile(QString &source, QString &destination, bool bOverwrite); + void download(); public slots: - void slot_asyncCopyFinished(QString resUrl); + void slot_asyncCopyFinished(QString srcUrl, QString resUrl); signals: - void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); private: QString mFrom; diff --git a/src/web/UBTrapFlashController.cpp b/src/web/UBTrapFlashController.cpp index 25cb37e9..6458f203 100644 --- a/src/web/UBTrapFlashController.cpp +++ b/src/web/UBTrapFlashController.cpp @@ -200,7 +200,7 @@ void UBTrapFlashController::createWidget() // flash widget UBWebKitUtils::HtmlObject selectedObject = mAvailableFlashes.at(selectedIndex - 1); UBApplication::applicationController->showBoard(); - UBApplication::boardController->downloadURL(QUrl(selectedObject.source), QPoint(0, 0), QSize(selectedObject.width, selectedObject.height)); + UBApplication::boardController->downloadURL(QUrl(selectedObject.source), QString(), QPoint(0, 0), QSize(selectedObject.width, selectedObject.height)); } QString freezedWidgetPath = UBPlatformUtils::applicationResourcesDirectory() + "/etc/freezedWidgetWrapper.html"; diff --git a/src/web/browser/WBWebTrapWebView.cpp b/src/web/browser/WBWebTrapWebView.cpp index 971ed75c..2c97626c 100644 --- a/src/web/browser/WBWebTrapWebView.cpp +++ b/src/web/browser/WBWebTrapWebView.cpp @@ -281,7 +281,7 @@ void WBWebTrapWebView::trapElementAtPos(const QPoint& pos) emit objectCaptured(QUrl(page()->currentFrame()->url().toString() + "/" + source), type, htr.boundingRect().width(), htr.boundingRect().height()); - UBApplication::boardController->downloadURL(QUrl(source), QPointF(0.0, 0.0)); + UBApplication::boardController->downloadURL(QUrl(source)); UBApplication::applicationController->showBoard(); } } From 9b1d2932e438fafec2c81655104d06fdd68a54e6 Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Mon, 24 Sep 2012 17:53:57 +0300 Subject: [PATCH 3/7] Fixed double copying of media items. Refactored downloader for local files. --- src/board/UBBoardController.cpp | 2 +- src/frameworks/UBFileSystemUtils.cpp | 40 ++++++++-------------------- src/frameworks/UBFileSystemUtils.h | 28 ++++--------------- 3 files changed, 17 insertions(+), 53 deletions(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 0a69f25d..6ebf07bf 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -594,7 +594,7 @@ UBGraphicsItem *UBBoardController::duplicateItem(UBItem *item) sourceUrl = mitem->mediaFileUrl(); downloadURL(sourceUrl, srcFile, itemPos, QSize(itemSize.width(), itemSize.height()), false, false); } - }break; + }return NULL; // async operation case UBMimeType::VectorImage: { diff --git a/src/frameworks/UBFileSystemUtils.cpp b/src/frameworks/UBFileSystemUtils.cpp index 877c6885..785fff67 100644 --- a/src/frameworks/UBFileSystemUtils.cpp +++ b/src/frameworks/UBFileSystemUtils.cpp @@ -859,24 +859,26 @@ QString UBFileSystemUtils::readTextFile(QString path) } -UBCopyThread::UBCopyThread(QObject *parent) + +UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent) : QThread(parent) -{ + , mDesc(desc) +{ + } -void UBCopyThread::download(const sDownloadFileDesc &desc) + +void UBAsyncLocalFileDownloader::download() { - if (!QFile::exists(QUrl(desc.srcUrl).toLocalFile())) { - qDebug() << "file" << desc.srcUrl << "does not present in fs"; + if (!QFile::exists(QUrl(mDesc.srcUrl).toLocalFile())) { + qDebug() << "file" << mDesc.srcUrl << "does not present in fs"; return; } - mDesc = desc; - start(); } -void UBCopyThread::run() +void UBAsyncLocalFileDownloader::run() { QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(mDesc.srcUrl); @@ -906,25 +908,5 @@ void UBCopyThread::run() if (mDesc.originalSrcUrl.isEmpty()) mDesc.originalSrcUrl = mDesc.srcUrl; - emit finished(mTo, mDesc.originalSrcUrl); -} - - -UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent) - : QObject(parent) - , mDesc(desc) -{ - -} - -void UBAsyncLocalFileDownloader::download() -{ - UBCopyThread *cpThread = new UBCopyThread(this); // possible memory leak. Delete helper at signal_asyncCopyFinished() handler - connect(cpThread, SIGNAL(finished(QString, QString)), this, SLOT(slot_asyncCopyFinished(QString, QString))); - cpThread->download(mDesc); -} - -void UBAsyncLocalFileDownloader::slot_asyncCopyFinished(QString srcUrl, QString contentUrl) -{ - emit signal_asyncCopyFinished(mDesc.id, !srcUrl.isEmpty(), QUrl::fromLocalFile(srcUrl), QUrl::fromLocalFile(contentUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); + emit signal_asyncCopyFinished(mDesc.id, !mTo.isEmpty(), QUrl::fromLocalFile(mTo), QUrl::fromLocalFile(mDesc.originalSrcUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); } \ No newline at end of file diff --git a/src/frameworks/UBFileSystemUtils.h b/src/frameworks/UBFileSystemUtils.h index bb857ee4..5e34e627 100644 --- a/src/frameworks/UBFileSystemUtils.h +++ b/src/frameworks/UBFileSystemUtils.h @@ -23,17 +23,19 @@ #include "core/UBDownloadManager.h" -class UBCopyThread : public QThread +class UBAsyncLocalFileDownloader : public QThread { Q_OBJECT public: - explicit UBCopyThread(QObject *parent = 0); + UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0); - void download(const sDownloadFileDesc &desc); + void download(); void run(); signals: void finished(QString srcUrl, QString resUrl); + void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + private: sDownloadFileDesc mDesc; @@ -41,26 +43,6 @@ private: QString mTo; }; -class UBAsyncLocalFileDownloader : public QObject -{ - Q_OBJECT - -public: - UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0); - - void download(); - -public slots: - void slot_asyncCopyFinished(QString srcUrl, QString resUrl); - -signals: - void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); - -private: - QString mFrom; - QString mTo; - sDownloadFileDesc mDesc; -}; class QuaZipFile; class UBProcessingProgressListener; From 385a1c8eb75db98ebc70f5a47e37ffa3ba862bc0 Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Tue, 25 Sep 2012 11:17:26 +0300 Subject: [PATCH 4/7] UBAsyncLocalFileDownloader moved to DownloadManager. --- src/core/UBDownloadManager.cpp | 53 ++++++++++++++++++++++++++ src/core/UBDownloadManager.h | 20 ++++++++++ src/frameworks/UBFileSystemUtils.cpp | 56 ---------------------------- src/frameworks/UBFileSystemUtils.h | 23 ------------ 4 files changed, 73 insertions(+), 79 deletions(-) diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index 90862470..efed5d2c 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -14,6 +14,7 @@ */ #include "UBDownloadManager.h" #include "core/UBApplication.h" +#include "core/UBPersistenceManager.h" #include "gui/UBMainWindow.h" #include "board/UBBoardController.h" #include "board/UBBoardPaletteManager.h" @@ -21,6 +22,58 @@ #include "core/memcheck.h" + +UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent) +: QThread(parent) +, mDesc(desc) +{ + +} + +void UBAsyncLocalFileDownloader::download() +{ + if (!QFile::exists(QUrl(mDesc.srcUrl).toLocalFile())) { + qDebug() << "file" << mDesc.srcUrl << "does not present in fs"; + return; + } + + start(); +} + +void UBAsyncLocalFileDownloader::run() +{ + + QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(mDesc.srcUrl); + + int position=mimeType.indexOf(";"); + if(position != -1) + mimeType=mimeType.left(position); + + UBMimeType::Enum itemMimeType = UBFileSystemUtils::mimeTypeFromString(mimeType); + + + QString destDirectory; + if (UBMimeType::Video == itemMimeType) + destDirectory = UBPersistenceManager::videoDirectory; + else + if (UBMimeType::Audio == itemMimeType) + destDirectory = UBPersistenceManager::audioDirectory; + + QString uuid = QUuid::createUuid(); + UBPersistenceManager::persistenceManager()->addFileToDocument(UBApplication::boardController->selectedDocument(), + QUrl(mDesc.srcUrl).toLocalFile(), + destDirectory, + uuid, + mTo, + NULL); + + if (mDesc.originalSrcUrl.isEmpty()) + mDesc.originalSrcUrl = mDesc.srcUrl; + + emit signal_asyncCopyFinished(mDesc.id, !mTo.isEmpty(), QUrl::fromLocalFile(mTo), QUrl::fromLocalFile(mDesc.originalSrcUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); +} + + /** The unique instance of the download manager */ static UBDownloadManager* pInstance = NULL; diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h index 0a5cffc9..13d94825 100644 --- a/src/core/UBDownloadManager.h +++ b/src/core/UBDownloadManager.h @@ -87,6 +87,26 @@ private: int mId; }; +class UBAsyncLocalFileDownloader : public QThread +{ + Q_OBJECT +public: + UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0); + + void download(); + void run(); + +signals: + void finished(QString srcUrl, QString resUrl); + void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + + +private: + sDownloadFileDesc mDesc; + QString mFrom; + QString mTo; +}; + class UBDownloadManager : public QObject { Q_OBJECT diff --git a/src/frameworks/UBFileSystemUtils.cpp b/src/frameworks/UBFileSystemUtils.cpp index 785fff67..2bde7364 100644 --- a/src/frameworks/UBFileSystemUtils.cpp +++ b/src/frameworks/UBFileSystemUtils.cpp @@ -19,11 +19,8 @@ #include "core/UBApplication.h" -#include "board/UBBoardController.h" #include "document/UBDocumentContainer.h" -#include "core/UBPersistenceManager.h" - #include "globals/UBGlobals.h" THIRD_PARTY_WARNINGS_DISABLE @@ -856,57 +853,4 @@ QString UBFileSystemUtils::readTextFile(QString path) } return ""; -} - - - -UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent) - : QThread(parent) - , mDesc(desc) -{ - -} - - -void UBAsyncLocalFileDownloader::download() -{ - if (!QFile::exists(QUrl(mDesc.srcUrl).toLocalFile())) { - qDebug() << "file" << mDesc.srcUrl << "does not present in fs"; - return; - } - - start(); -} - -void UBAsyncLocalFileDownloader::run() -{ - - QString mimeType = UBFileSystemUtils::mimeTypeFromFileName(mDesc.srcUrl); - - int position=mimeType.indexOf(";"); - if(position != -1) - mimeType=mimeType.left(position); - - UBMimeType::Enum itemMimeType = UBFileSystemUtils::mimeTypeFromString(mimeType); - - - QString destDirectory; - if (UBMimeType::Video == itemMimeType) - destDirectory = UBPersistenceManager::videoDirectory; - else - if (UBMimeType::Audio == itemMimeType) - destDirectory = UBPersistenceManager::audioDirectory; - - QString uuid = QUuid::createUuid(); - UBPersistenceManager::persistenceManager()->addFileToDocument(UBApplication::boardController->selectedDocument(), - QUrl(mDesc.srcUrl).toLocalFile(), - destDirectory, - uuid, - mTo, - NULL); - - if (mDesc.originalSrcUrl.isEmpty()) - mDesc.originalSrcUrl = mDesc.srcUrl; - - emit signal_asyncCopyFinished(mDesc.id, !mTo.isEmpty(), QUrl::fromLocalFile(mTo), QUrl::fromLocalFile(mDesc.originalSrcUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); } \ No newline at end of file diff --git a/src/frameworks/UBFileSystemUtils.h b/src/frameworks/UBFileSystemUtils.h index 5e34e627..6a0597f6 100644 --- a/src/frameworks/UBFileSystemUtils.h +++ b/src/frameworks/UBFileSystemUtils.h @@ -21,29 +21,6 @@ #include "core/UB.h" -#include "core/UBDownloadManager.h" - -class UBAsyncLocalFileDownloader : public QThread -{ - Q_OBJECT -public: - UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0); - - void download(); - void run(); - -signals: - void finished(QString srcUrl, QString resUrl); - void signal_asyncCopyFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); - - -private: - sDownloadFileDesc mDesc; - QString mFrom; - QString mTo; -}; - - class QuaZipFile; class UBProcessingProgressListener; From 51ebe365c3b0dd7ed2f2d6e9a6bb0c3ccf84d5dd Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Tue, 25 Sep 2012 11:58:25 +0300 Subject: [PATCH 5/7] UBDownloadHttpFile "downloadFinished" signal changed to use new interface of boardController. --- src/core/UBDownloadManager.cpp | 4 ++-- src/core/UBDownloadManager.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index efed5d2c..ee576505 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -367,7 +367,7 @@ void UBDownloadManager::startFileDownload(sDownloadFileDesc desc) { UBDownloadHttpFile* http = new UBDownloadHttpFile(desc.id, this); connect(http, SIGNAL(downloadProgress(int, qint64,qint64)), this, SLOT(onDownloadProgress(int,qint64,qint64))); - connect(http, SIGNAL(downloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QString, QByteArray, QPointF, QSize, bool))); + connect(http, SIGNAL(downloadFinished(int, bool, QUrl, QUrl, QString, QByteArray, QPointF, QSize, bool)), this, SLOT(onDownloadFinished(int, bool, QUrl, QUrl, QString, QByteArray, QPointF, QSize, bool))); //the desc.srcUrl is encoded. So we have to decode it before. QUrl url; @@ -558,7 +558,7 @@ void UBDownloadHttpFile::onDownloadFinished(bool pSuccess, QUrl sourceUrl, QStri if(pSuccess) { // Notify the end of the download - emit downloadFinished(mId, pSuccess, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); + emit downloadFinished(mId, pSuccess, sourceUrl, sourceUrl, pContentTypeHeader, pData, pPos, pSize, isBackground); } else { diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h index 13d94825..8b233a0f 100644 --- a/src/core/UBDownloadManager.h +++ b/src/core/UBDownloadManager.h @@ -76,7 +76,7 @@ public: signals: void downloadProgress(int id, qint64 current,qint64 total); - void downloadFinished(int id, bool pSuccess, QUrl sourceUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); + void downloadFinished(int id, bool pSuccess, QUrl sourceUrl, QUrl contentUrl, QString pContentTypeHeader, QByteArray pData, QPointF pPos, QSize pSize, bool isBackground); void downloadError(int id); private slots: From 4d5915422d643abb8d591c4f40b2eb3962a57ccc Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Tue, 25 Sep 2012 12:40:53 +0300 Subject: [PATCH 6/7] D'n'd from browser fixed for mac. --- src/board/UBBoardController.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/board/UBBoardController.cpp b/src/board/UBBoardController.cpp index 2fad8e51..e348bc9f 100644 --- a/src/board/UBBoardController.cpp +++ b/src/board/UBBoardController.cpp @@ -2341,7 +2341,7 @@ void UBBoardController::processMimeData(const QMimeData* pMimeData, const QPoint QString qsUrl = UBPlatformUtils::urlFromClipboard(); if("" != qsUrl){ // We finally got the url of the dropped ressource! Let's import it! - downloadURL(qsUrl, pPos); + downloadURL(qsUrl, qsUrl, pPos); return; } #endif From 96ef0f2557ee7e500dc2b51ef3eb001ea08a8cfe Mon Sep 17 00:00:00 2001 From: Aleksei Kanash Date: Tue, 25 Sep 2012 17:46:13 +0300 Subject: [PATCH 7/7] Added abort() method to UBAsyncLocalFileDpownloader. UBDownloadManaget was adapted to use UBAsyncLocalFileDpownloader and QNetrowrkRequest bouth. --- src/board/UBBoardPaletteManager.cpp | 1 + src/core/UBDownloadManager.cpp | 74 ++++++++++++++++++++++------- src/core/UBDownloadManager.h | 6 ++- 3 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/board/UBBoardPaletteManager.cpp b/src/board/UBBoardPaletteManager.cpp index 2652bb15..612813cd 100644 --- a/src/board/UBBoardPaletteManager.cpp +++ b/src/board/UBBoardPaletteManager.cpp @@ -995,6 +995,7 @@ void UBBoardPaletteManager::startDownloads() mDownloadInProgress = true; mpDownloadWidget->setVisibleState(true); mRightPalette->addTab(mpDownloadWidget); + mpDownloadWidget; } } diff --git a/src/core/UBDownloadManager.cpp b/src/core/UBDownloadManager.cpp index ee576505..e120038a 100644 --- a/src/core/UBDownloadManager.cpp +++ b/src/core/UBDownloadManager.cpp @@ -26,18 +26,21 @@ UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent) : QThread(parent) , mDesc(desc) +, m_bAborting(false) { } -void UBAsyncLocalFileDownloader::download() +UBAsyncLocalFileDownloader *UBAsyncLocalFileDownloader::download() { if (!QFile::exists(QUrl(mDesc.srcUrl).toLocalFile())) { qDebug() << "file" << mDesc.srcUrl << "does not present in fs"; - return; + return this; } start(); + + return this; } void UBAsyncLocalFileDownloader::run() @@ -59,6 +62,9 @@ void UBAsyncLocalFileDownloader::run() if (UBMimeType::Audio == itemMimeType) destDirectory = UBPersistenceManager::audioDirectory; + if (mDesc.originalSrcUrl.isEmpty()) + mDesc.originalSrcUrl = mDesc.srcUrl; + QString uuid = QUuid::createUuid(); UBPersistenceManager::persistenceManager()->addFileToDocument(UBApplication::boardController->selectedDocument(), QUrl(mDesc.srcUrl).toLocalFile(), @@ -67,12 +73,19 @@ void UBAsyncLocalFileDownloader::run() mTo, NULL); - if (mDesc.originalSrcUrl.isEmpty()) - mDesc.originalSrcUrl = mDesc.srcUrl; - - emit signal_asyncCopyFinished(mDesc.id, !mTo.isEmpty(), QUrl::fromLocalFile(mTo), QUrl::fromLocalFile(mDesc.originalSrcUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); + if (m_bAborting) + { + if (QFile::exists(mTo)) + QFile::remove(mTo); + } + else + emit signal_asyncCopyFinished(mDesc.id, !mTo.isEmpty(), QUrl::fromLocalFile(mTo), QUrl::fromLocalFile(mDesc.originalSrcUrl), "", NULL, mDesc.pos, mDesc.size, mDesc.isBackground); } +void UBAsyncLocalFileDownloader::abort() +{ + m_bAborting = true; +} /** The unique instance of the download manager */ static UBDownloadManager* pInstance = NULL; @@ -154,7 +167,7 @@ void UBDownloadManager::init() { mCrntDL.clear(); mPendingDL.clear(); - mReplies.clear(); + mDownloads.clear(); mLastID = 1; mDLAvailability.clear(); for(int i=0; idownload(); + QObject *res = dynamic_cast(cpHelper->download()); + if (!res) + delete res; + else + mDownloads[desc.id] = res; } else { @@ -373,7 +390,7 @@ void UBDownloadManager::startFileDownload(sDownloadFileDesc desc) QUrl url; url.setEncodedUrl(desc.srcUrl.toUtf8()); // We send here the request and store its reply in order to be able to cancel it if needed - mReplies[desc.id] = http->get(url, desc.pos, desc.size, desc.isBackground); + mDownloads[desc.id] = dynamic_cast(http->get(url, desc.pos, desc.size, desc.isBackground)); } } @@ -420,10 +437,18 @@ void UBDownloadManager::checkIfModalRemains() void UBDownloadManager::cancelDownloads() { // Stop the current downloads - QMap::iterator it = mReplies.begin(); - for(; it!=mReplies.end();it++) + QMap::iterator it = mDownloads.begin(); + for(; it!=mDownloads.end();it++) { - dynamic_cast(it.value())->abort(); + QNetworkReply *netReply = dynamic_cast(it.value()); + if (netReply) + netReply->abort(); + else + { + UBAsyncLocalFileDownloader *localDownload = dynamic_cast(it.value()); + if (localDownload) + localDownload->abort(); + } } // Clear all the lists @@ -436,7 +461,8 @@ void UBDownloadManager::cancelDownloads() void UBDownloadManager::onDownloadError(int id) { - QNetworkReply* pReply = mReplies.value(id); + QNetworkReply *pReply = dynamic_cast(mDownloads.value(id)); + if(NULL != pReply) { // Check which error occured: @@ -466,9 +492,25 @@ void UBDownloadManager::finishDownloads(bool cancel) void UBDownloadManager::cancelDownload(int id) { + if (!mDownloads.size()) + return; + // Stop the download - mReplies[id]->abort(); - mReplies.remove(id); + + QNetworkReply *pNetworkDownload = dynamic_cast(mDownloads[id]); + if (pNetworkDownload) + pNetworkDownload->abort(); + else + { + UBAsyncLocalFileDownloader *pLocalDownload = dynamic_cast(mDownloads[id]); + if (pLocalDownload) + { + if (pLocalDownload->isRunning()) + pLocalDownload->abort(); + } + } + + mDownloads.remove(id); // Remove the canceled download from the download lists bool bFound = false; diff --git a/src/core/UBDownloadManager.h b/src/core/UBDownloadManager.h index 8b233a0f..cefa8f40 100644 --- a/src/core/UBDownloadManager.h +++ b/src/core/UBDownloadManager.h @@ -93,8 +93,9 @@ class UBAsyncLocalFileDownloader : public QThread public: UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0); - void download(); + UBAsyncLocalFileDownloader *download(); void run(); + void abort(); signals: void finished(QString srcUrl, QString resUrl); @@ -103,6 +104,7 @@ signals: private: sDownloadFileDesc mDesc; + bool m_bAborting; QString mFrom; QString mTo; }; @@ -159,7 +161,7 @@ private: /** The current download availability (-1 = free, otherwise the file ID is recorded)*/ QVector mDLAvailability; /** A map containing the replies of the GET operations */ - QMap mReplies; + QMap mDownloads; }; #endif // UBDOWNLOADMANAGER_H