| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "dock/Dock.h" |
| 6 | |
| 7 | #include <QApplication> |
| 8 | |
| 9 | #include "Logging.h" |
| 10 | |
| 11 | #if defined(NHEKO_DBUS_SYS) |
| 12 | #include <qdbusconnectioninterface.h> |
| 13 | Dock::Dock(QObject *parent) |
| 14 | : QObject(parent) |
| 15 | , unityServiceWatcher(new QDBusServiceWatcher(this)) |
| 16 | { |
| 17 | unityServiceWatcher->setConnection(QDBusConnection::sessionBus()); |
| 18 | unityServiceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration | |
| 19 | QDBusServiceWatcher::WatchForRegistration); |
| 20 | unityServiceWatcher->addWatchedService(QStringLiteral("com.canonical.Unity")); |
| 21 | connect(unityServiceWatcher, |
| 22 | &QDBusServiceWatcher::serviceRegistered, |
| 23 | this, |
| 24 | [this](const QString &service) { |
| 25 | Q_UNUSED(service); |
| 26 | unityServiceAvailable = true; |
| 27 | nhlog::ui()->info("Unity service available: {}", unityServiceAvailable); |
| 28 | }); |
| 29 | connect(unityServiceWatcher, |
| 30 | &QDBusServiceWatcher::serviceUnregistered, |
| 31 | this, |
| 32 | [this](const QString &service) { |
| 33 | Q_UNUSED(service); |
| 34 | unityServiceAvailable = false; |
| 35 | nhlog::ui()->info("Unity service available: {}", unityServiceAvailable); |
| 36 | }); |
| 37 | QDBusPendingCall listNamesCall = |
| 38 | QDBusConnection::sessionBus().interface()->asyncCall(QStringLiteral("ListNames")); |
| 39 | QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(listNamesCall, this); |
| 40 | connect(callWatcher, |
| 41 | &QDBusPendingCallWatcher::finished, |
| 42 | this, |
| 43 | [this](QDBusPendingCallWatcher *watcher) { |
| 44 | QDBusPendingReply<QStringList> reply = *watcher; |
| 45 | watcher->deleteLater(); |
| 46 | |
| 47 | if (reply.isError()) { |
| 48 | nhlog::ui()->error("Failed to list dbus names"); |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | const QStringList &services = reply.value(); |
| 53 | |
| 54 | unityServiceAvailable = services.contains(QLatin1String("com.canonical.Unity")); |
| 55 | nhlog::ui()->info("Unity service available: {}", unityServiceAvailable); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | Dock::setUnreadCount(const int count) |
| 61 | { |
| 62 | unitySetNotificationCount(count); |
| 63 | } |
| 64 | void |
| 65 | Dock::unitySetNotificationCount(const int count) |
| 66 | { |
| 67 | if (unityServiceAvailable) { |
| 68 | const QString launcherId = |
| 69 | QLatin1String("application://%1.desktop").arg(qApp->desktopFileName()); |
| 70 | |
| 71 | const QVariantMap properties{{QStringLiteral("count-visible"), count > 0}, |
| 72 | {QStringLiteral("count"), count}}; |
| 73 | |
| 74 | QDBusMessage message = |
| 75 | QDBusMessage::createSignal(QStringLiteral("/im/nheko/Nheko/UnityLauncher"), |
| 76 | QStringLiteral("com.canonical.Unity.LauncherEntry"), |
| 77 | QStringLiteral("Update")); |
| 78 | message.setArguments({launcherId, properties}); |
| 79 | QDBusConnection::sessionBus().send(message); |
| 80 | } |
| 81 | } |
| 82 | #else |
| 83 | Dock::Dock(QObject *parent) |
| 84 | : QObject(parent) |
| 85 | { |
| 86 | } |
| 87 | void |
| 88 | Dock::setUnreadCount(const int count) |
| 89 | { |
| 90 | qGuiApp->setBadgeNumber(count); |
| 91 | } |
| 92 | #endif |
| 93 | |
| 94 | #include "moc_Dock.cpp" |