mirror of
https://github.com/transmission/transmission
synced 2024-12-21 15:22:37 +00:00
qt: refactor Application (#7092)
This commit is contained in:
parent
f2aeb11b07
commit
59c1c1e83b
2 changed files with 46 additions and 39 deletions
|
@ -42,7 +42,13 @@
|
|||
namespace
|
||||
{
|
||||
|
||||
auto const ConfigName = QLatin1String("transmission");
|
||||
auto const ConfigName = QStringLiteral("transmission");
|
||||
|
||||
#ifdef QT_DBUS_LIB
|
||||
auto const FDONotificationsServiceName = QStringLiteral("org.freedesktop.Notifications");
|
||||
auto const FDONotificationsPath = QStringLiteral("/org/freedesktop/Notifications");
|
||||
auto const FDONotificationsInterfaceName = QStringLiteral("org.freedesktop.Notifications");
|
||||
#endif
|
||||
|
||||
auto constexpr StatsRefreshIntervalMsec = 3000;
|
||||
auto constexpr SessionRefreshIntervalMsec = 3000;
|
||||
|
@ -61,6 +67,32 @@ bool loadTranslation(QTranslator& translator, QString const& name, QLocale const
|
|||
return false;
|
||||
}
|
||||
|
||||
void initUnits()
|
||||
{
|
||||
using Config = libtransmission::Values::Config;
|
||||
|
||||
Config::Speed = { Config::Base::Kilo,
|
||||
QObject::tr("B/s").toStdString(),
|
||||
QObject::tr("kB/s").toStdString(),
|
||||
QObject::tr("MB/s").toStdString(),
|
||||
QObject::tr("GB/s").toStdString(),
|
||||
QObject::tr("TB/s").toStdString() };
|
||||
|
||||
Config::Memory = { Config::Base::Kibi,
|
||||
QObject::tr("B").toStdString(),
|
||||
QObject::tr("KiB").toStdString(),
|
||||
QObject::tr("MiB").toStdString(),
|
||||
QObject::tr("GiB").toStdString(),
|
||||
QObject::tr("TiB").toStdString() };
|
||||
|
||||
Config::Storage = { Config::Base::Kilo,
|
||||
QObject::tr("B").toStdString(),
|
||||
QObject::tr("kB").toStdString(),
|
||||
QObject::tr("MB").toStdString(),
|
||||
QObject::tr("GB").toStdString(),
|
||||
QObject::tr("TB").toStdString() };
|
||||
}
|
||||
|
||||
[[nodiscard]] auto makeWindowIcon()
|
||||
{
|
||||
// first, try to load it from the system theme
|
||||
|
@ -221,10 +253,10 @@ Application::Application(
|
|||
if (auto bus = QDBusConnection::sessionBus(); bus.isConnected())
|
||||
{
|
||||
bus.connect(
|
||||
fdo_notifications_service_name_,
|
||||
fdo_notifications_path_,
|
||||
fdo_notifications_interface_name_,
|
||||
QLatin1String("ActionInvoked"),
|
||||
FDONotificationsServiceName,
|
||||
FDONotificationsPath,
|
||||
FDONotificationsInterfaceName,
|
||||
QStringLiteral("ActionInvoked"),
|
||||
this,
|
||||
SLOT(onNotificationActionInvoked(quint32, QString)));
|
||||
}
|
||||
|
@ -271,25 +303,6 @@ void Application::loadTranslations()
|
|||
}
|
||||
}
|
||||
|
||||
void Application::initUnits()
|
||||
{
|
||||
using Config = libtransmission::Values::Config;
|
||||
|
||||
Config::Speed = { Config::Base::Kilo, tr("B/s").toStdString(), tr("kB/s").toStdString(),
|
||||
tr("MB/s").toStdString(), tr("GB/s").toStdString(), tr("TB/s").toStdString() };
|
||||
|
||||
Config::Memory = { Config::Base::Kibi, tr("B").toStdString(), tr("KiB").toStdString(),
|
||||
tr("MiB").toStdString(), tr("GiB").toStdString(), tr("TiB").toStdString() };
|
||||
|
||||
Config::Storage = { Config::Base::Kilo, tr("B").toStdString(), tr("kB").toStdString(),
|
||||
tr("MB").toStdString(), tr("GB").toStdString(), tr("TB").toStdString() };
|
||||
}
|
||||
|
||||
void Application::quitLater() const
|
||||
{
|
||||
QTimer::singleShot(0, this, SLOT(quit()));
|
||||
}
|
||||
|
||||
void Application::onTorrentsEdited(torrent_ids_t const& torrent_ids) const
|
||||
{
|
||||
// the backend's tr_info has changed, so reload those fields
|
||||
|
@ -353,7 +366,7 @@ void Application::onTorrentsNeedInfo(torrent_ids_t const& torrent_ids) const
|
|||
void Application::notifyTorrentAdded(Torrent const* tor) const
|
||||
{
|
||||
QStringList actions;
|
||||
actions << QString{ QLatin1String("start-now(%1)") }.arg(tor->id()) << QObject::tr("Start Now");
|
||||
actions << QString{ QStringLiteral("start-now(%1)") }.arg(tor->id()) << QObject::tr("Start Now");
|
||||
notifyApp(tr("Torrent Added"), tor->name(), actions);
|
||||
}
|
||||
|
||||
|
@ -505,9 +518,9 @@ bool Application::notifyApp(QString const& title, QString const& body, QStringLi
|
|||
if (auto bus = QDBusConnection::sessionBus(); bus.isConnected())
|
||||
{
|
||||
QDBusMessage m = QDBusMessage::createMethodCall(
|
||||
fdo_notifications_service_name_,
|
||||
fdo_notifications_path_,
|
||||
fdo_notifications_interface_name_,
|
||||
FDONotificationsServiceName,
|
||||
FDONotificationsPath,
|
||||
FDONotificationsInterfaceName,
|
||||
QStringLiteral("Notify"));
|
||||
QVariantList args;
|
||||
args.append(QStringLiteral("Transmission")); // app_name
|
||||
|
@ -538,7 +551,9 @@ bool Application::notifyApp(QString const& title, QString const& body, QStringLi
|
|||
#ifdef QT_DBUS_LIB
|
||||
void Application::onNotificationActionInvoked(quint32 /* notification_id */, QString action_key)
|
||||
{
|
||||
auto const match = start_now_regex_.match(action_key);
|
||||
static QRegularExpression const start_now_regex{ QStringLiteral(R"rgx(start-now\((\d+)\))rgx") };
|
||||
|
||||
auto const match = start_now_regex.match(action_key);
|
||||
if (match.hasMatch())
|
||||
{
|
||||
int const torrent_id = match.captured(1).toInt();
|
||||
|
|
|
@ -10,6 +10,8 @@
|
|||
#include <unordered_set>
|
||||
|
||||
#include <QApplication>
|
||||
#include <QPixmap>
|
||||
#include <QPointer>
|
||||
#include <QRegularExpression>
|
||||
#include <QTimer>
|
||||
#include <QTranslator>
|
||||
|
@ -99,9 +101,7 @@ private slots:
|
|||
private:
|
||||
void maybeUpdateBlocklist() const;
|
||||
void loadTranslations();
|
||||
void initUnits();
|
||||
QStringList getNames(torrent_ids_t const& ids) const;
|
||||
void quitLater() const;
|
||||
void notifyTorrentAdded(Torrent const*) const;
|
||||
|
||||
std::unordered_set<QString> interned_strings_;
|
||||
|
@ -119,14 +119,6 @@ private:
|
|||
QTranslator app_translator_;
|
||||
|
||||
FaviconCache<QPixmap> favicon_cache_;
|
||||
|
||||
#ifdef QT_DBUS_LIB
|
||||
QString const fdo_notifications_service_name_ = QStringLiteral("org.freedesktop.Notifications");
|
||||
QString const fdo_notifications_path_ = QStringLiteral("/org/freedesktop/Notifications");
|
||||
QString const fdo_notifications_interface_name_ = QStringLiteral("org.freedesktop.Notifications");
|
||||
#endif
|
||||
|
||||
QRegularExpression const start_now_regex_{ QStringLiteral(R"rgx(start-now\((\d+)\))rgx") };
|
||||
};
|
||||
|
||||
#define trApp dynamic_cast<Application*>(Application::instance())
|
||||
|
|
Loading…
Reference in a new issue