diff options
Diffstat (limited to 'src/downloadManager.cpp')
-rw-r--r-- | src/downloadManager.cpp | 90 |
1 files changed, 45 insertions, 45 deletions
diff --git a/src/downloadManager.cpp b/src/downloadManager.cpp index 01769ef..3d47990 100644 --- a/src/downloadManager.cpp +++ b/src/downloadManager.cpp @@ -1,72 +1,72 @@ #include "downloadManager.h" #include "fbgui.h" -#include <QFileInfo> -#include <QByteArray> int downloadManager::downloaded = 0; // ---------------------------------------------------------------------------------------- downloadManager::downloadManager() { - //logView->write(QString("testing @ DM init")); + qxtLog->debug() << "Initializing download manager..."; + checkDownloadDirectory(); qnam = new QNetworkAccessManager(); dip = false; - downloadDir = QDir(downloadPath); +} +// ---------------------------------------------------------------------------------------- +void downloadManager::checkDownloadDirectory(){ /* check if downloadPath exists, if not create it. */ + QDir downloadDir = QDir(downloadPath); if (!downloadDir.exists()){ - if (debug) qDebug() << "Download directory: " << downloadDir.path() << "doesn't exist."; + qxtLog->debug() << "Download directory: " << downloadDir.path() << " doesn't exist."; QDir::current().mkdir(downloadPath); - if (downloadDir.exists() && debug) - qDebug() << "Created download directory: " << downloadDir.path(); + if (downloadDir.exists()){ + qxtLog->debug() << "Created download directory: " << downloadDir.path(); + } + else { + qxtLog->debug() << "Failed to create directory: " << downloadDir.path(); + emit notify(QString("Failed to create download directory!")); + } } - else if (debug) qDebug() << "Download directory: " << downloadDir.path() << "exists."; + else qxtLog->debug() << "Download directory: " << downloadDir.path() << " exists."; } // ---------------------------------------------------------------------------------------- void downloadManager::downloadFile(QString& filename) { - if (debug) qDebug() << "Received downloadFile signal for:" << filename; - QUrl fileUrl; - fileUrl = baseURL.resolved(QUrl(filename)); - if (debug) qDebug() << "fileUrl: " << fileUrl; + QUrl fileUrl(baseURL.resolved(QUrl(filename))); this->processDownloadRequest(fileUrl); } // ---------------------------------------------------------------------------------------- void downloadManager::downloadFile(QUrl& fileUrl) { - if (debug) qDebug() << "Received downloadFile signal for:" << fileUrl; this->processDownloadRequest(fileUrl); } // ---------------------------------------------------------------------------------------- void downloadManager::processDownloadRequest(QUrl& url) { if (url.isEmpty()){ - if (debug) qDebug() << "No URL specified for download."; + qxtLog->debug() << "No URL specified for download."; return; } /* if download in progress, enqueue file and return. */ - if (dip) - { - if (debug) qDebug() << "Download in progress! Enqueueing:" << url.toString() - << "(" << dlQ.size() << "in queue)"; + if (dip){ + qxtLog->debug() << "Download in progress! Enqueueing:" << url.toString() + << "(" << dlQ.size() << " in queue)"; dlQ.enqueue(url); return; } /* no running downloads: enqueue and start next download. */ dlQ.enqueue(url); - if (debug) qDebug() << "Enqueueing:" << url.toString() << endl; + qxtLog->debug() << "Enqueueing:" << url.toString(); startNextDownload(); } // ---------------------------------------------------------------------------------------- void downloadManager::startNextDownload() { - - if (dlQ.isEmpty()) - { + if (dlQ.isEmpty()){ emit downloadQueueEmpty(); - if (debug) qDebug() << "Download manager ready. (1)"; + qxtLog->debug() << "Download manager ready. (1)"; return; } - if (debug) qDebug() << "Starting next download: " << dlQ.head().toString() - << "(" << dlQ.size() << "in queue.)"; + qxtLog->debug() << "Starting next download: " << dlQ.head().toString() + << " (" << dlQ.size() - 1 << " in queue.)"; /* dequeue next URL to download. */ QUrl url = dlQ.dequeue(); @@ -85,7 +85,8 @@ void downloadManager::startNextDownload() outfile.setFileName(downloadPath + "/" + tmp); if (!outfile.open(QIODevice::WriteOnly)){ - if (debug) qDebug() << "Couldn't open file! Skipping..."; + qxtLog->debug() << "Couldn't open file! Skipping..."; + emit notify(QString("Couldn't open " + downloadPath + "/" + tmp + "! Skipping...")); return; } @@ -121,9 +122,7 @@ void downloadManager::downloadReady() // ---------------------------------------------------------------------------------------- void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) { - // "fix" for the weird bytesTotal = -1 initial reading... - if (bytesIn > bytesTotal) - return; + if (bytesIn > bytesTotal) return; /* calculate current speed */ double speed = bytesIn * 1000 / dltime.elapsed(); QString unit; @@ -143,7 +142,7 @@ void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) if (currentProgress - lastProgress >= updateInterval){ lastProgress = currentProgress; emit updateProgress(currentProgress, speed, unit); - if (debug) qDebug() << "Download progress of " << currentDownload->url().toString() + qxtLog->debug() << "Download progress of " << currentDownload->url().toString() << ": " << bytesIn << "/" << bytesTotal << "(" << currentProgress << "\%)"; } return; @@ -153,25 +152,26 @@ void downloadManager::downloadFinished() { /* check for errors */ if (currentDownload->error()){ - if (debug) qDebug() << "Download of" << currentDownload->url().toString() - << "failed with status code: " << currentDownload->error(); currentDownload->deleteLater(); outfile.remove(); - return; + int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + qxtLog->debug() << "Download of " << currentDownload->url().toString() + << " failed with HTTP error code: " << statusCode; + emit notify(QString("HTTP Error: %1").arg(statusCode)); + } + else{ + /* end download */ + currentDownload->deleteLater(); + outfile.close(); + downloaded++; + qxtLog->debug() << "Download of " << currentDownload->url().toString() + << " finished. (downloaded = "<< downloaded << ")"; } - // TODO Handle errors. - if (debug) qDebug() << "NetworkCode: " << currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); - - /* end download */ - outfile.close(); - currentDownload->deleteLater(); - downloaded++; dip = false; - if (debug) qDebug() << "Download of " << currentDownload->url().toString() - << "finished. (dlcount = "<< downloaded << ")"; + /* process next in queue */ if (dlQ.isEmpty()){ emit downloadQueueEmpty(); - if (debug) qDebug() << "Download manager ready. (2)"; + qxtLog->debug() << "Download manager ready. (2)"; return; } startNextDownload(); @@ -194,9 +194,9 @@ tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); qDebug() << "Trying to rename " << tmp << " to --> " << currentTargetFilename; if (outfile.rename(downloadPath + "/" + currentTargetFilename)) { - if (debug) qDebug() << "Renamed file!"; + qxtLog->debug() << "Renamed file!"; } else { - if (debug) qDebug() << "Failure to rename file!"; + qxtLog->debug() << "Failure to rename file!"; } */ |