summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/DownloadManager.cpp87
-rw-r--r--src/DownloadManager.h20
-rw-r--r--src/fbbrowser.cpp13
3 files changed, 101 insertions, 19 deletions
diff --git a/src/DownloadManager.cpp b/src/DownloadManager.cpp
index 436518f..e7df70d 100644
--- a/src/DownloadManager.cpp
+++ b/src/DownloadManager.cpp
@@ -1,26 +1,86 @@
#include "DownloadManager.h"
+#include <QTimer>
-void DownloadManager::downloadFile(QString& filename)
+void DownloadManager::get(QString& filename)
{
+ // Forge URL from the given filename and the base URL.
QUrl u = this->baseUrl.resolved(filename);
- this->request.setUrl(u);
- this->filename = filename;
- qDebug() << "Downloading file: " << u.toString();
- this->reply = this->qnam->get(this->request);
- QObject::connect(this->reply, SIGNAL(finished()), this, SLOT(saveData()));
+ // If download in progress, enqueue file and return.
+ if (dip)
+ {
+ qDebug() << "Download in progress! Enqueueing:" << u.toString();
+ dlQ.enqueue(u);
+ return;
+ }
+ // No running downloads.
+ dlQ.enqueue(u);
+ qDebug() << "Enqueueing :" << u.toString();
+ startNextDownload();
}
-
-void DownloadManager::saveData()
+void DownloadManager::startNextDownload()
{
- QFile outfile(this->filename);
+ qDebug() << "Starting next download: " << dlQ.head().toString()
+ << "(" << dlQ.size() << "in queue.)";
+ // Set flag for download in progress.
+ dip = true;
+ if (dlQ.isEmpty())
+ {
+ qDebug() << "Download queue empty! Exiting...";
+ return;
+ }
+
+ QUrl url = dlQ.dequeue();
+ qDebug() << "Dequeueing..." << url.toString();
+ //** TEST **
+ QString path = url.path();
+ QString basename = QFileInfo(path).fileName();
+ if (basename.isEmpty())
+ this->filename = "download";
+ else
+ this->filename = basename;
+ //qDebug() << "Path is: " << path << endl;
+ //qDebug() << "Basename is: " << basename << endl;
+ //** TEST **
+ //qDebug() << "Filename is: " << this->filename;
+ outfile.setFileName(this->filename);
if (!outfile.open(QIODevice::WriteOnly))
{
- qDebug() << "Couldnt open file! exiting...";
- exit(1);
+ qDebug() << "Couldn't open file! Exiting...";
+ // Skip this file
+ startNextDownload();
+ return;
}
- outfile.write(this->reply->readAll());
+
+ QNetworkRequest request(url);
+ currentDownload = (*qnam).get(request);
+ QObject::connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReady()));
+ QObject::connect(currentDownload, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64)));
+ QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished()));
+}
+
+void DownloadManager::downloadReady()
+{
+ outfile.write(this->currentDownload->readAll());
+}
+
+void DownloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal)
+{
+ qDebug() << "Download progress of " << currentDownload->url().toString() << ": " << bytesIn << "/" << bytesTotal;
+}
+
+void DownloadManager::downloadFinished()
+{
+ if (currentDownload->isFinished())
+ qDebug() << "Downloaded " << currentDownload->url().toString() << endl;
outfile.close();
- qDebug() << "Download done.";
+ // If queue is empty, we are done.
+ // TODO: not sure if this is actually needed...
+ if (dlQ.isEmpty())
+ return;
+ // Queue not empty, start next download in queue.
+ dip = false;
+ currentDownload->deleteLater();
+ startNextDownload();
}
void DownloadManager::print()
@@ -32,6 +92,7 @@ DownloadManager::DownloadManager(const QUrl& baseUrl)
{
this->qnam = new QNetworkAccessManager();
this->baseUrl = baseUrl;
+ dip = false;
}
DownloadManager::~DownloadManager()
diff --git a/src/DownloadManager.h b/src/DownloadManager.h
index 41ef4be..8d6e88b 100644
--- a/src/DownloadManager.h
+++ b/src/DownloadManager.h
@@ -11,18 +11,32 @@ public:
DownloadManager(const QUrl& baseUrl);
~DownloadManager();
void print();
- void downloadFile(QString& filename);
+ void get(QString& filename);
private:
QNetworkAccessManager* qnam;
QUrl baseUrl;
+ QFile outfile;
QString filename;
QNetworkRequest request;
- QNetworkReply* reply;
+ // QNetworkReply* reply;
+ QQueue<QUrl> dlQ;
+ QNetworkReply* currentDownload;
+
+ bool dip;
+
+
+signals:
+ void finished();
private slots:
- void saveData();
+ void startNextDownload();
+
+ void downloadReady();
+ void downloadProgress(qint64 bytesIn, qint64 bytesTotal);
+ void downloadFinished();
+
};
diff --git a/src/fbbrowser.cpp b/src/fbbrowser.cpp
index 67909f6..009aae7 100644
--- a/src/fbbrowser.cpp
+++ b/src/fbbrowser.cpp
@@ -28,15 +28,22 @@ fbbrowser::fbbrowser(const QUrl & url)
}
else
{
- qDebug() << "QNetworkReply error code is: " << reply->error();
+ qDebug() << "QNetworkReply error code is: " << reply->error();
qDebug() << "Error occured, loading error page...";
view->load(QUrl("qrc:/html/errorPage.html"));
}
// **** TEST ****
DownloadManager* dm = new DownloadManager(baseUrl);
- QString qs = "test.php";
- dm->downloadFile(qs);
+ QString qs;
+
+ qs = "test.php";
+ dm->get(qs);
+ qs = "blacklist.txt";
+ dm->get(qs);
+ qs = "whitelist.tar.gz";
+ dm->get(qs);
// **** TEST ****
+ qDebug() << "DM blocking app?";
//remove the window decoration
this->setWindowFlags(Qt::SplashScreen);