Skip to content

Commit

Permalink
fix(Notify): Use notification categories on Linux
Browse files Browse the repository at this point in the history
Use categories as defined by FreeDesktop. Addresses #424
  • Loading branch information
Pigpog committed Jan 12, 2025
1 parent 127e0cb commit 4a1cad0
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/model/notificationdata.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ struct NotificationData
{
QString title;
QString message;
QString category;
QPixmap pixmap;
};
23 changes: 23 additions & 0 deletions src/model/notificationgenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ NotificationData NotificationGenerator::friendMessageNotification(const Friend*
ret.title = generateTitle(friendNotifications, conferenceNotifications, f);
ret.message =
generateContent(friendNotifications, conferenceNotifications, message, f->getPublicKey());
ret.category = "im.received";
ret.pixmap = getSenderAvatar(profile, f->getPublicKey());

return ret;
}

NotificationData NotificationGenerator::incomingCallNotification(const Friend* f)
{
friendNotifications[f]++;

NotificationData ret;

if (notificationSettings.getNotifyHide()) {
ret.title = tr("Incoming call");
return ret;
}

ret.title = generateTitle(friendNotifications, conferenceNotifications, f);
ret.message = generateContent(friendNotifications, conferenceNotifications, "", f->getPublicKey());
ret.category = "call.incoming";
ret.pixmap = getSenderAvatar(profile, f->getPublicKey());

return ret;
Expand Down Expand Up @@ -195,6 +215,7 @@ NotificationData NotificationGenerator::fileTransferNotification(const Friend* f
ret.message = filename + " (" + getHumanReadableSize(fileSize) + ")";
}

ret.category = "transfer";
ret.pixmap = getSenderAvatar(profile, f->getPublicKey());

return ret;
Expand All @@ -211,6 +232,7 @@ NotificationData NotificationGenerator::conferenceInvitationNotification(const F

ret.title = tr("%1 invites you to join a conference.").arg(from->getDisplayedName());
ret.message = "";
ret.category = "im";
ret.pixmap = getSenderAvatar(profile, from->getPublicKey());

return ret;
Expand All @@ -228,6 +250,7 @@ NotificationData NotificationGenerator::friendRequestNotification(const ToxPk& s

ret.title = tr("Friend request received from %1").arg(sender.toString());
ret.message = message;
ret.category = "im";

return ret;
}
Expand Down
1 change: 1 addition & 0 deletions src/model/notificationgenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class NotificationGenerator : public QObject
NotificationGenerator& operator=(NotificationGenerator&&) = delete;

NotificationData friendMessageNotification(const Friend* f, const QString& message);
NotificationData incomingCallNotification(const Friend* f);
NotificationData conferenceMessageNotification(const Conference* c, const ToxPk& sender,
const QString& message);
NotificationData fileTransferNotification(const Friend* f, const QString& filename,
Expand Down
2 changes: 1 addition & 1 deletion src/platform/desktop_notifications/desktopnotify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void DesktopNotify::notifyMessage(const NotificationData& notificationData)
// Try system-backends first.
if (d->settings.getNotifySystemBackend()) {
if (d->dbus->showMessage(notificationData.title, notificationData.message,
notificationData.pixmap)) {
notificationData.category, notificationData.pixmap)) {
return;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/platform/desktop_notifications/desktopnotifybackend.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class DesktopNotifyBackend : public QObject
public:
explicit DesktopNotifyBackend(QObject* parent);
~DesktopNotifyBackend() override;

bool showMessage(const QString& title, const QString& message, const QPixmap& pixmap);
bool showMessage(const QString& title, const QString& message, const QString& category,
const QPixmap& pixmap);

signals:
void messageClicked();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,13 @@ DesktopNotifyBackend::DesktopNotifyBackend(QObject* parent)
DesktopNotifyBackend::~DesktopNotifyBackend() = default;

bool DesktopNotifyBackend::showMessage(const QString& title, const QString& message,
const QPixmap& pixmap)
const QString& category, const QPixmap& pixmap)
{
// Try Notify first.
if (d->notifyInterface.isValid()) {
QVariantMap hints{
{QStringLiteral("action-icons"), true},
{QStringLiteral("category"), QStringLiteral("im.received")},
{QStringLiteral("category"), category},
{QStringLiteral("sender-pid"),
QVariant::fromValue<quint64>(QCoreApplication::applicationPid())},
};
Expand Down
14 changes: 10 additions & 4 deletions src/widget/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1564,10 +1564,16 @@ bool Widget::newFriendMessageAlert(const ToxPk& friendId, const QString& text, b
widget->updateStatusLight();
ui->friendList->trackWidget(settings, style, widget);
if (notifier != nullptr) {
auto notificationData =
filename.isEmpty()
? notificationGenerator->friendMessageNotification(f, text)
: notificationGenerator->fileTransferNotification(f, filename, filesize);
NotificationData notificationData;
if (filename.isEmpty()) {
if (text.isEmpty()) {
notificationData = notificationGenerator->incomingCallNotification(f);
} else {
notificationData = notificationGenerator->friendMessageNotification(f, text);
}
} else {
notificationData = notificationGenerator->fileTransferNotification(f, filename, filesize);
}
notifier->notifyMessage(notificationData);
}

Expand Down

0 comments on commit 4a1cad0

Please sign in to comment.