2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "dbusnotifier.h"
32 #include <QDBusConnection>
33 #include <QDBusPendingCallWatcher>
34 #include <QDBusPendingReply>
36 #include "base/global.h"
37 #include "dbusnotificationsinterface.h"
39 DBusNotifier::DBusNotifier(QObject
*parent
)
41 , m_notificationsInterface
{new DBusNotificationsInterface(u
"org.freedesktop.Notifications"_s
42 , u
"/org/freedesktop/Notifications"_s
, QDBusConnection::sessionBus(), this)}
44 // Testing for 'DBusNotificationsInterface::isValid()' isn't helpful here.
45 // If the notification daemon is configured to run 'as needed'
46 // the above check can be false if the daemon wasn't started
47 // by another application. In this case DBus will be able to
48 // start the notification daemon and complete our request. Such
49 // a daemon is xfce4-notifyd, DBus autostarts it and after
50 // some inactivity shuts it down. Other DEs, like GNOME, choose
51 // to start their daemons at the session startup and have it sit
52 // idling for the whole session.
54 connect(m_notificationsInterface
, &DBusNotificationsInterface::ActionInvoked
, this, &DBusNotifier::onActionInvoked
);
55 connect(m_notificationsInterface
, &DBusNotificationsInterface::NotificationClosed
, this, &DBusNotifier::onNotificationClosed
);
58 void DBusNotifier::showMessage(const QString
&title
, const QString
&message
, const int timeout
)
60 // Assign "default" action to notification to make it clickable
61 const QStringList actions
{u
"default"_s
, {}};
62 const QVariantMap hints
{{u
"desktop-entry"_s
, u
"org.qbittorrent.qBittorrent"_s
}};
63 const QDBusPendingReply
<uint
> reply
= m_notificationsInterface
->notify(u
"qBittorrent"_s
, 0
64 , u
"qbittorrent"_s
, title
, message
, actions
, hints
, timeout
);
65 auto *watcher
= new QDBusPendingCallWatcher(reply
, this);
66 connect(watcher
, &QDBusPendingCallWatcher::finished
, this, [this](QDBusPendingCallWatcher
*self
)
68 const QDBusPendingReply
<uint
> reply
= *self
;
71 const uint messageID
= reply
.value();
72 m_activeMessages
.insert(messageID
);
79 void DBusNotifier::onActionInvoked(const uint messageID
, [[maybe_unused
]] const QString
&action
)
81 // Check whether the notification is sent by qBittorrent
82 // to avoid reacting to unrelated notifications
83 if (m_activeMessages
.contains(messageID
))
84 emit
messageClicked();
87 void DBusNotifier::onNotificationClosed(const uint messageID
, [[maybe_unused
]] const uint reason
)
89 m_activeMessages
.remove(messageID
);