~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to statusnotifierwatcher/statusnotifierwatcher.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright 2009 by Marco Martin <notmart@gmail.com>                    *
 
3
 *                                                                         *
 
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.                                   *
 
8
 *                                                                         *
 
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.                          *
 
13
 *                                                                         *
 
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
 ***************************************************************************/
 
19
 
 
20
#include "statusnotifierwatcher.h"
 
21
 
 
22
#include <QDBusConnection>
 
23
#include <QDBusServiceWatcher>
 
24
 
 
25
#include <kglobal.h>
 
26
#include <kaboutdata.h>
 
27
#include <kdebug.h>
 
28
 
 
29
#include <kpluginfactory.h>
 
30
#include <kpluginloader.h>
 
31
#include "statusnotifierwatcheradaptor.h"
 
32
#include "statusnotifieritem_interface.h"
 
33
 
 
34
 
 
35
static inline KAboutData aboutData()
 
36
{
 
37
    return KAboutData("statusnotifierwatcher", 0, ki18n("statusnotifierwatcher"), KDE_VERSION_STRING);
 
38
}
 
39
 
 
40
K_PLUGIN_FACTORY(StatusNotifierWatcherFactory,
 
41
                 registerPlugin<StatusNotifierWatcher>();
 
42
    )
 
43
K_EXPORT_PLUGIN(StatusNotifierWatcherFactory(aboutData()))
 
44
 
 
45
StatusNotifierWatcher::StatusNotifierWatcher(QObject *parent, const QList<QVariant>&)
 
46
      : KDEDModule(parent)
 
47
{
 
48
    setModuleName("StatusNotifierWatcher");
 
49
    new StatusNotifierWatcherAdaptor(this);
 
50
    QDBusConnection dbus = QDBusConnection::sessionBus();
 
51
    dbus.registerService("org.kde.StatusNotifierWatcher");
 
52
    dbus.registerObject("/StatusNotifierWatcher", this);
 
53
 
 
54
    m_serviceWatcher = new QDBusServiceWatcher(this);
 
55
    m_serviceWatcher->setConnection(dbus);
 
56
    m_serviceWatcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
 
57
 
 
58
    connect(m_serviceWatcher, SIGNAL(serviceUnregistered(const QString&)), this, SLOT(serviceUnregistered(const QString&)));
 
59
}
 
60
 
 
61
StatusNotifierWatcher::~StatusNotifierWatcher()
 
62
{
 
63
    QDBusConnection dbus = QDBusConnection::sessionBus();
 
64
    dbus.unregisterService("org.kde.StatusNotifierWatcher");
 
65
}
 
66
 
 
67
 
 
68
void StatusNotifierWatcher::RegisterStatusNotifierItem(const QString &serviceOrPath)
 
69
{
 
70
    QString service;
 
71
    QString path;
 
72
    if (serviceOrPath.startsWith('/')) {
 
73
        service = message().service();
 
74
        path = serviceOrPath;
 
75
    } else {
 
76
        service = serviceOrPath;
 
77
        path = "/StatusNotifierItem";
 
78
    }
 
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";
 
83
 
 
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);
 
91
        }
 
92
    }
 
93
}
 
94
 
 
95
QStringList StatusNotifierWatcher::RegisteredStatusNotifierItems() const
 
96
{
 
97
    return m_registeredServices;
 
98
}
 
99
 
 
100
 
 
101
void StatusNotifierWatcher::serviceUnregistered(const QString& name)
 
102
{
 
103
    kDebug()<<"Service "<< name << "unregistered";
 
104
    m_serviceWatcher->removeWatchedService(name);
 
105
 
 
106
    QString match = name + '/';
 
107
    QStringList::Iterator it = m_registeredServices.begin();
 
108
    while (it != m_registeredServices.end()) {
 
109
        if (it->startsWith(match)) {
 
110
            QString name = *it;
 
111
            it = m_registeredServices.erase(it);
 
112
            emit StatusNotifierItemUnregistered(name);
 
113
        } else {
 
114
            ++it;
 
115
        }
 
116
    }
 
117
 
 
118
    if (m_statusNotifierHostServices.contains(name)) {
 
119
        m_statusNotifierHostServices.remove(name);
 
120
        emit StatusNotifierHostUnregistered();
 
121
    }
 
122
}
 
123
 
 
124
void StatusNotifierWatcher::RegisterStatusNotifierHost(const QString &service)
 
125
{
 
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";
 
130
 
 
131
        m_statusNotifierHostServices.insert(service);
 
132
        m_serviceWatcher->addWatchedService(service);
 
133
        emit StatusNotifierHostRegistered();
 
134
    }
 
135
}
 
136
 
 
137
bool StatusNotifierWatcher::IsStatusNotifierHostRegistered() const
 
138
{
 
139
    return !m_statusNotifierHostServices.isEmpty();
 
140
}
 
141
 
 
142
int StatusNotifierWatcher::ProtocolVersion() const
 
143
{
 
144
    return 0;
 
145
}
 
146
 
 
147
#include "statusnotifierwatcher.moc"