From 66244c1853caca19a24581bff37bd11dc47a0f53 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Fri, 25 Mar 2011 22:55:14 +0100 Subject: fixes --- src/downloadmanager.cpp | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) (limited to 'src/downloadmanager.cpp') diff --git a/src/downloadmanager.cpp b/src/downloadmanager.cpp index fbac866..3582a2a 100644 --- a/src/downloadmanager.cpp +++ b/src/downloadmanager.cpp @@ -2,15 +2,14 @@ #include "fbgui.h" int DownloadManager::downloaded = 0; -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- DownloadManager::DownloadManager(){ qxtLog->debug() << "Initializing download manager..."; checkDownloadDirectory(); qnam = new QNetworkAccessManager(); dip = false; } - -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- void DownloadManager::checkDownloadDirectory() { // check if downloadPath exists, if not create it. @@ -44,37 +43,40 @@ void DownloadManager::checkDownloadDirectory() qxtLog->debug() << "[dm] Saving downloads to: " << downloadDir.absolutePath(); //downloadPath = downloadDir.absolutePath(); } -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- +// Public access +// ------------------------------------------------------------------------------------------------------- void DownloadManager::downloadFile(const QString& filename) { QUrl fileUrl(baseURL.resolved(QUrl(filename))); this->processDownloadRequest(fileUrl); } -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- void DownloadManager::downloadFile(const QUrl& fileUrl) { this->processDownloadRequest(fileUrl); } -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- +// Private functions handling download requests and queueing +// ------------------------------------------------------------------------------------------------------- void DownloadManager::processDownloadRequest(const QUrl& url) { if (url.isEmpty()){ qxtLog->debug() << "[dm] No URL specified for download."; return; } - // if download in progress, enqueue file and return. + qxtLog->debug() << "[dm] Enqueueing:" << url.toString(); + dlQ.enqueue(url); if (dip){ - dlQ.enqueue(url); + // download in progress, return. qxtLog->debug() << "[dm] Download in progress! Queued:" << url.toString() << "(" << dlQ.size() << " in queue)"; return; } - // no running downloads: enqueue and start next download. - dlQ.enqueue(url); - qxtLog->debug() << "[dm] Enqueueing:" << url.toString(); + // no running downloads: start next in queue startNextDownload(); } -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- void DownloadManager::startNextDownload() { if (dlQ.isEmpty()){ @@ -120,9 +122,9 @@ void DownloadManager::startNextDownload() this, SLOT(downloadProgress(qint64, qint64))); QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished())); } -// ---------------------------------------------------------------------------------------- -// Private slots to process downloads -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- +// Private slots to handle a download in progress +// ------------------------------------------------------------------------------------------------------- void DownloadManager::processMetaInfo() { // fetch filesize from header & filename from URL (for now) @@ -131,13 +133,13 @@ void DownloadManager::processMetaInfo() QFileInfo fi(outfile); emit downloadInfo(outfile.fileName(), clinfo.toDouble()); } -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- void DownloadManager::downloadReady() { // data ready, save it outfile.write(currentDownload->readAll()); } -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- void DownloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) { if (bytesIn > bytesTotal) return; @@ -165,18 +167,18 @@ void DownloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) } return; } -// ---------------------------------------------------------------------------------------- +// ------------------------------------------------------------------------------------------------------- void DownloadManager::downloadFinished() { // check for errors if (currentDownload->error()){ - currentDownload->deleteLater(); outfile.close(); outfile.remove(); int statusCode = currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); qxtLog->debug() << "[dm] Download of " << currentDownload->url().toString() << " failed with HTTP error code: " << statusCode; emit notify(QString("Download failed! HTTP Status Code: %1").arg(statusCode)); + currentDownload->deleteLater(); } else{ // if kcl, append the session ID to it @@ -185,15 +187,15 @@ void DownloadManager::downloadFinished() outfile.write(" alpha=" + sessionID.toUtf8()); } // end download - currentDownload->deleteLater(); outfile.close(); downloaded++; qxtLog->debug() << "[dm] Download of " << currentDownload->url().toString() << " finished. (downloaded = "<< downloaded << ")"; emit notify(QString("Successfully downloaded %1").arg(currentDownload->url().toString())); + currentDownload->deleteLater(); } dip = false; - // process next in queue + // process next in queue, if any if (dlQ.isEmpty()){ emit downloadQueueEmpty(); qxtLog->debug() << "[dm] Download manager ready. (2)"; -- cgit v1.2.3-55-g7522