2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2009-2022 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-11 18:11:55 +00:00
|
|
|
#include "Application.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-06-05 19:02:11 +00:00
|
|
|
#include <array>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <ctime>
|
2022-01-13 02:13:58 +00:00
|
|
|
#include <memory>
|
2022-07-26 02:45:54 +00:00
|
|
|
#include <utility>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include <QIcon>
|
2009-10-06 00:27:26 +00:00
|
|
|
#include <QLibraryInfo>
|
2015-01-04 12:29:10 +00:00
|
|
|
#include <QMessageBox>
|
2013-01-20 23:57:09 +00:00
|
|
|
#include <QProcess>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <QRect>
|
2015-10-18 11:48:10 +00:00
|
|
|
#include <QSystemTrayIcon>
|
2020-11-02 15:16:12 +00:00
|
|
|
#include <QtDebug>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-12-16 20:01:03 +00:00
|
|
|
#ifdef QT_DBUS_LIB
|
2017-04-19 12:04:45 +00:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusMessage>
|
|
|
|
#include <QDBusReply>
|
2015-12-16 20:01:03 +00:00
|
|
|
#endif
|
|
|
|
|
2020-08-11 18:11:55 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
2022-08-04 13:44:18 +00:00
|
|
|
|
|
|
|
#include <libtransmission/tr-getopt.h>
|
2010-06-22 04:34:16 +00:00
|
|
|
#include <libtransmission/utils.h>
|
2009-04-13 19:04:21 +00:00
|
|
|
#include <libtransmission/version.h>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "AddData.h"
|
|
|
|
#include "Formatter.h"
|
2015-12-16 20:01:03 +00:00
|
|
|
#include "InteropHelper.h"
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "MainWindow.h"
|
|
|
|
#include "OptionsDialog.h"
|
|
|
|
#include "Prefs.h"
|
|
|
|
#include "Session.h"
|
|
|
|
#include "TorrentModel.h"
|
|
|
|
#include "WatchDir.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
std::array<tr_option, 8> const Opts = {
|
2020-06-05 19:02:11 +00:00
|
|
|
tr_option{ 'g', "config-dir", "Where to look for configuration files", "g", true, "<path>" },
|
2019-07-14 12:40:41 +00:00
|
|
|
{ 'm', "minimized", "Start minimized in system tray", "m", false, nullptr },
|
|
|
|
{ 'p', "port", "Port to use when connecting to an existing session", "p", true, "<port>" },
|
|
|
|
{ 'r', "remote", "Connect to an existing session at the specified hostname", "r", true, "<host>" },
|
|
|
|
{ 'u', "username", "Username to use when connecting to an existing session", "u", true, "<username>" },
|
|
|
|
{ 'v', "version", "Show version number and exit", "v", false, nullptr },
|
|
|
|
{ 'w', "password", "Password to use when connecting to an existing session", "w", true, "<password>" },
|
|
|
|
{ 0, nullptr, nullptr, nullptr, false, nullptr }
|
2017-04-19 12:04:45 +00:00
|
|
|
};
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* getUsage()
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2013-01-26 01:19:54 +00:00
|
|
|
return "Usage:\n"
|
2021-08-15 09:41:48 +00:00
|
|
|
" transmission [OPTIONS...] [torrent files]";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2022-02-09 02:25:19 +00:00
|
|
|
auto constexpr StatsRefreshIntervalMsec = int{ 3000 };
|
|
|
|
auto constexpr SessionRefreshIntervalMsec = int{ 3000 };
|
|
|
|
auto constexpr ModelRefreshIntervalMsec = int{ 3000 };
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
bool loadTranslation(QTranslator& translator, QString const& name, QLocale const& locale, QStringList const& search_directories)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
for (QString const& directory : search_directories)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-29 17:40:07 +00:00
|
|
|
if (translator.load(locale, name, QStringLiteral("_"), directory))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2014-12-22 01:08:19 +00:00
|
|
|
|
|
|
|
return false;
|
2013-01-26 01:19:54 +00:00
|
|
|
}
|
2010-08-09 00:18:26 +00:00
|
|
|
|
2022-07-03 01:42:43 +00:00
|
|
|
[[nodiscard]] auto makeWindowIcon()
|
|
|
|
{
|
|
|
|
// first, try to load it from the system theme
|
|
|
|
if (auto icon = QIcon::fromTheme(QStringLiteral("transmission")); !icon.isNull())
|
|
|
|
{
|
|
|
|
return icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if that fails, use our own as the fallback
|
|
|
|
return QIcon{ QStringLiteral(":/icons/transmission.svg") };
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
Application::Application(int& argc, char** argv)
|
|
|
|
: QApplication(argc, argv)
|
|
|
|
, config_name_{ QStringLiteral("transmission") }
|
|
|
|
, display_name_{ QStringLiteral("transmission-qt") }
|
2021-10-24 23:52:50 +00:00
|
|
|
, start_now_regex_{ QRegularExpression(QStringLiteral(R"rgx(start-now\((\d+)\))rgx")) }
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2020-08-15 15:42:51 +00:00
|
|
|
setApplicationName(config_name_);
|
2017-04-19 12:04:45 +00:00
|
|
|
loadTranslations();
|
|
|
|
|
|
|
|
#if defined(_WIN32) || defined(__APPLE__)
|
|
|
|
|
|
|
|
if (QIcon::themeName().isEmpty())
|
|
|
|
{
|
2020-05-29 17:40:07 +00:00
|
|
|
QIcon::setThemeName(QStringLiteral("Faenza"));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2015-01-04 02:14:58 +00:00
|
|
|
#endif
|
|
|
|
|
2022-07-03 01:42:43 +00:00
|
|
|
setWindowIcon(makeWindowIcon());
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2015-09-01 23:19:01 +00:00
|
|
|
#ifdef __APPLE__
|
2017-04-19 12:04:45 +00:00
|
|
|
setAttribute(Qt::AA_DontShowIconsInMenus);
|
2015-09-01 23:19:01 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// parse the command-line arguments
|
2022-02-08 03:56:04 +00:00
|
|
|
int c = 0;
|
2017-04-19 12:04:45 +00:00
|
|
|
bool minimized = false;
|
2022-02-08 03:56:04 +00:00
|
|
|
char const* optarg = nullptr;
|
2017-04-19 12:04:45 +00:00
|
|
|
QString host;
|
|
|
|
QString port;
|
|
|
|
QString username;
|
|
|
|
QString password;
|
2020-05-27 21:53:12 +00:00
|
|
|
QString config_dir;
|
2017-04-19 12:04:45 +00:00
|
|
|
QStringList filenames;
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
while ((c = tr_getopt(getUsage(), argc, const_cast<char const**>(argv), Opts.data(), &optarg)) != TR_OPT_DONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
switch (c)
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case 'g':
|
2020-05-27 21:53:12 +00:00
|
|
|
config_dir = QString::fromUtf8(optarg);
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
port = QString::fromUtf8(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
host = QString::fromUtf8(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
username = QString::fromUtf8(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
password = QString::fromUtf8(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
minimized = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
2020-11-02 15:16:12 +00:00
|
|
|
qInfo() << qPrintable(display_name_) << LONG_VERSION_STRING;
|
2017-04-19 12:04:45 +00:00
|
|
|
quitLater();
|
2015-01-28 22:57:46 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case TR_OPT_ERR:
|
2020-11-02 15:16:12 +00:00
|
|
|
qWarning() << qPrintable(QObject::tr("Invalid option"));
|
2020-08-15 15:42:51 +00:00
|
|
|
tr_getopt_usage(qPrintable(display_name_), getUsage(), Opts.data());
|
2017-04-19 12:04:45 +00:00
|
|
|
quitLater();
|
2015-01-28 22:57:46 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
filenames.append(QString::fromUtf8(optarg));
|
2015-01-28 22:57:46 +00:00
|
|
|
break;
|
2013-01-26 01:19:54 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// try to delegate the work to an existing copy of Transmission
|
|
|
|
// before starting ourselves...
|
2022-07-27 14:03:13 +00:00
|
|
|
InteropHelper const interop_client;
|
2022-02-01 17:30:51 +00:00
|
|
|
|
|
|
|
if (interop_client.isConnected())
|
2015-12-16 17:57:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
bool delegated = false;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QString const& filename : filenames)
|
2015-12-16 17:57:05 +00:00
|
|
|
{
|
2020-11-01 21:47:57 +00:00
|
|
|
auto const a = AddData(filename);
|
2017-04-19 12:04:45 +00:00
|
|
|
QString metainfo;
|
2015-12-16 17:57:05 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (a.type)
|
2015-12-16 17:57:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case AddData::URL:
|
|
|
|
metainfo = a.url.toString();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AddData::MAGNET:
|
|
|
|
metainfo = a.magnet;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AddData::FILENAME:
|
|
|
|
case AddData::METAINFO:
|
2020-05-29 17:40:07 +00:00
|
|
|
metainfo = QString::fromUtf8(a.toBase64());
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-12-16 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!metainfo.isEmpty() && interop_client.addMetainfo(metainfo))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
delegated = true;
|
|
|
|
}
|
2015-12-16 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (delegated)
|
2015-12-16 17:57:05 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
quitLater();
|
|
|
|
return;
|
2015-12-16 17:57:05 +00:00
|
|
|
}
|
|
|
|
}
|
2014-12-01 21:05:44 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// set the fallback config dir
|
2020-05-27 21:53:12 +00:00
|
|
|
if (config_dir.isNull())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2022-08-17 00:28:57 +00:00
|
|
|
config_dir = QString::fromStdString(tr_getDefaultConfigDir("transmission"));
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ensure our config directory exists
|
2022-07-27 14:03:13 +00:00
|
|
|
QDir const dir(config_dir);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!dir.exists())
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
dir.mkpath(config_dir);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// is this the first time we've run transmission?
|
2020-05-29 17:40:07 +00:00
|
|
|
bool const first_time = !dir.exists(QStringLiteral("settings.json"));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// initialize the prefs
|
2020-11-08 19:54:40 +00:00
|
|
|
prefs_ = std::make_unique<Prefs>(config_dir);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (!host.isNull())
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_->set(Prefs::SESSION_REMOTE_HOST, host);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!port.isNull())
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_->set(Prefs::SESSION_REMOTE_PORT, port.toUInt());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!username.isNull())
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_->set(Prefs::SESSION_REMOTE_USERNAME, username);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!password.isNull())
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_->set(Prefs::SESSION_REMOTE_PASSWORD, password);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!host.isNull() || !port.isNull() || !username.isNull() || !password.isNull())
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_->set(Prefs::SESSION_IS_REMOTE, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (prefs_->getBool(Prefs::START_MINIMIZED))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
minimized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start as minimized only if the system tray present
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!prefs_->getBool(Prefs::SHOW_TRAY_ICON))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
minimized = false;
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
session_ = std::make_unique<Session>(config_dir, *prefs_);
|
|
|
|
model_ = std::make_unique<TorrentModel>(*prefs_);
|
|
|
|
window_ = std::make_unique<MainWindow>(*session_, *prefs_, *model_, minimized);
|
|
|
|
watch_dir_ = std::make_unique<WatchDir>(*model_);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
connect(this, &QCoreApplication::aboutToQuit, this, &Application::saveGeometry);
|
|
|
|
connect(model_.get(), &TorrentModel::torrentsAdded, this, &Application::onTorrentsAdded);
|
|
|
|
connect(model_.get(), &TorrentModel::torrentsCompleted, this, &Application::onTorrentsCompleted);
|
|
|
|
connect(model_.get(), &TorrentModel::torrentsEdited, this, &Application::onTorrentsEdited);
|
|
|
|
connect(model_.get(), &TorrentModel::torrentsNeedInfo, this, &Application::onTorrentsNeedInfo);
|
|
|
|
connect(prefs_.get(), &Prefs::changed, this, &Application::refreshPref);
|
|
|
|
connect(session_.get(), &Session::sourceChanged, this, &Application::onSessionSourceChanged);
|
|
|
|
connect(session_.get(), &Session::torrentsRemoved, model_.get(), &TorrentModel::removeTorrents);
|
|
|
|
connect(session_.get(), &Session::torrentsUpdated, model_.get(), &TorrentModel::updateTorrents);
|
|
|
|
connect(watch_dir_.get(), &WatchDir::torrentFileAdded, this, qOverload<QString const&>(&Application::addTorrent));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// init from preferences
|
2019-11-09 14:44:40 +00:00
|
|
|
for (auto const key : { Prefs::DIR_WATCH })
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
refreshPref(key);
|
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QTimer* timer = &model_timer_;
|
2019-11-09 14:44:40 +00:00
|
|
|
connect(timer, &QTimer::timeout, this, &Application::refreshTorrents);
|
2017-04-19 12:04:45 +00:00
|
|
|
timer->setSingleShot(false);
|
2022-02-09 02:25:19 +00:00
|
|
|
timer->setInterval(ModelRefreshIntervalMsec);
|
2017-04-19 12:04:45 +00:00
|
|
|
timer->start();
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
timer = &stats_timer_;
|
2020-11-08 19:54:40 +00:00
|
|
|
connect(timer, &QTimer::timeout, session_.get(), &Session::refreshSessionStats);
|
2017-04-19 12:04:45 +00:00
|
|
|
timer->setSingleShot(false);
|
2022-02-09 02:25:19 +00:00
|
|
|
timer->setInterval(StatsRefreshIntervalMsec);
|
2017-04-19 12:04:45 +00:00
|
|
|
timer->start();
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
timer = &session_timer_;
|
2020-11-08 19:54:40 +00:00
|
|
|
connect(timer, &QTimer::timeout, session_.get(), &Session::refreshSessionInfo);
|
2017-04-19 12:04:45 +00:00
|
|
|
timer->setSingleShot(false);
|
2022-02-09 02:25:19 +00:00
|
|
|
timer->setInterval(SessionRefreshIntervalMsec);
|
2017-04-19 12:04:45 +00:00
|
|
|
timer->start();
|
|
|
|
|
|
|
|
maybeUpdateBlocklist();
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!first_time)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
session_->restart();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
window_->openSession();
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!prefs_->getBool(Prefs::USER_HAS_GIVEN_INFORMED_CONSENT))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
auto* dialog = new QMessageBox(
|
|
|
|
QMessageBox::Information,
|
|
|
|
QString(),
|
|
|
|
tr("<b>Transmission is a file sharing program.</b>"),
|
|
|
|
QMessageBox::Ok | QMessageBox::Cancel,
|
|
|
|
window_.get());
|
|
|
|
dialog->setInformativeText(
|
|
|
|
tr("When you run a torrent, its data will be made available to others by means of upload. "
|
|
|
|
"Any content you share is your sole responsibility."));
|
2017-04-19 12:04:45 +00:00
|
|
|
dialog->button(QMessageBox::Ok)->setText(tr("I &Agree"));
|
|
|
|
dialog->setDefaultButton(QMessageBox::Ok);
|
|
|
|
dialog->setModal(true);
|
|
|
|
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(dialog, &QDialog::finished, this, &Application::consentGiven);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
dialog->show();
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QString const& filename : filenames)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
addTorrent(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
InteropHelper::registerObject(this);
|
2021-10-23 14:26:28 +00:00
|
|
|
|
|
|
|
#ifdef QT_DBUS_LIB
|
|
|
|
QDBusConnection bus = QDBusConnection::sessionBus();
|
|
|
|
if (bus.isConnected())
|
|
|
|
{
|
|
|
|
bus.connect(
|
2021-10-24 23:52:50 +00:00
|
|
|
fdo_notifications_service_name_,
|
|
|
|
fdo_notifications_path_,
|
|
|
|
fdo_notifications_interface_name_,
|
2021-10-23 14:26:28 +00:00
|
|
|
QLatin1String("ActionInvoked"),
|
|
|
|
this,
|
|
|
|
SLOT(onNotificationActionInvoked(quint32, QString)));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Application::loadTranslations()
|
2015-11-15 11:03:27 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
QStringList const qt_qm_dirs = QStringList() << QLibraryInfo::location(QLibraryInfo::TranslationsPath) <<
|
2015-11-15 11:03:27 +00:00
|
|
|
#ifdef TRANSLATIONS_DIR
|
2020-05-29 17:40:07 +00:00
|
|
|
QStringLiteral(TRANSLATIONS_DIR) <<
|
2015-11-15 11:03:27 +00:00
|
|
|
#endif
|
2020-05-29 17:40:07 +00:00
|
|
|
(applicationDirPath() + QStringLiteral("/translations"));
|
2015-11-15 11:03:27 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QStringList const app_qm_dirs = QStringList() <<
|
2015-11-15 11:03:27 +00:00
|
|
|
#ifdef TRANSLATIONS_DIR
|
2020-05-29 17:40:07 +00:00
|
|
|
QStringLiteral(TRANSLATIONS_DIR) <<
|
2015-11-15 11:03:27 +00:00
|
|
|
#endif
|
2020-05-29 17:40:07 +00:00
|
|
|
(applicationDirPath() + QStringLiteral("/translations"));
|
2015-11-15 11:03:27 +00:00
|
|
|
|
2020-05-29 17:40:07 +00:00
|
|
|
auto const qt_file_name = QStringLiteral("qtbase");
|
2015-11-15 11:03:27 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QLocale const locale;
|
2020-05-27 21:53:12 +00:00
|
|
|
QLocale const english_locale(QLocale::English, QLocale::UnitedStates);
|
2015-11-15 11:03:27 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (loadTranslation(qt_translator_, qt_file_name, locale, qt_qm_dirs) ||
|
|
|
|
loadTranslation(qt_translator_, qt_file_name, english_locale, qt_qm_dirs))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
installTranslator(&qt_translator_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2017-02-06 21:58:51 +00:00
|
|
|
|
2020-08-15 15:42:51 +00:00
|
|
|
if (loadTranslation(app_translator_, config_name_, locale, app_qm_dirs) ||
|
|
|
|
loadTranslation(app_translator_, config_name_, english_locale, app_qm_dirs))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
installTranslator(&app_translator_);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2015-11-15 11:03:27 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 01:13:32 +00:00
|
|
|
void Application::quitLater() const
|
2015-01-28 22:57:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QTimer::singleShot(0, this, SLOT(quit()));
|
2015-01-28 22:57:46 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Application::onTorrentsEdited(torrent_ids_t const& torrent_ids) const
|
2019-11-12 23:13:42 +00:00
|
|
|
{
|
|
|
|
// the backend's tr_info has changed, so reload those fields
|
2022-09-08 23:26:18 +00:00
|
|
|
session_->initTorrents(torrent_ids);
|
2019-11-12 23:13:42 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
QStringList Application::getNames(torrent_ids_t const& torrent_ids) const
|
2010-08-01 14:51:28 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
QStringList names;
|
2022-09-08 23:26:18 +00:00
|
|
|
for (auto const& id : torrent_ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
names.push_back(model_->getTorrentFromId(id)->name());
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-08-02 03:07:42 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
names.sort();
|
|
|
|
return names;
|
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Application::onTorrentsAdded(torrent_ids_t const& torrent_ids) const
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
if (!prefs_->getBool(Prefs::SHOW_NOTIFICATION_ON_ADD))
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto id : torrent_ids)
|
|
|
|
{
|
|
|
|
notifyTorrentAdded(model_->getTorrentFromId(id));
|
2010-08-01 15:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-26 02:56:30 +00:00
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Application::onTorrentsCompleted(torrent_ids_t const& torrent_ids) const
|
2011-07-26 02:56:30 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (prefs_->getBool(Prefs::SHOW_NOTIFICATION_ON_COMPLETE))
|
2011-07-26 02:56:30 +00:00
|
|
|
{
|
2022-10-11 15:39:41 +00:00
|
|
|
auto const title = tr("Torrent(s) Completed", nullptr, static_cast<int>(std::size(torrent_ids)));
|
2022-09-08 23:26:18 +00:00
|
|
|
auto const body = getNames(torrent_ids).join(QStringLiteral("\n"));
|
2019-11-09 14:44:40 +00:00
|
|
|
notifyApp(title, body);
|
|
|
|
}
|
2013-01-20 23:57:09 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (prefs_->getBool(Prefs::COMPLETE_SOUND_ENABLED))
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(Q_OS_WIN) || defined(Q_OS_MAC)
|
2019-11-09 14:44:40 +00:00
|
|
|
beep();
|
2013-01-21 21:48:52 +00:00
|
|
|
#else
|
2021-05-25 16:21:41 +00:00
|
|
|
auto args = prefs_->get<QStringList>(Prefs::COMPLETE_SOUND_COMMAND);
|
|
|
|
auto const command = args.takeFirst();
|
|
|
|
QProcess::execute(command, args);
|
2013-01-21 21:48:52 +00:00
|
|
|
#endif
|
2011-07-26 02:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-08 23:26:18 +00:00
|
|
|
void Application::onTorrentsNeedInfo(torrent_ids_t const& torrent_ids) const
|
2010-08-01 15:47:42 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
if (!torrent_ids.empty())
|
2010-08-01 15:47:42 +00:00
|
|
|
{
|
2022-09-08 23:26:18 +00:00
|
|
|
session_->initTorrents(torrent_ids);
|
2010-08-01 15:47:42 +00:00
|
|
|
}
|
2010-08-01 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 14:26:28 +00:00
|
|
|
void Application::notifyTorrentAdded(Torrent const* tor) const
|
|
|
|
{
|
|
|
|
QStringList actions;
|
|
|
|
actions << QString(QLatin1String("start-now(%1)")).arg(tor->id()) << QObject::tr("Start Now");
|
|
|
|
notifyApp(tr("Torrent Added"), tor->name(), actions);
|
|
|
|
}
|
|
|
|
|
2011-07-26 02:56:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
void Application::consentGiven(int result) const
|
2009-10-02 22:53:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (result == QMessageBox::Ok)
|
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
prefs_->set<bool>(Prefs::USER_HAS_GIVEN_INFORMED_CONSENT, true);
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quit();
|
|
|
|
}
|
2009-10-02 22:53:19 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
void Application::saveGeometry() const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (prefs_ != nullptr && window_ != nullptr)
|
2014-12-01 21:05:44 +00:00
|
|
|
{
|
2020-06-05 19:02:11 +00:00
|
|
|
auto const geometry = window_->geometry();
|
|
|
|
prefs_->set(Prefs::MAIN_WINDOW_HEIGHT, std::max(100, geometry.height()));
|
|
|
|
prefs_->set(Prefs::MAIN_WINDOW_WIDTH, std::max(100, geometry.width()));
|
|
|
|
prefs_->set(Prefs::MAIN_WINDOW_X, geometry.x());
|
|
|
|
prefs_->set(Prefs::MAIN_WINDOW_Y, geometry.y());
|
2014-12-01 21:05:44 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
void Application::refreshPref(int key) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
switch (key)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case Prefs::BLOCKLIST_UPDATES_ENABLED:
|
|
|
|
maybeUpdateBlocklist();
|
2013-01-26 01:19:54 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case Prefs::DIR_WATCH:
|
|
|
|
case Prefs::DIR_WATCH_ENABLED:
|
2021-08-15 09:41:48 +00:00
|
|
|
watch_dir_->setPath(prefs_->getString(Prefs::DIR_WATCH), prefs_->getBool(Prefs::DIR_WATCH_ENABLED));
|
2020-11-02 01:13:32 +00:00
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
default:
|
2013-01-26 01:19:54 +00:00
|
|
|
break;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
void Application::maybeUpdateBlocklist() const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!prefs_->getBool(Prefs::BLOCKLIST_UPDATES_ENABLED))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
QDateTime const last_updated_at = prefs_->getDateTime(Prefs::BLOCKLIST_DATE);
|
|
|
|
QDateTime const next_update_at = last_updated_at.addDays(7);
|
2017-04-20 16:02:19 +00:00
|
|
|
QDateTime const now = QDateTime::currentDateTime();
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (now < next_update_at)
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
session_->updateBlocklist();
|
|
|
|
prefs_->set(Prefs::BLOCKLIST_DATE, now);
|
2013-01-26 01:19:54 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
void Application::onSessionSourceChanged() const
|
2010-06-16 03:11:10 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
session_->initTorrents();
|
|
|
|
session_->refreshSessionStats();
|
|
|
|
session_->refreshSessionInfo();
|
2010-06-16 03:11:10 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Application::refreshTorrents()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// usually we just poll the torrents that have shown recent activity,
|
|
|
|
// but we also periodically ask for updates on the others to ensure
|
|
|
|
// nothing's falling through the cracks.
|
2017-04-30 09:29:58 +00:00
|
|
|
time_t const now = time(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (last_full_update_time_ + 60 >= now)
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
session_->refreshActiveTorrents();
|
2013-01-26 01:19:54 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
last_full_update_time_ = now;
|
|
|
|
session_->refreshAllTorrents();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 03:10:32 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
void Application::addTorrent(QString const& addme) const
|
2020-11-01 21:47:57 +00:00
|
|
|
{
|
|
|
|
addTorrent(AddData(addme));
|
|
|
|
}
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
void Application::addTorrent(AddData const& addme) const
|
2010-08-01 18:55:04 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
if (addme.type == addme.NONE)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2010-08-01 18:55:04 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (!prefs_->getBool(Prefs::OPTIONS_PROMPT))
|
2010-05-13 23:54:32 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
session_->addTorrent(addme);
|
2010-05-13 23:54:32 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2010-05-13 23:54:32 +00:00
|
|
|
{
|
2020-11-08 19:54:40 +00:00
|
|
|
auto* o = new OptionsDialog(*session_, *prefs_, addme, window_.get());
|
2017-04-19 12:04:45 +00:00
|
|
|
o->show();
|
2010-05-13 23:54:32 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
raise();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2010-08-01 14:51:28 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-11-08 19:54:40 +00:00
|
|
|
void Application::raise() const
|
2010-04-27 03:10:32 +00:00
|
|
|
{
|
2020-11-08 19:54:40 +00:00
|
|
|
alert(window_.get());
|
2010-04-27 03:10:32 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2021-10-23 14:26:28 +00:00
|
|
|
bool Application::notifyApp(QString const& title, QString const& body, QStringList const& actions) const
|
2010-08-01 14:51:28 +00:00
|
|
|
{
|
2015-12-16 20:01:03 +00:00
|
|
|
#ifdef QT_DBUS_LIB
|
2022-02-03 13:02:11 +00:00
|
|
|
if (auto bus = QDBusConnection::sessionBus(); bus.isConnected())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2021-10-24 23:52:50 +00:00
|
|
|
QDBusMessage m = QDBusMessage::createMethodCall(
|
|
|
|
fdo_notifications_service_name_,
|
|
|
|
fdo_notifications_path_,
|
|
|
|
fdo_notifications_interface_name_,
|
|
|
|
QStringLiteral("Notify"));
|
2017-04-19 12:04:45 +00:00
|
|
|
QVariantList args;
|
2020-05-29 17:40:07 +00:00
|
|
|
args.append(QStringLiteral("Transmission")); // app_name
|
2017-04-19 12:04:45 +00:00
|
|
|
args.append(0U); // replaces_id
|
2020-05-29 17:40:07 +00:00
|
|
|
args.append(QStringLiteral("transmission")); // icon
|
2017-04-19 12:04:45 +00:00
|
|
|
args.append(title); // summary
|
|
|
|
args.append(body); // body
|
2021-10-23 14:26:28 +00:00
|
|
|
args.append(actions);
|
2020-07-28 21:51:01 +00:00
|
|
|
args.append(QVariantMap({
|
2021-08-15 09:41:48 +00:00
|
|
|
std::make_pair(QStringLiteral("category"), QVariant(QStringLiteral("transfer.complete"))),
|
2020-07-28 21:51:01 +00:00
|
|
|
})); // hints
|
2017-04-19 12:04:45 +00:00
|
|
|
args.append(static_cast<int32_t>(-1)); // use the default timeout period
|
|
|
|
m.setArguments(args);
|
2020-05-27 21:53:12 +00:00
|
|
|
QDBusReply<quint32> const reply_msg = bus.call(m);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
if (reply_msg.isValid() && reply_msg.value() > 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-18 11:48:10 +00:00
|
|
|
}
|
2022-02-01 17:30:51 +00:00
|
|
|
|
2015-12-16 20:01:03 +00:00
|
|
|
#endif
|
2015-10-18 11:48:10 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
window_->trayIcon().showMessage(title, body);
|
2017-04-19 12:04:45 +00:00
|
|
|
return true;
|
2010-08-01 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
2021-10-23 14:26:28 +00:00
|
|
|
#ifdef QT_DBUS_LIB
|
|
|
|
void Application::onNotificationActionInvoked(quint32 /* notification_id */, QString action_key)
|
|
|
|
{
|
2021-10-24 23:52:50 +00:00
|
|
|
auto const match = start_now_regex_.match(action_key);
|
2021-10-23 14:26:28 +00:00
|
|
|
if (match.hasMatch())
|
|
|
|
{
|
|
|
|
int const torrent_id = match.captured(1).toInt();
|
|
|
|
session_->startTorrentsNow({ torrent_id });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FaviconCache& Application::faviconCache()
|
2015-06-12 22:12:12 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
return favicons_;
|
2015-06-12 22:12:12 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2020-06-05 19:02:11 +00:00
|
|
|
int tr_main(int argc, char** argv)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
InteropHelper::initialize();
|
2015-12-16 20:01:03 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
Application::setAttribute(Qt::AA_EnableHighDpiScaling);
|
|
|
|
Application::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
2017-02-07 22:06:28 +00:00
|
|
|
|
2022-07-27 14:03:13 +00:00
|
|
|
Application const app(argc, argv);
|
2020-11-02 01:13:32 +00:00
|
|
|
return QApplication::exec();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|