2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctime>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#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>
|
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
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#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 "Application.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
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QLatin1String const MY_CONFIG_NAME("transmission");
|
|
|
|
QLatin1String const MY_READABLE_NAME("transmission-qt");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_option const opts[] =
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-07-14 12:40:41 +00:00
|
|
|
{ 'g', "config-dir", "Where to look for configuration files", "g", true, "<path>" },
|
|
|
|
{ '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"
|
2019-02-10 11:05:16 +00:00
|
|
|
" transmission [OPTIONS...] [torrent files]";
|
2017-04-19 12:04:45 +00:00
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
STATS_REFRESH_INTERVAL_MSEC = 3000,
|
2013-01-26 01:19:54 +00:00
|
|
|
SESSION_REFRESH_INTERVAL_MSEC = 3000,
|
2017-04-19 12:04:45 +00:00
|
|
|
MODEL_REFRESH_INTERVAL_MSEC = 3000
|
|
|
|
};
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool loadTranslation(QTranslator& translator, QString const& name, QLocale const& locale, QStringList const& searchDirectories)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QString const& directory : searchDirectories)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (translator.load(locale, name, QLatin1String("_"), directory))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Application::Application(int& argc, char** argv) :
|
|
|
|
QApplication(argc, argv),
|
|
|
|
myPrefs(nullptr),
|
|
|
|
mySession(nullptr),
|
|
|
|
myModel(nullptr),
|
|
|
|
myWindow(nullptr),
|
|
|
|
myWatchDir(nullptr),
|
|
|
|
myLastFullUpdateTime(0)
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
setApplicationName(MY_CONFIG_NAME);
|
|
|
|
loadTranslations();
|
|
|
|
|
|
|
|
Formatter::initUnits();
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
#if defined(_WIN32) || defined(__APPLE__)
|
|
|
|
|
|
|
|
if (QIcon::themeName().isEmpty())
|
|
|
|
{
|
|
|
|
QIcon::setThemeName(QLatin1String("Faenza"));
|
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2015-01-04 02:14:58 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// set the default icon
|
|
|
|
QIcon icon = QIcon::fromTheme(QLatin1String("transmission"));
|
|
|
|
|
|
|
|
if (icon.isNull())
|
2014-12-12 21:47:22 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
QList<int> sizes;
|
|
|
|
sizes << 16 << 22 << 24 << 32 << 48 << 64 << 72 << 96 << 128 << 192 << 256;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (int const size : sizes)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
icon.addPixmap(QPixmap(QString::fromLatin1(":/icons/transmission-%1.png").arg(size)));
|
|
|
|
}
|
2014-12-12 21:47:22 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
setWindowIcon(icon);
|
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
|
|
|
|
int c;
|
|
|
|
bool minimized = false;
|
2017-04-20 16:02:19 +00:00
|
|
|
char const* optarg;
|
2017-04-19 12:04:45 +00:00
|
|
|
QString host;
|
|
|
|
QString port;
|
|
|
|
QString username;
|
|
|
|
QString password;
|
|
|
|
QString configDir;
|
|
|
|
QStringList filenames;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while ((c = tr_getopt(getUsage(), argc, const_cast<char const**>(argv), opts, &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':
|
|
|
|
configDir = QString::fromUtf8(optarg);
|
|
|
|
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':
|
|
|
|
std::cerr << MY_READABLE_NAME.latin1() << ' ' << LONG_VERSION_STRING << std::endl;
|
|
|
|
quitLater();
|
2015-01-28 22:57:46 +00:00
|
|
|
return;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
case TR_OPT_ERR:
|
|
|
|
std::cerr << qPrintable(QObject::tr("Invalid option")) << std::endl;
|
|
|
|
tr_getopt_usage(MY_READABLE_NAME.latin1(), getUsage(), opts);
|
|
|
|
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...
|
|
|
|
InteropHelper interopClient;
|
|
|
|
|
|
|
|
if (interopClient.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
|
|
|
{
|
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
|
|
|
AddData a(filename);
|
|
|
|
|
|
|
|
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:
|
|
|
|
metainfo = QString::fromLatin1(a.toBase64());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case AddData::METAINFO:
|
|
|
|
metainfo = QString::fromLatin1(a.toBase64());
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2015-12-16 17:57:05 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!metainfo.isEmpty() && interopClient.addMetainfo(metainfo))
|
|
|
|
{
|
|
|
|
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
|
|
|
|
if (configDir.isNull())
|
|
|
|
{
|
|
|
|
configDir = QString::fromUtf8(tr_getDefaultConfigDir("transmission"));
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure our config directory exists
|
|
|
|
QDir dir(configDir);
|
|
|
|
|
|
|
|
if (!dir.exists())
|
|
|
|
{
|
|
|
|
dir.mkpath(configDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// is this the first time we've run transmission?
|
2017-04-20 16:02:19 +00:00
|
|
|
bool const firstTime = !dir.exists(QLatin1String("settings.json"));
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// initialize the prefs
|
|
|
|
myPrefs = new Prefs(configDir);
|
|
|
|
|
|
|
|
if (!host.isNull())
|
|
|
|
{
|
|
|
|
myPrefs->set(Prefs::SESSION_REMOTE_HOST, host);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!port.isNull())
|
|
|
|
{
|
|
|
|
myPrefs->set(Prefs::SESSION_REMOTE_PORT, port.toUInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!username.isNull())
|
|
|
|
{
|
|
|
|
myPrefs->set(Prefs::SESSION_REMOTE_USERNAME, username);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!password.isNull())
|
|
|
|
{
|
|
|
|
myPrefs->set(Prefs::SESSION_REMOTE_PASSWORD, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!host.isNull() || !port.isNull() || !username.isNull() || !password.isNull())
|
|
|
|
{
|
|
|
|
myPrefs->set(Prefs::SESSION_IS_REMOTE, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myPrefs->getBool(Prefs::START_MINIMIZED))
|
|
|
|
{
|
|
|
|
minimized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// start as minimized only if the system tray present
|
|
|
|
if (!myPrefs->getBool(Prefs::SHOW_TRAY_ICON))
|
|
|
|
{
|
|
|
|
minimized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mySession = new Session(configDir, *myPrefs);
|
|
|
|
myModel = new TorrentModel(*myPrefs);
|
|
|
|
myWindow = new MainWindow(*mySession, *myPrefs, *myModel, minimized);
|
|
|
|
myWatchDir = new WatchDir(*myModel);
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
connect(myModel, &TorrentModel::torrentsAdded, this, &Application::onTorrentsAdded);
|
|
|
|
connect(myModel, &TorrentModel::torrentsCompleted, this, &Application::onTorrentsCompleted);
|
|
|
|
connect(myModel, &TorrentModel::torrentsNeedInfo, this, &Application::onTorrentsNeedInfo);
|
|
|
|
connect(myPrefs, &Prefs::changed, this, &Application::refreshPref);
|
|
|
|
connect(mySession, &Session::sourceChanged, this, &Application::onSessionSourceChanged);
|
|
|
|
connect(mySession, &Session::torrentsRemoved, myModel, &TorrentModel::removeTorrents);
|
|
|
|
connect(mySession, &Session::torrentsUpdated, myModel, &TorrentModel::updateTorrents);
|
|
|
|
connect(myWatchDir, &WatchDir::torrentFileAdded, this, &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);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTimer* timer = &myModelTimer;
|
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);
|
|
|
|
timer->setInterval(MODEL_REFRESH_INTERVAL_MSEC);
|
|
|
|
timer->start();
|
|
|
|
|
|
|
|
timer = &myStatsTimer;
|
2019-11-09 14:44:40 +00:00
|
|
|
connect(timer, &QTimer::timeout, mySession, &Session::refreshSessionStats);
|
2017-04-19 12:04:45 +00:00
|
|
|
timer->setSingleShot(false);
|
|
|
|
timer->setInterval(STATS_REFRESH_INTERVAL_MSEC);
|
|
|
|
timer->start();
|
|
|
|
|
|
|
|
timer = &mySessionTimer;
|
2019-11-09 14:44:40 +00:00
|
|
|
connect(timer, &QTimer::timeout, mySession, &Session::refreshSessionInfo);
|
2017-04-19 12:04:45 +00:00
|
|
|
timer->setSingleShot(false);
|
|
|
|
timer->setInterval(SESSION_REFRESH_INTERVAL_MSEC);
|
|
|
|
timer->start();
|
|
|
|
|
|
|
|
maybeUpdateBlocklist();
|
|
|
|
|
|
|
|
if (!firstTime)
|
|
|
|
{
|
|
|
|
mySession->restart();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
myWindow->openSession();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!myPrefs->getBool(Prefs::USER_HAS_GIVEN_INFORMED_CONSENT))
|
|
|
|
{
|
|
|
|
QMessageBox* dialog = new QMessageBox(QMessageBox::Information, QString(),
|
|
|
|
tr("<b>Transmission is a file sharing program.</b>"), QMessageBox::Ok | QMessageBox::Cancel, myWindow);
|
|
|
|
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."));
|
|
|
|
dialog->button(QMessageBox::Ok)->setText(tr("I &Agree"));
|
|
|
|
dialog->setDefaultButton(QMessageBox::Ok);
|
|
|
|
dialog->setModal(true);
|
|
|
|
|
|
|
|
connect(dialog, SIGNAL(finished(int)), this, SLOT(consentGiven(int)));
|
|
|
|
|
|
|
|
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);
|
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
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QStringList const qtQmDirs = QStringList() << QLibraryInfo::location(QLibraryInfo::TranslationsPath) <<
|
2015-11-15 11:03:27 +00:00
|
|
|
#ifdef TRANSLATIONS_DIR
|
2017-04-19 12:04:45 +00:00
|
|
|
QString::fromUtf8(TRANSLATIONS_DIR) <<
|
2015-11-15 11:03:27 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
(applicationDirPath() + QLatin1String("/translations"));
|
2015-11-15 11:03:27 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QStringList const appQmDirs = QStringList() <<
|
2015-11-15 11:03:27 +00:00
|
|
|
#ifdef TRANSLATIONS_DIR
|
2017-04-19 12:04:45 +00:00
|
|
|
QString::fromUtf8(TRANSLATIONS_DIR) <<
|
2015-11-15 11:03:27 +00:00
|
|
|
#endif
|
2017-04-19 12:04:45 +00:00
|
|
|
(applicationDirPath() + QLatin1String("/translations"));
|
2015-11-15 11:03:27 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const qtFileName = QLatin1String("qtbase");
|
2015-11-15 11:03:27 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QLocale const locale;
|
|
|
|
QLocale const englishLocale(QLocale::English, QLocale::UnitedStates);
|
2015-11-15 11:03:27 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (loadTranslation(myQtTranslator, qtFileName, locale, qtQmDirs) ||
|
|
|
|
loadTranslation(myQtTranslator, qtFileName, englishLocale, qtQmDirs))
|
|
|
|
{
|
|
|
|
installTranslator(&myQtTranslator);
|
|
|
|
}
|
2017-02-06 21:58:51 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (loadTranslation(myAppTranslator, MY_CONFIG_NAME, locale, appQmDirs) ||
|
|
|
|
loadTranslation(myAppTranslator, MY_CONFIG_NAME, englishLocale, appQmDirs))
|
|
|
|
{
|
|
|
|
installTranslator(&myAppTranslator);
|
|
|
|
}
|
2015-11-15 11:03:27 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Application::quitLater()
|
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
|
|
|
}
|
|
|
|
|
2019-11-12 23:13:42 +00:00
|
|
|
void Application::onTorrentsEdited(torrent_ids_t const& ids)
|
|
|
|
{
|
|
|
|
// the backend's tr_info has changed, so reload those fields
|
|
|
|
mySession->initTorrents(ids);
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
QStringList Application::getNames(torrent_ids_t const& ids) const
|
2010-08-01 14:51:28 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
QStringList names;
|
|
|
|
for (auto const& id : ids)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
names.push_back(myModel->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
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Application::onTorrentsAdded(torrent_ids_t const& ids)
|
2019-11-09 14:44:40 +00:00
|
|
|
{
|
|
|
|
if (myPrefs->getBool(Prefs::SHOW_NOTIFICATION_ON_ADD))
|
|
|
|
{
|
|
|
|
auto const title = tr("Torrent(s) Added", nullptr, ids.size());
|
|
|
|
auto const body = getNames(ids).join(QStringLiteral("\n"));
|
|
|
|
notifyApp(title, body);
|
2010-08-01 15:47:42 +00:00
|
|
|
}
|
|
|
|
}
|
2011-07-26 02:56:30 +00:00
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Application::onTorrentsCompleted(torrent_ids_t const& ids)
|
2011-07-26 02:56:30 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
if (myPrefs->getBool(Prefs::SHOW_NOTIFICATION_ON_COMPLETE))
|
2011-07-26 02:56:30 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
auto const title = tr("Torrent Completed", nullptr, ids.size());
|
|
|
|
auto const body = getNames(ids).join(QStringLiteral("\n"));
|
|
|
|
notifyApp(title, body);
|
|
|
|
}
|
2013-01-20 23:57:09 +00:00
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
if (myPrefs->getBool(Prefs::COMPLETE_SOUND_ENABLED))
|
|
|
|
{
|
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
|
2019-11-09 14:44:40 +00:00
|
|
|
QProcess::execute(myPrefs->getString(Prefs::COMPLETE_SOUND_COMMAND));
|
2013-01-21 21:48:52 +00:00
|
|
|
#endif
|
2011-07-26 02:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-12 01:37:05 +00:00
|
|
|
void Application::onTorrentsNeedInfo(torrent_ids_t const& ids)
|
2010-08-01 15:47:42 +00:00
|
|
|
{
|
2019-11-12 01:37:05 +00:00
|
|
|
if (!ids.empty())
|
2010-08-01 15:47:42 +00:00
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
mySession->initTorrents(ids);
|
2010-08-01 15:47:42 +00:00
|
|
|
}
|
2010-08-01 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
2011-07-26 02:56:30 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Application::consentGiven(int result)
|
2009-10-02 22:53:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (result == QMessageBox::Ok)
|
|
|
|
{
|
|
|
|
myPrefs->set<bool>(Prefs::USER_HAS_GIVEN_INFORMED_CONSENT, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
quit();
|
|
|
|
}
|
2009-10-02 22:53:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
Application::~Application()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (myPrefs != nullptr && myWindow != nullptr)
|
2014-12-01 21:05:44 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QRect const mainwinRect(myWindow->geometry());
|
2017-04-19 12:04:45 +00:00
|
|
|
myPrefs->set(Prefs::MAIN_WINDOW_HEIGHT, std::max(100, mainwinRect.height()));
|
|
|
|
myPrefs->set(Prefs::MAIN_WINDOW_WIDTH, std::max(100, mainwinRect.width()));
|
|
|
|
myPrefs->set(Prefs::MAIN_WINDOW_X, mainwinRect.x());
|
|
|
|
myPrefs->set(Prefs::MAIN_WINDOW_Y, mainwinRect.y());
|
2014-12-01 21:05:44 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
delete myWatchDir;
|
|
|
|
delete myWindow;
|
|
|
|
delete myModel;
|
|
|
|
delete mySession;
|
|
|
|
delete myPrefs;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Application::refreshPref(int key)
|
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:
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const path(myPrefs->getString(Prefs::DIR_WATCH));
|
|
|
|
bool const isEnabled(myPrefs->getBool(Prefs::DIR_WATCH_ENABLED));
|
2017-04-19 12:04:45 +00:00
|
|
|
myWatchDir->setPath(path, isEnabled);
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Application::maybeUpdateBlocklist()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!myPrefs->getBool(Prefs::BLOCKLIST_UPDATES_ENABLED))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QDateTime const lastUpdatedAt = myPrefs->getDateTime(Prefs::BLOCKLIST_DATE);
|
|
|
|
QDateTime const nextUpdateAt = lastUpdatedAt.addDays(7);
|
|
|
|
QDateTime const now = QDateTime::currentDateTime();
|
2013-01-26 01:19:54 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (now < nextUpdateAt)
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
mySession->updateBlocklist();
|
|
|
|
myPrefs->set(Prefs::BLOCKLIST_DATE, now);
|
2013-01-26 01:19:54 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Application::onSessionSourceChanged()
|
2010-06-16 03:11:10 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
mySession->initTorrents();
|
|
|
|
mySession->refreshSessionStats();
|
|
|
|
mySession->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
|
|
|
|
|
|
|
if (myLastFullUpdateTime + 60 >= now)
|
2013-01-26 01:19:54 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
mySession->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
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
myLastFullUpdateTime = now;
|
|
|
|
mySession->refreshAllTorrents();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-27 03:10:32 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2019-11-09 14:44:40 +00:00
|
|
|
void Application::addTorrent(AddData const& addme)
|
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
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (!myPrefs->getBool(Prefs::OPTIONS_PROMPT))
|
2010-05-13 23:54:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
mySession->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
|
|
|
{
|
2019-11-09 14:44:40 +00:00
|
|
|
auto o = new OptionsDialog(*mySession, *myPrefs, addme, myWindow);
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void Application::raise()
|
2010-04-27 03:10:32 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
alert(myWindow);
|
2010-04-27 03:10:32 +00:00
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
bool Application::notifyApp(QString const& title, QString const& body) const
|
2010-08-01 14:51:28 +00:00
|
|
|
{
|
2015-12-16 20:01:03 +00:00
|
|
|
#ifdef QT_DBUS_LIB
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
QLatin1String const dbusServiceName("org.freedesktop.Notifications");
|
|
|
|
QLatin1String const dbusInterfaceName("org.freedesktop.Notifications");
|
|
|
|
QLatin1String const dbusPath("/org/freedesktop/Notifications");
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
QDBusConnection bus = QDBusConnection::sessionBus();
|
|
|
|
|
|
|
|
if (bus.isConnected())
|
|
|
|
{
|
|
|
|
QDBusMessage m = QDBusMessage::createMethodCall(dbusServiceName, dbusPath, dbusInterfaceName, QLatin1String("Notify"));
|
|
|
|
QVariantList args;
|
|
|
|
args.append(QLatin1String("Transmission")); // app_name
|
|
|
|
args.append(0U); // replaces_id
|
|
|
|
args.append(QLatin1String("transmission")); // icon
|
|
|
|
args.append(title); // summary
|
|
|
|
args.append(body); // body
|
|
|
|
args.append(QStringList()); // actions - unused for plain passive popups
|
|
|
|
args.append(QVariantMap()); // hints - unused atm
|
|
|
|
args.append(static_cast<int32_t>(-1)); // use the default timeout period
|
|
|
|
m.setArguments(args);
|
2017-04-20 16:02:19 +00:00
|
|
|
QDBusReply<quint32> const replyMsg = bus.call(m);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (replyMsg.isValid() && replyMsg.value() > 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2015-10-18 11:48:10 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2015-12-16 20:01:03 +00:00
|
|
|
#endif
|
2015-10-18 11:48:10 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
myWindow->trayIcon().showMessage(title, body);
|
|
|
|
return true;
|
2010-08-01 14:51:28 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
FaviconCache& Application::faviconCache()
|
2015-06-12 22:12:12 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return myFavicons;
|
2015-06-12 22:12:12 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +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-02-11 10:44:34 +00:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 6, 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
Application::setAttribute(Qt::AA_EnableHighDpiScaling);
|
2017-02-11 10:44:34 +00:00
|
|
|
#endif
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
Application::setAttribute(Qt::AA_UseHighDpiPixmaps);
|
2017-02-07 22:06:28 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
Application app(argc, argv);
|
|
|
|
return app.exec();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|