• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Engines

dbuswatcher.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2008  Alex Merry <alex.merry@kdemail.net>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
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     setObjectName("DBusWatcher");
00030     QDBusConnection sessionCon = QDBusConnection::sessionBus();
00031     if (sessionCon.isConnected()) {
00032         m_bus = sessionCon.interface();
00033         connect(m_bus, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
00034                 this,  SLOT(serviceChange(QString,QString,QString)));
00035     } else {
00036         kWarning() << "Couldn't connect to session bus";
00037     }
00038 }
00039 
00040 QList<Player::Ptr> DBusWatcher::players()
00041 {
00042     return m_players.values();
00043 }
00044 
00045 void DBusWatcher::addFactory(DBusPlayerFactory* factory)
00046 {
00047     m_factories.append(factory);
00048 
00049     QDBusReply<QStringList> reply = m_bus->registeredServiceNames();
00050     if (reply.isValid()) {
00051         QStringList services = reply.value();
00052         foreach (const QString &name, services) {
00053             if (factory->matches(name)) {
00054                 if (m_players.contains(name)) {
00055                     kWarning() << "Already got a player called" << name;
00056                 } else {
00057                     QVariantList args;
00058                     args << QVariant(name);
00059                     Player::Ptr player = factory->create(args);
00060                     if (!player.isNull()) {
00061                         m_players[name] = player;
00062                         emit(newPlayer(player));
00063                     } else {
00064                         kWarning() << "Failed to get player" << name;
00065                     }
00066                 }
00067             }
00068         }
00069     } else {
00070         kWarning() << "Couldn't get service names:" << reply.error().message();
00071     }
00072 }
00073 
00074 void DBusWatcher::serviceChange(const QString& name,
00075                                 const QString& oldOwner,
00076                                 const QString& newOwner)
00077 {
00078     if (oldOwner.isEmpty() && !newOwner.isEmpty()) {
00079         // got a new service
00080         foreach (DBusPlayerFactory* factory, m_factories) {
00081             if (factory->matches(name)) {
00082                 if (m_players.contains(name)) {
00083                     kWarning() << "Already got a player at" << name;
00084                 } else {
00085                     QVariantList args;
00086                     args << QVariant(name);
00087                     Player::Ptr player = factory->create(args);
00088                     if (!player.isNull()) {
00089                         m_players[name] = player;
00090                         emit(newPlayer(player));
00091                     } else {
00092                         kWarning() << "Failed to get player" << name;
00093                     }
00094                 }
00095             }
00096         }
00097     } else if (!oldOwner.isEmpty() && newOwner.isEmpty()) {
00098         // an old service disappeared
00099         if (m_players.contains(name)) {
00100             Player::Ptr player = m_players[name];
00101             m_players.remove(name);
00102             emit(playerDisappeared(player));
00103         }
00104     }
00105 }

Engines

Skip menu "Engines"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal