2023-02-11 20:49:42 +00:00
|
|
|
// This file Copyright © 2009-2023 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.
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <array>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <cassert>
|
2020-08-15 15:42:51 +00:00
|
|
|
#include <string_view>
|
2022-09-21 23:34:18 +00:00
|
|
|
#include <utility>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QByteArray>
|
2009-12-03 15:23:43 +00:00
|
|
|
#include <QClipboard>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QCoreApplication>
|
2014-12-27 20:08:10 +00:00
|
|
|
#include <QDebug>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QDesktopServices>
|
2014-12-01 19:24:07 +00:00
|
|
|
#include <QFile>
|
2014-12-22 00:02:27 +00:00
|
|
|
#include <QFileInfo>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QStyle>
|
2009-04-12 21:15:35 +00:00
|
|
|
#include <QTextStream>
|
2020-11-02 15:16:12 +00:00
|
|
|
#include <QtDebug>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-08-04 13:44:18 +00:00
|
|
|
|
|
|
|
#include <libtransmission/session-id.h>
|
2022-08-18 14:14:12 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2012-12-14 04:34:42 +00:00
|
|
|
#include <libtransmission/variant.h>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2022-08-06 19:27:37 +00:00
|
|
|
#include "Session.h"
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "AddData.h"
|
2020-05-19 23:42:17 +00:00
|
|
|
#include "CustomVariantType.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Prefs.h"
|
2016-04-19 20:41:59 +00:00
|
|
|
#include "RpcQueue.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "SessionDialog.h"
|
|
|
|
#include "Torrent.h"
|
|
|
|
#include "Utils.h"
|
2020-07-27 04:30:58 +00:00
|
|
|
#include "VariantHelpers.h"
|
|
|
|
|
|
|
|
using ::trqt::variant_helpers::dictAdd;
|
|
|
|
using ::trqt::variant_helpers::dictFind;
|
|
|
|
using ::trqt::variant_helpers::getValue;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void Session::sessionSet(tr_quark const key, QVariant const& value)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 1);
|
|
|
|
|
2023-02-10 17:58:43 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
|
|
|
|
switch (value.typeId())
|
|
|
|
#else
|
|
|
|
switch (static_cast<QMetaType::Type>(value.type()))
|
|
|
|
#endif
|
2013-02-03 19:40:20 +00:00
|
|
|
{
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::Bool:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value.toBool());
|
2013-02-03 19:40:20 +00:00
|
|
|
break;
|
|
|
|
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::Int:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value.toInt());
|
2013-02-03 19:40:20 +00:00
|
|
|
break;
|
|
|
|
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::Double:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value.toDouble());
|
2013-02-03 19:40:20 +00:00
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::QString:
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value.toString());
|
2013-02-03 19:40:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
exec("session-set", &args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::portTest()
|
|
|
|
{
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this]() { return exec("port-test", nullptr); });
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add(
|
|
|
|
[this](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
bool is_open = false;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (r.success)
|
2010-06-17 02:50:50 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const value = dictFind<bool>(r.args.get(), TR_KEY_port_is_open);
|
|
|
|
if (value)
|
|
|
|
{
|
|
|
|
is_open = *value;
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
emit portTested(is_open);
|
2017-04-19 12:04:45 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
q->run();
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void Session::copyMagnetLinkToClipboard(int torrent_id)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto constexpr MagnetLinkKey = std::string_view{ "magnetLink" };
|
2020-08-15 15:42:51 +00:00
|
|
|
auto constexpr Fields = std::array<std::string_view, 1>{ MagnetLinkKey };
|
2020-07-27 23:34:40 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, TR_KEY_ids, std::array<int, 1>{ torrent_id });
|
2020-08-15 15:42:51 +00:00
|
|
|
dictAdd(&args, TR_KEY_fields, Fields);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this, &args]() { return exec(TR_KEY_torrent_get, &args); });
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add(
|
|
|
|
[](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
tr_variant* torrents = nullptr;
|
2019-02-10 11:05:16 +00:00
|
|
|
if (!tr_variantDictFindList(r.args.get(), TR_KEY_torrents, &torrents))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_variant* const child = tr_variantListChild(torrents, 0);
|
2020-07-27 04:30:58 +00:00
|
|
|
if (child != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
auto const link = dictFind<QString>(child, TR_KEY_magnetLink);
|
|
|
|
if (link)
|
|
|
|
{
|
2020-11-02 01:13:32 +00:00
|
|
|
QApplication::clipboard()->setText(*link);
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
q->run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Session::updatePref(int key)
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (prefs_.isCore(key))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
switch (key)
|
|
|
|
{
|
|
|
|
case Prefs::ALT_SPEED_LIMIT_DOWN:
|
|
|
|
case Prefs::ALT_SPEED_LIMIT_ENABLED:
|
|
|
|
case Prefs::ALT_SPEED_LIMIT_TIME_BEGIN:
|
|
|
|
case Prefs::ALT_SPEED_LIMIT_TIME_DAY:
|
|
|
|
case Prefs::ALT_SPEED_LIMIT_TIME_ENABLED:
|
|
|
|
case Prefs::ALT_SPEED_LIMIT_TIME_END:
|
|
|
|
case Prefs::ALT_SPEED_LIMIT_UP:
|
|
|
|
case Prefs::BLOCKLIST_DATE:
|
|
|
|
case Prefs::BLOCKLIST_ENABLED:
|
|
|
|
case Prefs::BLOCKLIST_URL:
|
2022-02-20 17:54:20 +00:00
|
|
|
case Prefs::DEFAULT_TRACKERS:
|
2017-04-19 12:04:45 +00:00
|
|
|
case Prefs::DHT_ENABLED:
|
|
|
|
case Prefs::DOWNLOAD_QUEUE_ENABLED:
|
|
|
|
case Prefs::DOWNLOAD_QUEUE_SIZE:
|
|
|
|
case Prefs::DSPEED:
|
|
|
|
case Prefs::DSPEED_ENABLED:
|
|
|
|
case Prefs::IDLE_LIMIT:
|
|
|
|
case Prefs::IDLE_LIMIT_ENABLED:
|
|
|
|
case Prefs::INCOMPLETE_DIR:
|
|
|
|
case Prefs::INCOMPLETE_DIR_ENABLED:
|
|
|
|
case Prefs::LPD_ENABLED:
|
|
|
|
case Prefs::PEER_LIMIT_GLOBAL:
|
|
|
|
case Prefs::PEER_LIMIT_TORRENT:
|
|
|
|
case Prefs::PEER_PORT:
|
|
|
|
case Prefs::PEER_PORT_RANDOM_ON_START:
|
|
|
|
case Prefs::QUEUE_STALLED_MINUTES:
|
|
|
|
case Prefs::PEX_ENABLED:
|
|
|
|
case Prefs::PORT_FORWARDING:
|
|
|
|
case Prefs::RENAME_PARTIAL_FILES:
|
|
|
|
case Prefs::SCRIPT_TORRENT_DONE_ENABLED:
|
|
|
|
case Prefs::SCRIPT_TORRENT_DONE_FILENAME:
|
2022-02-14 04:07:12 +00:00
|
|
|
case Prefs::SCRIPT_TORRENT_DONE_SEEDING_ENABLED:
|
|
|
|
case Prefs::SCRIPT_TORRENT_DONE_SEEDING_FILENAME:
|
2017-04-19 12:04:45 +00:00
|
|
|
case Prefs::START:
|
|
|
|
case Prefs::TRASH_ORIGINAL:
|
|
|
|
case Prefs::USPEED:
|
|
|
|
case Prefs::USPEED_ENABLED:
|
|
|
|
case Prefs::UTP_ENABLED:
|
2020-05-27 21:53:12 +00:00
|
|
|
sessionSet(prefs_.getKey(key), prefs_.variant(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Prefs::DOWNLOAD_DIR:
|
2020-05-27 21:53:12 +00:00
|
|
|
sessionSet(prefs_.getKey(key), prefs_.variant(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
/* this will change the 'freespace' argument, so refresh */
|
|
|
|
refreshSessionInfo();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Prefs::RATIO:
|
2020-05-27 21:53:12 +00:00
|
|
|
sessionSet(TR_KEY_seedRatioLimit, prefs_.variant(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Prefs::RATIO_ENABLED:
|
2020-05-27 21:53:12 +00:00
|
|
|
sessionSet(TR_KEY_seedRatioLimited, prefs_.variant(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Prefs::ENCRYPTION:
|
2022-02-03 13:02:11 +00:00
|
|
|
switch (int const i = prefs_.variant(key).toInt(); i)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-03 13:02:11 +00:00
|
|
|
case 0:
|
|
|
|
sessionSet(prefs_.getKey(key), QStringLiteral("tolerated"));
|
|
|
|
break;
|
2022-02-01 17:30:51 +00:00
|
|
|
|
2022-02-03 13:02:11 +00:00
|
|
|
case 1:
|
|
|
|
sessionSet(prefs_.getKey(key), QStringLiteral("preferred"));
|
|
|
|
break;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-03 13:02:11 +00:00
|
|
|
case 2:
|
|
|
|
sessionSet(prefs_.getKey(key), QStringLiteral("required"));
|
2010-06-17 02:50:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-02-03 13:02:11 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case Prefs::RPC_AUTH_REQUIRED:
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionSetRPCPasswordEnabled(session_, prefs_.getBool(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case Prefs::RPC_ENABLED:
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionSetRPCEnabled(session_, prefs_.getBool(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case Prefs::RPC_PASSWORD:
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionSetRPCPassword(session_, prefs_.getString(key).toUtf8().constData());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case Prefs::RPC_PORT:
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-04-21 15:58:13 +00:00
|
|
|
tr_sessionSetRPCPort(session_, static_cast<uint16_t>(prefs_.getInt(key)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Prefs::RPC_USERNAME:
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionSetRPCUsername(session_, prefs_.getString(key).toUtf8().constData());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Prefs::RPC_WHITELIST_ENABLED:
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionSetRPCWhitelistEnabled(session_, prefs_.getBool(key));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Prefs::RPC_WHITELIST:
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionSetRPCWhitelist(session_, prefs_.getString(key).toUtf8().constData());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2020-11-02 15:16:12 +00:00
|
|
|
qWarning() << "unhandled pref:" << key;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
Session::Session(QString config_dir, Prefs& prefs)
|
|
|
|
: config_dir_(std::move(config_dir))
|
|
|
|
, prefs_(prefs)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(&prefs_, &Prefs::changed, this, &Session::updatePref);
|
|
|
|
connect(&rpc_, &RpcClient::httpAuthenticationRequired, this, &Session::httpAuthenticationRequired);
|
|
|
|
connect(&rpc_, &RpcClient::dataReadProgress, this, &Session::dataReadProgress);
|
|
|
|
connect(&rpc_, &RpcClient::dataSendProgress, this, &Session::dataSendProgress);
|
|
|
|
connect(&rpc_, &RpcClient::networkResponse, this, &Session::networkResponse);
|
2020-08-27 01:42:41 +00:00
|
|
|
|
|
|
|
duplicates_timer_.setSingleShot(true);
|
|
|
|
connect(&duplicates_timer_, &QTimer::timeout, this, &Session::onDuplicatesTimer);
|
2009-05-03 17:37:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
Session::~Session()
|
2009-05-03 17:37:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
stop();
|
2009-05-03 17:37:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::stop()
|
2009-05-03 17:37:39 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
rpc_.stop();
|
2009-05-03 17:37:39 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionClose(session_);
|
|
|
|
session_ = nullptr;
|
2009-05-03 17:37:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::restart()
|
2009-05-03 17:37:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
stop();
|
|
|
|
start();
|
2009-05-03 17:37:39 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::start()
|
2009-05-03 17:37:39 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (prefs_.get<bool>(Prefs::SESSION_IS_REMOTE))
|
2009-05-03 17:37:39 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QUrl url;
|
2023-01-26 06:24:04 +00:00
|
|
|
if (prefs_.get<bool>(Prefs::SESSION_REMOTE_HTTPS))
|
|
|
|
{
|
|
|
|
url.setScheme(QStringLiteral("https"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
url.setScheme(QStringLiteral("http"));
|
|
|
|
}
|
2020-05-27 21:53:12 +00:00
|
|
|
url.setHost(prefs_.get<QString>(Prefs::SESSION_REMOTE_HOST));
|
|
|
|
url.setPort(prefs_.get<int>(Prefs::SESSION_REMOTE_PORT));
|
2020-05-29 17:40:07 +00:00
|
|
|
url.setPath(QStringLiteral("/transmission/rpc"));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (prefs_.get<bool>(Prefs::SESSION_REMOTE_AUTH))
|
2010-08-04 13:56:52 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
url.setUserName(prefs_.get<QString>(Prefs::SESSION_REMOTE_USERNAME));
|
|
|
|
url.setPassword(prefs_.get<QString>(Prefs::SESSION_REMOTE_PASSWORD));
|
2009-05-03 17:37:39 +00:00
|
|
|
}
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
rpc_.start(url);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant settings;
|
|
|
|
tr_variantInitDict(&settings, 0);
|
2020-05-27 21:53:12 +00:00
|
|
|
tr_sessionLoadSettings(&settings, config_dir_.toUtf8().constData(), "qt");
|
|
|
|
session_ = tr_sessionInit(config_dir_.toUtf8().constData(), true, &settings);
|
2022-08-18 01:25:42 +00:00
|
|
|
tr_variantClear(&settings);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
rpc_.start(session_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
auto* const ctor = tr_ctorNew(session_);
|
2022-08-18 14:14:12 +00:00
|
|
|
tr_sessionLoadTorrents(session_, ctor);
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_ctorFree(ctor);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit sourceChanged();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool Session::isServer() const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return session_ != nullptr;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
bool Session::isLocal() const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!session_id_.isEmpty())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return is_definitely_local_session_;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
return rpc_.isLocal();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Session::addOptionalIds(tr_variant* args_dict, torrent_ids_t const& torrent_ids) const
|
2009-04-12 21:15:35 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto constexpr RecentlyActiveKey = std::string_view{ "recently-active" };
|
2020-07-27 23:34:40 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (&torrent_ids == &RecentlyActiveIDs)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
dictAdd(args_dict, TR_KEY_ids, RecentlyActiveKey);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2022-09-08 23:26:18 +00:00
|
|
|
else if (!std::empty(torrent_ids))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
dictAdd(args_dict, TR_KEY_ids, torrent_ids);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-12 21:15:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 16:56:23 +00:00
|
|
|
Session::Tag Session::torrentSetImpl(tr_variant* args)
|
|
|
|
{
|
|
|
|
auto* const q = new RpcQueue();
|
|
|
|
auto const tag = q->tag();
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this, args]() { return rpc_.exec(TR_KEY_torrent_set, args); });
|
|
|
|
q->add([this, tag]() { emit sessionCalled(tag); });
|
2020-07-29 16:56:23 +00:00
|
|
|
q->setTolerateErrors();
|
|
|
|
q->run();
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
Session::Tag Session::torrentSet(torrent_ids_t const& torrent_ids, tr_quark const key, double value)
|
2009-04-12 21:15:35 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value);
|
2020-07-29 16:56:23 +00:00
|
|
|
return torrentSetImpl(&args);
|
2009-04-12 21:15:35 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
Session::Tag Session::torrentSet(torrent_ids_t const& torrent_ids, tr_quark const key, int value)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value);
|
2020-07-29 16:56:23 +00:00
|
|
|
return torrentSetImpl(&args);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
Session::Tag Session::torrentSet(torrent_ids_t const& torrent_ids, tr_quark const key, bool value)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value);
|
2020-07-29 16:56:23 +00:00
|
|
|
return torrentSetImpl(&args);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
Session::Tag Session::torrentSet(torrent_ids_t const& torrent_ids, tr_quark const key, QString const& value)
|
2022-02-18 17:52:01 +00:00
|
|
|
{
|
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2022-02-18 17:52:01 +00:00
|
|
|
dictAdd(&args, key, value);
|
|
|
|
return torrentSetImpl(&args);
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
Session::Tag Session::torrentSet(torrent_ids_t const& torrent_ids, tr_quark const key, QStringList const& value)
|
2010-06-30 05:55:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value);
|
2020-07-29 16:56:23 +00:00
|
|
|
return torrentSetImpl(&args);
|
2010-06-30 05:55:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
Session::Tag Session::torrentSet(torrent_ids_t const& torrent_ids, tr_quark const key, QList<int> const& value)
|
2010-09-14 06:23:48 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, key, value);
|
2020-07-29 16:56:23 +00:00
|
|
|
return torrentSetImpl(&args);
|
2010-09-14 06:23:48 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Session::torrentSetLocation(torrent_ids_t const& torrent_ids, QString const& path, bool do_move)
|
2013-01-20 01:31:58 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 3);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
|
|
|
dictAdd(&args, TR_KEY_location, path);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, TR_KEY_move, do_move);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
exec(TR_KEY_torrent_set_location, &args);
|
|
|
|
}
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Session::torrentRenamePath(torrent_ids_t const& torrent_ids, QString const& oldpath, QString const& newname)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, TR_KEY_path, oldpath);
|
|
|
|
dictAdd(&args, TR_KEY_name, newname);
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add(
|
|
|
|
[this, &args]() { return exec("torrent-rename-path", &args); },
|
fix: gcc warnings in libtransmission/ and utils/ (#843)
* fix: __attribute__(__printf__) warnings
* fix: implicit fallthrough warning
* fixup! fix: implicit fallthrough warning
* fix: disable warnings for 3rd party code
Since we want to leave upstream code as-is
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: disable warnings for 3rd party code
* silence spurious alignment warning
Xrefs
Discussion: https://stackoverflow.com/a/35554349
Macro inspiration: https://pagure.io/SSSD/sssd/blob/90ac46f71068d131391492360a8553bdd005b5a7/f/src/util/util_safealign.h#_35
* fixup! fix: disable warnings for 3rd party code
* fixup! fix: implicit fallthrough warning
* make uncrustify happy
* remove uncrustify-test.sh
that's probably off-topic for this PR
* fixup! fix: __attribute__(__printf__) warnings
* Update libtransmission/CMakeLists.txt
Co-Authored-By: ckerr <ckerr@github.com>
* fixup! silence spurious alignment warning
* use -w for DISABLE_WARNINGS in Clang
* refactor: fix libtransmission deprecation warnings
* fix: pthread_create's start_routine's return value
This was defined as `void` on non-Windows but should have been `void*`
* chore: uncrustify
* fix: add DISABLE_WARNINGS option for SunPro Studio
* fix "unused in lambda capture" warnings by clang++
* fix 'increases required alignment' warning
Caused from storing int16_t's in a char array.
* fix net.c 'increases required alignment' warning
The code passes in a `struct sockaddr_storage*` which is a padded struct
large enough for the necessary alignment. Unfortunately it was recast as
a `struct sockaddr*` which has less padding and a smaller alignment. The
warning occrred because of these differing alignments.
* make building quieter so warnings are more visible
* fixup! fix 'increases required alignment' warning
* Fix -Wcast-function-type warnings in GTK+ app code
https://gitlab.gnome.org/GNOME/gnome-terminal/issues/96 talks about both
the issue and its solution.
GCC 8's -Wcast-function-type, enabled by -Wextra, is problematic in glib
applications because it's idiomatic there to recast function signatures,
e.g. `g_slist_free(list, (GFunc)g_free, NULL);`.
Disabling the warning with pragmas causes "unrecognized pragma" warnings
on clang and older versions of gcc, and disabling the warning could miss
actual bugs. GCC defines `void (*)(void)` as a special case that matches
anything so we can silence warnings by double-casting through GCallback.
In the previous example, the warning is silenced by changing the code to
read `g_slist_free(list, (GFunc)(GCallback)g_free, NULL);`).
* fixup! fix "unused in lambda capture" warnings by clang++
* fixup! fix "unused in lambda capture" warnings by clang++
* fix two more libtransmission compiler warnings
* fix: in watchdir, use TR_ENABLE_ASSERTS not NDEBUG
2019-11-06 17:27:03 +00:00
|
|
|
[](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-11-15 16:25:12 +00:00
|
|
|
auto const path = dictFind<QString>(r.args.get(), TR_KEY_path).value_or(QStringLiteral("(unknown)"));
|
|
|
|
auto const name = dictFind<QString>(r.args.get(), TR_KEY_name).value_or(QStringLiteral("(unknown)"));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
auto* d = new QMessageBox(
|
|
|
|
QMessageBox::Information,
|
|
|
|
tr("Error Renaming Path"),
|
|
|
|
tr(R"(<p><b>Unable to rename "%1" as "%2": %3.</b></p><p>Please correct the errors and try again.</p>)")
|
|
|
|
.arg(path)
|
|
|
|
.arg(name)
|
|
|
|
.arg(r.result),
|
|
|
|
QMessageBox::Close,
|
2020-11-02 01:13:32 +00:00
|
|
|
QApplication::activeWindow());
|
|
|
|
QObject::connect(d, &QMessageBox::rejected, d, &QMessageBox::deleteLater);
|
2017-04-19 12:04:45 +00:00
|
|
|
d->show();
|
|
|
|
});
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
q->add([this, torrent_ids](RpcResponse const& /*r*/) { refreshTorrents(torrent_ids, TorrentProperties::Rename); });
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
q->run();
|
2013-01-20 01:31:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
std::vector<std::string_view> const& Session::getKeyNames(TorrentProperties props)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
std::vector<std::string_view>& names = names_[props];
|
2020-07-27 23:34:40 +00:00
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
if (names.empty())
|
2020-07-27 04:30:58 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
// unchanging fields needed by the main window
|
2020-10-13 15:33:56 +00:00
|
|
|
static auto constexpr MainInfoKeys = std::array<tr_quark, 8>{
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_addedDate, //
|
|
|
|
TR_KEY_downloadDir, //
|
|
|
|
TR_KEY_file_count, //
|
|
|
|
TR_KEY_hashString, //
|
|
|
|
TR_KEY_name, //
|
|
|
|
TR_KEY_primary_mime_type, //
|
|
|
|
TR_KEY_totalSize, //
|
|
|
|
TR_KEY_trackers, //
|
2020-08-15 15:42:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// changing fields needed by the main window
|
|
|
|
static auto constexpr MainStatKeys = std::array<tr_quark, 25>{
|
|
|
|
TR_KEY_downloadedEver,
|
|
|
|
TR_KEY_editDate,
|
|
|
|
TR_KEY_error,
|
|
|
|
TR_KEY_errorString,
|
|
|
|
TR_KEY_eta,
|
|
|
|
TR_KEY_haveUnchecked,
|
|
|
|
TR_KEY_haveValid,
|
|
|
|
TR_KEY_isFinished,
|
|
|
|
TR_KEY_leftUntilDone,
|
|
|
|
TR_KEY_manualAnnounceTime,
|
|
|
|
TR_KEY_metadataPercentComplete,
|
|
|
|
TR_KEY_peersConnected,
|
|
|
|
TR_KEY_peersGettingFromUs,
|
|
|
|
TR_KEY_peersSendingToUs,
|
|
|
|
TR_KEY_percentDone,
|
|
|
|
TR_KEY_queuePosition,
|
|
|
|
TR_KEY_rateDownload,
|
|
|
|
TR_KEY_rateUpload,
|
|
|
|
TR_KEY_recheckProgress,
|
|
|
|
TR_KEY_seedRatioLimit,
|
|
|
|
TR_KEY_seedRatioMode,
|
|
|
|
TR_KEY_sizeWhenDone,
|
|
|
|
TR_KEY_status,
|
|
|
|
TR_KEY_uploadedEver,
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_webseedsSendingToUs,
|
2020-08-15 15:42:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// unchanging fields needed by the details dialog
|
2022-02-18 17:52:01 +00:00
|
|
|
static auto constexpr DetailInfoKeys = std::array<tr_quark, 9>{
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_comment, //
|
|
|
|
TR_KEY_creator, //
|
|
|
|
TR_KEY_dateCreated, //
|
|
|
|
TR_KEY_files, //
|
|
|
|
TR_KEY_isPrivate, //
|
|
|
|
TR_KEY_pieceCount, //
|
|
|
|
TR_KEY_pieceSize, //
|
2022-02-18 17:52:01 +00:00
|
|
|
TR_KEY_trackerList, //
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_trackers, //
|
2020-08-15 15:42:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// changing fields needed by the details dialog
|
2021-10-24 04:45:10 +00:00
|
|
|
static auto constexpr DetailStatKeys = std::array<tr_quark, 18>{
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_activityDate, //
|
|
|
|
TR_KEY_bandwidthPriority, //
|
|
|
|
TR_KEY_corruptEver, //
|
|
|
|
TR_KEY_desiredAvailable, //
|
|
|
|
TR_KEY_downloadedEver, //
|
|
|
|
TR_KEY_downloadLimit, //
|
|
|
|
TR_KEY_downloadLimited, //
|
|
|
|
TR_KEY_fileStats, //
|
2021-10-24 04:45:10 +00:00
|
|
|
TR_KEY_haveUnchecked, //
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_honorsSessionLimits, //
|
|
|
|
TR_KEY_peer_limit, //
|
|
|
|
TR_KEY_peers, //
|
|
|
|
TR_KEY_seedIdleLimit, //
|
|
|
|
TR_KEY_seedIdleMode, //
|
|
|
|
TR_KEY_startDate, //
|
|
|
|
TR_KEY_trackerStats, //
|
|
|
|
TR_KEY_uploadLimit, //
|
|
|
|
TR_KEY_uploadLimited, //
|
2020-08-15 15:42:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// keys needed after renaming a torrent
|
|
|
|
static auto constexpr RenameKeys = std::array<tr_quark, 3>{
|
|
|
|
TR_KEY_fileStats,
|
|
|
|
TR_KEY_files,
|
2021-08-15 09:41:48 +00:00
|
|
|
TR_KEY_name,
|
2020-08-15 15:42:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
auto const append = [&names](tr_quark key)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-08-28 21:17:07 +00:00
|
|
|
names.emplace_back(tr_quark_get_string_view(key));
|
2021-08-15 09:41:48 +00:00
|
|
|
};
|
2020-08-15 15:42:51 +00:00
|
|
|
|
|
|
|
switch (props)
|
|
|
|
{
|
|
|
|
case TorrentProperties::DetailInfo:
|
|
|
|
std::for_each(DetailInfoKeys.begin(), DetailInfoKeys.end(), append);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TorrentProperties::DetailStat:
|
|
|
|
std::for_each(DetailStatKeys.begin(), DetailStatKeys.end(), append);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TorrentProperties::MainAll:
|
|
|
|
std::for_each(MainInfoKeys.begin(), MainInfoKeys.end(), append);
|
|
|
|
std::for_each(MainStatKeys.begin(), MainStatKeys.end(), append);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TorrentProperties::MainInfo:
|
|
|
|
std::for_each(MainInfoKeys.begin(), MainInfoKeys.end(), append);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TorrentProperties::MainStats:
|
|
|
|
std::for_each(MainStatKeys.begin(), MainStatKeys.end(), append);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TorrentProperties::Rename:
|
|
|
|
std::for_each(RenameKeys.begin(), RenameKeys.end(), append);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be in every torrent req
|
|
|
|
append(TR_KEY_id);
|
|
|
|
|
|
|
|
// sort and remove dupes
|
|
|
|
std::sort(names.begin(), names.end());
|
|
|
|
names.erase(std::unique(names.begin(), names.end()), names.end());
|
2020-07-27 04:30:58 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Session::refreshTorrents(torrent_ids_t const& torrent_ids, TorrentProperties props)
|
2020-08-15 15:42:51 +00:00
|
|
|
{
|
|
|
|
auto constexpr Table = std::string_view{ "table" };
|
|
|
|
|
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 3);
|
|
|
|
dictAdd(&args, TR_KEY_format, Table);
|
|
|
|
dictAdd(&args, TR_KEY_fields, getKeyNames(props));
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this, &args]() { return exec(TR_KEY_torrent_get, &args); });
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
bool const all_torrents = std::empty(torrent_ids);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add(
|
|
|
|
[this, all_torrents](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
tr_variant* torrents = nullptr;
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (tr_variantDictFindList(r.args.get(), TR_KEY_torrents, &torrents))
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
emit torrentsUpdated(torrents, all_torrents);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tr_variantDictFindList(r.args.get(), TR_KEY_removed, &torrents))
|
|
|
|
{
|
|
|
|
emit torrentsRemoved(torrents);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
q->run();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::refreshDetailInfo(torrent_ids_t const& ids)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
refreshTorrents(ids, TorrentProperties::DetailInfo);
|
2019-11-09 14:44:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::refreshExtraStats(torrent_ids_t const& ids)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
refreshTorrents(ids, TorrentProperties::DetailStat);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Session::sendTorrentRequest(std::string_view request, torrent_ids_t const& torrent_ids)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 1);
|
2022-09-08 23:26:18 +00:00
|
|
|
addOptionalIds(&args, torrent_ids);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this, request, &args]() { return exec(request, &args); });
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
q->add([this, torrent_ids]() { refreshTorrents(torrent_ids, TorrentProperties::MainStats); });
|
2012-12-22 20:35:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
q->run();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::pauseTorrents(torrent_ids_t const& ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest("torrent-stop", ids);
|
|
|
|
}
|
2017-04-20 16:02:19 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::startTorrents(torrent_ids_t const& ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest("torrent-start", ids);
|
|
|
|
}
|
2017-04-20 16:02:19 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::startTorrentsNow(torrent_ids_t const& ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest("torrent-start-now", ids);
|
|
|
|
}
|
2017-04-20 16:02:19 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::queueMoveTop(torrent_ids_t const& ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest("queue-move-top", ids);
|
|
|
|
}
|
2017-04-20 16:02:19 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::queueMoveUp(torrent_ids_t const& ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest("queue-move-up", ids);
|
|
|
|
}
|
2017-04-20 16:02:19 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::queueMoveDown(torrent_ids_t const& ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest("queue-move-down", ids);
|
|
|
|
}
|
2017-04-20 16:02:19 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::queueMoveBottom(torrent_ids_t const& ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest("queue-move-bottom", ids);
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::refreshActiveTorrents()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
// If this object is passed as "ids" (compared by address), then recently active torrents are queried.
|
|
|
|
refreshTorrents(RecentlyActiveIDs, TorrentProperties::MainStats);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::refreshAllTorrents()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
// if an empty ids object is used, all torrents are queried.
|
|
|
|
torrent_ids_t const ids = {};
|
|
|
|
refreshTorrents(ids, TorrentProperties::MainStats);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::initTorrents(torrent_ids_t const& ids)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
refreshTorrents(ids, TorrentProperties::MainAll);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::refreshSessionStats()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this]() { return exec("session-stats", nullptr); });
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this](RpcResponse const& r) { updateStats(r.args.get()); });
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
q->run();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::refreshSessionInfo()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this]() { return exec("session-get", nullptr); });
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this](RpcResponse const& r) { updateInfo(r.args.get()); });
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
q->run();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::updateBlocklist()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add([this]() { return exec("blocklist-update", nullptr); });
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add(
|
|
|
|
[this](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto const size = dictFind<int>(r.args.get(), TR_KEY_blocklist_size); size)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
setBlocklistSize(*size);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
q->run();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
RpcResponseFuture Session::exec(tr_quark method, tr_variant* args)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return rpc_.exec(method, args);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 04:30:58 +00:00
|
|
|
RpcResponseFuture Session::exec(std::string_view method, tr_variant* args)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return rpc_.exec(method, args);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Session::updateStats(tr_variant* args_dict, tr_session_stats* stats)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const value = dictFind<uint64_t>(args_dict, TR_KEY_uploadedBytes); value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
stats->uploadedBytes = *value;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const value = dictFind<uint64_t>(args_dict, TR_KEY_downloadedBytes); value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
stats->downloadedBytes = *value;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const value = dictFind<uint64_t>(args_dict, TR_KEY_filesAdded); value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
stats->filesAdded = *value;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const value = dictFind<uint64_t>(args_dict, TR_KEY_sessionCount); value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
stats->sessionCount = *value;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const value = dictFind<uint64_t>(args_dict, TR_KEY_secondsActive); value)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
stats->secondsActive = *value;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
stats->ratio = static_cast<float>(tr_getRatio(stats->uploadedBytes, stats->downloadedBytes));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
void Session::updateStats(tr_variant* dict)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
if (tr_variant* var = nullptr; tr_variantDictFindDict(dict, TR_KEY_current_stats, &var))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
updateStats(var, &stats_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
if (tr_variant* var = nullptr; tr_variantDictFindDict(dict, TR_KEY_cumulative_stats, &var))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-02-08 03:56:04 +00:00
|
|
|
updateStats(var, &cumulative_stats_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit statsUpdated();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Session::updateInfo(tr_variant* args_dict)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-10-31 18:56:12 +00:00
|
|
|
disconnect(&prefs_, &Prefs::changed, this, &Session::updatePref);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
for (int i = Prefs::FIRST_CORE_PREF; i <= Prefs::LAST_CORE_PREF; ++i)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
tr_variant const* b(tr_variantDictFind(args_dict, prefs_.getKey(i)));
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (b == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (i == Prefs::ENCRYPTION)
|
2010-06-17 02:50:50 +00:00
|
|
|
{
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto const str = getValue<QString>(b); str)
|
2010-06-17 02:50:50 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
if (*str == QStringLiteral("required"))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_.set(i, 2);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-07-27 04:30:58 +00:00
|
|
|
else if (*str == QStringLiteral("preferred"))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_.set(i, 1);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2020-07-27 04:30:58 +00:00
|
|
|
else if (*str == QStringLiteral("tolerated"))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_.set(i, 0);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-06-17 02:50:50 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
continue;
|
2010-06-17 02:50:50 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
switch (prefs_.type(i))
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::Int:
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto const value = getValue<int>(b); value)
|
2013-02-03 19:40:20 +00:00
|
|
|
{
|
2022-02-03 13:02:11 +00:00
|
|
|
prefs_.set(i, *value);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-03 13:02:11 +00:00
|
|
|
break;
|
|
|
|
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::Double:
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto const value = getValue<double>(b); value)
|
2013-02-03 19:40:20 +00:00
|
|
|
{
|
2022-02-03 13:02:11 +00:00
|
|
|
prefs_.set(i, *value);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-03 13:02:11 +00:00
|
|
|
break;
|
|
|
|
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::Bool:
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto const value = getValue<bool>(b); value)
|
2013-02-03 19:40:20 +00:00
|
|
|
{
|
2022-02-03 13:02:11 +00:00
|
|
|
prefs_.set(i, *value);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-03 13:02:11 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case CustomVariantType::FilterModeType:
|
|
|
|
case CustomVariantType::SortModeType:
|
2023-02-10 17:58:43 +00:00
|
|
|
case QMetaType::QString:
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto const value = getValue<QString>(b); value)
|
2013-02-03 19:40:20 +00:00
|
|
|
{
|
2022-02-03 13:02:11 +00:00
|
|
|
prefs_.set(i, *value);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2022-02-03 13:02:11 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2013-02-03 19:40:20 +00:00
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const b = dictFind<bool>(args_dict, TR_KEY_seedRatioLimited); b)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
prefs_.set(Prefs::RATIO_ENABLED, *b);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const x = dictFind<double>(args_dict, TR_KEY_seedRatioLimit); x)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
prefs_.set(Prefs::RATIO, *x);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
/* Use the C API to get settings that, for security reasons, aren't supported by RPC */
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ != nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_.set(Prefs::RPC_ENABLED, tr_sessionIsRPCEnabled(session_));
|
|
|
|
prefs_.set(Prefs::RPC_AUTH_REQUIRED, tr_sessionIsRPCPasswordEnabled(session_));
|
|
|
|
prefs_.set(Prefs::RPC_PASSWORD, QString::fromUtf8(tr_sessionGetRPCPassword(session_)));
|
|
|
|
prefs_.set(Prefs::RPC_PORT, tr_sessionGetRPCPort(session_));
|
|
|
|
prefs_.set(Prefs::RPC_USERNAME, QString::fromUtf8(tr_sessionGetRPCUsername(session_)));
|
|
|
|
prefs_.set(Prefs::RPC_WHITELIST_ENABLED, tr_sessionGetRPCWhitelistEnabled(session_));
|
|
|
|
prefs_.set(Prefs::RPC_WHITELIST, QString::fromUtf8(tr_sessionGetRPCWhitelist(session_)));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const size = dictFind<int>(args_dict, TR_KEY_blocklist_size); size && *size != blocklistSize())
|
2016-10-03 18:26:28 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
setBlocklistSize(*size);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const str = dictFind<QString>(args_dict, TR_KEY_version); str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
session_version_ = *str;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (auto const str = dictFind<QString>(args_dict, TR_KEY_session_id); str)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-07-27 04:30:58 +00:00
|
|
|
session_id_ = *str;
|
2023-05-06 04:11:05 +00:00
|
|
|
is_definitely_local_session_ = tr_session_id::is_local(session_id_.toUtf8().constData());
|
2016-10-03 18:26:28 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2016-10-03 18:26:28 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
session_id_.clear();
|
2016-10-03 18:26:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(&prefs_, &Prefs::changed, this, &Session::updatePref);
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit sessionUpdated();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Session::setBlocklistSize(int64_t i)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
blocklist_size_ = i;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
emit blocklistUpdated(i);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Session::addTorrent(AddData add_me, tr_variant* args_dict, bool trash_original)
|
2009-10-05 19:53:38 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
assert(tr_variantDictFind(args_dict, TR_KEY_filename) == nullptr);
|
|
|
|
assert(tr_variantDictFind(args_dict, TR_KEY_metainfo) == nullptr);
|
2014-12-01 19:24:07 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
if (tr_variantDictFind(args_dict, TR_KEY_paused) == nullptr)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
dictAdd(args_dict, TR_KEY_paused, !prefs_.getBool(Prefs::START));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
switch (add_me.type)
|
2013-02-03 19:40:20 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case AddData::MAGNET:
|
2022-09-08 23:26:18 +00:00
|
|
|
dictAdd(args_dict, TR_KEY_filename, add_me.magnet);
|
2013-02-03 19:40:20 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case AddData::URL:
|
2022-09-08 23:26:18 +00:00
|
|
|
dictAdd(args_dict, TR_KEY_filename, add_me.url.toString());
|
2013-02-03 19:40:20 +00:00
|
|
|
break;
|
|
|
|
|
2021-10-06 17:24:02 +00:00
|
|
|
case AddData::FILENAME:
|
|
|
|
[[fallthrough]];
|
2017-04-19 12:04:45 +00:00
|
|
|
case AddData::METAINFO:
|
2022-09-08 23:26:18 +00:00
|
|
|
dictAdd(args_dict, TR_KEY_metainfo, add_me.toBase64());
|
2020-07-27 04:30:58 +00:00
|
|
|
break;
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2020-05-27 21:53:12 +00:00
|
|
|
qWarning() << "Unhandled AddData type: " << add_me.type;
|
2013-02-03 19:40:20 +00:00
|
|
|
break;
|
2010-04-27 03:10:32 +00:00
|
|
|
}
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* q = new RpcQueue();
|
2016-04-19 20:41:59 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add(
|
2022-09-08 23:26:18 +00:00
|
|
|
[this, args_dict]() { return exec("torrent-add", args_dict); },
|
2020-05-27 21:53:12 +00:00
|
|
|
[add_me](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto* d = new QMessageBox(
|
|
|
|
QMessageBox::Warning,
|
|
|
|
tr("Error Adding Torrent"),
|
|
|
|
QStringLiteral("<p><b>%1</b></p><p>%2</p>").arg(r.result).arg(add_me.readableName()),
|
|
|
|
QMessageBox::Close,
|
2020-11-02 01:13:32 +00:00
|
|
|
QApplication::activeWindow());
|
|
|
|
QObject::connect(d, &QMessageBox::rejected, d, &QMessageBox::deleteLater);
|
2017-04-19 12:04:45 +00:00
|
|
|
d->show();
|
|
|
|
});
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
q->add(
|
2023-05-08 21:21:54 +00:00
|
|
|
[this, add_me, trash_original](RpcResponse const& r)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-08-27 01:42:41 +00:00
|
|
|
bool session_has_torrent = false;
|
2019-02-10 11:05:16 +00:00
|
|
|
|
2022-02-08 03:56:04 +00:00
|
|
|
if (tr_variant* dup = nullptr; tr_variantDictFindDict(r.args.get(), TR_KEY_torrent_added, &dup))
|
2019-02-10 11:05:16 +00:00
|
|
|
{
|
2020-08-27 01:42:41 +00:00
|
|
|
session_has_torrent = true;
|
|
|
|
}
|
|
|
|
else if (tr_variantDictFindDict(r.args.get(), TR_KEY_torrent_duplicate, &dup))
|
|
|
|
{
|
|
|
|
session_has_torrent = true;
|
|
|
|
|
|
|
|
auto const hash = dictFind<QString>(dup, TR_KEY_hashString);
|
|
|
|
if (hash)
|
|
|
|
{
|
|
|
|
duplicates_.try_emplace(add_me.readableShortName(), *hash);
|
|
|
|
duplicates_timer_.start(1000);
|
|
|
|
}
|
2019-02-10 11:05:16 +00:00
|
|
|
}
|
|
|
|
|
2023-05-08 21:21:54 +00:00
|
|
|
if (auto const& filename = add_me.filename;
|
|
|
|
session_has_torrent && !filename.isEmpty() && add_me.type == AddData::FILENAME)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2023-05-08 21:21:54 +00:00
|
|
|
auto file = QFile{ filename };
|
|
|
|
|
|
|
|
if (trash_original)
|
|
|
|
{
|
|
|
|
file.setPermissions(QFile::ReadOwner | QFile::WriteOwner);
|
|
|
|
file.remove();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
file.rename(QStringLiteral("%1.added").arg(filename));
|
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-07-15 05:46:31 +00:00
|
|
|
});
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
q->run();
|
2014-12-01 19:24:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-27 01:42:41 +00:00
|
|
|
void Session::onDuplicatesTimer()
|
|
|
|
{
|
|
|
|
decltype(duplicates_) duplicates;
|
|
|
|
duplicates.swap(duplicates_);
|
|
|
|
|
|
|
|
QStringList lines;
|
2022-01-24 00:53:35 +00:00
|
|
|
for (auto [dupe, original] : duplicates)
|
2020-08-27 01:42:41 +00:00
|
|
|
{
|
2022-01-24 00:53:35 +00:00
|
|
|
lines.push_back(tr("%1 (copy of %2)").arg(dupe).arg(original.left(7)));
|
2020-08-27 01:42:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!lines.empty())
|
|
|
|
{
|
|
|
|
lines.sort(Qt::CaseInsensitive);
|
2020-09-07 15:20:48 +00:00
|
|
|
auto const title = tr("Duplicate Torrent(s)", "", lines.size());
|
|
|
|
auto const detail = lines.join(QStringLiteral("\n"));
|
|
|
|
auto const detail_text = tr("Unable to add %n duplicate torrent(s)", "", lines.size());
|
|
|
|
auto const use_detail = lines.size() > 1;
|
|
|
|
auto const text = use_detail ? detail_text : detail;
|
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
auto* d = new QMessageBox(QMessageBox::Warning, title, text, QMessageBox::Close, QApplication::activeWindow());
|
2020-09-07 15:20:48 +00:00
|
|
|
if (use_detail)
|
|
|
|
{
|
|
|
|
d->setDetailedText(detail);
|
|
|
|
}
|
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
QObject::connect(d, &QMessageBox::rejected, d, &QMessageBox::deleteLater);
|
2020-08-27 01:42:41 +00:00
|
|
|
d->show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 21:38:34 +00:00
|
|
|
void Session::addTorrent(AddData add_me)
|
2014-12-01 19:24:07 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 3);
|
2014-12-01 19:24:07 +00:00
|
|
|
|
2022-09-07 21:38:34 +00:00
|
|
|
addTorrent(std::move(add_me), &args, prefs_.getBool(Prefs::TRASH_ORIGINAL));
|
2010-04-27 03:10:32 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void Session::addNewlyCreatedTorrent(QString const& filename, QString const& local_path)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QByteArray const b64 = AddData(filename).toBase64();
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 3);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, TR_KEY_download_dir, local_path);
|
|
|
|
dictAdd(&args, TR_KEY_paused, !prefs_.getBool(Prefs::START));
|
|
|
|
dictAdd(&args, TR_KEY_metainfo, b64);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
exec("torrent-add", &args);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void Session::removeTorrents(torrent_ids_t const& ids, bool delete_files)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!ids.empty())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 2);
|
|
|
|
addOptionalIds(&args, ids);
|
2020-07-27 04:30:58 +00:00
|
|
|
dictAdd(&args, TR_KEY_delete_local_data, delete_files);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
exec("torrent-remove", &args);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::verifyTorrents(torrent_ids_t const& ids)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!ids.empty())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 1);
|
|
|
|
addOptionalIds(&args, ids);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
exec("torrent-verify", &args);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Session::reannounceTorrents(torrent_ids_t const& ids)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!ids.empty())
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_variant args;
|
|
|
|
tr_variantInitDict(&args, 1);
|
|
|
|
addOptionalIds(&args, ids);
|
2014-12-27 20:03:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
exec("torrent-reannounce", &args);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-11-09 03:31:02 +00:00
|
|
|
void Session::launchWebInterface() const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QUrl url;
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (session_ == nullptr) // remote session
|
2012-07-22 15:18:52 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
url = rpc_.url();
|
2020-05-29 17:40:07 +00:00
|
|
|
url.setPath(QStringLiteral("/transmission/web/"));
|
2012-07-22 15:18:52 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else // local session
|
2012-07-22 15:18:52 +00:00
|
|
|
{
|
2020-05-29 17:40:07 +00:00
|
|
|
url.setScheme(QStringLiteral("http"));
|
|
|
|
url.setHost(QStringLiteral("localhost"));
|
2020-05-27 21:53:12 +00:00
|
|
|
url.setPort(prefs_.getInt(Prefs::RPC_PORT));
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2013-02-03 19:40:20 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
QDesktopServices::openUrl(url);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|