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  kdDebug() << k_funcinfo << endl;
51  // init connection to dbus
52  initDBUS();
53 }
54 
56 {
57  kdDebug() << k_funcinfo << endl;
58  close();
59 }
60 
68 {
69  kdDebug() << k_funcinfo << endl;
70  // close D-Bus connection
71  close();
72  // init D-Bus conntection
73  return (initDBUS());
74 }
75 
83 {
84  kdDebug() << k_funcinfo << endl;
85  return dBusConn.isConnected();
86 }
87 
94 {
95  kdDebug() << k_funcinfo << endl;
96  return &dBusConn;
97 }
98 
106 {
107  kdDebug() << k_funcinfo << endl;
108 
109  if (mSession)
110  delete mSession;
111  if (mFileTransfer)
112  delete mFileTransfer;
113  if (mClient)
114  delete mClient;
115  if (dBusConn.isConnected())
116  dBusConn.closeConnection(DBUS_CONN_NAME);
117  return true;
118 }
119 
127 {
128  kdDebug() << k_funcinfo << endl;
129  dBusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SessionBus, DBUS_CONN_NAME);
130  if (!dBusConn.isConnected())
131  {
132  kdError() << "Failed to open connection to system message bus: " << dBusConn.lastError().message() << endl;
133  TQTimer::singleShot(4000, this, TQ_SLOT(reconnect()));
134  return false;
135  }
137 
139  TQT_DBusError dbuserror;
140  if (!GetManagedObjects(objects, dbuserror))
141  {
142  tqDebug(i18n("GetManagedObjects(objects, dbuserror) failed: %1").arg(dbuserror.message()));
143  return false;
144  }
145 
147  for (it; it != objects.end(); ++it)
148  {
149  bool ok = false;
150  TQT_DBusDataMap<TQString> tqMap1 = it.data().toStringKeyMap(&ok);
151  if (!ok)
152  {
153  tqWarning(i18n("Failed to convert dbus data to string map: %1").arg(it.key()));
154  return false;
155  }
156  slotInterfacesAdded(it.key(), tqMap1);
157  }
158 
159  connect(this, TQ_SIGNAL(InterfacesAdded(const TQT_DBusObjectPath&, const TQT_DBusDataMap< TQString >&)), this, TQ_SLOT(slotInterfacesAdded(const TQT_DBusObjectPath&, const TQT_DBusDataMap< TQString >& )));
160  connect(this, TQ_SIGNAL(InterfacesRemoved(const TQT_DBusObjectPath& , const TQStringList& )), this, TQ_SLOT(slotInterfacesRemoved(const TQT_DBusObjectPath& , const TQStringList& )));
161  return true;
162 }
163 
169 {
170  kdDebug() << k_funcinfo << endl;
171  return mAgentManager;
172 }
173 
179 {
180  kdDebug() << k_funcinfo << endl;
181  return mClient;
182 }
183 
184 void ObexObjectManagerImpl::slotInterfacesAdded(const TQT_DBusObjectPath& object, const TQT_DBusDataMap<TQString>& interfaces)
185 {
186  kdDebug() << k_funcinfo << endl;
187 
188  TQT_DBusDataMap<TQString>::const_iterator it1 = interfaces.begin();
189  for (it1; it1 != interfaces.end(); it1++)
190  {
191  TQString interface = it1.key();
192  if (interface == "org.bluez.obex.AgentManager1")
193  {
194  mAgentManager = new org::bluez::obex::AgentManager1Proxy("org.bluez.obex", object);
195  if (mAgentManager)
196  {
198  }
199  else
200  {
201  tqDebug(i18n("org.bluez.obex.AgentManager1 initialization failed"));
202  }
203  }
204  else if (interface == "org.bluez.obex.Client1")
205  {
206  mClient = new org::bluez::obex::Client1Proxy("org.bluez.obex", object);
207  if (mClient)
208  {
210  }
211  else
212  {
213  tqDebug(i18n("org.bluez.obex.Client1 initialization failed"));
214  }
215  }
216  else if (interface == "org.bluez.obex.Session1")
217  {
218  mSession = new org::bluez::obex::Session1Proxy("org.bluez.obex", object);
219  if (mSession)
220  {
222  }
223  else
224  {
225  tqDebug(i18n("org.bluez.obex.Session1 initialization failed"));
226  }
227  }
228  else if (interface == "org.bluez.obex.FileTransfer1")
229  {
230  mFileTransfer = new org::bluez::obex::FileTransfer1Proxy("org.bluez.obex", object);
231  if (mFileTransfer)
232  {
234  }
235  else
236  {
237  tqDebug(i18n("org.bluez.obex.FileTransfer1 initialization failed"));
238  }
239  }
240  else if (interface == "org.freedesktop.DBus.Introspectable")
241  {
242  // do nothing
243  }
244  else
245  {
246  tqWarning(i18n("Interface not implemented: %1").arg(interface));
247  }
248  }
249 }
250 
251 void ObexObjectManagerImpl::slotInterfacesRemoved(const TQT_DBusObjectPath& object, const TQStringList& interfaces)
252 {
253  kdDebug() << k_funcinfo << endl;
254  // TODO: remove interface
255  for (TQValueListConstIterator<TQString> it = interfaces.begin();
256  it != interfaces.end(); ++it)
257  {
258  if ((*it) == "org.bluez.obex.AgentManager1")
259  {
260  // TODO: remove AgentManager1
261  }
262  else if ((*it) == "org.bluez.obex.Client1")
263  {
264  // TODO: remove Client1
265  }
266  else if ((*it) == "org.bluez.obex.Session1")
267  {
268  // TODO: remove Session1
269  }
270  else if ((*it) == "org.bluez.obex.FileTransfer1")
271  {
272  // TODO: remove FileTransfer1
273  }
274  else
275  {
276  tqWarning(i18n("Interface not implemented: %1").arg((*it)));
277  }
278  }
279 }
280 
281 };
282 // namespace TDEObex
283 
284 #include "obexobjectmanagerImpl.moc"
285 // End of File
286 
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