summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJonathan Bauer2011-03-02 18:14:52 +0100
committerJonathan Bauer2011-03-02 18:14:52 +0100
commit10dc9cfd5f6b24230414fd9ccda811efbfc5e92a (patch)
tree68c1c0294237ab58efedc68aeb2ea40ced7df4dc /src
parentQueueing for DM implemented. (need to clean debug msgs a bit) (diff)
downloadfbgui-10dc9cfd5f6b24230414fd9ccda811efbfc5e92a.tar.gz
fbgui-10dc9cfd5f6b24230414fd9ccda811efbfc5e92a.tar.xz
fbgui-10dc9cfd5f6b24230414fd9ccda811efbfc5e92a.zip
comments & formatting...
Diffstat (limited to 'src')
-rw-r--r--src/DownloadManager.cpp54
-rw-r--r--src/DownloadManager.h16
2 files changed, 38 insertions, 32 deletions
diff --git a/src/DownloadManager.cpp b/src/DownloadManager.cpp
index e7df70d..146127d 100644
--- a/src/DownloadManager.cpp
+++ b/src/DownloadManager.cpp
@@ -1,6 +1,7 @@
#include "DownloadManager.h"
#include <QTimer>
+// ----------------------------------------------------------------------------------------
void DownloadManager::get(QString& filename)
{
// Forge URL from the given filename and the base URL.
@@ -12,26 +13,28 @@ void DownloadManager::get(QString& filename)
dlQ.enqueue(u);
return;
}
- // No running downloads.
+ // No running downloads: enqueue and start next download.
dlQ.enqueue(u);
qDebug() << "Enqueueing :" << u.toString();
startNextDownload();
}
+// ----------------------------------------------------------------------------------------
void DownloadManager::startNextDownload()
{
qDebug() << "Starting next download: " << dlQ.head().toString()
- << "(" << dlQ.size() << "in queue.)";
+ << "(" << dlQ.size() << "in queue.)";
// Set flag for download in progress.
dip = true;
+ // TODO: needed ?
if (dlQ.isEmpty())
{
qDebug() << "Download queue empty! Exiting...";
return;
}
-
+ // Dequeue next URL to download.
QUrl url = dlQ.dequeue();
qDebug() << "Dequeueing..." << url.toString();
- //** TEST **
+ // Extract the filename from the URL
QString path = url.path();
QString basename = QFileInfo(path).fileName();
if (basename.isEmpty())
@@ -40,61 +43,70 @@ void DownloadManager::startNextDownload()
this->filename = basename;
//qDebug() << "Path is: " << path << endl;
//qDebug() << "Basename is: " << basename << endl;
- //** TEST **
//qDebug() << "Filename is: " << this->filename;
outfile.setFileName(this->filename);
+ // If error upon opening, skip this file.
if (!outfile.open(QIODevice::WriteOnly))
{
qDebug() << "Couldn't open file! Exiting...";
- // Skip this file
startNextDownload();
return;
}
-
+ // Start the request for this URL.
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(downloadProgress(qint64, qint64)),
+ this, SLOT(downloadProgress(qint64, qint64)));
QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished()));
}
-
+// ----------------------------------------------------------------------------------------
+// Slots for the signals emmited by currentDownload.
+// ----------------------------------------------------------------------------------------
+// This slot listens to readyRead() emmited when data is available for reading.
void DownloadManager::downloadReady()
{
+ // readyRead() fired, so save the readable data.
outfile.write(this->currentDownload->readAll());
}
-
+// ----------------------------------------------------------------------------------------
+// This slot listens to the downloadProgress(..)
+// which provides information on the download progress of the file.
void DownloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal)
{
- qDebug() << "Download progress of " << currentDownload->url().toString() << ": " << bytesIn << "/" << bytesTotal;
+ qDebug() << "Download progress of " << currentDownload->url().toString()
+ << ": " << bytesIn << "/" << bytesTotal;
}
-
+// ----------------------------------------------------------------------------------------
+// This slot listens to the finished() which is emmited
+// when all the data from the reply has been read.
void DownloadManager::downloadFinished()
{
+ // Second check if the download actually is finished.
if (currentDownload->isFinished())
qDebug() << "Downloaded " << currentDownload->url().toString() << endl;
+ // Close output file.
outfile.close();
// 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;
+ // Queue not empty.
+ // Delete current reply object.
currentDownload->deleteLater();
+ // Initialise next download.
+ dip = false;
startNextDownload();
}
-
-void DownloadManager::print()
-{
- qDebug() << "The download manager is still working";
-}
-
+// ----------------------------------------------------------------------------------------
+// Constructor.
DownloadManager::DownloadManager(const QUrl& baseUrl)
{
this->qnam = new QNetworkAccessManager();
this->baseUrl = baseUrl;
dip = false;
}
-
+// Destructor.
DownloadManager::~DownloadManager()
{
}
diff --git a/src/DownloadManager.h b/src/DownloadManager.h
index 8d6e88b..d365b0a 100644
--- a/src/DownloadManager.h
+++ b/src/DownloadManager.h
@@ -10,34 +10,28 @@ Q_OBJECT
public:
DownloadManager(const QUrl& baseUrl);
~DownloadManager();
- void print();
void get(QString& filename);
private:
+ // Object required for downloading.
QNetworkAccessManager* qnam;
QUrl baseUrl;
- QFile outfile;
- QString filename;
- QNetworkRequest request;
- // QNetworkReply* reply;
-
QQueue<QUrl> dlQ;
+ QNetworkRequest request;
QNetworkReply* currentDownload;
-
+ QFile outfile;
+ QString filename;
+ // Download-in-progress flag.
bool dip;
-
signals:
void finished();
private slots:
void startNextDownload();
-
void downloadReady();
void downloadProgress(qint64 bytesIn, qint64 bytesTotal);
void downloadFinished();
-
-
};
#endif // DOWNLOADMANAGER_H