2 * Copyright 2008 Alex Merry <alex.merry@kdemail.net>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "dbuswatcher.h"
20 #include "playerfactory.h"
25 DBusWatcher::DBusWatcher(QObject
* parent
)
29 setObjectName("DBusWatcher");
30 QDBusConnection sessionCon
= QDBusConnection::sessionBus();
31 if (sessionCon
.isConnected()) {
32 m_bus
= sessionCon
.interface();
33 connect(m_bus
, SIGNAL(serviceOwnerChanged(QString
,QString
,QString
)),
34 this, SLOT(serviceChange(QString
,QString
,QString
)));
36 kWarning() << "Couldn't connect to session bus";
40 QList
<Player::Ptr
> DBusWatcher::players()
42 return m_players
.values();
45 void DBusWatcher::addFactory(DBusPlayerFactory
* factory
)
47 m_factories
.append(factory
);
49 QDBusReply
<QStringList
> reply
= m_bus
->registeredServiceNames();
50 if (reply
.isValid()) {
51 QStringList services
= reply
.value();
52 foreach (const QString
&name
, services
) {
53 if (factory
->matches(name
)) {
54 if (m_players
.contains(name
)) {
55 kWarning() << "Already got a player called" << name
;
58 args
<< QVariant(name
);
59 Player::Ptr player
= factory
->create(args
);
60 if (!player
.isNull()) {
61 m_players
[name
] = player
;
62 emit(newPlayer(player
));
64 kWarning() << "Failed to get player" << name
;
70 kWarning() << "Couldn't get service names:" << reply
.error().message();
74 void DBusWatcher::serviceChange(const QString
& name
,
75 const QString
& oldOwner
,
76 const QString
& newOwner
)
78 if (oldOwner
.isEmpty() && !newOwner
.isEmpty()) {
80 foreach (DBusPlayerFactory
* factory
, m_factories
) {
81 if (factory
->matches(name
)) {
82 if (m_players
.contains(name
)) {
83 kWarning() << "Already got a player at" << name
;
86 args
<< QVariant(name
);
87 Player::Ptr player
= factory
->create(args
);
88 if (!player
.isNull()) {
89 m_players
[name
] = player
;
90 emit(newPlayer(player
));
92 kWarning() << "Failed to get player" << name
;
97 } else if (!oldOwner
.isEmpty() && newOwner
.isEmpty()) {
98 // an old service disappeared
99 if (m_players
.contains(name
)) {
100 Player::Ptr player
= m_players
[name
];
101 m_players
.remove(name
);
102 emit(playerDisappeared(player
));