diff options
Diffstat (limited to 'src/downloadManager.cpp')
-rw-r--r-- | src/downloadManager.cpp | 72 |
1 files changed, 29 insertions, 43 deletions
diff --git a/src/downloadManager.cpp b/src/downloadManager.cpp index c15454a..dca99d3 100644 --- a/src/downloadManager.cpp +++ b/src/downloadManager.cpp @@ -1,7 +1,5 @@ #include "downloadManager.h" -//#include <QDir> #include <QFileInfo> -#include <QVariant> #include <QByteArray> int downloadManager::downloaded = 0; @@ -10,9 +8,8 @@ downloadManager::downloadManager() { qnam = new QNetworkAccessManager(); dip = false; - // the whole QDir thing is questionable.. downloadDir = QDir(downloadPath); - // Check if downloadPath exists, if not create it. + /* Check if downloadPath exists, if not create it. */ if (!downloadDir.exists()){ if (debug) qDebug() << "Download directory: " << downloadDir.path() << "doesn't exist."; QDir::current().mkdir(downloadPath); @@ -26,7 +23,7 @@ void downloadManager::downloadFile(QString& filename){ if (debug) qDebug() << "Received downloadFile signal for:" << filename; QUrl fileUrl; fileUrl = baseURL.resolved(QUrl(filename)); - qDebug() << "fileUrl: " << fileUrl; + if (debug) qDebug() << "fileUrl: " << fileUrl; this->processDownloadRequest(fileUrl); } // ---------------------------------------------------------------------------------------- @@ -37,13 +34,11 @@ void downloadManager::downloadFile(QUrl& fileUrl){ // ---------------------------------------------------------------------------------------- void downloadManager::processDownloadRequest(QUrl& url) { - // Test on empty URL in case such a call happens, which should not - // happen given how javascriptInterface::startDownload(..) is implemented. if (url.isEmpty()){ if (debug) qDebug() << "No URL specified for download."; return; } - // If download in progress, enqueue file and return. + /* If download in progress, enqueue file and return. */ if (dip) { if (debug) qDebug() << "Download in progress! Enqueueing:" << url.toString() @@ -51,7 +46,7 @@ void downloadManager::processDownloadRequest(QUrl& url) dlQ.enqueue(url); return; } - // No running downloads: enqueue and start next download. + /* No running downloads: enqueue and start next download. */ dlQ.enqueue(url); if (debug) qDebug() << "Enqueueing:" << url.toString() << endl; startNextDownload(); @@ -69,18 +64,15 @@ void downloadManager::startNextDownload() if (debug) qDebug() << "Starting next download: " << dlQ.head().toString() << "(" << dlQ.size() << "in queue.)"; - // Dequeue next URL to download. + /* Dequeue next URL to download. */ QUrl url = dlQ.dequeue(); - // Get temporary filename from URL. + /* Get temporary filename from URL. */ QString tmp = url.path(); tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); if (debug) qDebug() << "Extracted " << tmp << "from " << url.toString(); - // temp file name will be renamed to the filename parsed from the - // header ("Content-Disposition" --> filename) outfile.setFileName(downloadPath + "/" + tmp); - if (!outfile.open(QIODevice::WriteOnly)) { if (debug) qDebug() << "Couldn't open file! Skipping..."; @@ -95,7 +87,7 @@ void downloadManager::startNextDownload() return; } - currentProgress = 0; + lastProgress = 0; dip = true; QObject::connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReady())); QObject::connect(currentDownload, SIGNAL(downloadProgress(qint64, qint64)), @@ -103,46 +95,39 @@ void downloadManager::startNextDownload() QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished())); } // ---------------------------------------------------------------------------------------- -// Private slots +// Private slots // ---------------------------------------------------------------------------------------- -// This slot listens to readyRead() emmited when data is available for reading. void downloadManager::downloadReady() { - - // this is done every signal call atm, to fix... - const QByteArray cd = "Content-Disposition"; - QByteArray cdc = currentDownload->rawHeader(cd); - cdc.chop(1); - int x = cdc.indexOf("filename=\"") + 10; - cdc.remove(0, x); - // End file name = cdc. - currentTargetFilename = cdc; - // readyRead() fired, so save the readable data. + /* Data ready, save it */ outfile.write(currentDownload->readAll()); } // ---------------------------------------------------------------------------------------- -// This triggers sends the update progress back to the site. void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) { - - if (debug) qDebug() << "Download progress of " << currentDownload->url().toString() - << ": " << bytesIn << "/" << bytesTotal; - - int tmp = ((bytesIn * 100) / bytesTotal); - if (tmp > currentProgress){ - currentProgress = tmp; - emit updateProgress(currentDownload->url().toString(), tmp); + /* Update progress only if difference higher than the updateInterval setting */ + int currentProgress = ((bytesIn * 100) / bytesTotal); + if (currentProgress - lastProgress > updateInterval){ + lastProgress = currentProgress; + emit updateProgress(currentDownload->url().toString(), currentProgress); + if (debug) qDebug() << "Download progress of " << currentDownload->url().toString() + << ": " << bytesIn << "/" << bytesTotal << "(" << currentProgress << "\%)"; } - // Progress difference < 1% return; } // ---------------------------------------------------------------------------------------- -// This slot listens to the finished() which is emmited -// when all the data from the reply has been read. void downloadManager::downloadFinished() { - // Close output file. - outfile.close(); + /* Header filename fetching & renaming, old-ish + const QByteArray cd = "Content-Disposition"; + QByteArray cdc = currentDownload->rawHeader(cd); + int x = cdc.indexOf("filename=\"") + 10; + cdc.remove(0, x).chop(1); + if (!cdc.isEmpty()) + currentTargetFilename = cdc; + else + currentTargetFilename = QString("download.\%1").arg(downloaded); + QString tmp = outfile.fileName(); tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); qDebug() << "Trying to rename " << tmp << " to --> " << currentTargetFilename; @@ -153,19 +138,20 @@ void downloadManager::downloadFinished() else { if (debug) qDebug() << "Failure to rename file!"; } + */ + outfile.close(); currentDownload->deleteLater(); downloaded++; dip = false; if (debug) qDebug() << "Download of " << currentDownload->url().toString() << "finished. (dlcount = "<< downloaded << ")"; - // If queue is empty, we are done. if (dlQ.isEmpty()){ emit downloadQueueEmpty(); if (debug) qDebug() << "Download manager ready. (2)"; + return; } - // Queue not empty: initialise next download. startNextDownload(); } |