1
/***************************************************************************
2
* Copyright 2009 by Marco Martin <notmart@gmail.com> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
9
* This program is distributed in the hope that it will be useful, *
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12
* GNU General Public License for more details. *
14
* You should have received a copy of the GNU General Public License *
15
* along with this program; if not, write to the *
16
* Free Software Foundation, Inc., *
17
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18
***************************************************************************/
20
#include "statusnotifierwatcher.h"
22
#include <QDBusConnection>
23
#include <QDBusServiceWatcher>
26
#include <kaboutdata.h>
29
#include <kpluginfactory.h>
30
#include <kpluginloader.h>
31
#include "statusnotifierwatcheradaptor.h"
32
#include "statusnotifieritem_interface.h"
35
static inline KAboutData aboutData()
37
return KAboutData("statusnotifierwatcher", 0, ki18n("statusnotifierwatcher"), KDE_VERSION_STRING);
40
K_PLUGIN_FACTORY(StatusNotifierWatcherFactory,
41
registerPlugin<StatusNotifierWatcher>();
43
K_EXPORT_PLUGIN(StatusNotifierWatcherFactory(aboutData()))
45
StatusNotifierWatcher::StatusNotifierWatcher(QObject *parent, const QList<QVariant>&)
48
setModuleName("StatusNotifierWatcher");
49
new StatusNotifierWatcherAdaptor(this);
50
QDBusConnection dbus = QDBusConnection::sessionBus();
51
dbus.registerService("org.kde.StatusNotifierWatcher");
52
dbus.registerObject("/StatusNotifierWatcher", this);
54
m_serviceWatcher = new QDBusServiceWatcher(this);
55
m_serviceWatcher->setConnection(dbus);
56
m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
58
connect(m_serviceWatcher, SIGNAL(serviceUnregistered(const QString&)), this, SLOT(serviceUnregistered(const QString&)));
61
StatusNotifierWatcher::~StatusNotifierWatcher()
63
QDBusConnection dbus = QDBusConnection::sessionBus();
64
dbus.unregisterService("org.kde.StatusNotifierWatcher");
68
void StatusNotifierWatcher::RegisterStatusNotifierItem(const QString &serviceOrPath)
72
if (serviceOrPath.startsWith('/')) {
73
service = message().service();
76
service = serviceOrPath;
77
path = "/StatusNotifierItem";
79
QString notifierItemId = service + path;
80
if (QDBusConnection::sessionBus().interface()->isServiceRegistered(service).value() &&
81
!m_registeredServices.contains(notifierItemId)) {
82
kDebug()<<"Registering" << notifierItemId << "to system tray";
84
//check if the service has registered a SystemTray object
85
org::kde::StatusNotifierItem trayclient(service, path,
86
QDBusConnection::sessionBus());
87
if (trayclient.isValid()) {
88
m_registeredServices.append(notifierItemId);
89
m_serviceWatcher->addWatchedService(service);
90
emit StatusNotifierItemRegistered(notifierItemId);
95
QStringList StatusNotifierWatcher::RegisteredStatusNotifierItems() const
97
return m_registeredServices;
101
void StatusNotifierWatcher::serviceUnregistered(const QString& name)
103
kDebug()<<"Service "<< name << "unregistered";
104
m_serviceWatcher->removeWatchedService(name);
106
QString match = name + '/';
107
QStringList::Iterator it = m_registeredServices.begin();
108
while (it != m_registeredServices.end()) {
109
if (it->startsWith(match)) {
111
it = m_registeredServices.erase(it);
112
emit StatusNotifierItemUnregistered(name);
118
if (m_statusNotifierHostServices.contains(name)) {
119
m_statusNotifierHostServices.remove(name);
120
emit StatusNotifierHostUnregistered();
124
void StatusNotifierWatcher::RegisterStatusNotifierHost(const QString &service)
126
if (service.contains("org.kde.StatusNotifierHost-") &&
127
QDBusConnection::sessionBus().interface()->isServiceRegistered(service).value() &&
128
!m_statusNotifierHostServices.contains(service)) {
129
kDebug()<<"Registering"<<service<<"as system tray";
131
m_statusNotifierHostServices.insert(service);
132
m_serviceWatcher->addWatchedService(service);
133
emit StatusNotifierHostRegistered();
137
bool StatusNotifierWatcher::IsStatusNotifierHostRegistered() const
139
return !m_statusNotifierHostServices.isEmpty();
142
int StatusNotifierWatcher::ProtocolVersion() const
147
#include "statusnotifierwatcher.moc"