• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kremotecontrol

  • sources
  • kde-4.12
  • kdeutils
  • kremotecontrol
  • libkremotecontrol
remotecontrolmanager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) <2011> Michael Zanetti <mzanetti@kde.org>
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 along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18 */
19 
20 #include "remotecontrolmanager.h"
21 #include "remotecontrolmanager_p.h"
22 #include "ifaces/remotecontrolmanagerinterface.h"
23 #include "ifaces/remotecontrolinterface.h"
24 
25 #include <kglobal.h>
26 #include <kservicetypetrader.h>
27 #include <kservice.h>
28 #include <kdebug.h>
29 
30 K_GLOBAL_STATIC(RemoteControlManagerPrivate, globalRemoteControlManager)
31 
32 bool RemoteControlManager::connected()
33 {
34  return globalRemoteControlManager->connected();
35 }
36 
37 RemoteControlManager::Notifier* RemoteControlManager::notifier()
38 {
39  return globalRemoteControlManager;
40 }
41 
42 
43 /***********************************************************************
44  RemoteControlManagerPrivate
45  ***********************************************************************/
46 
47 RemoteControlManagerPrivate::RemoteControlManagerPrivate()
48 {
49  loadBackends("KRemoteControlManager");
50 }
51 
52 RemoteControlManagerPrivate::~RemoteControlManagerPrivate()
53 {
54  while(!m_backendList.isEmpty()) {
55  delete m_backendList.takeFirst();
56  }
57 }
58 
59 void RemoteControlManagerPrivate::loadBackends(const char *serviceName)
60 {
61  QStringList error_msg;
62 
63  KService::List offers = KServiceTypeTrader::self()->query(serviceName, "(Type == 'Service')");
64 
65  foreach (const KService::Ptr &ptr, offers) {
66  QString error_string;
67  QObject *backend = ptr->createInstance<QObject>(0, QVariantList(), &error_string);
68 
69  if(backend!=0) {
70  if(backend->inherits("Iface::RemoteControlManager")) {
71  kDebug() << "Backend loaded: " << ptr->name();
72  m_backendList.append(qobject_cast<Iface::RemoteControlManager*>(backend));
73  connect(backend, SIGNAL(remoteControlAdded(QString)), this, SLOT(_k_remoteControlAdded(QString)));
74  connect(backend, SIGNAL(remoteControlRemoved(QString)), this, SLOT(_k_remoteControlRemoved(QString)));
75  connect(backend, SIGNAL(statusChanged(bool)), this, SLOT(_k_statusChanged(bool)));
76  break;
77  } else {
78  kDebug() << "Failed loading:" << error_string;
79  QString error_string = i18n("Backend loaded but wrong type obtained, expected %1",
80  QLatin1String("Iface::RemoteControlManager"));
81 
82  kDebug() << "Error loading '" << ptr->name() << "': " << error_string;
83  error_msg.append(error_string);
84 
85  delete backend;
86  backend = 0;
87  }
88  } else {
89  kDebug() << "Error loading '" << ptr->name() << "', KService said: " << error_string;
90  error_msg.append(error_string);
91  }
92  }
93 
94  if (m_backendList.isEmpty()) {
95  if (offers.size() == 0) {
96  kDebug() << "No Backend found";
97  } else {
98  kDebug() << "could not load any of the backends";
99  }
100  }
101 }
102 
103 bool RemoteControlManagerPrivate::connected()
104 {
105  return m_connected;
106 }
107 
108 RemoteControlList RemoteControlManagerPrivate::buildDeviceList(const QStringList &remoteList)
109 {
110  RemoteControlList list;
111 
112  foreach (const QString &remote, remoteList) {
113  QPair<RemoteControl *, Iface::RemoteControl *> pair = findRegisteredRemoteControl(remote);
114 
115  if (pair.first!= 0) {
116  list.append(pair.first);
117  }
118  }
119 
120  return list;
121 
122 }
123 
124 void RemoteControlManagerPrivate::_k_remoteControlAdded(const QString &name)
125 {
126  Iface::RemoteControlManager *backendManager = qobject_cast<Iface::RemoteControlManager*>(sender());
127  if(backendManager == 0) {
128  return;
129  }
130  RemoteControl *rc = new RemoteControl(name);
131  Iface::RemoteControl *rcBackend = backendManager->createRemoteControl(name);
132  rc->d_ptr->setBackendObject(rcBackend);
133  m_remoteControlMap.insert(name, QPair<RemoteControl*, Iface::RemoteControl*>(rc, rcBackend));
134 
135  emit remoteControlAdded(name);
136 }
137 
138 void RemoteControlManagerPrivate::_k_remoteControlRemoved(const QString &name)
139 {
140  delete m_remoteControlMap[name].first;
141  delete m_remoteControlMap[name].second;
142  m_remoteControlMap.remove(name);
143 
144  emit remoteControlRemoved(name);
145 }
146 
147 void RemoteControlManagerPrivate::_k_statusChanged(bool connected)
148 {
149  if(connected == m_connected) {
150  return;
151  }
152 
153  if(!connected) {
154  // Is there still another backend connected?
155  foreach(Iface::RemoteControlManager* backend, m_backendList) {
156  if(backend->connected()) {
157  return;
158  }
159  }
160  }
161 
162  m_connected = connected;
163  emit statusChanged(connected);
164  kDebug() << "Remotecontrol backend status has changed to" << connected;
165 }
166 
167 RemoteControlList RemoteControlManagerPrivate::allRemotes()
168 {
169  QStringList remoteList;
170  foreach(Iface::RemoteControlManager *backend, m_backendList) {
171  remoteList.append(backend->remoteNames());
172  }
173 
174  if (!m_backendList.isEmpty()) {
175  return buildDeviceList(remoteList);
176  } else {
177  return RemoteControlList();
178  }
179 }
180 
181 RemoteControl* RemoteControlManagerPrivate::findRemoteControl(const QString &name)
182 {
183  return m_remoteControlMap.value(name).first;
184 }
185 
186 RemoteControl::RemoteControl(const QString &name): QObject(), d_ptr(new RemoteControlPrivate(this))
187 {
188  Q_D(RemoteControl);
189 
190  RemoteControl *other = globalRemoteControlManager->findRemoteControl(name);
191 
192  if(other) {
193  d->setBackendObject(other->d_ptr->backendObject());
194  }
195 }
196 
197 QList<RemoteControl*> RemoteControl::allRemotes()
198 {
199  return globalRemoteControlManager->allRemotes();
200 }
201 
202 QPair<RemoteControl *, Iface::RemoteControl *>
203 RemoteControlManagerPrivate::findRegisteredRemoteControl(const QString &remote)
204 {
205  if (m_remoteControlMap.contains(remote)) {
206  return m_remoteControlMap[remote];
207  } else {
208  foreach(Iface::RemoteControlManager *backend, m_backendList) {
209 
210  Iface::RemoteControl * iface = backend->createRemoteControl(remote);
211  RemoteControl *device = 0;
212  if(iface != 0) {
213  device = new RemoteControl(iface);
214  } else {
215  kDebug() << "Unknown Remote: " << remote;
216  }
217  if (device != 0) {
218  QPair<RemoteControl *, Iface::RemoteControl *> pair(device, iface);
219  connect(dynamic_cast<QObject*>(iface), SIGNAL(destroyed(QObject*)),
220  this, SLOT(_k_destroyed(QObject*)));
221  m_remoteControlMap[remote] = pair;
222  return pair;
223  } else {
224  return QPair<RemoteControl *, Iface::RemoteControl *>(0, 0);
225  }
226  }
227  }
228  return QPair<RemoteControl *, Iface::RemoteControl *>(0, 0);
229 }
230 
231 void RemoteControlManagerPrivate::_k_destroyed(QObject *object)
232 {
233  Iface::RemoteControl *remote = qobject_cast<Iface::RemoteControl*>(object);
234  if(remote) {
235  QString name = remote->name();
236  QPair<RemoteControl*, Iface::RemoteControl*> pair = m_remoteControlMap.take(name);
237  delete pair.first;
238  }
239 }
RemoteControlPrivate::setBackendObject
void setBackendObject(Iface::RemoteControl *object)
Definition: remotecontrol.cpp:63
RemoteControlManager::Notifier
Definition: remotecontrolmanager.h:45
RemoteControlManagerPrivate::RemoteControlManagerPrivate
RemoteControlManagerPrivate()
Definition: remotecontrolmanager.cpp:47
RemoteControlManagerPrivate::findRemoteControl
RemoteControl * findRemoteControl(const QString &name)
Definition: remotecontrolmanager.cpp:181
RemoteControlManager::Notifier::remoteControlRemoved
void remoteControlRemoved(const QString &name)
This signal is emitted when a remote control is not available anymore.
Iface::RemoteControlManager::connected
virtual bool connected() const =0
Get the manager connection state.
QObject
remotecontrolinterface.h
RemoteControlPrivate::backendObject
Iface::RemoteControl * backendObject() const
Definition: remotecontrol.cpp:74
remotecontrolmanagerinterface.h
Iface::RemoteControl
Definition: remotecontrolinterface.h:30
RemoteControl
Definition: remotecontrol.h:35
RemoteControl::d_ptr
RemoteControlPrivate * d_ptr
Definition: remotecontrol.h:106
Iface::RemoteControlManager::remoteNames
virtual QStringList remoteNames() const =0
Retrieves the list of all the remotes installed in the system.
RemoteControlManager::notifier
KREMOTECONTROL_EXPORT Notifier * notifier()
Definition: remotecontrolmanager.cpp:37
RemoteControlManagerPrivate::connected
bool connected()
Definition: remotecontrolmanager.cpp:103
RemoteControlManagerPrivate::allRemotes
RemoteControlList allRemotes()
Definition: remotecontrolmanager.cpp:167
RemoteControlManagerPrivate
Definition: remotecontrolmanager_p.h:31
RemoteControlList
QList< RemoteControl * > RemoteControlList
Definition: remotecontrol.h:110
RemoteControl::allRemotes
static QList< RemoteControl * > allRemotes()
Get all RemoteControl's available in the system.
Definition: remotecontrolmanager.cpp:197
RemoteControlManager::Notifier::remoteControlAdded
void remoteControlAdded(const QString &name)
This signal is emitted when a new remote control is available.
Iface::RemoteControlManager
This class specifies the interface a backend will have to implement in order to be used in the system...
Definition: remotecontrolmanagerinterface.h:39
remotecontrolmanager.h
remotecontrolmanager_p.h
RemoteControlManager::Notifier::statusChanged
void statusChanged(bool connected)
This signal is emitted when the remote control subsystem becomes available.
Iface::RemoteControlManager::createRemoteControl
virtual Iface::RemoteControl * createRemoteControl(const QString &name)=0
Instantiates a new RemoteControlInterface object from this backend given its remote.
RemoteControlManager::connected
KREMOTECONTROL_EXPORT bool connected()
Get the manager connection state.
Definition: remotecontrolmanager.cpp:32
RemoteControlPrivate
Definition: remotecontrol_p.h:31
RemoteControlManagerPrivate::~RemoteControlManagerPrivate
~RemoteControlManagerPrivate()
Definition: remotecontrolmanager.cpp:52
Iface::RemoteControl::name
virtual QString name() const =0
RemoteControl::RemoteControl
RemoteControl(const QString &name)
Definition: remotecontrolmanager.cpp:186
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kremotecontrol

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

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal