From eff8c08ccfd5095dd0226ac171ebebe93dae7a9c Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Wed, 13 Jan 2021 20:14:51 +0000 Subject: [PATCH] Add launcher badge to NeoChat showing the unread count --- src/roomlistmodel.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index fe5bc392f..abddc09e0 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -14,11 +14,26 @@ #include #include #include +#ifndef Q_OS_ANDROID +#include +#include +#include +#endif #include #include #include +#ifndef Q_OS_ANDROID +bool useUnityCounter() { + static const auto Result = QDBusInterface( + "com.canonical.Unity", + "/").isValid(); + + return Result; +} +#endif + RoomListModel::RoomListModel(QObject *parent) : QAbstractListModel(parent) { @@ -26,6 +41,37 @@ RoomListModel::RoomListModel(QObject *parent) for (auto collapsedSection : collapsedSections) { m_categoryVisibility[collapsedSection] = false; } + +#ifndef Q_OS_ANDROID + connect(this, &RoomListModel::notificationCountChanged, this, [this]() { + if (useUnityCounter()) { + // copied from Telegram desktop + const auto launcherUrl = "application://org.kde.neochat.desktop"; + // Gnome requires that count is a 64bit integer + const qint64 counterSlice = std::min(m_notificationCount, 9999); + QVariantMap dbusUnityProperties; + + if (counterSlice > 0) { + dbusUnityProperties["count"] = counterSlice; + dbusUnityProperties["count-visible"] = true; + } else { + dbusUnityProperties["count-visible"] = false; + } + + auto signal = QDBusMessage::createSignal( + "/com/canonical/unity/launcherentry/org.kde.neochat", + "com.canonical.Unity.LauncherEntry", + "Update"); + + signal.setArguments({ + launcherUrl, + dbusUnityProperties + }); + + QDBusConnection::sessionBus().send(signal); + } + }); +#endif } RoomListModel::~RoomListModel() = default; @@ -182,6 +228,9 @@ void RoomListModel::refreshNotificationCount() for (auto room : qAsConst(m_rooms)) { count += room->notificationCount(); } + if (m_notificationCount == count) { + return; + } m_notificationCount = count; Q_EMIT notificationCountChanged(); }