mirror of
https://github.com/transmission/transmission
synced 2024-12-27 18:18:10 +00:00
be74cb6356
* 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>
231 lines
5.1 KiB
C++
231 lines
5.1 KiB
C++
/*
|
|
* This file Copyright (C) 2020 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 <optional>
|
|
#include <string_view>
|
|
#include <vector>
|
|
|
|
#include <QString>
|
|
|
|
#include <libtransmission/variant.h>
|
|
|
|
class QByteArray;
|
|
|
|
class Speed;
|
|
class TorrentHash;
|
|
struct Peer;
|
|
struct TorrentFile;
|
|
struct TrackerStat;
|
|
|
|
namespace trqt
|
|
{
|
|
|
|
namespace variant_helpers
|
|
{
|
|
|
|
template<typename T, typename std::enable_if<std::is_same_v<T, bool>>::type* = nullptr>
|
|
auto getValue(tr_variant const* variant)
|
|
{
|
|
std::optional<T> ret;
|
|
auto value = T{};
|
|
if (tr_variantGetBool(variant, &value))
|
|
{
|
|
ret = value;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
template<
|
|
typename T,
|
|
typename std::enable_if<
|
|
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>>::type* = nullptr>
|
|
auto getValue(tr_variant const* variant)
|
|
{
|
|
std::optional<T> ret;
|
|
auto value = int64_t{};
|
|
if (tr_variantGetInt(variant, &value))
|
|
{
|
|
ret = value;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<std::is_same_v<T, double>>::type* = nullptr>
|
|
auto getValue(tr_variant const* variant)
|
|
{
|
|
std::optional<T> ret;
|
|
auto value = T{};
|
|
if (tr_variantGetReal(variant, &value))
|
|
{
|
|
ret = value;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<std::is_same_v<T, QString>>::type* = nullptr>
|
|
auto getValue(tr_variant const* variant)
|
|
{
|
|
std::optional<T> ret;
|
|
char const* str;
|
|
size_t len;
|
|
if (tr_variantGetStr(variant, &str, &len))
|
|
{
|
|
ret = QString::fromUtf8(str, len);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
template<typename T, typename std::enable_if<std::is_same_v<T, std::string_view>>::type* = nullptr>
|
|
auto getValue(tr_variant const* variant)
|
|
{
|
|
std::optional<T> ret;
|
|
char const* str;
|
|
size_t len;
|
|
if (tr_variantGetStr(variant, &str, &len))
|
|
{
|
|
ret = std::string_view(str, len);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
template<
|
|
typename C,
|
|
typename T = typename C::value_type,
|
|
typename std::enable_if<
|
|
std::is_same_v<C, QStringList> || std::is_same_v<C, QList<T>> || std::is_same_v<C, std::vector<T>>>::type* = nullptr>
|
|
auto getValue(tr_variant const* variant)
|
|
{
|
|
std::optional<C> ret;
|
|
|
|
if (tr_variantIsList(variant))
|
|
{
|
|
auto list = C{};
|
|
|
|
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;
|
|
}
|
|
|
|
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);
|
|
bool change(TorrentHash& setme, tr_variant const* value);
|
|
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>
|
|
bool change(std::vector<T>& setme, tr_variant const* value)
|
|
{
|
|
bool changed = false;
|
|
|
|
auto const n = tr_variantListSize(value);
|
|
if (setme.size() != n)
|
|
{
|
|
setme.resize(n);
|
|
changed = true;
|
|
}
|
|
|
|
for (size_t i = 0; i < n; ++i)
|
|
{
|
|
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;
|
|
auto const* child = tr_variantDictFind(dict, key);
|
|
if (child != nullptr)
|
|
{
|
|
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, unsigned 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);
|
|
void variantInit(tr_variant* init_me, char const* value) = delete; // use string_view
|
|
|
|
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
|
|
|
|
} // namespace trqt
|