1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2025-02-21 21:57:01 +00:00

Add a "Start Now" action to the notification window in the Qt client (#848)

* Add a "Start Now" action to the notification window in the Qt client

Co-authored-by: Charles Kerr <charles@charleskerr.com>

Co-authored-by: Mike Gelfand <mikedld@users.noreply.github.com>
This commit is contained in:
Daniel Kamil Kozar 2021-10-23 16:26:28 +02:00 committed by GitHub
parent 54b85583e6
commit 4996f3356a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 16 deletions

View file

@ -81,6 +81,14 @@ bool loadTranslation(QTranslator& translator, QString const& name, QLocale const
return false;
}
#ifdef QT_DBUS_LIB
QString const DbusServiceName = QStringLiteral("org.freedesktop.Notifications");
QString const DbusInterfaceName = QStringLiteral("org.freedesktop.Notifications");
QString const DbusPath = QStringLiteral("/org/freedesktop/Notifications");
#endif
} // namespace
Application::Application(int& argc, char** argv)
@ -353,6 +361,21 @@ Application::Application(int& argc, char** argv)
}
InteropHelper::registerObject(this);
#ifdef QT_DBUS_LIB
QDBusConnection bus = QDBusConnection::sessionBus();
if (bus.isConnected())
{
bus.connect(
DbusServiceName,
DbusPath,
DbusInterfaceName,
QLatin1String("ActionInvoked"),
this,
SLOT(onNotificationActionInvoked(quint32, QString)));
}
#endif
}
void Application::loadTranslations()
@ -414,9 +437,10 @@ void Application::onTorrentsAdded(torrent_ids_t const& ids) const
{
if (prefs_->getBool(Prefs::SHOW_NOTIFICATION_ON_ADD))
{
auto const title = tr("Torrent(s) Added", nullptr, static_cast<int>(ids.size()));
auto const body = getNames(ids).join(QStringLiteral("\n"));
notifyApp(title, body);
for (auto id : ids)
{
notifyTorrentAdded(model_->getTorrentFromId(id));
}
}
}
@ -449,6 +473,13 @@ void Application::onTorrentsNeedInfo(torrent_ids_t const& ids) const
}
}
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);
}
/***
****
***/
@ -580,30 +611,21 @@ void Application::raise() const
alert(window_.get());
}
bool Application::notifyApp(QString const& title, QString const& body) const
bool Application::notifyApp(QString const& title, QString const& body, QStringList const& actions) const
{
#ifdef QT_DBUS_LIB
auto const dbus_service_name = QStringLiteral("org.freedesktop.Notifications");
auto const dbus_interface_name = QStringLiteral("org.freedesktop.Notifications");
auto const dbus_path = QStringLiteral("/org/freedesktop/Notifications");
QDBusConnection bus = QDBusConnection::sessionBus();
if (bus.isConnected())
{
QDBusMessage m = QDBusMessage::createMethodCall(
dbus_service_name,
dbus_path,
dbus_interface_name,
QStringLiteral("Notify"));
QDBusMessage m = QDBusMessage::createMethodCall(DbusServiceName, DbusPath, DbusInterfaceName, QStringLiteral("Notify"));
QVariantList args;
args.append(QStringLiteral("Transmission")); // app_name
args.append(0U); // replaces_id
args.append(QStringLiteral("transmission")); // icon
args.append(title); // summary
args.append(body); // body
args.append(QStringList()); // actions - unused for plain passive popups
args.append(actions);
args.append(QVariantMap({
std::make_pair(QStringLiteral("category"), QVariant(QStringLiteral("transfer.complete"))),
})); // hints
@ -623,6 +645,20 @@ bool Application::notifyApp(QString const& title, QString const& body) const
return true;
}
#ifdef QT_DBUS_LIB
void Application::onNotificationActionInvoked(quint32 /* notification_id */, QString action_key)
{
static QRegularExpression const StartNowRegex(QLatin1String(R"rgx(start-now\((\d+)\))rgx"));
QRegularExpressionMatch const match = StartNowRegex.match(action_key);
if (match.hasMatch())
{
int const torrent_id = match.captured(1).toInt();
session_->startTorrentsNow({ torrent_id });
}
}
#endif
FaviconCache& Application::faviconCache()
{
return favicons_;

View file

@ -27,6 +27,7 @@ class Torrent;
class TorrentModel;
class MainWindow;
class WatchDir;
class Torrent;
class Application : public QApplication
{
@ -37,7 +38,7 @@ public:
Application(int& argc, char** argv);
void raise() const;
bool notifyApp(QString const& title, QString const& body) const;
bool notifyApp(QString const& title, QString const& body, QStringList const& actions = {}) const;
QString const& intern(QString const& in)
{
@ -60,12 +61,16 @@ private slots:
void refreshPref(int key) const;
void refreshTorrents();
void saveGeometry() const;
#ifdef QT_DBUS_LIB
void onNotificationActionInvoked(quint32 notification_id, QString action_key);
#endif
private:
void maybeUpdateBlocklist() const;
void loadTranslations();
QStringList getNames(torrent_ids_t const& ids) const;
void quitLater() const;
void notifyTorrentAdded(Torrent const*) const;
std::unique_ptr<Prefs> prefs_;
std::unique_ptr<Session> session_;