diff options
| author | xiphon <xiphon@protonmail.com> | 2020-07-29 17:43:02 +0000 |
|---|---|---|
| committer | xiphon <xiphon@protonmail.com> | 2020-07-30 16:49:22 +0000 |
| commit | 43aeea8eb7965e41f18ae14f3cd3db5e41e77ed7 (patch) | |
| tree | 96849be20f3de69fc490433f1afa14165f115510 /src | |
| parent | 7eeda0a8f075a0d0938486cf0d7718d9d9491c20 (diff) | |
| download | monzero-gui-43aeea8eb7965e41f18ae14f3cd3db5e41e77ed7.tar.gz monzero-gui-43aeea8eb7965e41f18ae14f3cd3db5e41e77ed7.tar.xz monzero-gui-43aeea8eb7965e41f18ae14f3cd3db5e41e77ed7.zip | |
Network: instantiable QML type, introduce proxyAddress property
# Conflicts:
# main.qml
Diffstat (limited to 'src')
| -rw-r--r-- | src/main/main.cpp | 4 | ||||
| -rw-r--r-- | src/qt/network.cpp | 31 | ||||
| -rw-r--r-- | src/qt/network.h | 15 |
3 files changed, 38 insertions, 12 deletions
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<clipboardAdapter>("moneroComponents.Clipboard", 1, 0, "Clipboard"); qmlRegisterType<Downloader>("moneroComponents.Downloader", 1, 0, "Downloader"); + qmlRegisterType<Network>("moneroComponents.Network", 1, 0, "Network"); qmlRegisterType<WalletKeysFilesModel>("moneroComponents.WalletKeysFilesModel", 1, 0, "WalletKeysFilesModel"); 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); - 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<abstract_http_client> httpClient = newClient(); + if (httpClient.get() == nullptr) + { + return QJSValueList({url, "", "failed to initialize a client"}); + } std::string response; - std::shared_ptr<http_simple_client> 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<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()) { 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<http_simple_client> httpClient, + std::shared_ptr<abstract_http_client> 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<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; +} 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 <net/http_client.h> +#include <net/http.h> #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<epee::net_utils::http::http_simple_client> httpClient, + std::shared_ptr<epee::net_utils::http::abstract_http_client> httpClient, const QString &url, std::string &response, const QString &contentType = {}) const; +signals: + void proxyAddressChanged() const; + +private: + std::shared_ptr<epee::net_utils::http::abstract_http_client> newClient() const; + private: + QString m_proxyAddress; mutable FutureScheduler m_scheduler; }; |
