~ubuntu-branches/ubuntu/intrepid/kdebluetooth/intrepid-proposed

« back to all changes in this revision

Viewing changes to kdebluetooth/libkbluetooth/dbussignal.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Mercatante
  • Date: 2008-08-07 09:49:47 UTC
  • mto: This revision was merged to the branch mainline in revision 56.
  • Revision ID: james.westby@ubuntu.com-20080807094947-pj6q3uxwuv7l844q
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *
3
 
 *  DBus function wrapper for signals 
4
 
 *
5
 
 *  Copyright (C) 2006  Daniel Gollub <dgollub@suse.de>
6
 
 *
7
 
 *
8
 
 *  This file is part of libkbluetooth.
9
 
 *
10
 
 *  libkbluetooth 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
 
 *  libkbluetooth 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 libkbluetooth; if not, write to the Free Software
22
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23
 
 *
24
 
 */
25
 
 
26
 
#include "dbussignal.h"
27
 
 
28
 
namespace KBluetooth {
29
 
 
30
 
DBusSignal::DBusSignal(const QString &service, const QString &path, const QString &interface, DBusConnection *dbusconn) 
31
 
        : QObject(), mService(service), mPath(path), mInterface(interface), conn(dbusconn)
32
 
{
33
 
        kdDebug() << k_funcinfo << ": " << service << " path: " << path << " interface: " << interface << " conn: " << dbusconn << endl;
34
 
}
35
 
 
36
 
DBusSignal::~DBusSignal() {
37
 
}
38
 
 
39
 
 
40
 
bool DBusSignal::serviceDown(DBusMessage *msg)
41
 
{
42
 
 
43
 
        if (!dbus_message_is_signal (msg, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
44
 
                return false;
45
 
 
46
 
        const char *service, *old_owner, *new_owner;
47
 
        dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &service,
48
 
                        DBUS_TYPE_STRING, &old_owner,
49
 
                        DBUS_TYPE_STRING, &new_owner,
50
 
                        DBUS_TYPE_INVALID);
51
 
 
52
 
        if (!strcmp(BLUEZ_SERVICE, service) && *new_owner == '\0') {
53
 
                kdDebug() << k_funcinfo << " SERVICE IS DOWN" << endl;
54
 
                return true;
55
 
        }
56
 
 
57
 
        return false;
58
 
}
59
 
 
60
 
bool DBusSignal::serviceUp(DBusMessage *msg)
61
 
{
62
 
 
63
 
        if (!dbus_message_is_signal (msg, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
64
 
                return false;
65
 
 
66
 
        const char *service, *old_owner, *new_owner;
67
 
        dbus_message_get_args(msg, NULL, DBUS_TYPE_STRING, &service,
68
 
                        DBUS_TYPE_STRING, &old_owner,
69
 
                        DBUS_TYPE_STRING, &new_owner,
70
 
                        DBUS_TYPE_INVALID);
71
 
 
72
 
        if (!strcmp(BLUEZ_SERVICE, service) && *old_owner == '\0' && *new_owner != '\0') {
73
 
                kdDebug() << k_funcinfo << " SERVICE IS UP" << endl;
74
 
                return true;
75
 
        }
76
 
 
77
 
        return false;
78
 
}
79
 
 
80
 
DBusMessage* DBusSignal::newMessage(const QString &method)
81
 
{
82
 
        kdDebug() << k_funcinfo << ": " << mService << " path: " << mPath << " mInterfacE: " << mInterface << " method: " << method << endl;
83
 
        DBusMessage *_msg = dbus_message_new_method_call(mService.ascii(), mPath.ascii(), mInterface.ascii(), method.ascii());
84
 
 
85
 
        if (!_msg) {
86
 
                kdDebug() << "libkbluetooth: Can't allocate new method call" << endl;
87
 
                return NULL;
88
 
        }
89
 
 
90
 
        return _msg;
91
 
}
92
 
 
93
 
bool DBusSignal::appendArgs(int first_type, va_list var_args)
94
 
{
95
 
        bool retval;
96
 
        retval = dbus_message_append_args_valist(msg, first_type, var_args);
97
 
        return retval;
98
 
}
99
 
 
100
 
bool DBusSignal::appendArgs(int first_type, ...)
101
 
{
102
 
 
103
 
        bool retval;
104
 
        va_list var_args;
105
 
 
106
 
        va_start(var_args, first_type);
107
 
        retval = appendArgs(first_type, var_args);
108
 
        va_end(var_args);
109
 
 
110
 
        return retval;
111
 
}
112
 
 
113
 
bool DBusSignal::getArgs(DBusMessage *message, DBusError *error, int first_type, ...) {
114
 
        bool retval;
115
 
        va_list var_args;
116
 
 
117
 
        va_start(var_args, first_type);
118
 
        retval = dbus_message_get_args_valist(message, error, first_type, var_args);
119
 
        va_end(var_args);
120
 
 
121
 
        return retval;
122
 
 
123
 
}
124
 
 
125
 
bool DBusSignal::send()
126
 
{
127
 
        DBusPendingCall *pcall = NULL;
128
 
        return dbus_connection_send_with_reply(conn, msg, &pcall, -1);
129
 
}
130
 
 
131
 
DBusMessage *DBusSignal::sendWithReply(DBusError *error, int timeout)
132
 
{
133
 
        return dbus_connection_send_with_reply_and_block(conn, msg, timeout, error);
134
 
}
135
 
 
136
 
bool DBusSignal::sendString(const QString &method)
137
 
{
138
 
        return sendString(method, DBUS_TYPE_INVALID);
139
 
}
140
 
 
141
 
bool DBusSignal::sendString(const QString &method, int first_type, ...)
142
 
{
143
 
        const char *string;
144
 
        DBusMessage *reply = NULL;
145
 
        va_list var_args;
146
 
 
147
 
 
148
 
        DBusError error;
149
 
        dbus_error_init(&error);
150
 
 
151
 
        msg = newMessage(method);
152
 
 
153
 
        va_start(var_args, first_type);
154
 
        if (!appendArgs(first_type, var_args))
155
 
                kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl; 
156
 
        va_end(var_args);
157
 
 
158
 
        bool ret = send();
159
 
 
160
 
        kdDebug() << k_funcinfo << " " << ret << endl;
161
 
 
162
 
        return ret;
163
 
}
164
 
 
165
 
QString DBusSignal::getString(const QString &method)
166
 
{
167
 
        return getString(method, DBUS_TYPE_INVALID);
168
 
}
169
 
 
170
 
QString DBusSignal::getString(const QString &method, int first_type, ...)
171
 
{       
172
 
        const char *string;
173
 
        QString ret;
174
 
        DBusMessage *reply = NULL;
175
 
        va_list var_args;
176
 
 
177
 
 
178
 
        DBusError error;
179
 
        dbus_error_init(&error);
180
 
 
181
 
        msg = newMessage(method);
182
 
 
183
 
        va_start(var_args, first_type);
184
 
        if (!appendArgs(first_type, var_args))
185
 
                kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl; 
186
 
        va_end(var_args);
187
 
 
188
 
        reply = sendWithReply(&error);
189
 
                
190
 
        if (!reply) {
191
 
                if (dbus_error_is_set(&error)) {
192
 
                        mError = QString(error.message);
193
 
                        kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << error.message << endl; 
194
 
                        dbus_error_free(&error);
195
 
                }
196
 
 
197
 
                goto error;
198
 
        }
199
 
 
200
 
        getArgs(reply, &error, DBUS_TYPE_STRING, &string, DBUS_TYPE_INVALID); 
201
 
        if (dbus_error_is_set(&error)) {
202
 
                mError = QString(error.message);
203
 
                kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << error.message << endl; 
204
 
                dbus_error_free(&error);
205
 
                return ret;
206
 
        }
207
 
 
208
 
        ret = QString::fromUtf8(string);
209
 
 
210
 
        dbus_message_unref(reply);
211
 
 
212
 
error:  
213
 
        dbus_message_unref(msg);
214
 
        return ret;
215
 
}
216
 
 
217
 
QValueList<QString> DBusSignal::getStringList(const QString &method)
218
 
{
219
 
        return getStringList(method, DBUS_TYPE_INVALID);
220
 
}
221
 
 
222
 
QValueList<QString> DBusSignal::getStringList(const QString &method, int first_type, ...)
223
 
{
224
 
        QValueList<QString> list;
225
 
 
226
 
        int num_adapters = 0;
227
 
        char **adapters = NULL;
228
 
 
229
 
        DBusMessage *reply = NULL;
230
 
        va_list var_args;
231
 
 
232
 
        DBusError error;
233
 
        dbus_error_init(&error);
234
 
 
235
 
        msg = newMessage(method);
236
 
 
237
 
        va_start(var_args, first_type);
238
 
        if (!appendArgs(first_type, var_args))
239
 
                kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl; 
240
 
 
241
 
        va_end(var_args);
242
 
 
243
 
        appendArgs(DBUS_TYPE_INVALID);
244
 
 
245
 
        reply = sendWithReply(&error);
246
 
 
247
 
        if (reply) {
248
 
                getArgs(reply, &error, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &adapters, &num_adapters, DBUS_TYPE_INVALID); 
249
 
        } else {
250
 
                if (dbus_error_is_set(&error)) {
251
 
                        mError = QString(error.message);
252
 
                        kdDebug() << "libkbluetooth: " << __func__ << "(): " << error.message << endl; 
253
 
                        dbus_error_free(&error);
254
 
                }
255
 
 
256
 
                goto error;
257
 
        }
258
 
 
259
 
        for (int i = 0; i < num_adapters; i++)
260
 
                list.append(adapters[i]);
261
 
 
262
 
        dbus_free_string_array(adapters);
263
 
        dbus_message_unref(reply);
264
 
 
265
 
error:  
266
 
        dbus_message_unref(msg);
267
 
        return list;
268
 
}
269
 
 
270
 
QValueList<Q_UINT8> DBusSignal::getByteList(const QString &method)
271
 
{
272
 
        return getByteList(method, DBUS_TYPE_INVALID);
273
 
}
274
 
 
275
 
QValueList<Q_UINT8> DBusSignal::getByteList(const QString &method, int first_type, ...)
276
 
{
277
 
        QValueList<Q_UINT8> list;
278
 
 
279
 
        int num_records = 0;
280
 
        Q_INT8 *records = NULL;
281
 
        va_list var_args;
282
 
 
283
 
        DBusError error;
284
 
        DBusMessage *reply = NULL;
285
 
 
286
 
        dbus_error_init(&error);
287
 
 
288
 
        msg = newMessage(method);
289
 
 
290
 
        va_start(var_args, first_type);
291
 
        if (!appendArgs(first_type, var_args))
292
 
                kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl; 
293
 
 
294
 
        va_end(var_args);
295
 
 
296
 
        appendArgs(DBUS_TYPE_INVALID);
297
 
 
298
 
        reply = sendWithReply(&error);
299
 
 
300
 
        if (reply) {
301
 
                getArgs(reply, &error, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &records, &num_records, DBUS_TYPE_INVALID); 
302
 
        } else {
303
 
                if (dbus_error_is_set(&error)) {
304
 
                        mError = QString(error.message);
305
 
                        kdDebug() << "libkbluetooth: " << __func__ << "(): " << error.message << endl; 
306
 
                        dbus_error_free(&error);
307
 
                }
308
 
                goto error;
309
 
        }
310
 
 
311
 
        for (int i = 0; i < num_records; i++)
312
 
                list.append(records[i]);
313
 
 
314
 
        dbus_message_unref(reply);
315
 
 
316
 
error:  
317
 
        dbus_message_unref(msg);
318
 
        return list;
319
 
}
320
 
 
321
 
QValueList<Q_UINT32> DBusSignal::getUInt32List(const QString &method)
322
 
{
323
 
        return getUInt32List(method, DBUS_TYPE_INVALID);
324
 
}
325
 
 
326
 
QValueList<Q_UINT32> DBusSignal::getUInt32List(const QString &method, int first_type, ...)
327
 
{
328
 
        QValueList<Q_UINT32> list;
329
 
 
330
 
        int num_records = 0;
331
 
        Q_INT32 *records = NULL;
332
 
        va_list var_args;
333
 
 
334
 
        DBusError error;
335
 
        DBusMessage *reply = NULL;
336
 
 
337
 
        dbus_error_init(&error);
338
 
 
339
 
        msg = newMessage(method);
340
 
 
341
 
        va_start(var_args, first_type);
342
 
        if (!appendArgs(first_type, var_args))
343
 
                kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl; 
344
 
 
345
 
        va_end(var_args);
346
 
 
347
 
        appendArgs(DBUS_TYPE_INVALID);
348
 
 
349
 
        reply = sendWithReply(&error);
350
 
 
351
 
        if (reply) {
352
 
                getArgs(reply, &error, DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &records, &num_records, DBUS_TYPE_INVALID); 
353
 
        } else {
354
 
                if (dbus_error_is_set(&error)) {
355
 
                        mError = QString(error.message);
356
 
                        kdDebug() << "libkbluetooth: " << __func__ << "(): " << error.message << endl; 
357
 
                        dbus_error_free(&error);
358
 
                }
359
 
                goto error;
360
 
        }
361
 
 
362
 
        kdDebug() << k_funcinfo << " Resullt: " << num_records << endl;
363
 
 
364
 
        for (int i = 0; i < num_records; i++)
365
 
                list.append(records[i]);
366
 
 
367
 
        dbus_message_unref(reply);
368
 
 
369
 
error:
370
 
        dbus_message_unref(msg);
371
 
        return list;
372
 
}
373
 
 
374
 
bool DBusSignal::setString(const QString &method)
375
 
{
376
 
        return setString(method, DBUS_TYPE_INVALID);
377
 
}
378
 
 
379
 
bool DBusSignal::setString(const QString &method, int first_type, ...)
380
 
{
381
 
        DBusMessage *reply = NULL;
382
 
        va_list var_args;
383
 
 
384
 
        DBusError error;
385
 
        dbus_error_init(&error);
386
 
 
387
 
        msg = newMessage(method);
388
 
        va_start(var_args, first_type);
389
 
        if (!appendArgs(first_type, var_args))
390
 
                kdDebug() << "libkbluetooth: " << __func__ << "(): " << __LINE__ << "appendArgs failed" << endl; 
391
 
        va_end(var_args);
392
 
 
393
 
        reply = sendWithReply(&error);
394
 
        if (!reply) {
395
 
                if (dbus_error_is_set(&error)) {
396
 
                        mError = QString(error.message);
397
 
                        kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << error.message << endl; 
398
 
                        dbus_error_free(&error);
399
 
                }
400
 
                goto error;
401
 
 
402
 
        }
403
 
 
404
 
        dbus_message_unref(reply);
405
 
        dbus_message_unref(msg);
406
 
 
407
 
        return true; 
408
 
 
409
 
error:
410
 
        dbus_message_unref(msg);
411
 
        return false;
412
 
}
413
 
 
414
 
int DBusSignal::getInteger(const QString &method, int type)
415
 
{
416
 
        return getInteger(method, type, DBUS_TYPE_INVALID);
417
 
}
418
 
 
419
 
int DBusSignal::getInteger(const QString &method, int type, int first_type, ...)
420
 
{
421
 
 
422
 
        int ret = -1;
423
 
        DBusMessage *reply = NULL;
424
 
        DBusError error;
425
 
        dbus_error_init(&error);
426
 
        va_list var_args;
427
 
 
428
 
        msg = newMessage(method);
429
 
 
430
 
        va_start(var_args, first_type);
431
 
        appendArgs(first_type, var_args);
432
 
        va_end(var_args);
433
 
 
434
 
        reply = sendWithReply(&error);
435
 
        if (!reply) {
436
 
                if (dbus_error_is_set(&error)) {
437
 
                        mError = QString(error.message);
438
 
                        kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << error.message << endl; 
439
 
                        dbus_error_free(&error);
440
 
                }
441
 
                goto error;
442
 
        }
443
 
 
444
 
        getArgs(reply, &error, type, &ret, DBUS_TYPE_INVALID);
445
 
 
446
 
        dbus_message_unref(reply);
447
 
 
448
 
error:
449
 
        dbus_message_unref(msg);
450
 
        return ret;
451
 
}
452
 
 
453
 
bool DBusSignal::getBoolean(const QString &method)
454
 
{
455
 
        return getBoolean(method, DBUS_TYPE_INVALID);
456
 
}
457
 
 
458
 
bool DBusSignal::getBoolean(const QString &method, int first_type, ...)
459
 
{
460
 
 
461
 
        dbus_bool_t ret = false;
462
 
        DBusMessage *reply = NULL;
463
 
        DBusError error;
464
 
        dbus_error_init(&error);
465
 
        va_list var_args;
466
 
        va_start(var_args, first_type);
467
 
 
468
 
        msg = newMessage(method);
469
 
 
470
 
        appendArgs(first_type, var_args);
471
 
 
472
 
        va_end(var_args);
473
 
 
474
 
        reply = sendWithReply(&error);
475
 
        if (!reply) {
476
 
                if (dbus_error_is_set(&error)) {
477
 
                        kdDebug() << __func__ << " (" << method << "): " << error.message << endl;
478
 
                        dbus_error_free(&error);
479
 
 
480
 
                }
481
 
                goto error;
482
 
        }
483
 
 
484
 
        getArgs(reply, &error, DBUS_TYPE_BOOLEAN, &ret, DBUS_TYPE_INVALID);
485
 
 
486
 
        dbus_message_unref(reply);
487
 
 
488
 
error:
489
 
        dbus_message_unref(msg);
490
 
        return ret;
491
 
}
492
 
 
493
 
}
494