Add BatchLoader

This commit is contained in:
Ryan Huffman 2015-01-22 14:13:54 -08:00
parent d8d0972cb6
commit 958d853b56
2 changed files with 127 additions and 0 deletions

View file

@ -0,0 +1,82 @@
//
// BatchLoader.cpp
// libraries/script-engine/src
//
// Created by Ryan Huffman on 01/22/15
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QFile>
#include "BatchLoader.h"
#include <NetworkAccessManager.h>
BatchLoader::BatchLoader(const QList<QUrl>& urls)
: QObject(),
_finished(false),
_urls(urls.toSet()),
_started(false),
_data() {
}
void BatchLoader::start() {
if (_started) {
return;
}
_started = true;
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
for (QUrl url : _urls) {
if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "ftp") {
QNetworkReply* reply = networkAccessManager.get(QNetworkRequest(url));
qDebug() << "Downloading file at" << url;
connect(reply, &QNetworkReply::finished, [=]() {
if (reply->error()) {
emit errorLoadingFile(url);
_data.insert(url, QString());
} else {
_data.insert(url, reply->readAll());
}
reply->deleteLater();
checkFinished();
});
// If we end up being destroyed before the reply finishes, clean it up
connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
} else {
#ifdef _WIN32
QString fileName = url.toString();
#else
QString fileName = url.toLocalFile();
#endif
qDebug() << "Reading file at " << fileName;
QFile scriptFile(fileName);
if (scriptFile.open(QFile::ReadOnly | QFile::Text)) {
QTextStream in(&scriptFile);
_data.insert(url, in.readAll());
// includeContents = in.readAll();
} else {
emit errorLoadingFile(url);
_data.insert(url, QString());
}
}
}
checkFinished();
}
void BatchLoader::checkFinished() {
if (!_finished && _urls.size() == _data.size()) {
_finished = true;
emit finished(_data);
}
}

View file

@ -0,0 +1,45 @@
//
// BatchLoader.h
// libraries/script-engine/src
//
// Created by Ryan Huffman on 01/22/15
// Copyright 2015 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#ifndef hifi_BatchLoader_h
#define hifi_BatchLoader_h
#include <QList>
#include <QMap>
#include <QObject>
#include <QSet>
#include <QString>
#include <QUrl>
#include <NetworkAccessManager.h>
class BatchLoader : public QObject {
Q_OBJECT
public:
BatchLoader(const QList<QUrl>& urls) ;
void start();
bool isFinished() const { return _finished; };
signals:
void finished(const QMap<QUrl, QString>& data);
void errorLoadingFile(QUrl url);
private:
void checkFinished();
bool _finished;
bool _started;
QSet<QUrl> _urls;
QMap<QUrl, QString> _data;
};
#endif // hifi_BatchLoader_h