2023-11-01 21:11:11 +00:00
|
|
|
// This file Copyright © Mnemosyne LLC.
|
2022-02-07 16:25:02 +00:00
|
|
|
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
|
2022-01-20 18:27:56 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
#include "VariantHelpers.h"
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <limits>
|
2022-04-08 01:50:26 +00:00
|
|
|
#include <string_view>
|
2020-08-15 15:42:51 +00:00
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
#include <QUrl>
|
|
|
|
|
|
|
|
#include "Application.h" // qApp
|
|
|
|
#include "Filters.h"
|
|
|
|
#include "Speed.h"
|
|
|
|
#include "Torrent.h"
|
|
|
|
|
|
|
|
namespace trqt::variant_helpers
|
|
|
|
{
|
|
|
|
|
|
|
|
bool change(double& setme, double const& value)
|
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
bool const changed = std::fabs(setme - value) > std::numeric_limits<double>::epsilon();
|
2020-07-27 04:30:58 +00:00
|
|
|
|
|
|
|
if (changed)
|
|
|
|
{
|
|
|
|
setme = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
bool change(Speed& setme, tr_variant const* value)
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
2023-11-13 17:13:17 +00:00
|
|
|
auto const byps = getValue<int>(value);
|
|
|
|
return byps && change(setme, Speed{ *byps, Speed::Units::Byps });
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 21:19:10 +00:00
|
|
|
bool change(TorrentHash& setme, tr_variant const* value)
|
|
|
|
{
|
|
|
|
auto const hash_string = getValue<std::string_view>(value);
|
|
|
|
return hash_string && change(setme, TorrentHash(hash_string->data()));
|
|
|
|
}
|
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
bool change(Peer& setme, tr_variant const* value)
|
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
auto changed = bool{ false };
|
2020-07-27 04:30:58 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
auto pos = size_t{ 0 };
|
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* child = nullptr;
|
2020-07-27 04:30:58 +00:00
|
|
|
while (tr_variantDictChild(const_cast<tr_variant*>(value), pos++, &key, &child))
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
#define HANDLE_KEY(key, field) \
|
|
|
|
case TR_KEY_##key: \
|
|
|
|
changed = change(setme.field, child) || changed; \
|
|
|
|
break;
|
2020-07-27 04:30:58 +00:00
|
|
|
|
|
|
|
HANDLE_KEY(address, address)
|
|
|
|
HANDLE_KEY(clientIsChoked, client_is_choked)
|
|
|
|
HANDLE_KEY(clientIsInterested, client_is_interested)
|
|
|
|
HANDLE_KEY(clientName, client_name)
|
|
|
|
HANDLE_KEY(flagStr, flags)
|
|
|
|
HANDLE_KEY(isDownloadingFrom, is_downloading_from)
|
|
|
|
HANDLE_KEY(isEncrypted, is_encrypted)
|
|
|
|
HANDLE_KEY(isIncoming, is_incoming)
|
|
|
|
HANDLE_KEY(isUploadingTo, is_uploading_to)
|
|
|
|
HANDLE_KEY(peerIsChoked, peer_is_choked)
|
|
|
|
HANDLE_KEY(peerIsInterested, peer_is_interested)
|
|
|
|
HANDLE_KEY(port, port)
|
|
|
|
HANDLE_KEY(progress, progress)
|
|
|
|
HANDLE_KEY(rateToClient, rate_to_client)
|
|
|
|
HANDLE_KEY(rateToPeer, rate_to_peer)
|
|
|
|
#undef HANDLE_KEY
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool change(TorrentFile& setme, tr_variant const* value)
|
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
auto changed = bool{ false };
|
2020-07-27 04:30:58 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
auto pos = size_t{ 0 };
|
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* child = nullptr;
|
2020-07-27 04:30:58 +00:00
|
|
|
while (tr_variantDictChild(const_cast<tr_variant*>(value), pos++, &key, &child))
|
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
#define HANDLE_KEY(key) \
|
|
|
|
case TR_KEY_##key: \
|
|
|
|
changed = change(setme.key, child) || changed; \
|
|
|
|
break;
|
2020-07-27 04:30:58 +00:00
|
|
|
|
|
|
|
HANDLE_KEY(have)
|
|
|
|
HANDLE_KEY(priority)
|
|
|
|
HANDLE_KEY(wanted)
|
|
|
|
#undef HANDLE_KEY
|
2021-08-15 09:41:48 +00:00
|
|
|
#define HANDLE_KEY(key, field) \
|
|
|
|
case TR_KEY_##key: \
|
|
|
|
changed = change(setme.field, child) || changed; \
|
|
|
|
break;
|
2020-07-27 04:30:58 +00:00
|
|
|
|
|
|
|
HANDLE_KEY(bytesCompleted, have)
|
|
|
|
HANDLE_KEY(length, size)
|
|
|
|
HANDLE_KEY(name, filename)
|
|
|
|
#undef HANDLE_KEY
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool change(TrackerStat& setme, tr_variant const* value)
|
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
bool changed = false;
|
|
|
|
bool site_changed = false;
|
2020-07-27 04:30:58 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
auto pos = size_t{ 0 };
|
|
|
|
auto key = tr_quark{};
|
|
|
|
tr_variant* child = nullptr;
|
2020-07-27 04:30:58 +00:00
|
|
|
while (tr_variantDictChild(const_cast<tr_variant*>(value), pos++, &key, &child))
|
|
|
|
{
|
|
|
|
bool field_changed = false;
|
|
|
|
|
|
|
|
switch (key)
|
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
#define HANDLE_KEY(key, field) \
|
|
|
|
case TR_KEY_##key: \
|
|
|
|
field_changed = change(setme.field, child); \
|
|
|
|
break;
|
2020-07-27 04:30:58 +00:00
|
|
|
HANDLE_KEY(announce, announce)
|
|
|
|
HANDLE_KEY(announceState, announce_state)
|
|
|
|
HANDLE_KEY(downloadCount, download_count)
|
|
|
|
HANDLE_KEY(hasAnnounced, has_announced)
|
|
|
|
HANDLE_KEY(hasScraped, has_scraped)
|
2020-08-11 18:11:55 +00:00
|
|
|
HANDLE_KEY(id, id)
|
|
|
|
HANDLE_KEY(isBackup, is_backup)
|
|
|
|
HANDLE_KEY(lastAnnouncePeerCount, last_announce_peer_count)
|
|
|
|
HANDLE_KEY(lastAnnounceResult, last_announce_result)
|
2020-07-27 04:30:58 +00:00
|
|
|
HANDLE_KEY(lastAnnounceStartTime, last_announce_start_time)
|
|
|
|
HANDLE_KEY(lastAnnounceSucceeded, last_announce_succeeded)
|
|
|
|
HANDLE_KEY(lastAnnounceTime, last_announce_time)
|
|
|
|
HANDLE_KEY(lastAnnounceTimedOut, last_announce_timed_out)
|
|
|
|
HANDLE_KEY(lastScrapeResult, last_scrape_result)
|
|
|
|
HANDLE_KEY(lastScrapeStartTime, last_scrape_start_time)
|
|
|
|
HANDLE_KEY(lastScrapeSucceeded, last_scrape_succeeded)
|
|
|
|
HANDLE_KEY(lastScrapeTime, last_scrape_time)
|
|
|
|
HANDLE_KEY(lastScrapeTimedOut, last_scrape_timed_out)
|
|
|
|
HANDLE_KEY(leecherCount, leecher_count)
|
|
|
|
HANDLE_KEY(nextAnnounceTime, next_announce_time)
|
|
|
|
HANDLE_KEY(nextScrapeTime, next_scrape_time)
|
|
|
|
HANDLE_KEY(scrapeState, scrape_state)
|
|
|
|
HANDLE_KEY(seederCount, seeder_count)
|
2022-02-12 17:30:27 +00:00
|
|
|
HANDLE_KEY(sitename, sitename)
|
2020-07-27 04:30:58 +00:00
|
|
|
HANDLE_KEY(tier, tier)
|
|
|
|
|
|
|
|
#undef HANDLE_KEY
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field_changed)
|
|
|
|
{
|
2022-02-12 17:30:27 +00:00
|
|
|
site_changed |= key == TR_KEY_announce || key == TR_KEY_sitename;
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
2022-02-12 17:30:27 +00:00
|
|
|
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (site_changed && !setme.sitename.isEmpty() && !setme.announce.isEmpty())
|
|
|
|
{
|
|
|
|
setme.announce = trApp->intern(setme.announce);
|
2023-04-23 02:11:16 +00:00
|
|
|
trApp->load_favicon(setme.announce);
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
void variantInit(tr_variant* init_me, bool value)
|
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
*init_me = value;
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void variantInit(tr_variant* init_me, int64_t value)
|
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
*init_me = value;
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void variantInit(tr_variant* init_me, int value)
|
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
*init_me = value;
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void variantInit(tr_variant* init_me, double value)
|
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
*init_me = value;
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void variantInit(tr_variant* init_me, QByteArray const& value)
|
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
*init_me = std::string_view{ value.constData(), static_cast<size_t>(value.size()) };
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void variantInit(tr_variant* init_me, QString const& value)
|
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
*init_me = value.toStdString();
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void variantInit(tr_variant* init_me, std::string_view value)
|
|
|
|
{
|
2023-11-10 21:41:32 +00:00
|
|
|
*init_me = value;
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace trqt::variant_helpers
|