summary refs log tree commit diff
path: root/src/dock/Dock.cpp
blob: a4745e547f424679eed033ab9b8ddf2656125dd3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "dock/Dock.h"

#include <QApplication>

#include "Logging.h"

#if defined(NHEKO_DBUS_SYS)
#include <qdbusconnectioninterface.h>
Dock::Dock(QObject *parent)
  : QObject(parent)
  , unityServiceWatcher(new QDBusServiceWatcher(this))
{
    unityServiceWatcher->setConnection(QDBusConnection::sessionBus());
    unityServiceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration |
                                      QDBusServiceWatcher::WatchForRegistration);
    unityServiceWatcher->addWatchedService(QStringLiteral("com.canonical.Unity"));
    connect(unityServiceWatcher,
            &QDBusServiceWatcher::serviceRegistered,
            this,
            [this](const QString &service) {
                Q_UNUSED(service);
                unityServiceAvailable = true;
                nhlog::ui()->info("Unity service available: {}", unityServiceAvailable);
            });
    connect(unityServiceWatcher,
            &QDBusServiceWatcher::serviceUnregistered,
            this,
            [this](const QString &service) {
                Q_UNUSED(service);
                unityServiceAvailable = false;
                nhlog::ui()->info("Unity service available: {}", unityServiceAvailable);
            });
    QDBusPendingCall listNamesCall =
      QDBusConnection::sessionBus().interface()->asyncCall(QStringLiteral("ListNames"));
    QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(listNamesCall, this);
    connect(callWatcher,
            &QDBusPendingCallWatcher::finished,
            this,
            [this](QDBusPendingCallWatcher *watcher) {
                QDBusPendingReply<QStringList> reply = *watcher;
                watcher->deleteLater();

                if (reply.isError()) {
                    nhlog::ui()->error("Failed to list dbus names");
                    return;
                }

                const QStringList &services = reply.value();

                unityServiceAvailable = services.contains(QLatin1String("com.canonical.Unity"));
                nhlog::ui()->info("Unity service available: {}", unityServiceAvailable);
            });
}

void
Dock::setUnreadCount(const int count)
{
    unitySetNotificationCount(count);
}
void
Dock::unitySetNotificationCount(const int count)
{
    if (unityServiceAvailable) {
        const QString launcherId =
          QLatin1String("application://%1.desktop").arg(qApp->desktopFileName());

        const QVariantMap properties{{QStringLiteral("count-visible"), count > 0},
                                     {QStringLiteral("count"), count}};

        QDBusMessage message =
          QDBusMessage::createSignal(QStringLiteral("/im/nheko/Nheko/UnityLauncher"),
                                     QStringLiteral("com.canonical.Unity.LauncherEntry"),
                                     QStringLiteral("Update"));
        message.setArguments({launcherId, properties});
        QDBusConnection::sessionBus().send(message);
    }
}
#else
Dock::Dock(QObject *parent)
  : QObject(parent)
{
}
void
Dock::setUnreadCount(const int count)
{
    qGuiApp->setBadgeNumber(count);
}
#endif