1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

feat(notification): Notifications now always replace the previous one

This is a feature/fix to improve notification behavior when we receive
over 3 messages.

SnoreNotify prevents over 3 notifications from being displayed before a
user clears the notification. This is presumably to avoid infinite
notification spam.

Unfortunately this results in the notifications just coming in _after_
the user clears them. This means if there are 100s of messages built up
the user has to clear their notifications N messages / 3 times.

This feature/fix folds all notifications into a single notification
representing the current qTox notification state. See
notificationgenerator_test.cpp for what the new messages look like.
This commit is contained in:
Mick Sayson 2020-03-01 14:37:57 -08:00 committed by Anthony Bilinski
parent a9f6543e43
commit 6e163ca5ed
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
11 changed files with 193 additions and 116 deletions

View File

@ -1,3 +1,22 @@
/*
Copyright © 2020 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <QString>

View File

@ -1,3 +1,22 @@
/*
Copyright © 2020 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#include "notificationgenerator.h"
#include "src/chatlog/content/filetransferwidget.h"
@ -24,8 +43,8 @@ namespace
QString generateMultiChatTitle(size_t numChats, size_t numMessages)
{
// FIXME: how do I tr this
return QObject::tr("%1 messages from %2 chats")
//: e.g. 3 messages from 2 chats
return QObject::tr("%1 message(s) from %2 chats")
.arg(numMessages)
.arg(numChats);
}
@ -37,14 +56,14 @@ namespace
{
if (numNotifications[contact] > 1)
{
return QObject::tr("%1 messages from %2")
//: e.g. 2 messages from Bob
return QObject::tr("%1 message(s) from %2")
.arg(numNotifications[contact])
.arg(contact->getDisplayedName());
}
else
{
return QObject::tr("%1")
.arg(contact->getDisplayedName());
return contact->getDisplayedName();
}
}
@ -200,7 +219,8 @@ NotificationData NotificationGenerator::fileTransferNotification(const Friend* f
}
else
{
ret.title = f->getDisplayedName() + " - " + tr("File sent");
//: e.g. Bob - file transfer
ret.title = tr("%1 - file transfer").arg(f->getDisplayedName());
ret.message = filename + " (" + FileTransferWidget::getHumanReadableSize(fileSize) + ")";
}
@ -218,7 +238,7 @@ NotificationData NotificationGenerator::groupInvitationNotification(const Friend
return ret;
}
ret.title = from->getDisplayedName() + tr(" invites you to join a group.");
ret.title = tr("%1 invites you to join a group.").arg(from->getDisplayedName());
ret.message = "";
ret.pixmap = getSenderAvatar(profile, from->getPublicKey());
@ -234,7 +254,7 @@ NotificationData NotificationGenerator::friendRequestNotification(const ToxPk& s
return ret;
}
ret.title = sender.toString() + tr(" sent you a friend request.");
ret.title = tr("Friend request received from %1").arg(sender.toString());
ret.message = message;
return ret;

View File

@ -1,3 +1,22 @@
/*
Copyright © 2020 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once

View File

@ -1,5 +1,5 @@
/*
Copyright © 2014-2019 by The qTox Project Contributors
Copyright © 2020 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
@ -17,8 +17,7 @@
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef INOTIFICATION_SETTINGS_H
#define INOTIFICATION_SETTINGS_H
#pragma once
#include <QStringList>
@ -46,5 +45,3 @@ public:
virtual bool getGroupAlwaysNotify() const = 0;
virtual void setGroupAlwaysNotify(bool newValue) = 0;
};
#endif /*INOTIFICATION_SETTINGS_H*/

View File

