2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2020-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.
|
2020-07-27 04:30:58 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <ctime>
|
2020-07-27 04:30:58 +00:00
|
|
|
#include <optional>
|
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-03 21:20:11 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <vector>
|
2020-07-27 04:30:58 +00:00
|
|
|
|
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
#include <libtransmission/variant.h>
|
|
|
|
|
|
|
|
class QByteArray;
|
|
|
|
|
|
|
|
class Speed;
|
2020-09-07 21:19:10 +00:00
|
|
|
class TorrentHash;
|
2020-07-27 04:30:58 +00:00
|
|
|
struct Peer;
|
|
|
|
struct TorrentFile;
|
|
|
|
struct TrackerStat;
|
|
|
|
|
|
|
|
namespace trqt
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace variant_helpers
|
|
|
|
{
|
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
template<typename T, typename std::enable_if_t<std::is_same_v<T, bool>>* = nullptr>
|
2020-07-27 04:30:58 +00:00
|
|
|
auto getValue(tr_variant const* variant)
|
|
|
|
{
|
|
|
|
std::optional<T> ret;
|
2022-02-03 13:02:11 +00:00
|
|
|
|
|
|
|
if (auto value = T{}; tr_variantGetBool(variant, &value))
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
|
|
|
ret = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
template<
|
|
|
|
typename T,
|
2022-01-24 00:53:35 +00:00
|
|
|
typename std::enable_if_t<
|
|
|
|
std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t> || std::is_same_v<T, int> || std::is_same_v<T, time_t>>* =
|
|
|
|
nullptr>
|
2020-07-27 04:30:58 +00:00
|
|
|
auto getValue(tr_variant const* variant)
|
|
|
|
{
|
|
|
|
std::optional<T> ret;
|
2022-02-03 13:02:11 +00:00
|
|
|
|
|
|
|
if (auto value = int64_t{}; tr_variantGetInt(variant, &value))
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
|
|
|
ret = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
template<typename T, typename std::enable_if_t<std::is_same_v<T, double>>* = nullptr>
|
2020-07-27 04:30:58 +00:00
|
|
|
auto getValue(tr_variant const* variant)
|
|
|
|
{
|
|
|
|
std::optional<T> ret;
|
2022-02-03 13:02:11 +00:00
|
|
|
|
|
|
|
if (auto value = T{}; tr_variantGetReal(variant, &value))
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
|
|
|
ret = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
template<typename T, typename std::enable_if_t<std::is_same_v<T, QString>>* = nullptr>
|
2020-07-27 04:30:58 +00:00
|
|
|
auto getValue(tr_variant const* variant)
|
|
|
|
{
|
|
|
|
std::optional<T> ret;
|
2021-11-28 01:58:35 +00:00
|
|
|
|
|
|
|
if (auto sv = std::string_view{}; tr_variantGetStrView(variant, &sv))
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
2021-11-14 23:28:37 +00:00
|
|
|
ret = QString::fromUtf8(std::data(sv), std::size(sv));
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-24 00:53:35 +00:00
|
|
|
template<typename T, typename std::enable_if_t<std::is_same_v<T, std::string_view>>* = nullptr>
|
2020-09-07 21:19:10 +00:00
|
|
|
auto getValue(tr_variant const* variant)
|
|
|
|
{
|
|
|
|
std::optional<T> ret;
|
2021-11-28 01:58:35 +00:00
|
|
|
|
|
|
|
if (auto sv = std::string_view{}; tr_variantGetStrView(variant, &sv))
|
2020-09-07 21:19:10 +00:00
|
|
|
{
|
2021-11-14 23:28:37 +00:00
|
|
|
ret = std::string_view(std::data(sv), std::size(sv));
|
2020-09-07 21:19:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
template<
|
|
|
|
typename C,
|
|
|
|
typename T = typename C::value_type,
|
2022-01-24 00:53:35 +00:00
|
|
|
typename std::enable_if_t<
|
|
|
|
std::is_same_v<C, QStringList> || std::is_same_v<C, QList<T>> || std::is_same_v<C, std::vector<T>>>* = nullptr>
|
2021-05-25 16:21:41 +00:00
|
|
|
auto getValue(tr_variant const* variant)
|
|
|
|
{
|
|
|
|
std::optional<C> ret;
|
|
|
|
|
|
|
|
if (tr_variantIsList(variant))
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto list = C{};
|
2021-05-25 16:21:41 +00:00
|
|
|
|
|
|
|
for (size_t i = 0, n = tr_variantListSize(variant); i < n; ++i)
|
|
|
|
{
|
|
|
|
tr_variant* const child = tr_variantListChild(const_cast<tr_variant*>(variant), i);
|
|
|
|
auto const value = getValue<T>(child);
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
list.push_back(*value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = list;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
template<typename T>
|
|
|
|
bool change(T& setme, T const& value)
|
|
|
|
{
|
|
|
|
bool const changed = setme != value;
|
|
|
|
|
|
|
|
if (changed)
|
|
|
|
{
|
|
|
|
setme = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool change(double& setme, double const& value);
|
|
|
|
bool change(Speed& setme, tr_variant const* value);
|
|
|
|
bool change(Peer& setme, tr_variant const* value);
|
|
|
|
bool change(TorrentFile& setme, tr_variant const* value);
|
2020-09-07 21:19:10 +00:00
|
|
|
bool change(TorrentHash& setme, tr_variant const* value);
|
2020-07-27 04:30:58 +00:00
|
|
|
bool change(TrackerStat& setme, tr_variant const* value);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool change(T& setme, tr_variant const* variant)
|
|
|
|
{
|
|
|
|
auto const value = getValue<T>(variant);
|
|
|
|
return value && change(setme, *value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
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-03 21:20:11 +00:00
|
|
|
bool change(std::vector<T>& setme, tr_variant const* value)
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
|
|
|
bool changed = false;
|
|
|
|
|
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-03 21:20:11 +00:00
|
|
|
auto const n = tr_variantListSize(value);
|
2020-07-27 04:30:58 +00:00
|
|
|
if (setme.size() != n)
|
|
|
|
{
|
|
|
|
setme.resize(n);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
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-03 21:20:11 +00:00
|
|
|
for (size_t i = 0; i < n; ++i)
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
|
|
|
changed = change(setme[i], tr_variantListChild(const_cast<tr_variant*>(value), i)) || changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
auto dictFind(tr_variant* dict, tr_quark key)
|
|
|
|
{
|
|
|
|
std::optional<T> ret;
|
2021-11-28 01:58:35 +00:00
|
|
|
|
|
|
|
if (auto const* child = tr_variantDictFind(dict, key); child != nullptr)
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
|
|
|
ret = getValue<T>(child);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
void variantInit(tr_variant* init_me, bool value);
|
|
|
|
void variantInit(tr_variant* init_me, int64_t value);
|
|
|
|
void variantInit(tr_variant* init_me, int value);
|
|
|
|
void variantInit(tr_variant* init_me, double value);
|
|
|
|
void variantInit(tr_variant* init_me, QByteArray const& value);
|
|
|
|
void variantInit(tr_variant* init_me, QString const& value);
|
|
|
|
void variantInit(tr_variant* init_me, std::string_view value);
|
2020-07-27 23:34:40 +00:00
|
|
|
void variantInit(tr_variant* init_me, char const* value) = delete; // use string_view
|
2020-07-27 04:30:58 +00:00
|
|
|
|
|
|
|
template<typename C, typename T = typename C::value_type>
|
|
|
|
void variantInit(tr_variant* init_me, C const& value)
|
|
|
|
{
|
|
|
|
tr_variantInitList(init_me, std::size(value));
|
|
|
|
for (auto const& item : value)
|
|
|
|
{
|
|
|
|
variantInit(tr_variantListAdd(init_me), item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void listAdd(tr_variant* list, T const& value)
|
|
|
|
{
|
|
|
|
variantInit(tr_variantListAdd(list), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void dictAdd(tr_variant* dict, tr_quark key, T const& value)
|
|
|
|
{
|
|
|
|
variantInit(tr_variantDictAdd(dict, key), value);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace variant_helpers
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
} // namespace trqt
|