From ab1846c64d417224a53e8dc4ef3c55f87a413cd6 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Sat, 5 Mar 2011 22:33:17 +0100 Subject: downloadFile now needs a QUrl&, simplifies things (startDownload slot in jso needs reworking, tempfix there but messy.) --- src/DownloadManager.cpp | 51 ++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 26 deletions(-) (limited to 'src/DownloadManager.cpp') diff --git a/src/DownloadManager.cpp b/src/DownloadManager.cpp index 5bb5387..b89a371 100644 --- a/src/DownloadManager.cpp +++ b/src/DownloadManager.cpp @@ -1,26 +1,25 @@ #include "DownloadManager.h" -void DownloadManager::downloadFile(QString name) +void DownloadManager::downloadFile(QUrl& fileUrl) { - qDebug() << "Received downloadFile signal for:" << name; - this->processDownloadRequest(name); + qDebug() << "Received downloadFile signal for:" << fileUrl; + this->processDownloadRequest(fileUrl); } // ---------------------------------------------------------------------------------------- -void DownloadManager::processDownloadRequest(QString& filename) +void DownloadManager::processDownloadRequest(QUrl& url) { // Forge URL from the given filename and the base URL. - QUrl u = this->baseUrl.resolved(filename); // If download in progress, enqueue file and return. if (dip) { - qDebug() << "Download in progress! Enqueueing:" << u.toString() + qDebug() << "Download in progress! Enqueueing:" << url.toString() << "(" << dlQ.size() << "in queue)"; - dlQ.enqueue(u); + dlQ.enqueue(url); return; } // No running downloads: enqueue and start next download. - dlQ.enqueue(u); - qDebug() << "Enqueueing:" << u.toString() << endl; + dlQ.enqueue(url); + qDebug() << "Enqueueing:" << url.toString() << endl; startNextDownload(); } // ---------------------------------------------------------------------------------------- @@ -36,14 +35,21 @@ void DownloadManager::startNextDownload() } // Dequeue next URL to download. QUrl url = dlQ.dequeue(); - // Extract the filename from the URL - QString path = url.path(); - QString basename = QFileInfo(path).fileName(); + QString filepath = url.path(); + // Get filename from URL. + int i = filepath.lastIndexOf(QChar('/')); + filepath.remove(0, i + 1); + qDebug() << "From: " << url.toString() << endl + << "Parsed: " << filepath; + outfile.setFileName(filepath); + /* + qDebug() << "url.path() is:" << url.path(); if (basename.isEmpty()) this->filename = "download"; else this->filename = basename; outfile.setFileName(this->filename); + */ // If error upon opening, skip this file. if (!outfile.open(QIODevice::WriteOnly)) { @@ -72,7 +78,7 @@ void DownloadManager::startNextDownload() void DownloadManager::downloadReady() { // readyRead() fired, so save the readable data. - outfile.write(this->currentDownload->readAll()); + outfile.write(currentDownload->readAll()); } // ---------------------------------------------------------------------------------------- // This slot listens to the downloadProgress(..) @@ -95,32 +101,25 @@ void DownloadManager::downloadFinished() qDebug() << "Downloaded " << currentDownload->url().toString() << endl; // Close output file. outfile.close(); + currentDownload->deleteLater(); + ++downloaded; // If queue is empty, we are done. - // TODO: not sure if this is actually needed... dip = false; if (dlQ.isEmpty()) - qDebug() << "dlQ empty! returning..."; return; - // Queue not empty. - - // Delete current reply object. - currentDownload->deleteLater(); - // Initialise next download. - ++downloaded; - // qDebug() << "DM downloaded " << downloaded << "files"; + // Queue not empty: initialise next download. startNextDownload(); } // ---------------------------------------------------------------------------------------- // Constructor. -DownloadManager::DownloadManager(const QUrl& baseUrl) +DownloadManager::DownloadManager() { - this->qnam = new QNetworkAccessManager(); - this->baseUrl = baseUrl; + qnam = new QNetworkAccessManager(); dip = false; downloaded = 0; } // Destructor. DownloadManager::~DownloadManager() { - delete[] this->qnam; + delete[] qnam; } -- cgit v1.2.3-55-g7522