1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-28 10:38:45 +00:00
transmission/qt/RpcQueue.h
Mike Gelfand be74cb6356
Qt 6 support (#2069)
* Bump minimum Qt version to 5.6

* Switch from QRegExp to QRegularExpression

While still available, QRegExp has been moved to Qt6::Core5Compat module
and is not part of Qt6::Core.

* Use qIsEffectiveTLD instead of QUrl::topLevelDomain

The latter is not part of Qt6::Core. The former is a private utility in
Qt6::Network; using it for now, until (and if) we switch to something
non-Qt-specific.

* Use QStyle::State_Horizontal state when drawing progress bars

Although available for a long time, this state either didn't apply to
progress bars before Qt 6, or was deduced based on bar size. With Qt 6,
failing to specify it results in bad rendering.

* Don't use QStringRef (and associated methods)

While still available, QStringRef has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core. Related method (e.g.
QString::midRef) have been removed in Qt 6.

* Use Qt::ItemIsAutoTristate instead of Qt::ItemIsTristate

The latter was deprecated and replaced with the former in Qt 5.6.

* Don't use QApplication::globalStrut

This property has been deprecated in Qt 5.15 and removed in Qt 6.

* Use QImage::fromHICON instead of QtWin::fromHICON

WinExtras module (providind the latter helper) has been removed in Qt 6.

* Use QStringDecoder instead of QTextCodec

While still available, QTextCodec has been moved to Qt6::Core5Compat
module and is not part of Qt6::Core.

* Don't forward-declare QStringList

Instead of being a standalone class, its definition has changed to
QList<QString> template specialization in Qt 6.

* Use explicit (since Qt 6) QFileInfo constructor

* Use QDateTime's {to,from}SecsSinceEpoch instead of {to,from}Time_t

The latter was deprecated in Qt 5.8 and removed in Qt 6.

* Don't use QFuture<>'s operator==

It has been removed in Qt 6. Since the original issue this code was
solving was caused by future reuse, just don't reuse futures and create
new finished ones when necessary.

* Use std::vector<> instead of QVector<>

The latter has been changed to a typedef for QList<>, which might not be
what one wants, and which also changed behavior a bit leading to
compilation errors.

* Don't use + for flags, cast to int explicitly

Operator+ for enum values has been deleted in Qt 6, so using operator|
instead. Then, there's no conversion from QFlags<> to QVariant, so need
to cast to int.

* Support Qt 6 in CMake and for MSI packaging

* Remove extra (empty) CMake variable use when constructing Qt target names

* Simplify logic in tr_qt_add_translation CMake helper

Co-authored-by: Charles Kerr <charles@charleskerr.com>
2021-11-04 00:20:11 +03:00

161 lines
4.7 KiB
C++

/*
* This file Copyright (C) 2016 Mnemosyne LLC
*
* It may be used under the GNU GPL versions 2 or 3
* or any future license endorsed by Mnemosyne LLC.
*
*/
#pragma once
#include <cstdint>
#include <functional>
#include <type_traits>
#include <QFutureInterface>
#include <QFutureWatcher>
#include <QObject>
#include <QPair>
#include <QQueue>
#include "Macros.h"
#include "RpcClient.h"
class RpcQueue : public QObject
{
Q_OBJECT
TR_DISABLE_COPY_MOVE(RpcQueue)
public:
explicit RpcQueue(QObject* parent = nullptr);
void setTolerateErrors(bool tolerate_errors = true)
{
tolerate_errors_ = tolerate_errors;
}
template<typename Func>
void add(Func func)
{
queue_.enqueue(qMakePair(normalizeFunc(func), ErrorHandlerFunction()));
}
template<typename Func, typename ErrorHandler>
void add(Func func, ErrorHandler error_handler)
{
queue_.enqueue(qMakePair(normalizeFunc(func), normalizeErrorHandler(error_handler)));
}
// The first function in queue is ran synchronously
// (hence it may be e. g. a lambda capturing local variables by reference).
void run();
using Tag = uint64_t;
Tag tag() const
{
return tag_;
}
private:
// Internally queued function. Takes the last response future, makes a
// request and returns a new response future.
using QueuedFunction = std::function<RpcResponseFuture(RpcResponseFuture const&)>;
// Internally stored error handler function. Takes the last response future and returns nothing.
using ErrorHandlerFunction = std::function<void(RpcResponseFuture const&)>;
void runNext(RpcResponseFuture const& response);
// These overloads convert various forms of input closures to what we store internally.
// normal closure, takes response and returns new future
template<
typename Func,
typename std::enable_if<
std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, RpcResponseFuture>>::type* = nullptr>
QueuedFunction normalizeFunc(Func const& func) const
{
return [func](RpcResponseFuture const& r)
{
return func(r.result());
};
}
// closure without argument (first step), takes nothing and returns new future
template<
typename Func,
typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func>, RpcResponseFuture>>::type* = nullptr>
QueuedFunction normalizeFunc(Func const& func) const
{
return [func](RpcResponseFuture const&)
{
return func();
};
}
// closure without return value ("auxiliary"), takes response and returns nothing
template<
typename Func,
typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, void>>::type* = nullptr>
QueuedFunction normalizeFunc(Func const& func) const
{
return [func](RpcResponseFuture const& r)
{
func(r.result());
return createFinishedFuture();
};
}
// closure without argument and return value, takes nothing and returns nothing -- next function will also get nothing
template<typename Func, typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func>, void>>::type* = nullptr>
QueuedFunction normalizeFunc(Func const& func) const
{
return [func](RpcResponseFuture const&)
{
func();
return createFinishedFuture();
};
}
// normal error handler, takes last response
template<
typename Func,
typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, void>>::type* = nullptr>
ErrorHandlerFunction normalizeErrorHandler(Func const& func) const
{
return [func](RpcResponseFuture const& r)
{
func(r.result());
};
}
// error handler without an argument, takes nothing
template<typename Func, typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func>, void>>::type* = nullptr>
ErrorHandlerFunction normalizeErrorHandler(Func const& func) const
{
return [func](RpcResponseFuture const&)
{
func();
};
}
static RpcResponseFuture createFinishedFuture()
{
QFutureInterface<RpcResponse> promise;
promise.reportStarted();
promise.reportFinished();
return promise.future();
}
Tag const tag_;
static Tag next_tag;
bool tolerate_errors_ = {};
QFutureInterface<RpcResponse> promise_;
QQueue<QPair<QueuedFunction, ErrorHandlerFunction>> queue_;
ErrorHandlerFunction next_error_handler_;
QFutureWatcher<RpcResponse> future_watcher_;
private slots:
void stepFinished();
};