diff options
| author | Jonathan Bauer | 2011-03-16 01:22:15 +0100 |
|---|---|---|
| committer | Jonathan Bauer | 2011-03-16 01:22:15 +0100 |
| commit | 50c0d85dbe0d604b00c35b67a5ddf6c19cd1c105 (patch) | |
| tree | 1c1fb412d84f71e6be496388de3bafd3ec77972a | |
| parent | fbgui.conf (diff) | |
| download | fbgui-50c0d85dbe0d604b00c35b67a5ddf6c19cd1c105.tar.gz fbgui-50c0d85dbe0d604b00c35b67a5ddf6c19cd1c105.tar.xz fbgui-50c0d85dbe0d604b00c35b67a5ddf6c19cd1c105.zip | |
javascriptInterface: downloadInfo(filename, filesize) filesize in bytes
| -rw-r--r-- | src/downloadManager.cpp | 30 | ||||
| -rw-r--r-- | src/downloadManager.h | 8 | ||||
| -rw-r--r-- | src/fbgui.conf | 2 | ||||
| -rw-r--r-- | src/fbgui.cpp | 5 | ||||
| -rw-r--r-- | src/javascriptInterface.cpp | 10 | ||||
| -rw-r--r-- | src/javascriptInterface.h | 4 |
6 files changed, 43 insertions, 16 deletions
diff --git a/src/downloadManager.cpp b/src/downloadManager.cpp index e17b459..25f012a 100644 --- a/src/downloadManager.cpp +++ b/src/downloadManager.cpp @@ -8,6 +8,7 @@ downloadManager::downloadManager() { qnam = new QNetworkAccessManager(); dip = false; + infoSent = false; downloadDir = QDir(downloadPath); /* Check if downloadPath exists, if not create it. */ if (!downloadDir.exists()){ @@ -89,6 +90,7 @@ void downloadManager::startNextDownload() dip = true; dltime.start(); QObject::connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReady())); + QObject::connect(currentDownload, SIGNAL(metaDataChanged()), this, SLOT(processMetaInfo())); QObject::connect(currentDownload, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(downloadProgress(qint64, qint64))); QObject::connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished())); @@ -96,8 +98,22 @@ void downloadManager::startNextDownload() // ---------------------------------------------------------------------------------------- // Private slots // ---------------------------------------------------------------------------------------- +void downloadManager::processMetaInfo(){ + const QByteArray cltag = "Content-Length"; + QByteArray clinfo = currentDownload->rawHeader(cltag); + qDebug() << "feel spammy?"; + if (!infoSent){ + if (debug) qDebug() << "MetaChanged: " << outfile.fileName() << ""; + QFileInfo fi(outfile); + emit downloadInfo(outfile.fileName(), clinfo.toDouble()); + infoSent = true; + } +} +// ---------------------------------------------------------------------------------------- void downloadManager::downloadReady() -{ +{ + /* Send file information */ + /* Data ready, save it */ outfile.write(currentDownload->readAll()); } @@ -119,13 +135,11 @@ void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) speed /= 1024*1024; unit = "MB/s"; } - if (debug) qDebug() << "speed: " << speed << unit; /* Update progress only if difference higher than the updateInterval setting */ currentProgress = ((bytesIn * 100) / bytesTotal); if (currentProgress - lastProgress >= updateInterval){ lastProgress = currentProgress; - - emit updateProgress(currentProgress, speed); + emit updateProgress(currentProgress, speed, unit); if (debug) qDebug() << "Download progress of " << currentDownload->url().toString() << ": " << bytesIn << "/" << bytesTotal << "(" << currentProgress << "\%)"; } @@ -135,9 +149,13 @@ void downloadManager::downloadProgress(qint64 bytesIn, qint64 bytesTotal) void downloadManager::downloadFinished() { - if (currentDownload->error()) + if (currentDownload->error()){ if (debug) qDebug() << "Download of" << currentDownload->url().toString() << "failed with status code: " << currentDownload->error(); + currentDownload->deleteLater(); + outfile.remove(); + return; + } // TODO Handle errors. if (debug) qDebug() << "NetworkCode: " << currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); @@ -145,12 +163,12 @@ void downloadManager::downloadFinished() currentDownload->deleteLater(); downloaded++; dip = false; + infoSent = false; if (debug) qDebug() << "Download of " << currentDownload->url().toString() << "finished. (dlcount = "<< downloaded << ")"; if (dlQ.isEmpty()){ emit downloadQueueEmpty(); if (debug) qDebug() << "Download manager ready. (2)"; - return; } startNextDownload(); diff --git a/src/downloadManager.h b/src/downloadManager.h index 6c96633..2c0a021 100644 --- a/src/downloadManager.h +++ b/src/downloadManager.h @@ -23,7 +23,7 @@ #include <QDir> #include <QMap> #include <QtNetwork> -#include <QTimer> +#include <QTime> extern bool debug; extern QUrl baseURL; @@ -48,7 +48,7 @@ private: QFile outfile; QDir downloadDir; QTime dltime; - bool dip; + bool dip, infoSent; int currentProgress, lastProgress; static int downloaded; @@ -56,7 +56,8 @@ private: signals: void finished(); - void updateProgress(int percent, double speed); + void downloadInfo(QString filename, double filesize); + void updateProgress(int percent, double speed, QString unit); void downloadQueueEmpty(); public slots: @@ -66,6 +67,7 @@ public slots: private slots: void startNextDownload(); void downloadReady(); + void processMetaInfo(); void downloadProgress(qint64 bytesIn, qint64 bytesTotal); void downloadFinished(); }; diff --git a/src/fbgui.conf b/src/fbgui.conf index c4642dc..58c35e8 100644 --- a/src/fbgui.conf +++ b/src/fbgui.conf @@ -1,4 +1,4 @@ [default] url=http://m.openslx.org downloadDirectory=/downloads -updateInterval=7 +updateInterval=2 diff --git a/src/fbgui.cpp b/src/fbgui.cpp index 5a2d985..7c7ed46 100644 --- a/src/fbgui.cpp +++ b/src/fbgui.cpp @@ -33,8 +33,9 @@ fbgui::fbgui() jsi, SLOT(attachToDOM())); /* Init Download Manager */ downloadManager* dm = new downloadManager(); + QObject::connect(dm, SIGNAL(downloadInfo(QString, double)), jsi, SLOT(downloadInfo(QString, double))); QObject::connect(jsi, SIGNAL(requestFile(QString&)), dm, SLOT(downloadFile(QString&))); - QObject::connect(dm, SIGNAL(updateProgress(int, double)), jsi, SLOT(updateProgressBar(int, double))); + QObject::connect(dm, SIGNAL(updateProgress(int, double, QString)), jsi, SLOT(updateProgressBar(int, double, QString))); QObject::connect(dm, SIGNAL(downloadQueueEmpty()), jsi, SLOT(callbackOnDlQueueFinished())); setAttribute(Qt::WA_QuitOnClose, true); @@ -48,7 +49,7 @@ void fbgui::checkHost() const { QHostInfo hostInfo = QHostInfo::fromName(baseURL.host()); if (hostInfo.error() != QHostInfo::NoError){ - qDebug() << "Lookup of " << baseURL.host() << "failed." << "Exiting..."; + qDebug() << "Lookup of " << baseURL.host() << "failed. Exiting..."; exit(EXIT_FAILURE); } } diff --git a/src/javascriptInterface.cpp b/src/javascriptInterface.cpp index 399f863..c4dd61b 100644 --- a/src/javascriptInterface.cpp +++ b/src/javascriptInterface.cpp @@ -33,10 +33,16 @@ void javascriptInterface::startDownload(QString filename) emit requestFile(filename); } //------------------------------------------------------------------------------------------------------- -void javascriptInterface::updateProgressBar(int percent, double speed) +void javascriptInterface::downloadInfo(QString filename, double filesize) +{ + QString code = QString("downloadInfo('\%1', \%2)").arg(filename).arg(filesize); + _parent->evaluateJavaScript(code); +} +//------------------------------------------------------------------------------------------------------- +void javascriptInterface::updateProgressBar(int percent, double speed, QString unit) { if (percent == 0) return; - QString code = QString("updateProgress('\%1', \%2)").arg(percent).arg(speed); + QString code = QString("updateProgress(\%1, \%2, '\%3')").arg(percent).arg(speed).arg(unit); if (debug) qDebug() << "To JS: " << code; _parent->evaluateJavaScript(code); } diff --git a/src/javascriptInterface.h b/src/javascriptInterface.h index beb8205..457254b 100644 --- a/src/javascriptInterface.h +++ b/src/javascriptInterface.h @@ -39,8 +39,8 @@ public slots: void startDownload(QString filename); void setCallbackOnDlQueueFinished(QString fctOnDownloadsFinished); void callbackOnDlQueueFinished(); - void updateProgressBar(int percent, double speed); - + void updateProgressBar(int percent, double speed, QString unit); + void downloadInfo(QString filename, double filesize); QString getSysInfo(QString info); void quit(); }; |
