diff --git a/main.qml b/main.qml index ae6d96de..c1d88d49 100644 --- a/main.qml +++ b/main.qml @@ -33,6 +33,7 @@ import QtQuick.Controls.Styles 1.1 import QtQuick.Dialogs 1.2 import QtGraphicalEffects 1.0 +import moneroComponents.Network 1.0 import moneroComponents.Wallet 1.0 import moneroComponents.WalletManager 1.0 import moneroComponents.PendingTransaction 1.0 @@ -1255,7 +1256,7 @@ ApplicationWindow { } var url = provider[userCurrency]; - Network.getJSON(url, fiatApiJsonReceived); + network.getJSON(url, fiatApiJsonReceived); } function fiatApiCurrencySymbol() { @@ -2237,6 +2238,10 @@ ApplicationWindow { dragMargin: 0 } + Network { + id: network + } + WalletManager { id: walletManager } diff --git a/src/main/main.cpp b/src/main/main.cpp index f5423940..25cb5296 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -357,6 +357,7 @@ Verify update binary using 'shasum'-compatible (SHA256 algo) output signed by tw // registering types for QML qmlRegisterType("moneroComponents.Clipboard", 1, 0, "Clipboard"); qmlRegisterType("moneroComponents.Downloader", 1, 0, "Downloader"); + qmlRegisterType("moneroComponents.Network", 1, 0, "Network"); qmlRegisterType("moneroComponents.WalletKeysFilesModel", 1, 0, "WalletKeysFilesModel"); qmlRegisterType("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); - Network network; - engine.rootContext()->setContextProperty("Network", &network); - // Load main window (context properties needs to be defined obove this line) engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); if (engine.rootObjects().isEmpty()) diff --git a/src/qt/network.cpp b/src/qt/network.cpp index 1a674508..93f18300 100644 --- a/src/qt/network.cpp +++ b/src/qt/network.cpp @@ -35,7 +35,7 @@ using epee::net_utils::http::fields_list; 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 */) : QObject(parent) @@ -78,7 +78,7 @@ bool HttpClient::on_header(const http_response_info &headers) m_received = 0; 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) @@ -91,7 +91,7 @@ bool HttpClient::handle_target_data(std::string &piece_of_transfer) m_received += piece_of_transfer.size(); 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) @@ -104,8 +104,12 @@ void Network::get(const QString &url, const QJSValue &callback, const QString &c { m_scheduler.run( [this, url, contentType] { + std::shared_ptr httpClient = newClient(); + if (httpClient.get() == nullptr) + { + return QJSValueList({url, "", "failed to initialize a client"}); + } std::string response; - std::shared_ptr httpClient(new http_simple_client()); QString error = get(httpClient, url, response, contentType); 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 response; - QString error = get(std::shared_ptr(new http_simple_client()), url, response, contentType); + std::shared_ptr 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()) { 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( - std::shared_ptr httpClient, + std::shared_ptr httpClient, const QString &url, std::string &response, const QString &contentType /* = {} */) const @@ -163,3 +172,13 @@ QString Network::get( response = std::move(pri->m_body); return {}; } + +std::shared_ptr Network::newClient() const +{ + std::shared_ptr client(new net::http::client()); + if (!client->set_proxy(m_proxyAddress.toStdString())) + { + throw std::runtime_error("failed to set proxy address"); + } + return client; +} diff --git a/src/qt/network.h b/src/qt/network.h index e741247d..f895ed3d 100644 --- a/src/qt/network.h +++ b/src/qt/network.h @@ -35,12 +35,12 @@ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wunused-parameter" #pragma GCC diagnostic ignored "-Wreorder" -#include +#include #pragma GCC diagnostic pop #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_PROPERTY(quint64 contentLength READ contentLength NOTIFY contentLengthChanged); @@ -70,6 +70,8 @@ private: class Network : public QObject { Q_OBJECT + Q_PROPERTY(QString proxyAddress MEMBER m_proxyAddress NOTIFY proxyAddressChanged) + public: Network(QObject *parent = nullptr); @@ -79,11 +81,18 @@ public: std::string get(const QString &url, const QString &contentType = {}) const; QString get( - std::shared_ptr httpClient, + std::shared_ptr httpClient, const QString &url, std::string &response, const QString &contentType = {}) const; +signals: + void proxyAddressChanged() const; + private: + std::shared_ptr newClient() const; + +private: + QString m_proxyAddress; mutable FutureScheduler m_scheduler; };