Plasma
dbuswatcher.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "dbuswatcher.h"
00018
00019 #include "player.h"
00020 #include "playerfactory.h"
00021
00022 #include <QtDBus>
00023 #include <KDebug>
00024
00025 DBusWatcher::DBusWatcher(QObject* parent)
00026 : QObject(parent),
00027 m_bus(0)
00028 {
00029 QDBusConnection sessionCon = QDBusConnection::sessionBus();
00030 if (sessionCon.isConnected()) {
00031 m_bus = sessionCon.interface();
00032 connect(m_bus, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
00033 this, SLOT(serviceChange(QString,QString,QString)));
00034 } else {
00035 kWarning() << "Couldn't connect to session bus";
00036 }
00037 }
00038
00039 QList<Player::Ptr> DBusWatcher::players()
00040 {
00041 return m_players.values();
00042 }
00043
00044 void DBusWatcher::addFactory(DBusPlayerFactory* factory)
00045 {
00046 m_factories.append(factory);
00047
00048 QDBusReply<QStringList> reply = m_bus->registeredServiceNames();
00049 if (reply.isValid()) {
00050 QStringList services = reply.value();
00051 foreach (const QString &name, services) {
00052 if (factory->matches(name)) {
00053 if (m_players.contains(name)) {
00054 kWarning() << "Already got a player called" << name;
00055 } else {
00056 QVariantList args;
00057 args << QVariant(name);
00058 Player::Ptr player = factory->create(args);
00059 if (!player.isNull()) {
00060 m_players[name] = player;
00061 emit(newPlayer(player));
00062 } else {
00063 kWarning() << "Failed to get player" << name;
00064 }
00065 }
00066 }
00067 }
00068 } else {
00069 kWarning() << "Couldn't get service names:" << reply.error().message();
00070 }
00071 }
00072
00073 void DBusWatcher::serviceChange(const QString& name,
00074 const QString& oldOwner,
00075 const QString& newOwner)
00076 {
00077 if (oldOwner.isEmpty() && !newOwner.isEmpty()) {
00078
00079 foreach (DBusPlayerFactory* factory, m_factories) {
00080 if (factory->matches(name)) {
00081 if (m_players.contains(name)) {
00082 kWarning() << "Already got a player at" << name;
00083 } else {
00084 QVariantList args;
00085 args << QVariant(name);
00086 Player::Ptr player = factory->create(args);
00087 if (!player.isNull()) {
00088 m_players[name] = player;
00089 emit(newPlayer(player));
00090 } else {
00091 kWarning() << "Failed to get player" << name;
00092 }
00093 }
00094 }
00095 }
00096 } else if (!oldOwner.isEmpty() && newOwner.isEmpty()) {
00097
00098 if (m_players.contains(name)) {
00099 Player::Ptr player = m_players[name];
00100 m_players.remove(name);
00101 emit(playerDisappeared(player));
00102 }
00103 }
00104 }