tdebluez
obexobjectmanagerImpl.cpp
Go to the documentation of this file.
1 /*
2  *
3  * Object Manager implementation of bluez5
4  *
5  * Copyright (C) 2018 Emanoil Kotsev <deloptes@gmail.com>
6  *
7  *
8  * This file is part of libtdebluez.
9  *
10  * libtdebluez is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * libtdebluez is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with kbluetooth; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 // TQt - Header
27 #include <tqtimer.h>
28 //
29 // debug
30 #include <kdebug.h>
31 
32 // declaration include
33 #include <tqdbusproxy.h>
34 #include <tqdbusmessage.h>
35 #include <tqdbusobjectpath.h>
36 #include <tqdbusdatamap.h>
37 #include <tqdbusdata.h>
38 //
39 #include <tqstringlist.h>
40 
41 #include "obexobjectmanagerImpl.h"
42 #include "../libtdebluez/btuuids.h"
43 
44 namespace TDEObex
45 {
46 
47 ObexObjectManagerImpl::ObexObjectManagerImpl(const TQString& service, const TQString& path, TQObject* parent, const char* name) :
48  org::freedesktop::DBus::ObjectManagerProxy(service, path, parent, name)
49 {
50  // Initialize all member pointers to prevent undefined behavior
51  mAgentManager = nullptr;
52  mClient = nullptr;
53  mSession = nullptr;
54  mFileTransfer = nullptr;
55 
56  kdDebug() << k_funcinfo << endl;
57  // init connection to dbus
58  initDBUS();
59 }
60 
62 {
63  kdDebug() << k_funcinfo << endl;
64  close();
65 }
66 
74 {
75  kdDebug() << k_funcinfo << endl;
76  // close D-Bus connection
77  close();
78  // init D-Bus connection
79  return (initDBUS());
80 }
81 
89 {
90  kdDebug() << k_funcinfo << endl;
91  return dBusConn.isConnected();
92 }
93 
100 {
101  kdDebug() << k_funcinfo << endl;
102  return &dBusConn;
103 }
104 
112 {
113  kdDebug() << k_funcinfo << endl;
114 
115  // Disconnect signals before deleting objects to prevent dangling signals
116  if (mAgentManager) {
117  delete mAgentManager;
118  mAgentManager = nullptr;
119  }
120 
121  if (mClient) {
122  delete mClient;
123  mClient = nullptr;
124  }
125 
126  if (mSession) {
127  delete mSession;
128  mSession = nullptr;
129  }
130 
131  if (mFileTransfer) {
132  delete mFileTransfer;
133  mFileTransfer = nullptr;
134  }
135 
136  if (dBusConn.isConnected()) {
137  dBusConn.closeConnection(DBUS_CONN_NAME);
138  }
139  return true;
140 }
141 
149 {
150  kdDebug() << k_funcinfo << endl;
151  dBusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SessionBus, DBUS_CONN_NAME);
152  if (!dBusConn.isConnected())
153  {
154  kdError() << "Failed to open connection to system message bus: " << dBusConn.lastError().message() << endl;
155  TQTimer::singleShot(4000, this, TQ_SLOT(reconnect()));
156  return false;
157  }
159 
161  TQT_DBusError dbuserror;
162  if (!GetManagedObjects(objects, dbuserror))
163  {
164  tqDebug(i18n("GetManagedObjects(objects, dbuserror) failed: %1").arg(dbuserror.message()));
165  return false;
166  }
167 
169  for (it; it != objects.end(); ++it)
170  {
171  bool ok = false;
172  TQT_DBusDataMap<TQString> tqMap1 = it.data().toStringKeyMap(&ok);
173  if (!ok)
174  {
175  tqWarning(i18n("Failed to convert dbus data to string map: %1").arg(it.key()));
176  return false;
177  }
178  slotInterfacesAdded(it.key(), tqMap1);
179  }
180 
181  connect(this, TQ_SIGNAL(InterfacesAdded(const TQT_DBusObjectPath&, const TQT_DBusDataMap< TQString >&)), this, TQ_SLOT(slotInterfacesAdded(const TQT_DBusObjectPath&, const TQT_DBusDataMap< TQString >& )));
182  connect(this, TQ_SIGNAL(InterfacesRemoved(const TQT_DBusObjectPath& , const TQStringList& )), this, TQ_SLOT(slotInterfacesRemoved(const TQT_DBusObjectPath& , const TQStringList& )));
183  return true;
184 }
185 
191 {
192  kdDebug() << k_funcinfo << endl;
193  return mAgentManager;
194 }
195 
201 {
202  kdDebug() << k_funcinfo << endl;
203  return mClient;
204 }
205 
206 void ObexObjectManagerImpl::slotInterfacesAdded(const TQT_DBusObjectPath& object, const TQT_DBusDataMap<TQString>& interfaces)
207 {
208  kdDebug() << k_funcinfo << endl;
209 
210  TQT_DBusDataMap<TQString>::const_iterator it1 = interfaces.begin();
211  for (it1; it1 != interfaces.end(); it1++)
212  {
213  TQString interface = it1.key();
214  if (interface == "org.bluez.obex.AgentManager1")
215  {
216  if (!mAgentManager) {
217  mAgentManager = new org::bluez::obex::AgentManager1Proxy("org.bluez.obex", object);
218  if (mAgentManager)
219  {
221  }
222  else
223  {
224  tqDebug(i18n("org.bluez.obex.AgentManager1 initialization failed"));
225  }
226  }
227  }
228  else if (interface == "org.bluez.obex.Client1")
229  {
230  if (!mClient) {
231  mClient = new org::bluez::obex::Client1Proxy("org.bluez.obex", object);
232  if (mClient)
233  {
235  }
236  else
237  {
238  tqDebug(i18n("org.bluez.obex.Client1 initialization failed"));
239  }
240  }
241  }
242  else if (interface == "org.bluez.obex.Session1")
243  {
244  if (!mSession) {
245  mSession = new org::bluez::obex::Session1Proxy("org.bluez.obex", object);
246  if (mSession)
247  {
249  }
250  else
251  {
252  tqDebug(i18n("org.bluez.obex.Session1 initialization failed"));
253  }
254  }
255  }
256  else if (interface == "org.bluez.obex.FileTransfer1")
257  {
258  if (!mFileTransfer) {
259  mFileTransfer = new org::bluez::obex::FileTransfer1Proxy("org.bluez.obex", object);
260  if (mFileTransfer)
261  {
263  }
264  else
265  {
266  tqDebug(i18n("org.bluez.obex.FileTransfer1 initialization failed"));
267  }
268  }
269  }
270  else if (interface == "org.freedesktop.DBus.Introspectable")
271  {
272  // do nothing
273  }
274  else
275  {
276  tqWarning(i18n("Interface not implemented: %1").arg(interface));
277  }
278  }
279 }
280 
281 void ObexObjectManagerImpl::slotInterfacesRemoved(const TQT_DBusObjectPath& object, const TQStringList& interfaces)
282 {
283  kdDebug() << k_funcinfo << endl;
284  // TODO: remove interface
285  for (TQValueListConstIterator<TQString> it = interfaces.begin();
286  it != interfaces.end(); ++it)
287  {
288  if ((*it) == "org.bluez.obex.AgentManager1")
289  {
290  // TODO: remove AgentManager1
291  }
292  else if ((*it) == "org.bluez.obex.Client1")
293  {
294  // TODO: remove Client1
295  }
296  else if ((*it) == "org.bluez.obex.Session1")
297  {
298  // TODO: remove Session1
299  }
300  else if ((*it) == "org.bluez.obex.FileTransfer1")
301  {
302  // TODO: remove FileTransfer1
303  }
304  else
305  {
306  tqWarning(i18n("Interface not implemented: %1").arg((*it)));
307  }
308  }
309 }
310 
311 };
312 // namespace TDEObex
313 
314 #include "obexobjectmanagerImpl.moc"
315 // End of File
ObexObjectManagerImpl(const TQString &service, const TQString &path, TQObject *parent=0, const char *name=0)
org::bluez::obex::AgentManager1Proxy * mAgentManager
org::bluez::obex::Client1Proxy * mClient
org::bluez::obex::AgentManager1Proxy * getAgentManager()
bool isConnectedToDBUS()
to get information if TDEBluez is connected to D-Bus
org::bluez::obex::Client1Proxy * getClient()
void slotInterfacesRemoved(const TQT_DBusObjectPath &object, const TQStringList &interfaces)
bool close()
to close the connection to D-Bus
TQT_DBusConnection dBusConn
TQt connection to D-Bus.
void slotInterfacesAdded(const TQT_DBusObjectPath &object, const TQT_DBusDataMap< TQString > &interfaces)
TQT_DBusConnection * getConnection()
pointer to the D-Bus connection
org::bluez::obex::FileTransfer1Proxy * mFileTransfer
org::bluez::obex::Session1Proxy * mSession
void setConnection(const TQT_DBusConnection &connection)
void setConnection(const TQT_DBusConnection &connection)
void setConnection(const TQT_DBusConnection &connection)
void setConnection(const TQT_DBusConnection &connection)
virtual bool GetManagedObjects(TQT_DBusDataMap< TQT_DBusObjectPath > &objects, TQT_DBusError &error)
void InterfacesAdded(const TQT_DBusObjectPath &object, const TQT_DBusDataMap< TQString > &interfaces)
void InterfacesRemoved(const TQT_DBusObjectPath &object, const TQStringList &interfaces)
#define DBUS_CONN_NAME