@ -25,6 +25,7 @@
#include <libsnore/snore.h>
#include <QDebug>
#include <QThread>
DesktopNotify::DesktopNotify()
: notifyCore{Snore::SnoreCore::instance()}
@ -37,43 +38,52 @@ DesktopNotify::DesktopNotify()
snoreApp = Snore::Application("qTox", snoreIcon);
notifyCore.registerApplication(snoreApp);
connect(&notifyCore, &Snore::SnoreCore::notificationClosed, this, &DesktopNotify::onNotificationClose);
}
void DesktopNotify::createNotification(const QString& title, const QString& text, Snore::Icon& icon)
void DesktopNotify::notifyMessage(const NotificationData& notificationData)
{
const Settings& s = Settings::getInstance();
if(!(s.getNotify() && s.getDesktopNotify())) {
return;
}
Snore::Notification notify{snoreApp, Snore::Alert(), title, text, icon};
auto icon = notificationData.pixmap.isNull() ? snoreIcon : Snore::Icon(notificationData.pixmap);
auto newNotification = Snore::Notification{snoreApp, Snore::Alert(), notificationData.title, notificationData.message, icon, 0};
latestId = newNotification.id();
notifyCore.broadcastNotification(notify);
}
void DesktopNotify::notifyMessage(const QString& title, const QString& message)
{
createNotification(title, message, snoreIcon);
}
void DesktopNotify::notifyMessagePixmap(const QString& title, const QString& message, QPixmap avatar)
{
Snore::Icon new_icon(avatar);
createNotification(title, message, new_icon);
}
void DesktopNotify::notifyMessageSimple(const MessageType type)
{
QString message;
switch (type) {
case MessageType::FRIEND: message = tr("New message"); break;
case MessageType::FRIEND_FILE: message = tr("Incoming file transfer"); break;
case MessageType::FRIEND_REQUEST: message = tr("Friend request received"); break;
case MessageType::GROUP: message = tr("New group message"); break;
case MessageType::GROUP_INVITE: message = tr("Group invite received"); break;
default: break;
if (lastNotification.isValid()) {
// Workaround for broken updating behavior in snore. Snore increments
// the message count when a notification is updated. Snore also caps the
// number of outgoing messages at 3. This means that if we update
// notifications more than 3 times we do not get notifications until the
// user activates the notification.
//
// We work around this by closing the existing notification and replacing
// it with a new one. We then only process the notification close if the
// latest notification id is the same as the one we are closing. This allows
// us to continue counting how many unread messages a user has until they
// close the notification themselves.
//
// I've filed a bug on the snorenotify mailing list but the project seems
// pretty dead. I filed a ticket on March 11 2020, and as of April 5 2020
// the moderators have not even acknowledged the message. A previous message
// got a response starting with "Snorenotify isn't that well maintained any more"
// (see https://mail.kde.org/pipermail/snorenotify/2019-March/000004.html)
// so I don't have hope of this being fixed any time soon
notifyCore.requestCloseNotification(lastNotification, Snore::Notification::CloseReasons::Dismissed);
}
createNotification(message, {}, snoreIcon);
notifyCore.broadcastNotification(newNotification);
lastNotification = newNotification;
}
void DesktopNotify::onNotificationClose(Snore::Notification notification)
{
if (notification.id() == latestId) {
lastNotification = {};
emit notificationClosed();
}
}
#endif

View File

@ -19,11 +19,14 @@
#pragma once
#if DESKTOP_NOTIFICATIONS
#include "src/model/notificationdata.h"
#include <libsnore/snore.h>
#include <QObject>
#include <memory>
#include <unordered_set>
class DesktopNotify : public QObject
{
@ -31,25 +34,19 @@ class DesktopNotify : public QObject
public:
DesktopNotify();
enum class MessageType {
FRIEND,
FRIEND_FILE,
FRIEND_REQUEST,
GROUP,
GROUP_INVITE
};
public slots:
void notifyMessage(const QString& title, const QString& message);
void notifyMessagePixmap(const QString& title, const QString& message, QPixmap avatar);
void notifyMessageSimple(const MessageType type);
void notifyMessage(const NotificationData& notificationData);
private:
void createNotification(const QString& title, const QString& text, Snore::Icon& icon);
signals:
void notificationClosed();
private slots:
void onNotificationClose(Snore::Notification notification);
private:
Snore::SnoreCore& notifyCore;
Snore::Application snoreApp;
Snore::Icon snoreIcon;
Snore::Notification lastNotification;
uint latestId;
};
#endif // DESKTOP_NOTIFICATIONS

View File

@ -294,6 +294,11 @@ void Widget::init()
profileInfo = new ProfileInfo(core, profile);
profileForm = new ProfileForm(profileInfo);
#if DESKTOP_NOTIFICATIONS
notificationGenerator.reset(new NotificationGenerator(settings, profile));
connect(&notifier, &DesktopNotify::notificationClosed, notificationGenerator.get(), &NotificationGenerator::onNotificationActivated);
#endif
// connect logout tray menu action
connect(actionLogout, &QAction::triggered, profileForm, &ProfileForm::onLogoutClicked);
@ -1498,7 +1503,7 @@ void Widget::addGroupDialog(Group* group, ContentDialog* dialog)
emit widget->chatroomWidgetClicked(widget);
}
bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, bool sound, bool file)
bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, bool sound, QString filename, size_t filesize)
{
bool hasActive;
QWidget* currentWindow;
@ -1535,17 +1540,9 @@ bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, b
widget->updateStatusLight();
ui->friendList->trackWidget(widget);
#if DESKTOP_NOTIFICATIONS
if (settings.getNotifyHide()) {
notifier.notifyMessageSimple(file ? DesktopNotify::MessageType::FRIEND_FILE
: DesktopNotify::MessageType::FRIEND);
} else {
QString title = f->getDisplayedName();
if (file) {
title += " - " + tr("File sent");
}
notifier.notifyMessagePixmap(title, text,
Nexus::getProfile()->loadAvatar(f->getPublicKey()));
}
auto notificationData = filename.isEmpty() ? notificationGenerator->friendMessageNotification(f, text)
: notificationGenerator->fileTransferNotification(f, filename, filesize);
notifier.notifyMessage(notificationData);
#endif
if (contentDialog == nullptr) {
@ -1586,18 +1583,8 @@ bool Widget::newGroupMessageAlert(const GroupId& groupId, const ToxPk& authorPk,
g->setEventFlag(true);
widget->updateStatusLight();
#if DESKTOP_NOTIFICATIONS
if (settings.getNotifyHide()) {
notifier.notifyMessageSimple(DesktopNotify::MessageType::GROUP);
} else {
Friend* f = FriendList::findFriend(authorPk);
QString title = g->getPeerList().value(authorPk) + " (" + g->getDisplayedName() + ")";
if (!f) {
notifier.notifyMessage(title, message);
} else {
notifier.notifyMessagePixmap(title, message,
Nexus::getProfile()->loadAvatar(f->getPublicKey()));
}
}
auto notificationData = notificationGenerator->groupMessageNotification(g, authorPk, message);
notifier.notifyMessage(notificationData);
#endif
if (contentDialog == nullptr) {
@ -1673,11 +1660,8 @@ void Widget::onFriendRequestReceived(const ToxPk& friendPk, const QString& messa
friendRequestsUpdate();
newMessageAlert(window(), isActiveWindow(), true, true);
#if DESKTOP_NOTIFICATIONS
if (settings.getNotifyHide()) {
notifier.notifyMessageSimple(DesktopNotify::MessageType::FRIEND_REQUEST);
} else {
notifier.notifyMessage(friendPk.toString() + tr(" sent you a friend request."), message);
}
auto notificationData = notificationGenerator->friendRequestNotification(friendPk, message);
notifier.notifyMessage(notificationData);
#endif
}
}
@ -1686,9 +1670,8 @@ void Widget::onFileReceiveRequested(const ToxFile& file)
{
const ToxPk& friendPk = FriendList::id2Key(file.friendId);
newFriendMessageAlert(friendPk,
file.fileName + " ("
+ FileTransferWidget::getHumanReadableSize(file.filesize) + ")",
true, true);
{},
true, file.fileName, file.filesize);
}
void Widget::updateFriendActivity(const Friend& frnd)
@ -1934,12 +1917,8 @@ void Widget::onGroupInviteReceived(const GroupInvite& inviteInfo)
groupInvitesUpdate();
newMessageAlert(window(), isActiveWindow(), true, true);
#if DESKTOP_NOTIFICATIONS
if (settings.getNotifyHide()) {
notifier.notifyMessageSimple(DesktopNotify::MessageType::GROUP_INVITE);
} else {
notifier.notifyMessagePixmap(f->getDisplayedName() + tr(" invites you to join a group."),
{}, Nexus::getProfile()->loadAvatar(f->getPublicKey()));
}
auto notificationData = notificationGenerator->groupInvitationNotification(f);
notifier.notifyMessage(notificationData);
#endif
}
} else {

View File

@ -38,6 +38,7 @@
#include "src/model/friendmessagedispatcher.h"
#include "src/model/groupmessagedispatcher.h"
#if DESKTOP_NOTIFICATIONS
#include "src/model/notificationgenerator.h"
#include "src/platform/desktop_notifications/desktopnotify.h"
#endif
@ -127,7 +128,7 @@ public:
void addFriendDialog(const Friend* frnd, ContentDialog* dialog);
void addGroupDialog(Group* group, ContentDialog* dialog);
bool newFriendMessageAlert(const ToxPk& friendId, const QString& text, bool sound = true,
bool file = false);
QString filename = QString(), size_t filesize = 0);
bool newGroupMessageAlert(const GroupId& groupId, const ToxPk& authorPk, const QString& message,
bool notify);
bool getIsWindowMinimized();
@ -360,6 +361,7 @@ private:
MessageProcessor::SharedParams sharedMessageProcessorParams;
#if DESKTOP_NOTIFICATIONS
std::unique_ptr<NotificationGenerator> notificationGenerator;
DesktopNotify notifier;
#endif

