2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2014-2022 Mnemosyne LLC.
|
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0), GPLv3 (SPDX: GPL-3.0),
|
|
|
|
// or any future license endorsed by Mnemosyne LLC.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2016-03-29 16:37:21 +00:00
|
|
|
#pragma once
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2015-07-13 00:32:48 +00:00
|
|
|
#include <memory>
|
2020-07-28 15:56:40 +00:00
|
|
|
#include <optional>
|
2020-07-27 04:30:58 +00:00
|
|
|
#include <string_view>
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureInterface>
|
|
|
|
#include <QHash>
|
2020-07-28 15:56:40 +00:00
|
|
|
#include <QNetworkRequest>
|
2014-12-27 20:03:10 +00:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
2022-02-08 03:56:04 +00:00
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
#include <libtransmission/quark.h>
|
2022-02-08 03:56:04 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2015-07-13 00:32:48 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2014-12-27 20:03:10 +00:00
|
|
|
|
|
|
|
class QNetworkAccessManager;
|
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
using TrVariantPtr = std::shared_ptr<tr_variant>;
|
2017-04-19 12:04:45 +00:00
|
|
|
Q_DECLARE_METATYPE(TrVariantPtr)
|
2015-07-13 00:32:48 +00:00
|
|
|
|
2014-12-27 20:03:10 +00:00
|
|
|
extern "C"
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
struct evbuffer;
|
|
|
|
struct tr_session;
|
2014-12-27 20:03:10 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
struct RpcResponse
|
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QString result;
|
|
|
|
TrVariantPtr args;
|
|
|
|
bool success = false;
|
|
|
|
QNetworkReply::NetworkError networkError = QNetworkReply::NoError;
|
2016-04-19 20:41:59 +00:00
|
|
|
};
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
Q_DECLARE_METATYPE(QFutureInterface<RpcResponse>)
|
2016-04-19 20:41:59 +00:00
|
|
|
|
|
|
|
// The response future -- the RPC engine returns one for each request made.
|
2020-05-20 01:32:51 +00:00
|
|
|
using RpcResponseFuture = QFuture<RpcResponse>;
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
class RpcClient : public QObject
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
2020-08-11 18:11:55 +00:00
|
|
|
TR_DISABLE_COPY_MOVE(RpcClient)
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
public:
|
2020-11-01 21:47:57 +00:00
|
|
|
explicit RpcClient(QObject* parent = nullptr);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void stop();
|
|
|
|
void start(tr_session* session);
|
2017-04-20 16:02:19 +00:00
|
|
|
void start(QUrl const& url);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool isLocal() const;
|
2017-04-20 16:02:19 +00:00
|
|
|
QUrl const& url() const;
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponseFuture exec(tr_quark method, tr_variant* args);
|
2020-07-27 04:30:58 +00:00
|
|
|
RpcResponseFuture exec(std::string_view method, tr_variant* args);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
signals:
|
|
|
|
void httpAuthenticationRequired();
|
|
|
|
void dataReadProgress();
|
|
|
|
void dataSendProgress();
|
2017-04-20 16:02:19 +00:00
|
|
|
void networkResponse(QNetworkReply::NetworkError code, QString const& message);
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
private slots:
|
|
|
|
void networkRequestFinished(QNetworkReply* reply);
|
|
|
|
void localRequestFinished(TrVariantPtr response);
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
private:
|
|
|
|
RpcResponseFuture sendRequest(TrVariantPtr json);
|
|
|
|
QNetworkAccessManager* networkAccessManager();
|
|
|
|
int64_t getNextTag();
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void sendNetworkRequest(TrVariantPtr json, QFutureInterface<RpcResponse> const& promise);
|
|
|
|
void sendLocalRequest(TrVariantPtr json, QFutureInterface<RpcResponse> const& promise, int64_t tag);
|
2020-11-09 03:31:02 +00:00
|
|
|
int64_t parseResponseTag(tr_variant& response) const;
|
|
|
|
RpcResponse parseResponseData(tr_variant& response) const;
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
static void localSessionCallback(tr_session* s, tr_variant* response, void* vself) noexcept;
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-07-28 15:56:40 +00:00
|
|
|
std::optional<QNetworkRequest> request_;
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_session* session_ = {};
|
|
|
|
QString session_id_;
|
|
|
|
QUrl url_;
|
|
|
|
QNetworkAccessManager* nam_ = {};
|
|
|
|
QHash<int64_t, QFutureInterface<RpcResponse>> local_requests_;
|
|
|
|
int64_t next_tag_ = {};
|
2022-02-08 03:56:04 +00:00
|
|
|
bool const verbose_ = tr_env_key_exists("TR_RPC_VERBOSE");
|
2014-12-27 20:03:10 +00:00
|
|
|
};
|