summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJonathan Bauer2011-03-10 23:40:48 +0100
committerJonathan Bauer2011-03-10 23:40:48 +0100
commit7759ad54643246bdab08accc247ef3e91c3e3238 (patch)
treec0941e0e923848960af1f9a96f0dc92f5d50b32d
parentsome changes pwd command doesn't work (diff)
downloadfbgui-7759ad54643246bdab08accc247ef3e91c3e3238.tar.gz
fbgui-7759ad54643246bdab08accc247ef3e91c3e3238.tar.xz
fbgui-7759ad54643246bdab08accc247ef3e91c3e3238.zip
filename now will/can be parsed from the reply header
-rw-r--r--src/downloadManager.cpp57
-rw-r--r--src/downloadManager.h2
-rw-r--r--src/javascriptInterface.cpp4
3 files changed, 41 insertions, 22 deletions
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 <QDir>
#include <QFileInfo>
+#include <QVariant>
+#include <QByteArray>
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 <QObject>
#include <QDir>
+#include <QMap>
#include <QtNetwork>
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);
}