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

Plasma

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     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         // got a new service
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         // an old service disappeared
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 }

Plasma

Skip menu "Plasma"
  • 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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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