Show system notifications in case of new activity on Comunic

This commit is contained in:
Pierre HUBERT 2020-06-13 14:59:02 +02:00
parent bf1ee550e3
commit 2635e92b8d
6 changed files with 89 additions and 0 deletions

View File

@ -24,3 +24,8 @@ void NotificationsNumber::setNewNotifs(int newNotifs)
{ {
mNewNotifs = newNotifs; mNewNotifs = newNotifs;
} }
int NotificationsNumber::sum() const
{
return mUnreadConversations + mNewNotifs;
}

View File

@ -18,6 +18,8 @@ public:
int newNotifs() const; int newNotifs() const;
void setNewNotifs(int newNotifs); void setNewNotifs(int newNotifs);
int sum() const;
private: private:
int mNewNotifs; int mNewNotifs;
int mUnreadConversations; int mUnreadConversations;

View File

@ -1,5 +1,7 @@
#include "refreshservice.h" #include "refreshservice.h"
#include <QGuiApplication>
RefreshService *RefreshService::svc = nullptr; RefreshService *RefreshService::svc = nullptr;
@ -8,6 +10,8 @@ void RefreshService::startService()
if(svc != nullptr) if(svc != nullptr)
return; return;
QGuiApplication::setQuitOnLastWindowClosed(false);
svc = new RefreshService(); svc = new RefreshService();
} }
@ -15,6 +19,9 @@ void RefreshService::stopService()
{ {
if(svc != nullptr) if(svc != nullptr)
svc->deleteLater(); svc->deleteLater();
QGuiApplication::setQuitOnLastWindowClosed(true);
svc = nullptr; svc = nullptr;
} }
@ -23,11 +30,33 @@ void RefreshService::connectedToWebSocket()
mNotifsHelper.getNewNotificationsNumbers(); mNotifsHelper.getNewNotificationsNumbers();
} }
void RefreshService::onNewNumberNotifications(NotificationsNumber numbers)
{
mNumbers = numbers;
mTrayIcon.onNewNumber(numbers);
}
void RefreshService::onNewNeumberUnreadNotifs(int num)
{
mNumbers.setNewNotifs(num);
mTrayIcon.onNewNumber(mNumbers);
}
void RefreshService::onNewNeumberUnreadConvs(int num)
{
mNumbers.setUnreadConversations(num);
mTrayIcon.onNewNumber(mNumbers);
}
RefreshService::RefreshService() RefreshService::RefreshService()
{ {
qDebug("Start refresh service"); qDebug("Start refresh service");
connect(&mWsClient, &WsClient::connected, this, &RefreshService::connectedToWebSocket); connect(&mWsClient, &WsClient::connected, this, &RefreshService::connectedToWebSocket);
connect(&mNotifsHelper, &NotificationsHelper::onNewNumber, this, &RefreshService::onNewNumberNotifications);
connect(&mWsClient, &WsClient::newNumberNotifs, this, &RefreshService::onNewNeumberUnreadNotifs);
connect(&mWsClient, &WsClient::newNumberConvs, this, &RefreshService::onNewNeumberUnreadConvs);
} }
RefreshService::~RefreshService() RefreshService::~RefreshService()

View File

@ -22,6 +22,9 @@ public:
private slots: private slots:
void connectedToWebSocket(); void connectedToWebSocket();
void onNewNumberNotifications(NotificationsNumber number);
void onNewNeumberUnreadNotifs(int num);
void onNewNeumberUnreadConvs(int num);
private: private:
RefreshService(); RefreshService();
@ -31,5 +34,6 @@ private:
static RefreshService *svc; static RefreshService *svc;
WsClient mWsClient; WsClient mWsClient;
NotificationsHelper mNotifsHelper; NotificationsHelper mNotifsHelper;
NotificationsNumber mNumbers;
TrayIcon mTrayIcon; TrayIcon mTrayIcon;
}; };

View File

@ -20,7 +20,41 @@ TrayIcon::~TrayIcon()
mMenu->deleteLater(); mMenu->deleteLater();
} }
void TrayIcon::onNewNumber(const NotificationsNumber &number)
{
if(number.sum() > mOldNumber) {
showNotification(number);
}
mOldNumber = number.sum();
}
void TrayIcon::onQuit() void TrayIcon::onQuit()
{ {
QApplication::exit(); QApplication::exit();
} }
void TrayIcon::showNotification(const NotificationsNumber &n)
{
QString msg;
if(n.newNotifs() == 1)
msg += tr("1 new notification");
else if(n.newNotifs() > 1)
msg += tr("%1 new notifications").arg(n.newNotifs());
if(n.unreadConversations() > 0) {
if(!msg.isEmpty())
msg += tr(" and ");
if(n.unreadConversations() == 1)
msg += tr("1 unread conversation");
else if(n.unreadConversations() > 1)
msg += tr("%1 unread conversations").arg(n.unreadConversations());
}
msg = tr("You have %1.").arg(msg);
mTrayIcon.showMessage(tr("Comunic"), msg, QIcon(":/logo_large.png"));
}

View File

@ -9,6 +9,8 @@
#include <QObject> #include <QObject>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include "notificationsnumber.h"
class TrayIcon : public QObject class TrayIcon : public QObject
{ {
Q_OBJECT Q_OBJECT
@ -16,13 +18,26 @@ public:
explicit TrayIcon(QObject *parent = nullptr); explicit TrayIcon(QObject *parent = nullptr);
~TrayIcon(); ~TrayIcon();
public slots:
void onNewNumber(const NotificationsNumber &number);
signals: signals:
private slots: private slots:
void onQuit(); void onQuit();
private: private:
/**
* Show a notification
*
* @param n The number of unread conversations
*/
void showNotification(const NotificationsNumber &n);
// Class members
QMenu *mMenu; QMenu *mMenu;
QSystemTrayIcon mTrayIcon; QSystemTrayIcon mTrayIcon;
int mOldNumber = 0;
}; };