Network: instantiable QML type, introduce proxyAddress property

# Conflicts:
#	main.qml
This commit is contained in:
xiphon 2020-07-29 17:43:02 +00:00
parent 7eeda0a8f0
commit 43aeea8eb7
4 changed files with 44 additions and 13 deletions

View File

@ -33,6 +33,7 @@ import QtQuick.Controls.Styles 1.1
import QtQuick.Dialogs 1.2 import QtQuick.Dialogs 1.2
import QtGraphicalEffects 1.0 import QtGraphicalEffects 1.0
import moneroComponents.Network 1.0
import moneroComponents.Wallet 1.0 import moneroComponents.Wallet 1.0
import moneroComponents.WalletManager 1.0 import moneroComponents.WalletManager 1.0
import moneroComponents.PendingTransaction 1.0 import moneroComponents.PendingTransaction 1.0
@ -1255,7 +1256,7 @@ ApplicationWindow {
} }
var url = provider[userCurrency]; var url = provider[userCurrency];
Network.getJSON(url, fiatApiJsonReceived); network.getJSON(url, fiatApiJsonReceived);
} }
function fiatApiCurrencySymbol() { function fiatApiCurrencySymbol() {
@ -2237,6 +2238,10 @@ ApplicationWindow {
dragMargin: 0 dragMargin: 0
} }
Network {
id: network
}
WalletManager { WalletManager {
id: walletManager id: walletManager
} }

View File

@ -357,6 +357,7 @@ Verify update binary using 'shasum'-compatible (SHA256 algo) output signed by tw
// registering types for QML // registering types for QML
qmlRegisterType<clipboardAdapter>("moneroComponents.Clipboard", 1, 0, "Clipboard"); qmlRegisterType<clipboardAdapter>("moneroComponents.Clipboard", 1, 0, "Clipboard");
qmlRegisterType<Downloader>("moneroComponents.Downloader", 1, 0, "Downloader"); qmlRegisterType<Downloader>("moneroComponents.Downloader", 1, 0, "Downloader");
qmlRegisterType<Network>("moneroComponents.Network", 1, 0, "Network");
qmlRegisterType<WalletKeysFilesModel>("moneroComponents.WalletKeysFilesModel", 1, 0, "WalletKeysFilesModel"); qmlRegisterType<WalletKeysFilesModel>("moneroComponents.WalletKeysFilesModel", 1, 0, "WalletKeysFilesModel");
qmlRegisterType<WalletManager>("moneroComponents.WalletManager", 1, 0, "WalletManager"); qmlRegisterType<WalletManager>("moneroComponents.WalletManager", 1, 0, "WalletManager");
@ -491,9 +492,6 @@ Verify update binary using 'shasum'-compatible (SHA256 algo) output signed by tw
engine.rootContext()->setContextProperty("moneroVersion", MONERO_VERSION_FULL); engine.rootContext()->setContextProperty("moneroVersion", MONERO_VERSION_FULL);
Network network;
engine.rootContext()->setContextProperty("Network", &network);
// Load main window (context properties needs to be defined obove this line) // Load main window (context properties needs to be defined obove this line)
engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
if (engine.rootObjects().isEmpty()) if (engine.rootObjects().isEmpty())

View File

@ -35,7 +35,7 @@
using epee::net_utils::http::fields_list; using epee::net_utils::http::fields_list;
using epee::net_utils::http::http_response_info; using epee::net_utils::http::http_response_info;
using epee::net_utils::http::http_simple_client; using epee::net_utils::http::abstract_http_client;
HttpClient::HttpClient(QObject *parent /* = nullptr */) HttpClient::HttpClient(QObject *parent /* = nullptr */)
: QObject(parent) : QObject(parent)
@ -78,7 +78,7 @@ bool HttpClient::on_header(const http_response_info &headers)
m_received = 0; m_received = 0;
emit receivedChanged(); emit receivedChanged();
return http_simple_client::on_header(headers); return net::http::client::on_header(headers);
} }
bool HttpClient::handle_target_data(std::string &piece_of_transfer) bool HttpClient::handle_target_data(std::string &piece_of_transfer)
@ -91,7 +91,7 @@ bool HttpClient::handle_target_data(std::string &piece_of_transfer)
m_received += piece_of_transfer.size(); m_received += piece_of_transfer.size();
emit receivedChanged(); emit receivedChanged();
return http_simple_client::handle_target_data(piece_of_transfer); return net::http::client::handle_target_data(piece_of_transfer);
} }
Network::Network(QObject *parent) Network::Network(QObject *parent)
@ -104,8 +104,12 @@ void Network::get(const QString &url, const QJSValue &callback, const QString &c
{ {
m_scheduler.run( m_scheduler.run(
[this, url, contentType] { [this, url, contentType] {
std::shared_ptr<abstract_http_client> httpClient = newClient();
if (httpClient.get() == nullptr)
{
return QJSValueList({url, "", "failed to initialize a client"});
}
std::string response; std::string response;
std::shared_ptr<http_simple_client> httpClient(new http_simple_client());
QString error = get(httpClient, url, response, contentType); QString error = get(httpClient, url, response, contentType);
return QJSValueList({url, QString::fromStdString(response), error}); return QJSValueList({url, QString::fromStdString(response), error});
}, },
@ -120,7 +124,12 @@ void Network::getJSON(const QString &url, const QJSValue &callback) const
std::string Network::get(const QString &url, const QString &contentType /* = {} */) const std::string Network::get(const QString &url, const QString &contentType /* = {} */) const
{ {
std::string response; std::string response;
QString error = get(std::shared_ptr<http_simple_client>(new http_simple_client()), url, response, contentType); std::shared_ptr<abstract_http_client> httpClient = newClient();
if (httpClient.get() == nullptr)
{
throw std::runtime_error("failed to initialize a client");
}
QString error = get(httpClient, url, response, contentType);
if (!error.isEmpty()) if (!error.isEmpty())
{ {
throw std::runtime_error(QString("failed to fetch %1: %2").arg(url).arg(error).toStdString()); throw std::runtime_error(QString("failed to fetch %1: %2").arg(url).arg(error).toStdString());
@ -129,7 +138,7 @@ std::string Network::get(const QString &url, const QString &contentType /* = {}
} }
QString Network::get( QString Network::get(
std::shared_ptr<http_simple_client> httpClient, std::shared_ptr<abstract_http_client> httpClient,
const QString &url, const QString &url,
std::string &response, std::string &response,
const QString &contentType /* = {} */) const const QString &contentType /* = {} */) const
@ -163,3 +172,13 @@ QString Network::get(
response = std::move(pri->m_body); response = std::move(pri->m_body);
return {}; return {};
} }
std::shared_ptr<abstract_http_client> Network::newClient() const
{
std::shared_ptr<abstract_http_client> client(new net::http::client());
if (!client->set_proxy(m_proxyAddress.toStdString()))
{
throw std::runtime_error("failed to set proxy address");
}
return client;
}

View File

@ -35,12 +35,12 @@
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wreorder" #pragma GCC diagnostic ignored "-Wreorder"
#include <net/http_client.h> #include <net/http.h>
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#include "FutureScheduler.h" #include "FutureScheduler.h"
class HttpClient : public QObject, public epee::net_utils::http::http_simple_client class HttpClient : public QObject, public net::http::client
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(quint64 contentLength READ contentLength NOTIFY contentLengthChanged); Q_PROPERTY(quint64 contentLength READ contentLength NOTIFY contentLengthChanged);
@ -70,6 +70,8 @@ private:
class Network : public QObject class Network : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString proxyAddress MEMBER m_proxyAddress NOTIFY proxyAddressChanged)
public: public:
Network(QObject *parent = nullptr); Network(QObject *parent = nullptr);
@ -79,11 +81,18 @@ public:
std::string get(const QString &url, const QString &contentType = {}) const; std::string get(const QString &url, const QString &contentType = {}) const;
QString get( QString get(
std::shared_ptr<epee::net_utils::http::http_simple_client> httpClient, std::shared_ptr<epee::net_utils::http::abstract_http_client> httpClient,
const QString &url, const QString &url,
std::string &response, std::string &response,
const QString &contentType = {}) const; const QString &contentType = {}) const;
signals:
void proxyAddressChanged() const;
private: private:
std::shared_ptr<epee::net_utils::http::abstract_http_client> newClient() const;
private:
QString m_proxyAddress;
mutable FutureScheduler m_scheduler; mutable FutureScheduler m_scheduler;
}; };