3
* DBus function wrapper for signals
5
* Copyright (C) 2006 Daniel Gollub <dgollub@suse.de>
8
* This file is part of libkbluetooth.
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.
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.
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
26
#include "dbussignal.h"
28
namespace KBluetooth {
30
DBusSignal::DBusSignal(const QString &service, const QString &path, const QString &interface, DBusConnection *dbusconn)
31
: QObject(), mService(service), mPath(path), mInterface(interface), conn(dbusconn)
33
kdDebug() << k_funcinfo << ": " << service << " path: " << path << " interface: " << interface << " conn: " << dbusconn << endl;
36
DBusSignal::~DBusSignal() {
40
bool DBusSignal::serviceDown(DBusMessage *msg)
43
if (!dbus_message_is_signal (msg, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
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,
52
if (!strcmp(BLUEZ_SERVICE, service) && *new_owner == '\0') {
53
kdDebug() << k_funcinfo << " SERVICE IS DOWN" << endl;
60
bool DBusSignal::serviceUp(DBusMessage *msg)
63
if (!dbus_message_is_signal (msg, DBUS_INTERFACE_DBUS, "NameOwnerChanged"))
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,
72
if (!strcmp(BLUEZ_SERVICE, service) && *old_owner == '\0' && *new_owner != '\0') {
73
kdDebug() << k_funcinfo << " SERVICE IS UP" << endl;
80
DBusMessage* DBusSignal::newMessage(const QString &method)
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());
86
kdDebug() << "libkbluetooth: Can't allocate new method call" << endl;
93
bool DBusSignal::appendArgs(int first_type, va_list var_args)
96
retval = dbus_message_append_args_valist(msg, first_type, var_args);
100
bool DBusSignal::appendArgs(int first_type, ...)
106
va_start(var_args, first_type);
107
retval = appendArgs(first_type, var_args);
113
bool DBusSignal::getArgs(DBusMessage *message, DBusError *error, int first_type, ...) {
117
va_start(var_args, first_type);
118
retval = dbus_message_get_args_valist(message, error, first_type, var_args);
125
bool DBusSignal::send()
127
DBusPendingCall *pcall = NULL;
128
return dbus_connection_send_with_reply(conn, msg, &pcall, -1);
131
DBusMessage *DBusSignal::sendWithReply(DBusError *error, int timeout)
133
return dbus_connection_send_with_reply_and_block(conn, msg, timeout, error);
136
bool DBusSignal::sendString(const QString &method)
138
return sendString(method, DBUS_TYPE_INVALID);
141
bool DBusSignal::sendString(const QString &method, int first_type, ...)
144
DBusMessage *reply = NULL;
149
dbus_error_init(&error);
151
msg = newMessage(method);
153
va_start(var_args, first_type);
154
if (!appendArgs(first_type, var_args))
155
kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl;
160
kdDebug() << k_funcinfo << " " << ret << endl;
165
QString DBusSignal::getString(const QString &method)
167
return getString(method, DBUS_TYPE_INVALID);
170
QString DBusSignal::getString(const QString &method, int first_type, ...)
174
DBusMessage *reply = NULL;
179
dbus_error_init(&error);
181
msg = newMessage(method);
183
va_start(var_args, first_type);
184
if (!appendArgs(first_type, var_args))
185
kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl;
188
reply = sendWithReply(&error);
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);
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);
208
ret = QString::fromUtf8(string);
210
dbus_message_unref(reply);
213
dbus_message_unref(msg);
217
QValueList<QString> DBusSignal::getStringList(const QString &method)
219
return getStringList(method, DBUS_TYPE_INVALID);
222
QValueList<QString> DBusSignal::getStringList(const QString &method, int first_type, ...)
224
QValueList<QString> list;
226
int num_adapters = 0;
227
char **adapters = NULL;
229
DBusMessage *reply = NULL;
233
dbus_error_init(&error);
235
msg = newMessage(method);
237
va_start(var_args, first_type);
238
if (!appendArgs(first_type, var_args))
239
kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl;
243
appendArgs(DBUS_TYPE_INVALID);
245
reply = sendWithReply(&error);
248
getArgs(reply, &error, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &adapters, &num_adapters, DBUS_TYPE_INVALID);
250
if (dbus_error_is_set(&error)) {
251
mError = QString(error.message);
252
kdDebug() << "libkbluetooth: " << __func__ << "(): " << error.message << endl;
253
dbus_error_free(&error);
259
for (int i = 0; i < num_adapters; i++)
260
list.append(adapters[i]);
262
dbus_free_string_array(adapters);
263
dbus_message_unref(reply);
266
dbus_message_unref(msg);
270
QValueList<Q_UINT8> DBusSignal::getByteList(const QString &method)
272
return getByteList(method, DBUS_TYPE_INVALID);
275
QValueList<Q_UINT8> DBusSignal::getByteList(const QString &method, int first_type, ...)
277
QValueList<Q_UINT8> list;
280
Q_INT8 *records = NULL;
284
DBusMessage *reply = NULL;
286
dbus_error_init(&error);
288
msg = newMessage(method);
290
va_start(var_args, first_type);
291
if (!appendArgs(first_type, var_args))
292
kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl;
296
appendArgs(DBUS_TYPE_INVALID);
298
reply = sendWithReply(&error);
301
getArgs(reply, &error, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &records, &num_records, DBUS_TYPE_INVALID);
303
if (dbus_error_is_set(&error)) {
304
mError = QString(error.message);
305
kdDebug() << "libkbluetooth: " << __func__ << "(): " << error.message << endl;
306
dbus_error_free(&error);
311
for (int i = 0; i < num_records; i++)
312
list.append(records[i]);
314
dbus_message_unref(reply);
317
dbus_message_unref(msg);
321
QValueList<Q_UINT32> DBusSignal::getUInt32List(const QString &method)
323
return getUInt32List(method, DBUS_TYPE_INVALID);
326
QValueList<Q_UINT32> DBusSignal::getUInt32List(const QString &method, int first_type, ...)
328
QValueList<Q_UINT32> list;
331
Q_INT32 *records = NULL;
335
DBusMessage *reply = NULL;
337
dbus_error_init(&error);
339
msg = newMessage(method);
341
va_start(var_args, first_type);
342
if (!appendArgs(first_type, var_args))
343
kdDebug() << "libkbluetooth: " << __func__ << "(" << method << "): " << __LINE__ << "appendArgs failed" << endl;
347
appendArgs(DBUS_TYPE_INVALID);
349
reply = sendWithReply(&error);
352
getArgs(reply, &error, DBUS_TYPE_ARRAY, DBUS_TYPE_UINT32, &records, &num_records, DBUS_TYPE_INVALID);
354
if (dbus_error_is_set(&error)) {
355
mError = QString(error.message);
356
kdDebug() << "libkbluetooth: " << __func__ << "(): " << error.message << endl;
357
dbus_error_free(&error);
362
kdDebug() << k_funcinfo << " Resullt: " << num_records << endl;
364
for (int i = 0; i < num_records; i++)
365
list.append(records[i]);
367
dbus_message_unref(reply);
370
dbus_message_unref(msg);
374
bool DBusSignal::setString(const QString &method)
376
return setString(method, DBUS_TYPE_INVALID);
379
bool DBusSignal::setString(const QString &method, int first_type, ...)
381
DBusMessage *reply = NULL;
385
dbus_error_init(&error);
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;
393
reply = sendWithReply(&error);
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);
404
dbus_message_unref(reply);
405
dbus_message_unref(msg);
410
dbus_message_unref(msg);
414
int DBusSignal::getInteger(const QString &method, int type)
416
return getInteger(method, type, DBUS_TYPE_INVALID);
419
int DBusSignal::getInteger(const QString &method, int type, int first_type, ...)
423
DBusMessage *reply = NULL;
425
dbus_error_init(&error);
428
msg = newMessage(method);
430
va_start(var_args, first_type);
431
appendArgs(first_type, var_args);
434
reply = sendWithReply(&error);
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);
444
getArgs(reply, &error, type, &ret, DBUS_TYPE_INVALID);
446
dbus_message_unref(reply);
449
dbus_message_unref(msg);
453
bool DBusSignal::getBoolean(const QString &method)
455
return getBoolean(method, DBUS_TYPE_INVALID);
458
bool DBusSignal::getBoolean(const QString &method, int first_type, ...)
461
dbus_bool_t ret = false;
462
DBusMessage *reply = NULL;
464
dbus_error_init(&error);
466
va_start(var_args, first_type);
468
msg = newMessage(method);
470
appendArgs(first_type, var_args);
474
reply = sendWithReply(&error);
476
if (dbus_error_is_set(&error)) {
477
kdDebug() << __func__ << " (" << method << "): " << error.message << endl;
478
dbus_error_free(&error);
484
getArgs(reply, &error, DBUS_TYPE_BOOLEAN, &ret, DBUS_TYPE_INVALID);
486
dbus_message_unref(reply);
489
dbus_message_unref(msg);