summaryrefslogtreecommitdiffstats
path: root/src/libtdeobex/obexobjectmanagerImpl.cpp
blob: f4bb5be8853fdf79e638c978e7aecd79596d5029 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 *
 *  Object Manager implementation of bluez5
 *
 *  Copyright (C) 2018  Emanoil Kotsev <deloptes@gmail.com>
 *
 *
 *  This file is part of libtdebluez.
 *
 *  libtdebluez is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  libtdebluez is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with kbluetooth; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

// TQt - Header
#include <tqtimer.h>
//
// debug
#include <kdebug.h>

// declaration include
#include <tqdbusproxy.h>
#include <tqdbusmessage.h>
#include <tqdbusobjectpath.h>
#include <tqdbusdatamap.h>
#include <tqdbusdata.h>
//
#include <tqstringlist.h>

#include "obexobjectmanagerImpl.h"
#include "../libtdebluez/btuuids.h"

namespace TDEObex
{

ObexObjectManagerImpl::ObexObjectManagerImpl(const TQString& service, const TQString& path, TQObject* parent, const char* name) :
        org::freedesktop::DBus::ObjectManagerProxy(service, path, parent, name)
{
    kdDebug() << k_funcinfo << endl;
    // init connection to dbus
    initDBUS();
}

ObexObjectManagerImpl::~ObexObjectManagerImpl()
{
    kdDebug() << k_funcinfo << endl;
    close();
}

/*!
 * This function try a reconnect to D-Bus.
 * \return boolean with the result of the operation
 * \retval true if successful reconnected to D-Bus
 * \retval false if unsuccessful
 */
bool ObexObjectManagerImpl::reconnect()
{
    kdDebug() << k_funcinfo << endl;
    // close D-Bus connection
    close();
    // init D-Bus conntection
    return (initDBUS());
}

/*!
 * This function return information about connection status to the DBUS daemon.
 * \return boolean with the state of the connection to D-Bus
 * \retval true if connected
 * \retval false if disconnected
 */
bool ObexObjectManagerImpl::isConnectedToDBUS()
{
    kdDebug() << k_funcinfo << endl;
    return dBusConn.isConnected();
}

/*!
 * This function returns pointer to connection of the DBUS.
 * \return TQT_DBusConnection* of the connection to D-Bus
 * \retval TQT_DBusConnection*
 */
TQT_DBusConnection* ObexObjectManagerImpl::getConnection()
{
    kdDebug() << k_funcinfo << endl;
    return &dBusConn;
}

/*!
 * This function close the connection to manager over the D-Bus daemon.
 * \return boolean with the result of the operation
 * \retval true if successful closed the connection
 * \retval false if any problems
 */
bool ObexObjectManagerImpl::close()
{
    kdDebug() << k_funcinfo << endl;

    if (mSession)
        delete mSession;
    if (mFileTransfer)
        delete mFileTransfer;
    if (mClient)
        delete mClient;
    if (dBusConn.isConnected())
        dBusConn.closeConnection(DBUS_CONN_NAME);
    return true;
}

/*!
 * This function initialise the connection to the D-Bus daemon.
 * \return boolean with the result of the operation
 * \retval true if successful initialised D-Bus connection
 * \retval false if unsuccessful
 */
bool ObexObjectManagerImpl::initDBUS()
{
    kdDebug() << k_funcinfo << endl;
    dBusConn = TQT_DBusConnection::addConnection(TQT_DBusConnection::SessionBus, DBUS_CONN_NAME);
    if (!dBusConn.isConnected())
    {
        kdError() << "Failed to open connection to system message bus: " << dBusConn.lastError().message() << endl;
        TQTimer::singleShot(4000, this, TQ_SLOT(reconnect()));
        return false;
    }
    setConnection(dBusConn);

    TQT_DBusDataMap<TQT_DBusObjectPath> objects;
    TQT_DBusError dbuserror;
    if (!GetManagedObjects(objects, dbuserror))
    {
        tqDebug(i18n("GetManagedObjects(objects, dbuserror) failed: %1").arg(dbuserror.message()));
        return false;
    }

    TQT_DBusDataMap<TQT_DBusObjectPath>::const_iterator it = objects.begin();
    for (it; it != objects.end(); ++it)
    {
        bool ok = false;
        TQT_DBusDataMap<TQString> tqMap1 = it.data().toStringKeyMap(&ok);
        if (!ok)
        {
            tqWarning(i18n("Failed to convert dbus data to string map: %1").arg(it.key()));
            return false;
        }
        slotInterfacesAdded(it.key(), tqMap1);
    }

    connect(this, TQ_SIGNAL(InterfacesAdded(const TQT_DBusObjectPath&, const TQT_DBusDataMap< TQString >&)), this, TQ_SLOT(slotInterfacesAdded(const TQT_DBusObjectPath&, const TQT_DBusDataMap< TQString >& )));
    connect(this, TQ_SIGNAL(InterfacesRemoved(const TQT_DBusObjectPath& , const TQStringList& )), this, TQ_SLOT(slotInterfacesRemoved(const TQT_DBusObjectPath& , const TQStringList& )));
    return true;
}

/*!
 * This function initialise the connection to the D-Bus daemon.
 * \return pointer to AgentManager1
 */
org::bluez::obex::AgentManager1Proxy * ObexObjectManagerImpl::getAgentManager()
{
    kdDebug() << k_funcinfo << endl;
    return mAgentManager;
}

/*!
 * This function initialise the connection to the D-Bus daemon.
 * \return pointer to AgentManager1
 */
org::bluez::obex::Client1Proxy * ObexObjectManagerImpl::getClient()
{
    kdDebug() << k_funcinfo << endl;
    return mClient;
}

void ObexObjectManagerImpl::slotInterfacesAdded(const TQT_DBusObjectPath& object, const TQT_DBusDataMap<TQString>& interfaces)
{
    kdDebug() << k_funcinfo << endl;

    TQT_DBusDataMap<TQString>::const_iterator it1 = interfaces.begin();
    for (it1; it1 != interfaces.end(); it1++)
    {
        TQString interface = it1.key();
        if (interface == "org.bluez.obex.AgentManager1")
        {
            mAgentManager = new org::bluez::obex::AgentManager1Proxy("org.bluez.obex", object);
            if (mAgentManager)
            {
                mAgentManager->setConnection(dBusConn);
            }
            else
            {
                tqDebug(i18n("org.bluez.obex.AgentManager1 initialization failed"));
            }
        }
        else if (interface == "org.bluez.obex.Client1")
        {
            mClient = new org::bluez::obex::Client1Proxy("org.bluez.obex", object);
            if (mClient)
            {
                mClient->setConnection(dBusConn);
            }
            else
            {
                tqDebug(i18n("org.bluez.obex.Client1 initialization failed"));
            }
        }
        else if (interface == "org.bluez.obex.Session1")
        {
            mSession = new org::bluez::obex::Session1Proxy("org.bluez.obex", object);
            if (mSession)
            {
                mSession->setConnection(dBusConn);
            }
            else
            {
                tqDebug(i18n("org.bluez.obex.Session1 initialization failed"));
            }
        }
        else if (interface == "org.bluez.obex.FileTransfer1")
        {
            mFileTransfer = new org::bluez::obex::FileTransfer1Proxy("org.bluez.obex", object);
            if (mFileTransfer)
            {
                mFileTransfer->setConnection(dBusConn);
            }
            else
            {
                tqDebug(i18n("org.bluez.obex.FileTransfer1 initialization failed"));
            }
        }
        else if (interface == "org.freedesktop.DBus.Introspectable")
        {
            // do nothing
        }
        else
        {
            tqWarning(i18n("Interface not implemented: %1").arg(interface));
        }
    }
}

void ObexObjectManagerImpl::slotInterfacesRemoved(const TQT_DBusObjectPath& object, const TQStringList& interfaces)
{
    kdDebug() << k_funcinfo << endl;
    // TODO: remove interface
    for (TQValueListConstIterator<TQString> it = interfaces.begin();
            it != interfaces.end(); ++it)
    {
        if ((*it) == "org.bluez.obex.AgentManager1")
        {
            // TODO: remove AgentManager1
        }
        else if ((*it) == "org.bluez.obex.Client1")
        {
            // TODO: remove Client1
        }
        else if ((*it) == "org.bluez.obex.Session1")
        {
            // TODO: remove Session1
        }
        else if ((*it) == "org.bluez.obex.FileTransfer1")
        {
            // TODO: remove FileTransfer1
        }
        else
        {
            tqWarning(i18n("Interface not implemented: %1").arg((*it)));
        }
    }
}

};
// namespace TDEObex

#include "obexobjectmanagerImpl.moc"
// End of File