qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
desktopnotify.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox 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.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #if DESKTOP_NOTIFICATIONS
21 #include "desktopnotify.h"
22 
24 
25 #include <libsnore/snore.h>
26 
27 #include <QDebug>
28 #include <QThread>
29 
31  : notifyCore{Snore::SnoreCore::instance()}
32  , snoreIcon{":/img/icons/qtox.svg"}
33 {
34 
35  notifyCore.loadPlugins(Snore::SnorePlugin::Backend);
36  qDebug() << "primary notification backend:" << notifyCore.primaryNotificationBackend();
37 
38  snoreApp = Snore::Application("qTox", snoreIcon);
39 
40  notifyCore.registerApplication(snoreApp);
41 
42  connect(&notifyCore, &Snore::SnoreCore::notificationClosed, this, &DesktopNotify::onNotificationClose);
43 }
44 
45 void DesktopNotify::notifyMessage(const NotificationData& notificationData)
46 {
47  const Settings& s = Settings::getInstance();
48  if(!(s.getNotify() && s.getDesktopNotify())) {
49  return;
50  }
51 
52  auto icon = notificationData.pixmap.isNull() ? snoreIcon : Snore::Icon(notificationData.pixmap);
53  auto newNotification = Snore::Notification{snoreApp, Snore::Alert(), notificationData.title, notificationData.message, icon, 0};
54  latestId = newNotification.id();
55 
56  if (lastNotification.isValid()) {
57  // Workaround for broken updating behavior in snore. Snore increments
58  // the message count when a notification is updated. Snore also caps the
59  // number of outgoing messages at 3. This means that if we update
60  // notifications more than 3 times we do not get notifications until the
61  // user activates the notification.
62  //
63  // We work around this by closing the existing notification and replacing
64  // it with a new one. We then only process the notification close if the
65  // latest notification id is the same as the one we are closing. This allows
66  // us to continue counting how many unread messages a user has until they
67  // close the notification themselves.
68  //
69  // I've filed a bug on the snorenotify mailing list but the project seems
70  // pretty dead. I filed a ticket on March 11 2020, and as of April 5 2020
71  // the moderators have not even acknowledged the message. A previous message
72  // got a response starting with "Snorenotify isn't that well maintained any more"
73  // (see https://mail.kde.org/pipermail/snorenotify/2019-March/000004.html)
74  // so I don't have hope of this being fixed any time soon
75  notifyCore.requestCloseNotification(lastNotification, Snore::Notification::CloseReasons::Dismissed);
76  }
77 
78  notifyCore.broadcastNotification(newNotification);
79  lastNotification = newNotification;
80 }
81 
82 void DesktopNotify::onNotificationClose(Snore::Notification notification)
83 {
84  if (notification.id() == latestId) {
85  lastNotification = {};
86  emit notificationClosed();
87  }
88 }
89 #endif
Settings
Definition: settings.h:51
NotificationData::pixmap
QPixmap pixmap
Definition: notificationdata.h:29
settings.h
desktopnotify.h
Settings::getNotify
bool getNotify() const override
Definition: settings.cpp:1559
DesktopNotify::lastNotification
Snore::Notification lastNotification
Definition: desktopnotify.h:50
DesktopNotify::notificationClosed
void notificationClosed()
NotificationData
Definition: notificationdata.h:25
DesktopNotify::snoreIcon
Snore::Icon snoreIcon
Definition: desktopnotify.h:49
DesktopNotify::notifyCore
Snore::SnoreCore & notifyCore
Definition: desktopnotify.h:47
DesktopNotify::latestId
uint latestId
Definition: desktopnotify.h:51
Settings::getInstance
static Settings & getInstance()
Returns the singleton instance.
Definition: settings.cpp:88
NotificationData::message
QString message
Definition: notificationdata.h:28
NotificationData::title
QString title
Definition: notificationdata.h:27
DesktopNotify::onNotificationClose
void onNotificationClose(Snore::Notification notification)
Settings::getDesktopNotify
bool getDesktopNotify() const override
Definition: settings.cpp:1585
DesktopNotify::notifyMessage
void notifyMessage(const NotificationData &notificationData)
DesktopNotify::snoreApp
Snore::Application snoreApp
Definition: desktopnotify.h:48
DesktopNotify::DesktopNotify
DesktopNotify()