From 7759ad54643246bdab08accc247ef3e91c3e3238 Mon Sep 17 00:00:00 2001 From: Jonathan Bauer Date: Thu, 10 Mar 2011 23:40:48 +0100 Subject: filename now will/can be parsed from the reply header --- src/downloadManager.cpp | 57 +++++++++++++++++++++++++++++---------------- src/downloadManager.h | 2 ++ src/javascriptInterface.cpp | 4 ++-- 3 files changed, 41 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/downloadManager.cpp b/src/downloadManager.cpp index c437710..c15454a 100644 --- a/src/downloadManager.cpp +++ b/src/downloadManager.cpp @@ -1,6 +1,8 @@ #include "downloadManager.h" //#include #include +#include +#include int downloadManager::downloaded = 0; // ---------------------------------------------------------------------------------------- @@ -21,9 +23,10 @@ downloadManager::downloadManager() } // ---------------------------------------------------------------------------------------- void downloadManager::downloadFile(QString& filename){ - if (debug) qDebug() << "DM received signal for: " << filename; + if (debug) qDebug() << "Received downloadFile signal for:" << filename; QUrl fileUrl; fileUrl = baseURL.resolved(QUrl(filename)); + qDebug() << "fileUrl: " << fileUrl; this->processDownloadRequest(fileUrl); } // ---------------------------------------------------------------------------------------- @@ -56,6 +59,7 @@ void downloadManager::processDownloadRequest(QUrl& url) // ---------------------------------------------------------------------------------------- void downloadManager::startNextDownload() { + if (dlQ.isEmpty()) { emit downloadQueueEmpty(); @@ -64,40 +68,35 @@ void downloadManager::startNextDownload() } if (debug) qDebug() << "Starting next download: " << dlQ.head().toString() << "(" << dlQ.size() << "in queue.)"; + // Dequeue next URL to download. QUrl url = dlQ.dequeue(); - // Get 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(); - // TODO: check for if relative path, if so prepend binPath + + // temp file name will be renamed to the filename parsed from the + // header ("Content-Disposition" --> filename) outfile.setFileName(downloadPath + "/" + tmp); - if (debug) qDebug() << "Trying to save to: " << downloadPath + "/" + tmp; - if (outfile.exists()){ - if (debug) qDebug() << "File already exists. Skipping: " << url.toString(); - startNextDownload(); - return; - } - // If error upon opening, skip this file. + if (!outfile.open(QIODevice::WriteOnly)) { - if (debug) qDebug() << "Couldn't open file! Skipping: " << url.toString(); - startNextDownload(); + if (debug) qDebug() << "Couldn't open file! Skipping..."; return; } - // Start the request for this URL. - if (debug) qDebug() << "Saving " << url.toString() << "to " << outfile.fileName(); + QNetworkRequest request(url); currentDownload = qnam->get(request); - // TODO: Error handling not working properly... if (currentDownload->error() != QNetworkReply::NoError) { if (debug) qDebug() << "Network reply error, skipping download..."; return; } - dip = true; + currentProgress = 0; + dip = true; QObject::connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReady())); QObject::connect(currentDownload, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); @@ -109,6 +108,15 @@ void downloadManager::startNextDownload() // 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. outfile.write(currentDownload->readAll()); } @@ -133,15 +141,24 @@ void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) // when all the data from the reply has been read. void downloadManager::downloadFinished() { - // Second check if the download actually is finished just to be sure. - if (currentDownload->isFinished()) - if (debug) qDebug() << "Download of " << currentDownload->url().toString() - << "finished." << endl; // Close output file. outfile.close(); + QString tmp = outfile.fileName(); + tmp.remove(0, tmp.lastIndexOf(QChar('/')) + 1); + qDebug() << "Trying to rename " << tmp << " to --> " << currentTargetFilename; + + if (outfile.rename(downloadPath + "/" + currentTargetFilename)) { + if (debug) qDebug() << "Renamed file!"; + } + else { + if (debug) qDebug() << "Failure to rename file!"; + } + 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(); diff --git a/src/downloadManager.h b/src/downloadManager.h index 461d31a..bf2d341 100644 --- a/src/downloadManager.h +++ b/src/downloadManager.h @@ -4,6 +4,7 @@ #include "fbgui.h" #include #include +#include #include extern bool debug; @@ -32,6 +33,7 @@ private: bool dip; int currentProgress; static int downloaded; + QString currentTargetFilename; signals: void finished(); diff --git a/src/javascriptInterface.cpp b/src/javascriptInterface.cpp index 881ebf8..f8b8ef6 100644 --- a/src/javascriptInterface.cpp +++ b/src/javascriptInterface.cpp @@ -33,13 +33,13 @@ void javascriptInterface::attachToDOM() void javascriptInterface::startDownload(QString filename) { /* return if no filename in input field */ - if (debug) qDebug() << "javascriptInterace: requesting download: " << filename; + //if (debug) qDebug() << "javascriptInterace: requesting download: " << filename; if (filename.isEmpty()) { _parent->evaluateJavaScript("alert(\"No filename!\")"); return; } - if (debug) qDebug() << "Request download: " << baseURL.resolved(QUrl(filename)).toString(); + //if (debug) qDebug() << "Request download: " << baseURL.resolved(QUrl(filename)).toString(); emit requestFile(filename); } -- cgit v1.2.3-55-g7522