Added abort() method to UBAsyncLocalFileDpownloader.

UBDownloadManaget was adapted to use UBAsyncLocalFileDpownloader and QNetrowrkRequest bouth.
preferencesAboutTextFull
Aleksei Kanash 12 years ago
parent 4d5915422d
commit 96ef0f2557
  1. 1
      src/board/UBBoardPaletteManager.cpp
  2. 72
      src/core/UBDownloadManager.cpp
  3. 6
      src/core/UBDownloadManager.h

@ -995,6 +995,7 @@ void UBBoardPaletteManager::startDownloads()
mDownloadInProgress = true; mDownloadInProgress = true;
mpDownloadWidget->setVisibleState(true); mpDownloadWidget->setVisibleState(true);
mRightPalette->addTab(mpDownloadWidget); mRightPalette->addTab(mpDownloadWidget);
mpDownloadWidget;
} }
} }

@ -26,18 +26,21 @@
UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent) UBAsyncLocalFileDownloader::UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent)
: QThread(parent) : QThread(parent)
, mDesc(desc) , mDesc(desc)
, m_bAborting(false)
{ {
} }
void UBAsyncLocalFileDownloader::download() UBAsyncLocalFileDownloader *UBAsyncLocalFileDownloader::download()
{ {
if (!QFile::exists(QUrl(mDesc.srcUrl).toLocalFile())) { if (!QFile::exists(QUrl(mDesc.srcUrl).toLocalFile())) {
qDebug() << "file" << mDesc.srcUrl << "does not present in fs"; qDebug() << "file" << mDesc.srcUrl << "does not present in fs";
return; return this;
} }
start(); start();
return this;
} }
void UBAsyncLocalFileDownloader::run() void UBAsyncLocalFileDownloader::run()
@ -59,6 +62,9 @@ void UBAsyncLocalFileDownloader::run()
if (UBMimeType::Audio == itemMimeType) if (UBMimeType::Audio == itemMimeType)
destDirectory = UBPersistenceManager::audioDirectory; destDirectory = UBPersistenceManager::audioDirectory;
if (mDesc.originalSrcUrl.isEmpty())
mDesc.originalSrcUrl = mDesc.srcUrl;
QString uuid = QUuid::createUuid(); QString uuid = QUuid::createUuid();
UBPersistenceManager::persistenceManager()->addFileToDocument(UBApplication::boardController->selectedDocument(), UBPersistenceManager::persistenceManager()->addFileToDocument(UBApplication::boardController->selectedDocument(),
QUrl(mDesc.srcUrl).toLocalFile(), QUrl(mDesc.srcUrl).toLocalFile(),
@ -67,12 +73,19 @@ void UBAsyncLocalFileDownloader::run()
mTo, mTo,
NULL); NULL);
if (mDesc.originalSrcUrl.isEmpty()) if (m_bAborting)
mDesc.originalSrcUrl = mDesc.srcUrl; {
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); 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 */ /** The unique instance of the download manager */
static UBDownloadManager* pInstance = NULL; static UBDownloadManager* pInstance = NULL;
@ -154,7 +167,7 @@ void UBDownloadManager::init()
{ {
mCrntDL.clear(); mCrntDL.clear();
mPendingDL.clear(); mPendingDL.clear();
mReplies.clear(); mDownloads.clear();
mLastID = 1; mLastID = 1;
mDLAvailability.clear(); mDLAvailability.clear();
for(int i=0; i<SIMULTANEOUS_DOWNLOAD; i++) for(int i=0; i<SIMULTANEOUS_DOWNLOAD; i++)
@ -314,7 +327,7 @@ void UBDownloadManager::updateFileCurrentSize(int id, qint64 received, qint64 to
mCrntDL.remove(i); mCrntDL.remove(i);
// Here we don't forget to remove the reply related to the finished download // 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 // Free the download slot used by the finished file
for(int j=0; j<mDLAvailability.size();j++) for(int j=0; j<mDLAvailability.size();j++)
@ -361,7 +374,11 @@ void UBDownloadManager::startFileDownload(sDownloadFileDesc desc)
{ {
UBAsyncLocalFileDownloader * cpHelper = new UBAsyncLocalFileDownloader(desc, this); 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))); 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 else
{ {
@ -373,7 +390,7 @@ void UBDownloadManager::startFileDownload(sDownloadFileDesc desc)
QUrl url; QUrl url;
url.setEncodedUrl(desc.srcUrl.toUtf8()); url.setEncodedUrl(desc.srcUrl.toUtf8());
// We send here the request and store its reply in order to be able to cancel it if needed // 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() void UBDownloadManager::cancelDownloads()
{ {
// Stop the current downloads // Stop the current downloads
QMap<int, QNetworkReply*>::iterator it = mReplies.begin(); QMap<int, QObject*>::iterator it = mDownloads.begin();
for(; it!=mReplies.end();it++) 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 // Clear all the lists
@ -436,7 +461,8 @@ void UBDownloadManager::cancelDownloads()
void UBDownloadManager::onDownloadError(int id) void UBDownloadManager::onDownloadError(int id)
{ {
QNetworkReply* pReply = mReplies.value(id); QNetworkReply *pReply = dynamic_cast<QNetworkReply *>(mDownloads.value(id));
if(NULL != pReply) if(NULL != pReply)
{ {
// Check which error occured: // Check which error occured:
@ -466,9 +492,25 @@ void UBDownloadManager::finishDownloads(bool cancel)
void UBDownloadManager::cancelDownload(int id) void UBDownloadManager::cancelDownload(int id)
{ {
if (!mDownloads.size())
return;
// Stop the download // 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 // Remove the canceled download from the download lists
bool bFound = false; bool bFound = false;

@ -93,8 +93,9 @@ class UBAsyncLocalFileDownloader : public QThread
public: public:
UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0); UBAsyncLocalFileDownloader(sDownloadFileDesc desc, QObject *parent = 0);
void download(); UBAsyncLocalFileDownloader *download();
void run(); void run();
void abort();
signals: signals:
void finished(QString srcUrl, QString resUrl); void finished(QString srcUrl, QString resUrl);
@ -103,6 +104,7 @@ signals:
private: private:
sDownloadFileDesc mDesc; sDownloadFileDesc mDesc;
bool m_bAborting;
QString mFrom; QString mFrom;
QString mTo; QString mTo;
}; };
@ -159,7 +161,7 @@ private:
/** The current download availability (-1 = free, otherwise the file ID is recorded)*/ /** The current download availability (-1 = free, otherwise the file ID is recorded)*/
QVector<int> mDLAvailability; QVector<int> mDLAvailability;
/** A map containing the replies of the GET operations */ /** A map containing the replies of the GET operations */
QMap<int, QNetworkReply*> mReplies; QMap<int, QObject*> mDownloads;
}; };
#endif // UBDOWNLOADMANAGER_H #endif // UBDOWNLOADMANAGER_H

Loading…
Cancel
Save