From 3916aec358cb9ec81e7d55930bdc3ae55c38bc79 Mon Sep 17 00:00:00 2001 From: blue Date: Sat, 23 Apr 2022 16:58:08 +0300 Subject: [PATCH] unread messages count now is displayed on the launcher icon --- main/application.cpp | 18 ++++++++++++++++-- main/application.h | 1 + main/main.cpp | 1 + ui/models/element.cpp | 1 + ui/models/element.h | 1 + ui/models/roster.cpp | 13 +++++++++++++ ui/models/roster.h | 2 ++ 7 files changed, 35 insertions(+), 2 deletions(-) diff --git a/main/application.cpp b/main/application.cpp index f6ffe07..696ec03 100644 --- a/main/application.cpp +++ b/main/application.cpp @@ -21,7 +21,7 @@ Application::Application(Core::Squawk* p_core): availability(Shared::Availability::offline), core(p_core), squawk(nullptr), - notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications", QDBusConnection::sessionBus()), + notifications("org.freedesktop.Notifications", "/org/freedesktop/Notifications", "org.freedesktop.Notifications"), roster(), conversations(), dialogueQueue(roster), @@ -29,6 +29,7 @@ Application::Application(Core::Squawk* p_core): destroyingSquawk(false) { connect(&roster, &Models::Roster::unnoticedMessage, this, &Application::notify); + connect(&roster, &Models::Roster::unreadMessagesCountChanged, this, &Application::unreadMessagesCountChanged); //connecting myself to the backed @@ -100,6 +101,7 @@ void Application::quit() emit quitting(); writeSettings(); + unreadMessagesCountChanged(0); //this notification persist in the desktop, for now I'll zero it on quit not to confuse people for (Conversations::const_iterator itr = conversations.begin(), end = conversations.end(); itr != end; ++itr) { disconnect(itr->second, &Conversation::destroyed, this, &Application::onConversationClosed); itr->second->close(); @@ -212,7 +214,7 @@ void Application::notify(const QString& account, const Shared::Message& msg) args << body; args << QStringList(); args << QVariantMap({ - {"desktop-entry", QString(QCoreApplication::applicationName())}, + {"desktop-entry", qApp->desktopFileName()}, {"category", QString("message")}, // {"sound-file", "/path/to/macaw/squawk"}, {"sound-name", QString("message-new-instant")} @@ -225,6 +227,18 @@ void Application::notify(const QString& account, const Shared::Message& msg) } } +void Application::unreadMessagesCountChanged(int count) +{ + QDBusMessage signal = QDBusMessage::createSignal("/", "com.canonical.Unity.LauncherEntry", "Update"); + signal << qApp->desktopFileName() + QLatin1String(".desktop"); + signal << QVariantMap ({ + {"count-visible", count != 0}, + {"count", count} + }); + QDBusConnection::sessionBus().send(signal); +} + + void Application::setState(Shared::Availability p_availability) { if (availability != p_availability) { diff --git a/main/application.h b/main/application.h index 15adce7..7d70877 100644 --- a/main/application.h +++ b/main/application.h @@ -65,6 +65,7 @@ public slots: protected slots: void notify(const QString& account, const Shared::Message& msg); + void unreadMessagesCountChanged(int count); void setState(Shared::Availability availability); void changeAccount(const QString& account, const QMap& data); diff --git a/main/main.cpp b/main/main.cpp index 77719a2..60b3c83 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -50,6 +50,7 @@ int main(int argc, char *argv[]) QApplication::setOrganizationName("macaw.me"); QApplication::setApplicationDisplayName("Squawk"); QApplication::setApplicationVersion("0.2.2"); + app.setDesktopFileName("squawk"); QTranslator qtTranslator; qtTranslator.load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); diff --git a/ui/models/element.cpp b/ui/models/element.cpp index 4e741a4..0c709ab 100644 --- a/ui/models/element.cpp +++ b/ui/models/element.cpp @@ -171,6 +171,7 @@ void Models::Element::fileError(const QString& messageId, const QString& error, void Models::Element::onFeedUnreadMessagesCountChanged() { + emit unreadMessagesCountChanged(); if (type == contact) { changed(4); } else if (type == room) { diff --git a/ui/models/element.h b/ui/models/element.h index 94d67cb..dd90ea1 100644 --- a/ui/models/element.h +++ b/ui/models/element.h @@ -52,6 +52,7 @@ signals: void requestArchive(const QString& before); void fileDownloadRequest(const QString& url); void unnoticedMessage(const QString& account, const Shared::Message& msg); + void unreadMessagesCountChanged(); void localPathInvalid(const QString& path); protected: diff --git a/ui/models/roster.cpp b/ui/models/roster.cpp index fef3e43..b2caf6b 100644 --- a/ui/models/roster.cpp +++ b/ui/models/roster.cpp @@ -463,6 +463,7 @@ void Models::Roster::addContact(const QString& account, const QString& jid, cons connect(contact, &Contact::fileDownloadRequest, this, &Roster::fileDownloadRequest); connect(contact, &Contact::unnoticedMessage, this, &Roster::unnoticedMessage); connect(contact, &Contact::localPathInvalid, this, &Roster::localPathInvalid); + connect(contact, &Contact::unreadMessagesCountChanged, this, &Roster::recalculateUnreadMessages); contacts.insert(std::make_pair(id, contact)); } else { contact = itr->second; @@ -805,6 +806,7 @@ void Models::Roster::addRoom(const QString& account, const QString jid, const QM connect(room, &Room::fileDownloadRequest, this, &Roster::fileDownloadRequest); connect(room, &Room::unnoticedMessage, this, &Roster::unnoticedMessage); connect(room, &Room::localPathInvalid, this, &Roster::localPathInvalid); + connect(room, &Room::unreadMessagesCountChanged, this, &Roster::recalculateUnreadMessages); rooms.insert(std::make_pair(id, room)); acc->appendChild(room); } @@ -1049,3 +1051,14 @@ void Models::Roster::onAccountReconnected() } } +void Models::Roster::recalculateUnreadMessages() +{ + int count(0); + for (const std::pair& pair : contacts) { + count += pair.second->getMessagesCount(); + } + for (const std::pair& pair : rooms) { + count += pair.second->getMessagesCount(); + } + emit unreadMessagesCountChanged(count); +} diff --git a/ui/models/roster.h b/ui/models/roster.h index 60adf13..249947b 100644 --- a/ui/models/roster.h +++ b/ui/models/roster.h @@ -99,6 +99,7 @@ public: signals: void requestArchive(const QString& account, const QString& jid, const QString& before); void fileDownloadRequest(const QString& url); + void unreadMessagesCountChanged(int count); void unnoticedMessage(const QString& account, const Shared::Message& msg); void localPathInvalid(const QString& path); @@ -113,6 +114,7 @@ private slots: void onChildIsAboutToBeMoved(Item* source, int first, int last, Item* destination, int newIndex); void onChildMoved(); void onElementRequestArchive(const QString& before); + void recalculateUnreadMessages(); private: Item* root;