diff --git a/notificationsnumber.cpp b/notificationsnumber.cpp index f8b92f1..2ba71a4 100644 --- a/notificationsnumber.cpp +++ b/notificationsnumber.cpp @@ -24,3 +24,8 @@ void NotificationsNumber::setNewNotifs(int newNotifs) { mNewNotifs = newNotifs; } + +int NotificationsNumber::sum() const +{ + return mUnreadConversations + mNewNotifs; +} diff --git a/notificationsnumber.h b/notificationsnumber.h index 38f9f87..8308c57 100644 --- a/notificationsnumber.h +++ b/notificationsnumber.h @@ -18,6 +18,8 @@ public: int newNotifs() const; void setNewNotifs(int newNotifs); + int sum() const; + private: int mNewNotifs; int mUnreadConversations; diff --git a/refreshservice.cpp b/refreshservice.cpp index 376163a..936061d 100644 --- a/refreshservice.cpp +++ b/refreshservice.cpp @@ -1,5 +1,7 @@ #include "refreshservice.h" +#include + 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() diff --git a/refreshservice.h b/refreshservice.h index 2f2bea4..58439a9 100644 --- a/refreshservice.h +++ b/refreshservice.h @@ -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; }; diff --git a/trayicon.cpp b/trayicon.cpp index cafad97..7e3b081 100644 --- a/trayicon.cpp +++ b/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")); +} diff --git a/trayicon.h b/trayicon.h index addd1ab..7363294 100644 --- a/trayicon.h +++ b/trayicon.h @@ -9,6 +9,8 @@ #include #include +#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; };