blob: c07a0f6e391791bab159deb210ed5c42136932ab (
plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
#include "xdgstatusnotifierwatcher.h"
#include <statusnotifierwatcheradaptor.h>
#include <XdgStatusNotifierItemIface.h>
#include <QDBusConnection>
#include <debug.h>
using namespace xdg;
StatusNotifierWatcher::StatusNotifierWatcher(QObject *parent)
: QObject{parent}
{
QDBusConnection bus = QDBusConnection::sessionBus();
if (!bus.isConnected()) {
qCWarning(SYSTEM_TRAY) << "Cannot connect to the D-Bus session bus.\n" << "Please check your system settings and try again.\n";
}
if(!bus.registerService("org.kde.StatusNotifierWatcher"))
{
qCWarning(SYSTEM_TRAY) << "Don't register StatusNotifierWatcher service, probably already exists";
return;
}
qCDebug(SYSTEM_TRAY) << "Register StatusNotifierWatcher service";
new StatusNotifierWatcherAdaptor(this);
bus.registerObject("/StatusNotifierWatcher", this);
m_serviceWatcher = new QDBusServiceWatcher(this);
m_serviceWatcher->setConnection(bus);
m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &StatusNotifierWatcher::_serviceUnregistered);
}
StatusNotifierWatcher::~StatusNotifierWatcher()
{
}
void StatusNotifierWatcher::RegisterStatusNotifierItem(const QString &service)
{
QString path;
path = QStringLiteral("/StatusNotifierItem");
qCDebug(SYSTEM_TRAY) << "Watcher: RegisterStatusNotifierItem(" << service << ")";
QString notifierItemId = service + path;
if (m_items.contains(notifierItemId)) {
return;
}
m_serviceWatcher->addWatchedService(service);
if (QDBusConnection::sessionBus().interface()->isServiceRegistered(service).value()) {
// check if the service has registered a SystemTray object
org::kde::StatusNotifierItem trayclient(service, path, QDBusConnection::sessionBus());
if (trayclient.isValid()) {
qDebug() << "Registering" << notifierItemId << "to system tray";
m_items.append(notifierItemId);
emit StatusNotifierItemRegistered(notifierItemId);
} else {
m_serviceWatcher->removeWatchedService(service);
}
} else {
m_serviceWatcher->removeWatchedService(service);
}
}
void StatusNotifierWatcher::RegisterStatusNotifierHost(const QString &service)
{
qCDebug(SYSTEM_TRAY) << "Watcher: RegisterStatusNotifierHost(" << service << ")";
if (m_hosts.contains(service)) {
return;
}
m_serviceWatcher->addWatchedService(service);
m_hosts.append(service);
}
QStringList StatusNotifierWatcher::RegisteredStatusNotifierItems() const
{
return m_items;
}
bool StatusNotifierWatcher::IsStatusNotifierHostRegistered() const
{
return m_hosts.size() > 0;
}
int StatusNotifierWatcher::ProtocolVersion() const
{
return XDG_STATUS_NOTIFIER_WATCHER_PROTOCOL_VERSION;
}
void StatusNotifierWatcher::_serviceUnregistered(const QString &name)
{
qCDebug(SYSTEM_TRAY) << "Service " << name << "unregistered (ITEMS:" << m_items << " HOSTS:" << m_hosts << ")";
m_serviceWatcher->removeWatchedService(name);
const QString match = name + QLatin1Char('/');
QStringList::Iterator it = m_items.begin();
while (it != m_items.end()) {
if (it->startsWith(match)) {
QString name = *it;
it = m_items.erase(it);
emit StatusNotifierItemUnregistered(name);
} else {
++it;
}
}
it = m_hosts.begin();
while (it != m_hosts.end()) {
if (it->startsWith(match)) {
QString name = *it;
it = m_hosts.erase(it);
emit StatusNotifierHostUnregistered();
} else {
++it;
}
}
}
|