Commit c0714872 authored by Philipp Erhardt's avatar Philipp Erhardt
Browse files

Print progress when downloading from URL.

parent e5bd4aa0
#include "download.h"
#include <iostream>
#include <QEventLoop>
#include <QUrl>
#include <QNetworkRequest>
......@@ -10,6 +8,8 @@
#include <QDir>
#include <QFileInfo>
using namespace std;
Download::Download() :
manager(new QNetworkAccessManager()),
......@@ -21,27 +21,23 @@ Download::~Download() {
delete file;
}
QString Download::load(QString f) {
QUrl url(f);
if( url.isLocalFile() || url.isRelative() ) {
// found local file
// do not download
if (url.isLocalFile() || url.isRelative()) {
// found local file, do not download
return QDir::toNativeSeparators(url.toLocalFile());
}
QEventLoop loop;
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), &loop, SLOT(quit()));
QNetworkRequest request(url);
QNetworkReply *reply = manager->get(request);
QObject::connect(manager, SIGNAL(finished(QNetworkReply *)), &loop, SLOT(quit()));
QNetworkReply *reply = manager->get(QNetworkRequest(url));
QObject::connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(progress(qint64, qint64)));
loop.exec();
#ifdef DEBUG
std::cerr << "File downloaded" << std::endl;
cerr << "File downloaded" << endl;
#endif
// find uniq temporary filename
// find unique temporary filename
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
delete file;
......@@ -49,18 +45,25 @@ QString Download::load(QString f) {
file->setAutoRemove(true);
file->open();
if(reply->error() != QNetworkReply::NoError || !reply->isReadable()) {
std::cerr << "Download failed" << std::cerr;
if (reply->error() != QNetworkReply::NoError || !reply->isReadable()) {
cerr << reply->errorString().toStdString() << endl;
return NULL;
}
// store data in tempfile
QByteArray data = reply->readAll();
qint64 len = file->write(data);
reply->deleteLater();
file->write(data);
file->close();
#ifdef DEBUG
std::cerr << "filename: " << file.toStdString() << std::endl;
cerr << "filename: " << file.toStdString() << endl;
#endif
return file->fileName();
}
void Download::progress(qint64 bytes_received, qint64 bytes_total) {
cout.precision(1);
cout << fixed << (bytes_received / 1024.0f) << "/";
cout << (bytes_total / 1024.0f) << "KB downloaded\r";
}
......@@ -6,7 +6,8 @@
#include <QTemporaryFile>
class Download {
class Download : public QObject {
Q_OBJECT
public:
Download();
......@@ -14,10 +15,13 @@ public:
QString load(QString);
private slots:
void progress(qint64 bytes_received, qint64 bytes_total);
private:
QNetworkAccessManager *manager;
QTemporaryFile *file;
};
#endif
......@@ -61,6 +61,9 @@ int main(int argc, char *argv[]) {
Download download;
QString file = download.load(QString::fromUtf8(argv[optind]));
if (file == NULL) {
return 1;
}
Viewer katarakt(file);
if (!katarakt.is_valid()) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment