File indexing completed on 2024-05-05 05:38:57
0001 /* 0002 SPDX-FileCopyrightText: 2009 Marco Martin <notmart@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "statusnotifierwatcher.h" 0008 0009 #include <QDBusConnection> 0010 #include <QDBusServiceWatcher> 0011 #include <QDebug> 0012 0013 #include <kpluginfactory.h> 0014 0015 #include "statusnotifieritem_interface.h" 0016 #include "statusnotifierwatcheradaptor.h" 0017 0018 K_PLUGIN_CLASS_WITH_JSON(StatusNotifierWatcher, "statusnotifierwatcher.json") 0019 0020 StatusNotifierWatcher::StatusNotifierWatcher(QObject *parent, const QList<QVariant> &) 0021 : KDEDModule(parent) 0022 { 0023 setModuleName(QStringLiteral("StatusNotifierWatcher")); 0024 new StatusNotifierWatcherAdaptor(this); 0025 QDBusConnection dbus = QDBusConnection::sessionBus(); 0026 dbus.registerObject(QStringLiteral("/StatusNotifierWatcher"), this); 0027 0028 m_serviceWatcher = new QDBusServiceWatcher(this); 0029 m_serviceWatcher->setConnection(dbus); 0030 m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration); 0031 0032 connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &StatusNotifierWatcher::serviceUnregistered); 0033 } 0034 0035 StatusNotifierWatcher::~StatusNotifierWatcher() 0036 { 0037 } 0038 0039 void StatusNotifierWatcher::RegisterStatusNotifierItem(const QString &serviceOrPath) 0040 { 0041 QString service; 0042 QString path; 0043 if (serviceOrPath.startsWith(QLatin1Char('/'))) { 0044 service = message().service(); 0045 path = serviceOrPath; 0046 } else { 0047 service = serviceOrPath; 0048 path = QStringLiteral("/StatusNotifierItem"); 0049 } 0050 QString notifierItemId = service + path; 0051 if (m_registeredServices.contains(notifierItemId)) { 0052 return; 0053 } 0054 m_serviceWatcher->addWatchedService(service); 0055 if (QDBusConnection::sessionBus().interface()->isServiceRegistered(service).value()) { 0056 // check if the service has registered a SystemTray object 0057 org::kde::StatusNotifierItem trayclient(service, path, QDBusConnection::sessionBus()); 0058 if (trayclient.isValid()) { 0059 qDebug() << "Registering" << notifierItemId << "to system tray"; 0060 m_registeredServices.append(notifierItemId); 0061 Q_EMIT StatusNotifierItemRegistered(notifierItemId); 0062 } else { 0063 m_serviceWatcher->removeWatchedService(service); 0064 } 0065 } else { 0066 m_serviceWatcher->removeWatchedService(service); 0067 } 0068 } 0069 0070 QStringList StatusNotifierWatcher::RegisteredStatusNotifierItems() const 0071 { 0072 return m_registeredServices; 0073 } 0074 0075 void StatusNotifierWatcher::serviceUnregistered(const QString &name) 0076 { 0077 qDebug() << "Service " << name << "unregistered"; 0078 m_serviceWatcher->removeWatchedService(name); 0079 0080 const QString match = name + QLatin1Char('/'); 0081 QStringList::Iterator it = m_registeredServices.begin(); 0082 while (it != m_registeredServices.end()) { 0083 if (it->startsWith(match)) { 0084 QString name = *it; 0085 it = m_registeredServices.erase(it); 0086 Q_EMIT StatusNotifierItemUnregistered(name); 0087 } else { 0088 ++it; 0089 } 0090 } 0091 } 0092 0093 void StatusNotifierWatcher::RegisterStatusNotifierHost(const QString &service) 0094 { 0095 Q_UNUSED(service) 0096 } 0097 0098 bool StatusNotifierWatcher::IsStatusNotifierHostRegistered() const 0099 { 0100 return true; 0101 } 0102 0103 int StatusNotifierWatcher::ProtocolVersion() const 0104 { 0105 return 0; 0106 } 0107 0108 #include "statusnotifierwatcher.moc"