1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-22 07:42:37 +00:00

qt: refactor Application (#7092)

This commit is contained in:
H5117 2024-12-19 00:47:37 +03:00 committed by GitHub
parent f2aeb11b07
commit 59c1c1e83b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 46 additions and 39 deletions

View file

@ -42,7 +42,13 @@
namespace 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 StatsRefreshIntervalMsec = 3000;
auto constexpr SessionRefreshIntervalMsec = 3000; auto constexpr SessionRefreshIntervalMsec = 3000;
@ -61,6 +67,32 @@ bool loadTranslation(QTranslator& translator, QString const& name, QLocale const
return false; 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() [[nodiscard]] auto makeWindowIcon()
{ {
// first, try to load it from the system theme // first, try to load it from the system theme
@ -221,10 +253,10 @@ Application::Application(
if (auto bus = QDBusConnection::sessionBus(); bus.isConnected()) if (auto bus = QDBusConnection::sessionBus(); bus.isConnected())
{ {
bus.connect( bus.connect(
fdo_notifications_service_name_, FDONotificationsServiceName,
fdo_notifications_path_, FDONotificationsPath,
fdo_notifications_interface_name_, FDONotificationsInterfaceName,
QLatin1String("ActionInvoked"), QStringLiteral("ActionInvoked"),
this, this,
SLOT(onNotificationActionInvoked(quint32, QString))); 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 void Application::onTorrentsEdited(torrent_ids_t const& torrent_ids) const
{ {
// the backend's tr_info has changed, so reload those fields // 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 void Application::notifyTorrentAdded(Torrent const* tor) const
{ {
QStringList actions; 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); 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()) if (auto bus = QDBusConnection::sessionBus(); bus.isConnected())
{ {
QDBusMessage m = QDBusMessage::createMethodCall( QDBusMessage m = QDBusMessage::createMethodCall(
fdo_notifications_service_name_, FDONotificationsServiceName,
fdo_notifications_path_, FDONotificationsPath,
fdo_notifications_interface_name_, FDONotificationsInterfaceName,
QStringLiteral("Notify")); QStringLiteral("Notify"));
QVariantList args; QVariantList args;
args.append(QStringLiteral("Transmission")); // app_name args.append(QStringLiteral("Transmission")); // app_name
@ -538,7 +551,9 @@ bool Application::notifyApp(QString const& title, QString const& body, QStringLi
#ifdef QT_DBUS_LIB #ifdef QT_DBUS_LIB
void Application::onNotificationActionInvoked(quint32 /* notification_id */, QString action_key) 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()) if (match.hasMatch())
{ {
int const torrent_id = match.captured(1).toInt(); int const torrent_id = match.captured(1).toInt();

View file

@ -10,6 +10,8 @@
#include <unordered_set> #include <unordered_set>
#include <QApplication> #include <QApplication>
#include <QPixmap>
#include <QPointer>
#include <QRegularExpression> #include <QRegularExpression>
#include <QTimer> #include <QTimer>
#include <QTranslator> #include <QTranslator>
@ -99,9 +101,7 @@ private slots:
private: private:
void maybeUpdateBlocklist() const; void maybeUpdateBlocklist() const;
void loadTranslations(); void loadTranslations();
void initUnits();
QStringList getNames(torrent_ids_t const& ids) const; QStringList getNames(torrent_ids_t const& ids) const;
void quitLater() const;
void notifyTorrentAdded(Torrent const*) const; void notifyTorrentAdded(Torrent const*) const;
std::unordered_set<QString> interned_strings_; std::unordered_set<QString> interned_strings_;
@ -119,14 +119,6 @@ private:
QTranslator app_translator_; QTranslator app_translator_;
FaviconCache<QPixmap> favicon_cache_; 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()) #define trApp dynamic_cast<Application*>(Application::instance())