From 96ef0f2557ee7e500dc2b51ef3eb001ea08a8cfe Mon Sep 17 00:00:00 2001
From: Aleksei Kanash <sc.kanash.aleksei@gmail.com>
Date: Tue, 25 Sep 2012 17:46:13 +0300
Subject: [PATCH] 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; i<SIMULTANEOUS_DOWNLOAD; i++)
@@ -314,7 +327,7 @@ void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 to
                 mCrntDL.remove(i);
 
                 // Here we don't forget to remove the reply related to the finished download
-                mReplies.remove(id);
+                mDownloads.remove(id);
 
                 // Free the download slot used by the finished file
                 for(int j=0; j<mDLAvailability.size();j++)
@@ -361,7 +374,11 @@ void UBDownloadManager::startFileDownload(sDownloadFileDesc desc)
     {
         UBAsyncLocalFileDownloader * cpHelper = new UBAsyncLocalFileDownloader(desc, this);
         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();
+        QObject *res = dynamic_cast<QObject *>(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<QObject *>(http->get(url, desc.pos, desc.size, desc.isBackground));
     } 
 }
 
@@ -420,10 +437,18 @@ void UBDownloadManager::checkIfModalRemains()
 void UBDownloadManager::cancelDownloads()
 {
     // Stop the current downloads
-    QMap<int, QNetworkReply*>::iterator it = mReplies.begin();
-    for(; it!=mReplies.end();it++)
+    QMap<int, QObject*>::iterator it = mDownloads.begin();
+    for(; it!=mDownloads.end();it++)
     {
-        dynamic_cast<QNetworkReply*>(it.value())->abort();
+        QNetworkReply *netReply = dynamic_cast<QNetworkReply*>(it.value());
+        if (netReply)
+            netReply->abort();
+        else
+        {        
+            UBAsyncLocalFileDownloader *localDownload = dynamic_cast<UBAsyncLocalFileDownloader *>(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<QNetworkReply *>(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<QNetworkReply *>(mDownloads[id]);
+    if (pNetworkDownload)
+        pNetworkDownload->abort();
+    else
+    {
+        UBAsyncLocalFileDownloader *pLocalDownload = dynamic_cast<UBAsyncLocalFileDownloader *>(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<int> mDLAvailability;
     /** A map containing the replies of the GET operations */
-    QMap<int, QNetworkReply*> mReplies;
+    QMap<int, QObject*> mDownloads;
 };
 
 #endif // UBDOWNLOADMANAGER_H