mirror of
https://github.com/lubosz/overte.git
synced 2025-08-07 19:41:20 +02:00
Simple class to download files (sync/async)
This commit is contained in:
parent
2f13d1e30c
commit
a88872a21f
2 changed files with 77 additions and 0 deletions
42
libraries/shared/src/FileDownloader.cpp
Normal file
42
libraries/shared/src/FileDownloader.cpp
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
//
|
||||||
|
// FileDownloader.cpp
|
||||||
|
// hifi
|
||||||
|
//
|
||||||
|
// Created by Clement Brisset on 3/14/14.
|
||||||
|
// Copyright (c) 2014 HighFidelity, Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <QUrl>
|
||||||
|
#include <QNetworkRequest>
|
||||||
|
#include <QNetworkReply>
|
||||||
|
#include <QEventLoop>
|
||||||
|
|
||||||
|
#include "FileDownloader.h"
|
||||||
|
|
||||||
|
FileDownloader::FileDownloader(const QUrl dataURL, QObject* parent) : QObject(parent) {
|
||||||
|
connect(&_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(processReply(QNetworkReply*)));
|
||||||
|
|
||||||
|
QNetworkRequest request(dataURL);
|
||||||
|
_networkAccessManager.get(request);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FileDownloader::processReply(QNetworkReply *reply) {
|
||||||
|
if (reply->error() != QNetworkReply::NoError) {
|
||||||
|
emit done(reply->error());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_downloadedData = reply->readAll();
|
||||||
|
reply->deleteLater();
|
||||||
|
|
||||||
|
emit done(QNetworkReply::NoError);
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray FileDownloader::download(const QUrl dataURL) {
|
||||||
|
QEventLoop loop;
|
||||||
|
FileDownloader downloader(dataURL);
|
||||||
|
connect(&downloader, SIGNAL(done()), &loop, SLOT(quit()));
|
||||||
|
loop.exec();
|
||||||
|
return downloader.getData();
|
||||||
|
}
|
35
libraries/shared/src/FileDownloader.h
Normal file
35
libraries/shared/src/FileDownloader.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
//
|
||||||
|
// FileDownloader.h
|
||||||
|
// hifi
|
||||||
|
//
|
||||||
|
// Created by Clement Brisset on 3/14/14.
|
||||||
|
// Copyright (c) 2014 HighFidelity, Inc. All rights reserved.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef __hifi__FileDownloader__
|
||||||
|
#define __hifi__FileDownloader__
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QNetworkAccessManager>
|
||||||
|
|
||||||
|
class FileDownloader : public QObject {
|
||||||
|
public:
|
||||||
|
FileDownloader(const QUrl dataURL, QObject* parent = NULL);
|
||||||
|
QByteArray getData() const { return _downloadedData; }
|
||||||
|
|
||||||
|
static QByteArray download(const QUrl dataURL);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void done(QNetworkReply::NetworkError);
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void processReply(QNetworkReply* reply);
|
||||||
|
|
||||||
|
private:
|
||||||
|
QNetworkAccessManager _networkAccessManager;
|
||||||
|
QByteArray _downloadedData;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* defined(__hifi__FileDownloader__) */
|
Loading…
Reference in a new issue