statusnotifierwatcher.cpp 5.25 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/***************************************************************************
 *   Copyright 2009 by Marco Martin <notmart@gmail.com>                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
 ***************************************************************************/

#include "statusnotifierwatcher.h"

#include <QDBusConnection>
#include <QDBusServiceWatcher>
24
#include <QDebug>
25
26

#include <kpluginfactory.h>
Lukáš Tinkl's avatar
Lukáš Tinkl committed
27

28
29
30
#include "statusnotifierwatcheradaptor.h"
#include "statusnotifieritem_interface.h"

31
K_PLUGIN_CLASS_WITH_JSON(StatusNotifierWatcher, "statusnotifierwatcher.json")
32
33
34
35

StatusNotifierWatcher::StatusNotifierWatcher(QObject *parent, const QList<QVariant>&)
      : KDEDModule(parent)
{
36
    setModuleName(QStringLiteral("StatusNotifierWatcher"));
37
38
    new StatusNotifierWatcherAdaptor(this);
    QDBusConnection dbus = QDBusConnection::sessionBus();
39
    dbus.registerObject(QStringLiteral("/StatusNotifierWatcher"), this);
40
    dbus.registerService(QStringLiteral("org.kde.StatusNotifierWatcher"));
41
42
43
44
45

    m_serviceWatcher = new QDBusServiceWatcher(this);
    m_serviceWatcher->setConnection(dbus);
    m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);

46
    connect(m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &StatusNotifierWatcher::serviceUnregistered);
47
48
49
50
51
}

StatusNotifierWatcher::~StatusNotifierWatcher()
{
    QDBusConnection dbus = QDBusConnection::sessionBus();
52
    dbus.unregisterService(QStringLiteral("org.kde.StatusNotifierWatcher"));
53
54
55
56
57
58
59
}


void StatusNotifierWatcher::RegisterStatusNotifierItem(const QString &serviceOrPath)
{
    QString service;
    QString path;
60
    if (serviceOrPath.startsWith(QLatin1Char('/'))) {
61
62
63
64
        service = message().service();
        path = serviceOrPath;
    } else {
        service = serviceOrPath;
65
        path = QStringLiteral("/StatusNotifierItem");
66
67
    }
    QString notifierItemId = service + path;
68
69
70
71
72
    if (m_registeredServices.contains(notifierItemId)) {
        return;
    }
    m_serviceWatcher->addWatchedService(service);
    if (QDBusConnection::sessionBus().interface()->isServiceRegistered(service).value()) {
73
        //check if the service has registered a SystemTray object
74
        org::kde::StatusNotifierItem trayclient(service, path, QDBusConnection::sessionBus());
75
        if (trayclient.isValid()) {
76
            qDebug() << "Registering" << notifierItemId << "to system tray";
77
78
            m_registeredServices.append(notifierItemId);
            emit StatusNotifierItemRegistered(notifierItemId);
79
80
        } else {
            m_serviceWatcher->removeWatchedService(service);
81
        }
82
83
    } else {
        m_serviceWatcher->removeWatchedService(service);
84
85
86
87
88
89
90
91
92
93
94
    }
}

QStringList StatusNotifierWatcher::RegisteredStatusNotifierItems() const
{
    return m_registeredServices;
}


void StatusNotifierWatcher::serviceUnregistered(const QString& name)
{
95
    qDebug()<<"Service "<< name << "unregistered";
96
97
    m_serviceWatcher->removeWatchedService(name);

98
    QString match = name + QLatin1Char('/');
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
    QStringList::Iterator it = m_registeredServices.begin();
    while (it != m_registeredServices.end()) {
        if (it->startsWith(match)) {
            QString name = *it;
            it = m_registeredServices.erase(it);
            emit StatusNotifierItemUnregistered(name);
        } else {
            ++it;
        }
    }

    if (m_statusNotifierHostServices.contains(name)) {
        m_statusNotifierHostServices.remove(name);
        emit StatusNotifierHostUnregistered();
    }
}

void StatusNotifierWatcher::RegisterStatusNotifierHost(const QString &service)
{
Laurent Montel's avatar
Laurent Montel committed
118
    if (service.contains(QLatin1String("org.kde.StatusNotifierHost-")) &&
119
120
        QDBusConnection::sessionBus().interface()->isServiceRegistered(service).value() &&
        !m_statusNotifierHostServices.contains(service)) {
121
        qDebug()<<"Registering"<<service<<"as system tray";
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139

        m_statusNotifierHostServices.insert(service);
        m_serviceWatcher->addWatchedService(service);
        emit StatusNotifierHostRegistered();
    }
}

bool StatusNotifierWatcher::IsStatusNotifierHostRegistered() const
{
    return !m_statusNotifierHostServices.isEmpty();
}

int StatusNotifierWatcher::ProtocolVersion() const
{
    return 0;
}

#include "statusnotifierwatcher.moc"