mirror of
https://gitlab.com/comunic/comunicwatcher
synced 2024-11-23 22:09:27 +00:00
Show system notifications in case of new activity on Comunic
This commit is contained in:
parent
bf1ee550e3
commit
2635e92b8d
@ -24,3 +24,8 @@ void NotificationsNumber::setNewNotifs(int newNotifs)
|
||||
{
|
||||
mNewNotifs = newNotifs;
|
||||
}
|
||||
|
||||
int NotificationsNumber::sum() const
|
||||
{
|
||||
return mUnreadConversations + mNewNotifs;
|
||||
}
|
||||
|
@ -18,6 +18,8 @@ public:
|
||||
int newNotifs() const;
|
||||
void setNewNotifs(int newNotifs);
|
||||
|
||||
int sum() const;
|
||||
|
||||
private:
|
||||
int mNewNotifs;
|
||||
int mUnreadConversations;
|
||||
|
@ -1,5 +1,7 @@
|
||||
#include "refreshservice.h"
|
||||
|
||||
#include <QGuiApplication>
|
||||
|
||||
RefreshService *RefreshService::svc = nullptr;
|
||||
|
||||
|
||||
@ -8,6 +10,8 @@ void RefreshService::startService()
|
||||
if(svc != nullptr)
|
||||
return;
|
||||
|
||||
QGuiApplication::setQuitOnLastWindowClosed(false);
|
||||
|
||||
svc = new RefreshService();
|
||||
}
|
||||
|
||||
@ -15,6 +19,9 @@ void RefreshService::stopService()
|
||||
{
|
||||
if(svc != nullptr)
|
||||
svc->deleteLater();
|
||||
|
||||
QGuiApplication::setQuitOnLastWindowClosed(true);
|
||||
|
||||
svc = nullptr;
|
||||
}
|
||||
|
||||
@ -23,11 +30,33 @@ void RefreshService::connectedToWebSocket()
|
||||
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()
|
||||
{
|
||||
qDebug("Start refresh service");
|
||||
|
||||
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()
|
||||
|
@ -22,6 +22,9 @@ public:
|
||||
|
||||
private slots:
|
||||
void connectedToWebSocket();
|
||||
void onNewNumberNotifications(NotificationsNumber number);
|
||||
void onNewNeumberUnreadNotifs(int num);
|
||||
void onNewNeumberUnreadConvs(int num);
|
||||
|
||||
private:
|
||||
RefreshService();
|
||||
@ -31,5 +34,6 @@ private:
|
||||
static RefreshService *svc;
|
||||
WsClient mWsClient;
|
||||
NotificationsHelper mNotifsHelper;
|
||||
NotificationsNumber mNumbers;
|
||||
TrayIcon mTrayIcon;
|
||||
};
|
||||
|
34
trayicon.cpp
34
trayicon.cpp
@ -20,7 +20,41 @@ TrayIcon::~TrayIcon()
|
||||
mMenu->deleteLater();
|
||||
}
|
||||
|
||||
void TrayIcon::onNewNumber(const NotificationsNumber &number)
|
||||
{
|
||||
if(number.sum() > mOldNumber) {
|
||||
showNotification(number);
|
||||
}
|
||||
|
||||
mOldNumber = number.sum();
|
||||
}
|
||||
|
||||
void TrayIcon::onQuit()
|
||||
{
|
||||
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"));
|
||||
}
|
||||
|
15
trayicon.h
15
trayicon.h
@ -9,6 +9,8 @@
|
||||
#include <QObject>
|
||||
#include <QSystemTrayIcon>
|
||||
|
||||
#include "notificationsnumber.h"
|
||||
|
||||
class TrayIcon : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -16,13 +18,26 @@ public:
|
||||
explicit TrayIcon(QObject *parent = nullptr);
|
||||
~TrayIcon();
|
||||
|
||||
public slots:
|
||||
void onNewNumber(const NotificationsNumber &number);
|
||||
|
||||
signals:
|
||||
|
||||
private slots:
|
||||
void onQuit();
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* Show a notification
|
||||
*
|
||||
* @param n The number of unread conversations
|
||||
*/
|
||||
void showNotification(const NotificationsNumber &n);
|
||||
|
||||
// Class members
|
||||
QMenu *mMenu;
|
||||
QSystemTrayIcon mTrayIcon;
|
||||
int mOldNumber = 0;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user