2014-12-27 20:03:10 +00:00
|
|
|
/*
|
2016-04-19 20:41:59 +00:00
|
|
|
* This file Copyright (C) 2014-2016 Mnemosyne LLC
|
2014-12-27 20:03:10 +00:00
|
|
|
*
|
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
|
2016-04-19 20:41:59 +00:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureInterface>
|
|
|
|
#include <QHash>
|
2014-12-27 20:03:10 +00:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/quark.h>
|
2015-07-13 00:32:48 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2014-12-27 20:03:10 +00:00
|
|
|
|
|
|
|
class QByteArray;
|
|
|
|
class QNetworkAccessManager;
|
|
|
|
|
2015-07-13 00:32:48 +00:00
|
|
|
typedef std::shared_ptr<tr_variant> TrVariantPtr;
|
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"
|
|
|
|
{
|
2017-04-19 12:04:45 +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
|
|
|
};
|
|
|
|
|
2017-04-19 12:04:45 +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.
|
|
|
|
typedef QFuture<RpcResponse> RpcResponseFuture;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
class RpcClient : public QObject
|
2014-12-27 20:03:10 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
public:
|
|
|
|
RpcClient(QObject* parent = nullptr);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
virtual ~RpcClient()
|
|
|
|
{
|
|
|
|
}
|
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);
|
2017-04-20 16:02:19 +00:00
|
|
|
RpcResponseFuture exec(char const* 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
|
|
|
|
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);
|
2017-04-19 12:04:45 +00:00
|
|
|
int64_t parseResponseTag(tr_variant& response);
|
|
|
|
RpcResponse parseResponseData(tr_variant& response);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void localSessionCallback(tr_session* s, tr_variant* response, void* vself);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
private slots:
|
|
|
|
void networkRequestFinished(QNetworkReply* reply);
|
|
|
|
void localRequestFinished(TrVariantPtr response);
|
|
|
|
|
|
|
|
private:
|
|
|
|
tr_session* mySession;
|
2014-12-27 20:03:10 +00:00
|
|
|
QString mySessionId;
|
|
|
|
QUrl myUrl;
|
2017-04-19 12:04:45 +00:00
|
|
|
QNetworkAccessManager* myNAM;
|
2016-04-19 20:41:59 +00:00
|
|
|
QHash<int64_t, QFutureInterface<RpcResponse>> myLocalRequests;
|
|
|
|
int64_t myNextTag;
|
2014-12-27 20:03:10 +00:00
|
|
|
};
|