View File

@ -1,3 +1,22 @@
/*
Copyright © 2020 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "src/core/icoreidhandler.h"

View File

@ -1,3 +1,22 @@
/*
Copyright © 2020 by The qTox Project Contributors
This file is part of qTox, a Qt-based graphical interface for Tox.
qTox is libre software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
qTox is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with qTox. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include "src/core/icoregroupquery.h"

View File

@ -114,11 +114,11 @@ void TestNotificationGenerator::testMultipleFriendMessages()
f.setName("friendName");
notificationGenerator->friendMessageNotification(&f, "test");
auto notificationData = notificationGenerator->friendMessageNotification(&f, "test2");
QVERIFY(notificationData.title == "2 messages from friendName");
QVERIFY(notificationData.title == "2 message(s) from friendName");
QVERIFY(notificationData.message == "test2");
notificationData = notificationGenerator->friendMessageNotification(&f, "test3");
QVERIFY(notificationData.title == "3 messages from friendName");
QVERIFY(notificationData.title == "3 message(s) from friendName");
QVERIFY(notificationData.message == "test3");
}
@ -140,7 +140,6 @@ void TestNotificationGenerator::testNotificationClear()
void TestNotificationGenerator::testGroupMessage()
{
Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler);
g.setName("groupName");
auto sender = groupQuery->getGroupPeerPk(0, 0);
g.updateUsername(sender, "sender1");
@ -152,15 +151,17 @@ void TestNotificationGenerator::testGroupMessage()
void TestNotificationGenerator::testMultipleGroupMessages()
{
Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler);
g.setName("groupName");
auto sender = groupQuery->getGroupPeerPk(0, 0);
g.updateUsername(sender, "sender1");
auto sender2 = groupQuery->getGroupPeerPk(0, 1);
g.updateUsername(sender2, "sender2");
notificationGenerator->groupMessageNotification(&g, sender, "test1");
auto notificationData = notificationGenerator->groupMessageNotification(&g, sender2, "test2");
QVERIFY(notificationData.title == "groupName");
QVERIFY(notificationData.title == "2 message(s) from groupName");
QVERIFY(notificationData.message == "sender2: test2");
}
@ -175,17 +176,14 @@ void TestNotificationGenerator::testMultipleFriendSourceMessages()
notificationGenerator->friendMessageNotification(&f, "test1");
auto notificationData = notificationGenerator->friendMessageNotification(&f2, "test2");
QVERIFY(notificationData.title == "2 messages from 2 chats");
QVERIFY(notificationData.title == "2 message(s) from 2 chats");
QVERIFY(notificationData.message == "friend1, friend2");
}
void TestNotificationGenerator::testMultipleGroupSourceMessages()
{
Group g(0, GroupId(QByteArray(32, 0)), "groupName", false, "selfName", *groupQuery, *coreIdHandler);
g.setName("groupName");
Group g2(1, GroupId(QByteArray(32, 1)), "groupName", false, "selfName", *groupQuery, *coreIdHandler);
g.setName("groupName2");
Group g2(1, GroupId(QByteArray(32, 1)), "groupName2", false, "selfName", *groupQuery, *coreIdHandler);
auto sender = groupQuery->getGroupPeerPk(0, 0);
g.updateUsername(sender, "sender1");
@ -193,7 +191,7 @@ void TestNotificationGenerator::testMultipleGroupSourceMessages()
notificationGenerator->groupMessageNotification(&g, sender, "test1");
auto notificationData = notificationGenerator->groupMessageNotification(&g2, sender, "test1");
QVERIFY(notificationData.title == "2 messages from 2 chats");
QVERIFY(notificationData.title == "2 message(s) from 2 chats");
QVERIFY(notificationData.message == "groupName, groupName2");
}
@ -202,8 +200,7 @@ void TestNotificationGenerator::testMixedSourceMessages()
Friend f(0, ToxPk());
f.setName("friend");
Group g(0, GroupId(QByteArray(32, 0)), "groupName", false, "selfName", *groupQuery, *coreIdHandler);
g.setName("group");
Group g(0, GroupId(QByteArray(32, 0)), "group", false, "selfName", *groupQuery, *coreIdHandler);
auto sender = groupQuery->getGroupPeerPk(0, 0);
g.updateUsername(sender, "sender1");
@ -211,11 +208,11 @@ void TestNotificationGenerator::testMixedSourceMessages()
notificationGenerator->friendMessageNotification(&f, "test1");
auto notificationData = notificationGenerator->groupMessageNotification(&g, sender, "test2");
QVERIFY(notificationData.title == "2 messages from 2 chats");
QVERIFY(notificationData.title == "2 message(s) from 2 chats");
QVERIFY(notificationData.message == "friend, group");
notificationData = notificationGenerator->fileTransferNotification(&f, "file", 0);
QVERIFY(notificationData.title == "3 messages from 2 chats");
QVERIFY(notificationData.title == "3 message(s) from 2 chats");
QVERIFY(notificationData.message == "friend, group");
}
@ -226,7 +223,7 @@ void TestNotificationGenerator::testFileTransfer()
auto notificationData = notificationGenerator->fileTransferNotification(&f, "file", 5 * 1024 * 1024 /* 5MB */);
QVERIFY(notificationData.title == "friend - File sent");
QVERIFY(notificationData.title == "friend - file transfer");
QVERIFY(notificationData.message == "file (5.00MiB)");
}
@ -238,7 +235,7 @@ void TestNotificationGenerator::testFileTransferAfterMessage()
notificationGenerator->friendMessageNotification(&f, "test1");
auto notificationData = notificationGenerator->fileTransferNotification(&f, "file", 5 * 1024 * 1024 /* 5MB */);
QVERIFY(notificationData.title == "2 messages from friend");
QVERIFY(notificationData.title == "2 message(s) from friend");
QVERIFY(notificationData.message == "Incoming file transfer");
}
@ -262,7 +259,7 @@ void TestNotificationGenerator::testGroupInviteUncounted()
notificationGenerator->groupInvitationNotification(&f);
auto notificationData = notificationGenerator->friendMessageNotification(&f, "test2");
QVERIFY(notificationData.title == "2 messages from friend");
QVERIFY(notificationData.title == "2 message(s) from friend");
QVERIFY(notificationData.message == "test2");
}
@ -272,7 +269,7 @@ void TestNotificationGenerator::testFriendRequest()
auto notificationData = notificationGenerator->friendRequestNotification(sender, "request");
QVERIFY(notificationData.title == "0000000000000000000000000000000000000000000000000000000000000000 sent you a friend request.");
QVERIFY(notificationData.title == "Friend request received from 0000000000000000000000000000000000000000000000000000000000000000");
QVERIFY(notificationData.message == "request");
}
@ -286,7 +283,7 @@ void TestNotificationGenerator::testFriendRequestUncounted()
notificationGenerator->friendRequestNotification(sender, "request");
auto notificationData = notificationGenerator->friendMessageNotification(&f, "test2");
QVERIFY(notificationData.title == "2 messages from friend");
QVERIFY(notificationData.title == "2 message(s) from friend");
QVERIFY(notificationData.message == "test2");
}
@ -319,7 +316,6 @@ void TestNotificationGenerator::testSimpleFileTransfer()
void TestNotificationGenerator::testSimpleGroupMessage()
{
Group g(0, GroupId(0), "groupName", false, "selfName", *groupQuery, *coreIdHandler);
g.setName("groupName");
auto sender = groupQuery->getGroupPeerPk(0, 0);
g.updateUsername(sender, "sender1");
@ -367,7 +363,7 @@ void TestNotificationGenerator::testSimpleMessageToggle()
auto notificationData = notificationGenerator->friendMessageNotification(&f, "test2");
QVERIFY(notificationData.title == "2 messages from friend");
QVERIFY(notificationData.title == "2 message(s) from friend");
QVERIFY(notificationData.message == "test2");
}