Mercurial > pidgin
annotate src/dbus-server.c @ 13759:a849cf259d00
[gaim-migrate @ 16170]
Hopefully provide a better message in the future when dbus
doesn't know about an object that you're trying to pass
to signal functions and stuff
committer: Tailor Script <tailor@pidgin.im>
| author | Mark Doliner <mark@kingant.net> |
|---|---|
| date | Wed, 10 May 2006 02:35:09 +0000 |
| parents | ac5bc9a7b603 |
| children | ec858948d292 |
| rev | line source |
|---|---|
| 11055 | 1 /* |
| 2 * gaim | |
| 3 * | |
| 4 * Gaim is the legal property of its developers, whose names are too numerous | |
| 5 * to list here. Please refer to the COPYRIGHT file distributed with this | |
| 6 * source distribution. | |
| 7 * | |
| 8 * This program is free software; you can redistribute it and/or modify | |
| 9 * it under the terms of the GNU General Public License as published by | |
| 10 * the Free Software Foundation; either version 2 of the License, or | |
| 11 * (at your option) any later version. | |
| 12 * | |
| 13 * This program is distributed in the hope that it will be useful, | |
| 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 16 * GNU General Public License for more details. | |
| 17 * | |
| 18 * You should have received a copy of the GNU General Public License | |
| 19 * along with this program; if not, write to the Free Software | |
| 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 21 * | |
| 22 */ | |
| 23 | |
| 11146 | 24 #define DBUS_API_SUBJECT_TO_CHANGE |
| 25 | |
| 11055 | 26 #include <stdio.h> |
| 27 #include <stdlib.h> | |
| 28 #include <string.h> | |
| 29 | |
| 30 #include "account.h" | |
| 11067 | 31 #include "blist.h" |
| 32 #include "conversation.h" | |
| 11055 | 33 #include "dbus-gaim.h" |
| 34 #include "dbus-server.h" | |
| 11146 | 35 #include "dbus-useful.h" |
| 11171 | 36 #include "dbus-bindings.h" |
| 11055 | 37 #include "debug.h" |
| 38 #include "core.h" | |
| 11067 | 39 #include "value.h" |
| 11055 | 40 |
| 11080 | 41 |
| 11067 | 42 /**************************************************************************/ |
| 11171 | 43 /** @name Gaim DBUS pointer registration mechanism */ |
| 11080 | 44 /**************************************************************************/ |
| 11055 | 45 |
| 11171 | 46 /* |
| 47 Here we include the list of #GAIM_DBUS_DEFINE_TYPE statements for | |
| 48 all structs defined in gaim. This file has been generated by the | |
| 49 #dbus-analize-types.py script. | |
| 50 */ | |
| 11055 | 51 |
| 11171 | 52 #include "dbus-types.c" |
| 11055 | 53 |
| 11171 | 54 /* The following three hashtables map are used to translate between |
| 55 pointers (nodes) and the corresponding handles (ids). */ | |
| 56 | |
| 57 static GHashTable *map_node_id; | |
| 58 static GHashTable *map_id_node; | |
| 59 static GHashTable *map_id_type; | |
| 11080 | 60 |
| 61 | |
| 11171 | 62 /* This function initializes the pointer-id traslation system. It |
| 63 creates the three above hashtables and defines parents of some types. | |
| 64 */ | |
| 65 void gaim_dbus_init_ids(void) { | |
| 66 map_id_node = g_hash_table_new (g_direct_hash, g_direct_equal); | |
| 67 map_id_type = g_hash_table_new (g_direct_hash, g_direct_equal); | |
| 68 map_node_id = g_hash_table_new (g_direct_hash, g_direct_equal); | |
| 69 | |
| 70 GAIM_DBUS_TYPE(GaimBuddy)->parent = GAIM_DBUS_TYPE(GaimBlistNode); | |
| 71 GAIM_DBUS_TYPE(GaimContact)->parent = GAIM_DBUS_TYPE(GaimBlistNode); | |
| 72 GAIM_DBUS_TYPE(GaimChat)->parent = GAIM_DBUS_TYPE(GaimBlistNode); | |
| 73 GAIM_DBUS_TYPE(GaimGroup)->parent = GAIM_DBUS_TYPE(GaimBlistNode); | |
| 74 } | |
| 75 | |
| 76 void gaim_dbus_register_pointer(gpointer node, GaimDBusType *type) | |
| 77 { | |
| 78 static gint last_id = 0; | |
| 79 | |
| 11291 | 80 g_return_if_fail(map_node_id); |
| 81 g_return_if_fail(g_hash_table_lookup(map_node_id, node) == NULL); | |
| 11171 | 82 |
| 83 last_id++; | |
| 84 g_hash_table_insert(map_node_id, node, GINT_TO_POINTER(last_id)); | |
| 85 g_hash_table_insert(map_id_node, GINT_TO_POINTER(last_id), node); | |
| 86 g_hash_table_insert(map_id_type, GINT_TO_POINTER(last_id), type); | |
| 87 } | |
| 88 | |
| 89 void gaim_dbus_unregister_pointer(gpointer node) { | |
| 90 gpointer id = g_hash_table_lookup(map_node_id, node); | |
| 91 | |
| 92 g_hash_table_remove(map_node_id, node); | |
| 93 g_hash_table_remove(map_id_node, GINT_TO_POINTER(id)); | |
| 94 g_hash_table_remove(map_id_type, GINT_TO_POINTER(id)); | |
| 95 } | |
| 11055 | 96 |
| 11171 | 97 gint gaim_dbus_pointer_to_id(gpointer node) { |
| 98 gint id = GPOINTER_TO_INT(g_hash_table_lookup(map_node_id, node)); | |
| 13759 | 99 if ((id == 0) && (node != NULL)) |
| 100 { | |
| 101 gaim_debug_warning("dbus", | |
| 102 "Need to register an object with the dbus subsystem.\n"); | |
| 103 g_return_if_reached(); | |
| 104 } | |
| 11171 | 105 return id; |
| 106 } | |
| 107 | |
| 108 gpointer gaim_dbus_id_to_pointer(gint id, GaimDBusType *type) { | |
| 109 GaimDBusType *objtype = | |
| 110 (GaimDBusType*) g_hash_table_lookup(map_id_type, | |
| 111 GINT_TO_POINTER(id)); | |
| 112 | |
| 113 while (objtype != type && objtype != NULL) | |
| 114 objtype = objtype->parent; | |
| 115 | |
| 116 if (objtype == type) | |
| 117 return g_hash_table_lookup(map_id_node, GINT_TO_POINTER(id)); | |
| 118 else | |
| 119 return NULL; | |
| 120 } | |
| 121 | |
| 122 gint gaim_dbus_pointer_to_id_error(gpointer ptr, DBusError *error) | |
| 123 { | |
| 124 gint id = gaim_dbus_pointer_to_id(ptr); | |
| 11055 | 125 |
| 11171 | 126 if (ptr != NULL && id == 0) |
|
13220
ac5bc9a7b603
[gaim-migrate @ 15584]
Richard Laager <rlaager@wiktel.com>
parents:
13134
diff
changeset
|
127 dbus_set_error(error, "net.sf.gaim.ObjectNotFound", |
| 11171 | 128 "The return object is not mapped (this is a Gaim error)"); |
| 129 | |
| 130 return id; | |
| 131 } | |
| 132 | |
| 133 gpointer gaim_dbus_id_to_pointer_error(gint id, GaimDBusType *type, | |
| 134 const char *typename, DBusError *error) | |
| 135 { | |
| 136 gpointer ptr = gaim_dbus_id_to_pointer(id, type); | |
| 11055 | 137 |
| 11171 | 138 if (ptr == NULL && id != 0) |
|
13220
ac5bc9a7b603
[gaim-migrate @ 15584]
Richard Laager <rlaager@wiktel.com>
parents:
13134
diff
changeset
|
139 dbus_set_error(error, "net.sf.gaim.InvalidHandle", |
| 11171 | 140 "%s object with ID = %i not found", typename, id); |
| 141 | |
| 142 return ptr; | |
| 143 } | |
| 11187 | 144 |
| 145 | |
| 146 /**************************************************************************/ | |
| 147 /** @name Modified versions of some DBus functions */ | |
| 148 /**************************************************************************/ | |
| 149 | |
| 150 dbus_bool_t | |
| 151 gaim_dbus_message_get_args (DBusMessage *message, | |
| 11277 | 152 DBusError *error, |
| 153 int first_arg_type, | |
| 154 ...) | |
| 11187 | 155 { |
| 156 dbus_bool_t retval; | |
| 157 va_list var_args; | |
| 158 | |
| 159 va_start (var_args, first_arg_type); | |
| 160 retval = gaim_dbus_message_get_args_valist (message, error, first_arg_type, var_args); | |
| 161 va_end (var_args); | |
| 162 | |
| 163 return retval; | |
| 164 } | |
| 165 | |
| 166 dbus_bool_t | |
| 167 gaim_dbus_message_get_args_valist (DBusMessage *message, | |
| 168 DBusError *error, | |
| 169 int first_arg_type, | |
| 170 va_list var_args) | |
| 171 { | |
| 172 DBusMessageIter iter; | |
| 173 | |
| 174 dbus_message_iter_init (message, &iter); | |
| 175 return gaim_dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args); | |
| 176 } | |
| 177 | |
| 178 dbus_bool_t | |
| 179 gaim_dbus_message_iter_get_args(DBusMessageIter *iter, | |
| 180 DBusError *error, | |
| 181 int first_arg_type, | |
| 182 ...) | |
| 183 { | |
| 184 dbus_bool_t retval; | |
| 185 va_list var_args; | |
| 186 | |
| 187 va_start (var_args, first_arg_type); | |
| 188 retval = gaim_dbus_message_iter_get_args_valist(iter, error, first_arg_type, var_args); | |
| 189 va_end (var_args); | |
| 190 | |
| 191 return retval; | |
| 192 } | |
| 193 | |
| 194 #define TYPE_IS_CONTAINER(typecode) \ | |
| 195 ((typecode) == DBUS_TYPE_STRUCT || \ | |
| 196 (typecode) == DBUS_TYPE_DICT_ENTRY || \ | |
| 197 (typecode) == DBUS_TYPE_VARIANT || \ | |
| 198 (typecode) == DBUS_TYPE_ARRAY) | |
| 199 | |
| 200 | |
| 201 dbus_bool_t | |
| 202 gaim_dbus_message_iter_get_args_valist (DBusMessageIter *iter, | |
| 203 DBusError *error, | |
| 204 int first_arg_type, | |
| 205 va_list var_args) | |
| 206 { | |
| 207 int spec_type, msg_type, i; | |
| 208 | |
| 209 spec_type = first_arg_type; | |
| 210 | |
| 211 for(i=0; spec_type != DBUS_TYPE_INVALID; i++) { | |
| 212 msg_type = dbus_message_iter_get_arg_type (iter); | |
| 213 | |
| 214 if (msg_type != spec_type) { | |
| 215 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, | |
| 216 "Argument %d is specified to be of type \"%i\", but " | |
| 217 "is actually of type \"%i\"\n", i, | |
| 218 spec_type, msg_type); | |
| 219 return FALSE; | |
| 220 } | |
| 221 | |
| 222 if (!TYPE_IS_CONTAINER(spec_type)) { | |
| 223 gpointer ptr; | |
| 224 ptr = va_arg (var_args, gpointer); | |
| 225 dbus_message_iter_get_basic(iter, ptr); | |
| 226 } | |
| 227 else { | |
| 228 DBusMessageIter *sub; | |
| 229 sub = va_arg (var_args, DBusMessageIter*); | |
| 230 dbus_message_iter_recurse(iter, sub); | |
| 231 g_print("subiter %i:%i\n", (int) sub, * (int*) sub); | |
| 232 break; /* for testing only! */ | |
| 233 } | |
| 11171 | 234 |
| 11187 | 235 spec_type = va_arg (var_args, int); |
| 236 if (!dbus_message_iter_next(iter) && spec_type != DBUS_TYPE_INVALID) { | |
| 237 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, | |
| 238 "Message has only %d arguments, but more were expected", i); | |
| 239 return FALSE; | |
| 240 } | |
| 241 } | |
| 242 return TRUE; | |
| 243 } | |
| 244 | |
| 245 | |
| 246 | |
| 11171 | 247 /**************************************************************************/ |
| 248 /** @name Useful functions */ | |
| 249 /**************************************************************************/ | |
| 11146 | 250 |
| 11171 | 251 const char* empty_to_null(const char *str) { |
| 11146 | 252 if (str == NULL || str[0] == 0) |
| 253 return NULL; | |
| 254 else | |
| 255 return str; | |
| 256 } | |
| 257 | |
| 11171 | 258 const char* null_to_empty(const char *s) { |
| 11080 | 259 if (s) |
| 260 return s; | |
| 261 else | |
| 262 return ""; | |
| 11055 | 263 } |
| 264 | |
| 11171 | 265 dbus_int32_t* gaim_dbusify_GList(GList *list, gboolean free_memory, |
| 266 dbus_int32_t *len) | |
| 267 { | |
| 268 dbus_int32_t *array; | |
| 269 int i; | |
| 270 GList *elem; | |
| 11055 | 271 |
| 11171 | 272 *len = g_list_length(list); |
| 273 array = g_new0(dbus_int32_t, g_list_length(list)); | |
| 274 for(i = 0, elem = list; elem != NULL; elem = elem->next, i++) | |
| 275 array[i] = gaim_dbus_pointer_to_id(elem->data); | |
| 276 | |
| 277 if (free_memory) | |
| 278 g_list_free(list); | |
| 279 | |
| 280 return array; | |
| 11080 | 281 } |
| 11055 | 282 |
| 11171 | 283 dbus_int32_t* gaim_dbusify_GSList(GSList *list, gboolean free_memory, |
| 284 dbus_int32_t *len) | |
| 285 { | |
| 286 dbus_int32_t *array; | |
| 11146 | 287 int i; |
| 11171 | 288 GSList *elem; |
| 11146 | 289 |
| 11171 | 290 *len = g_slist_length(list); |
| 291 array = g_new0(dbus_int32_t, g_slist_length(list)); | |
| 292 for(i = 0, elem = list; elem != NULL; elem = elem->next, i++) | |
| 293 array[i] = gaim_dbus_pointer_to_id(elem->data); | |
| 11067 | 294 |
| 11171 | 295 if (free_memory) |
| 296 g_slist_free(list); | |
| 11080 | 297 |
| 11171 | 298 return array; |
| 11067 | 299 } |
| 300 | |
| 11187 | 301 gpointer* gaim_GList_to_array(GList *list, gboolean free_memory, |
| 302 dbus_int32_t *len) | |
| 303 { | |
| 304 gpointer *array; | |
| 305 int i; | |
| 306 GList *elem; | |
| 307 | |
| 308 *len = g_list_length(list); | |
| 309 array = g_new0(gpointer, g_list_length(list)); | |
| 310 for(i = 0, elem = list; elem != NULL; elem = elem->next, i++) | |
| 311 array[i] = elem->data; | |
| 312 | |
| 313 if (free_memory) | |
| 314 g_list_free(list); | |
| 315 | |
| 316 return array; | |
| 317 } | |
| 318 | |
| 319 gpointer* gaim_GSList_to_array(GSList *list, gboolean free_memory, | |
| 320 dbus_int32_t *len) | |
| 321 { | |
| 322 gpointer *array; | |
| 323 int i; | |
| 324 GSList *elem; | |
| 325 | |
| 326 *len = g_slist_length(list); | |
| 327 array = g_new0(gpointer, g_slist_length(list)); | |
| 328 for(i = 0, elem = list; elem != NULL; elem = elem->next, i++) | |
| 329 array[i] = elem->data; | |
| 330 | |
| 331 if (free_memory) | |
| 332 g_slist_free(list); | |
| 333 | |
| 334 return array; | |
| 335 } | |
| 336 | |
| 337 GHashTable *gaim_dbus_iter_hash_table(DBusMessageIter *iter, DBusError *error) { | |
| 338 GHashTable *hash; | |
| 339 | |
| 340 /* we do not need to destroy strings because they are part of the message */ | |
| 341 hash = g_hash_table_new(g_str_hash, g_str_equal); | |
| 342 | |
| 343 do { | |
| 344 char *key, *value; | |
| 345 DBusMessageIter subiter; | |
| 346 | |
| 347 if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_DICT_ENTRY) | |
| 348 goto error; /* With all due respect to Dijkstra, | |
| 349 this goto is for exception | |
| 350 handling, and it is ok because it | |
| 351 avoids duplication of the code | |
| 352 responsible for destroying the hash | |
| 353 table. Exceptional instructions | |
| 354 for exceptional situations. */ | |
| 355 | |
| 356 dbus_message_iter_recurse(iter, &subiter); | |
| 357 if (!gaim_dbus_message_iter_get_args(&subiter, error, | |
| 358 DBUS_TYPE_STRING, &key, | |
| 359 DBUS_TYPE_STRING, &value, | |
| 360 DBUS_TYPE_INVALID)) | |
| 361 goto error; /* same here */ | |
| 362 | |
| 363 g_hash_table_insert(hash, key, value); | |
| 364 } while (dbus_message_iter_next(iter)); | |
| 365 | |
| 366 return hash; | |
| 367 | |
| 368 error: | |
| 369 g_hash_table_destroy(hash); | |
| 370 return NULL; | |
| 371 } | |
| 372 | |
| 11171 | 373 /**************************************************************/ |
| 374 /* DBus bindings ... */ | |
| 375 /**************************************************************/ | |
| 376 | |
| 377 static DBusConnection *gaim_dbus_connection; | |
| 378 | |
| 379 DBusConnection *gaim_dbus_get_connection(void) { | |
| 380 return gaim_dbus_connection; | |
| 381 } | |
| 382 | |
| 383 #include "dbus-bindings.c" | |
| 384 | |
| 385 void *gaim_dbus_get_handle(void) { | |
| 386 static int handle; | |
| 387 | |
| 388 return &handle; | |
| 11067 | 389 } |
| 390 | |
| 11171 | 391 static gboolean |
| 392 gaim_dbus_dispatch_cb(DBusConnection *connection, | |
| 393 DBusMessage *message, | |
| 394 void *user_data) | |
| 395 { | |
| 396 const char *name; | |
| 397 GaimDBusBinding *bindings; | |
| 398 int i; | |
| 399 | |
| 400 bindings = (GaimDBusBinding*) user_data; | |
| 401 | |
| 11175 | 402 if (!dbus_message_has_path(message, DBUS_PATH_GAIM)) |
| 11171 | 403 return FALSE; |
| 404 | |
| 405 name = dbus_message_get_member(message); | |
| 406 | |
| 11175 | 407 if (name == NULL) |
| 408 return FALSE; | |
| 409 | |
| 410 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL) | |
| 11171 | 411 return FALSE; |
| 412 | |
| 413 for(i=0; bindings[i].name; i++) | |
| 414 if (!strcmp(name, bindings[i].name)) { | |
| 415 DBusMessage *reply; | |
| 416 DBusError error; | |
| 417 | |
| 418 dbus_error_init(&error); | |
| 419 | |
| 420 reply = bindings[i].handler(message, &error); | |
| 421 | |
| 422 if (reply == NULL && dbus_error_is_set(&error)) | |
| 423 reply = dbus_message_new_error (message, | |
| 424 error.name, | |
| 425 error.message); | |
| 426 | |
| 427 if (reply != NULL) { | |
| 428 dbus_connection_send (connection, reply, NULL); | |
| 429 dbus_message_unref(reply); | |
| 430 } | |
| 431 | |
| 432 return TRUE; /* return reply! */ | |
| 433 } | |
| 434 | |
| 435 return FALSE; | |
| 11067 | 436 } |
| 11146 | 437 |
| 11175 | 438 |
| 439 static const char *gettext(const char **ptr) { | |
| 440 const char *text = *ptr; | |
| 441 *ptr += strlen(text) + 1; | |
| 442 return text; | |
| 443 } | |
| 444 | |
| 445 static void | |
| 446 gaim_dbus_introspect_cb(GList **bindings_list, void *bindings) { | |
| 447 *bindings_list = g_list_prepend(*bindings_list, bindings); | |
| 448 } | |
| 449 | |
| 450 static DBusMessage *gaim_dbus_introspect(DBusMessage *message) | |
| 451 { | |
| 452 DBusMessage *reply; | |
| 453 GString *str; | |
| 454 GList *bindings_list, *node; | |
| 455 | |
| 456 str = g_string_sized_new(0x1000); /* fixme: why this size? */ | |
| 457 | |
| 458 g_string_append(str, "<!DOCTYPE node PUBLIC '-//freedesktop//DTD D-BUS Object Introspection 1.0//EN' 'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>\n"); | |
| 459 g_string_append_printf(str, "<node name='%s'>\n", DBUS_PATH_GAIM); | |
| 460 g_string_append_printf(str, "<interface name='%s'>\n", DBUS_INTERFACE_GAIM); | |
| 461 | |
| 462 bindings_list = NULL; | |
| 463 gaim_signal_emit(gaim_dbus_get_handle(), "dbus-introspect", &bindings_list); | |
| 464 | |
| 465 for(node = bindings_list; node; node = node->next) { | |
| 466 GaimDBusBinding *bindings; | |
| 467 int i; | |
| 468 | |
| 469 bindings = (GaimDBusBinding*) node->data; | |
| 470 | |
| 471 for(i=0; bindings[i].name; i++) { | |
| 472 const char *text; | |
| 473 | |
| 474 g_string_append_printf(str, "<method name='%s'>\n", bindings[i].name); | |
| 475 | |
| 476 text = bindings[i].parameters; | |
| 477 while (*text) { | |
| 478 const char *name, *direction, *type; | |
| 479 | |
| 480 direction = gettext(&text); | |
| 481 type = gettext(&text); | |
| 482 name = gettext(&text); | |
| 483 | |
| 11187 | 484 g_string_append_printf(str, |
| 485 "<arg name='%s' type='%s' direction='%s'/>\n", | |
| 486 name, type, direction); | |
| 11175 | 487 } |
| 488 g_string_append(str, "</method>\n"); | |
| 489 } | |
| 490 } | |
| 491 | |
| 492 g_string_append(str, "</interface>\n</node>\n"); | |
| 493 | |
| 494 reply = dbus_message_new_method_return (message); | |
| 495 dbus_message_append_args(reply, DBUS_TYPE_STRING, &(str->str), | |
| 496 DBUS_TYPE_INVALID); | |
| 497 g_string_free(str, TRUE); | |
| 498 g_list_free(bindings_list); | |
| 499 | |
| 500 return reply; | |
| 501 | |
| 502 } | |
| 503 | |
| 11171 | 504 static DBusHandlerResult gaim_dbus_dispatch(DBusConnection *connection, |
| 505 DBusMessage *message, | |
| 506 void *user_data) | |
| 507 { | |
| 508 if (gaim_signal_emit_return_1(gaim_dbus_get_handle(), | |
| 509 "dbus-method-called", | |
| 510 connection, message)) | |
| 511 return DBUS_HANDLER_RESULT_HANDLED; | |
| 11175 | 512 |
| 513 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL && | |
| 514 dbus_message_has_path(message, DBUS_PATH_GAIM) && | |
| 515 dbus_message_has_interface(message, DBUS_INTERFACE_INTROSPECTABLE) && | |
| 516 dbus_message_has_member(message, "Introspect")) | |
| 517 { | |
| 518 DBusMessage *reply; | |
| 519 reply = gaim_dbus_introspect(message); | |
| 520 dbus_connection_send (connection, reply, NULL); | |
| 521 dbus_message_unref(reply); | |
| 522 return DBUS_HANDLER_RESULT_HANDLED; | |
| 523 } | |
| 524 | |
| 525 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; | |
| 11067 | 526 } |
| 11171 | 527 |
| 528 void gaim_dbus_register_bindings(void *handle, GaimDBusBinding *bindings) { | |
| 529 gaim_signal_connect(gaim_dbus_get_handle(), "dbus-method-called", | |
| 530 handle, | |
| 531 GAIM_CALLBACK(gaim_dbus_dispatch_cb), | |
| 532 bindings); | |
| 11175 | 533 gaim_signal_connect(gaim_dbus_get_handle(), "dbus-introspect", |
| 534 handle, | |
| 535 GAIM_CALLBACK(gaim_dbus_introspect_cb), | |
| 536 bindings); | |
| 11171 | 537 } |
| 11146 | 538 |
| 539 | |
| 11171 | 540 |
| 12627 | 541 static gboolean gaim_dbus_dispatch_init(void) |
| 11171 | 542 { |
| 12627 | 543 static DBusObjectPathVTable vtable = {NULL, &gaim_dbus_dispatch, NULL, NULL, NULL, NULL}; |
| 11171 | 544 |
| 545 DBusError error; | |
| 546 int result; | |
| 547 | |
| 548 dbus_error_init (&error); | |
| 549 gaim_dbus_connection = dbus_bus_get (DBUS_BUS_STARTER, &error); | |
| 11146 | 550 |
| 11171 | 551 if (gaim_dbus_connection == NULL) { |
| 552 gaim_debug_error("dbus", "Failed to get connection\n"); | |
| 553 dbus_error_free(&error); | |
| 554 return FALSE; | |
| 555 } | |
| 556 | |
| 557 if (!dbus_connection_register_object_path (gaim_dbus_connection, | |
| 558 DBUS_PATH_GAIM, &vtable, NULL)) | |
| 559 { | |
| 560 gaim_debug_error("dbus", "Failed to get name: %s\n", error.name); | |
| 561 dbus_error_free(&error); | |
| 562 return FALSE; | |
| 563 } | |
| 564 | |
| 11146 | 565 |
| 11171 | 566 result = dbus_bus_request_name (gaim_dbus_connection, DBUS_SERVICE_GAIM, |
| 567 0, &error); | |
| 568 | |
| 569 if (dbus_error_is_set (&error)) { | |
| 570 dbus_connection_unref(gaim_dbus_connection); | |
| 571 dbus_error_free(&error); | |
| 11903 | 572 gaim_dbus_connection = NULL; |
| 11171 | 573 gaim_debug_error("dbus", "Failed to get serv name: %s\n", error.name); |
| 574 return FALSE; | |
| 575 } | |
| 576 | |
| 577 dbus_connection_setup_with_g_main(gaim_dbus_connection, NULL); | |
| 11146 | 578 |
| 11171 | 579 gaim_debug_misc ("dbus", "okkk\n"); |
| 580 | |
| 581 gaim_signal_register(gaim_dbus_get_handle(), "dbus-method-called", | |
| 582 gaim_marshal_BOOLEAN__POINTER_POINTER, | |
| 583 gaim_value_new(GAIM_TYPE_BOOLEAN), 2, | |
| 584 gaim_value_new(GAIM_TYPE_POINTER), | |
| 585 gaim_value_new(GAIM_TYPE_POINTER)); | |
| 586 | |
| 11175 | 587 gaim_signal_register(gaim_dbus_get_handle(), "dbus-introspect", |
| 588 gaim_marshal_VOID__POINTER, NULL, 1, | |
| 589 gaim_value_new_outgoing(GAIM_TYPE_POINTER)); | |
| 590 | |
| 11171 | 591 GAIM_DBUS_REGISTER_BINDINGS(gaim_dbus_get_handle()); |
| 592 | |
| 593 return TRUE; | |
| 594 } | |
| 595 | |
| 11146 | 596 |
| 597 | |
| 598 /**************************************************************************/ | |
| 599 /** @name Signals */ | |
| 600 /**************************************************************************/ | |
| 601 | |
| 602 | |
| 603 | |
| 604 static char *gaim_dbus_convert_signal_name(const char *gaim_name) | |
| 605 { | |
| 606 int gaim_index, g_index; | |
| 607 char *g_name = g_new(char, strlen(gaim_name)+1); | |
| 608 gboolean capitalize_next = TRUE; | |
| 609 | |
| 610 for(gaim_index = g_index = 0; gaim_name[gaim_index]; gaim_index++) | |
| 611 if (gaim_name[gaim_index] != '-' && gaim_name[gaim_index] != '_') { | |
| 612 if (capitalize_next) | |
| 613 g_name[g_index++] = g_ascii_toupper(gaim_name[gaim_index]); | |
| 614 else | |
| 615 g_name[g_index++] = gaim_name[gaim_index]; | |
| 616 capitalize_next = FALSE; | |
| 617 } else | |
| 618 capitalize_next = TRUE; | |
| 619 g_name[g_index] = 0; | |
| 620 | |
| 621 return g_name; | |
| 622 } | |
| 623 | |
| 624 #define my_arg(type) (ptr != NULL ? * ((type *)ptr) : va_arg(data, type)) | |
| 625 | |
| 626 static void gaim_dbus_message_append_gaim_values(DBusMessageIter *iter, | |
| 627 int number, | |
| 628 GaimValue **gaim_values, | |
| 629 va_list data) | |
| 630 { | |
| 631 int i; | |
| 632 | |
| 633 for(i=0; i<number; i++) { | |
| 634 const char *str; | |
| 635 int id; | |
| 636 gint xint; | |
| 637 guint xuint; | |
| 638 gboolean xboolean; | |
| 639 gpointer ptr = NULL; | |
| 640 if (gaim_value_is_outgoing(gaim_values[i])) { | |
| 641 ptr = my_arg(gpointer); | |
| 11291 | 642 g_return_if_fail(ptr); |
| 11146 | 643 } |
| 644 | |
| 645 switch(gaim_values[i]->type) { | |
| 646 case GAIM_TYPE_INT: | |
| 647 xint = my_arg(gint); | |
| 648 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &xint); | |
| 649 break; | |
| 650 case GAIM_TYPE_UINT: | |
| 651 xuint = my_arg(guint); | |
| 652 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &xuint); | |
| 653 break; | |
| 654 case GAIM_TYPE_BOOLEAN: | |
| 655 xboolean = my_arg(gboolean); | |
| 656 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &xboolean); | |
| 657 break; | |
| 658 case GAIM_TYPE_STRING: | |
| 659 str = null_to_empty(my_arg(char*)); | |
| 660 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str); | |
| 661 break; | |
| 662 case GAIM_TYPE_SUBTYPE: /* registered pointers only! */ | |
| 663 case GAIM_TYPE_POINTER: | |
|
13134
b2d838b7eb00
[gaim-migrate @ 15496]
Richard Laager <rlaager@wiktel.com>
parents:
12627
diff
changeset
|
664 case GAIM_TYPE_OBJECT: |
|
b2d838b7eb00
[gaim-migrate @ 15496]
Richard Laager <rlaager@wiktel.com>
parents:
12627
diff
changeset
|
665 case GAIM_TYPE_BOXED: |
| 11146 | 666 id = gaim_dbus_pointer_to_id(my_arg(gpointer)); |
|
13134
b2d838b7eb00
[gaim-migrate @ 15496]
Richard Laager <rlaager@wiktel.com>
parents:
12627
diff
changeset
|
667 dbus_message_iter_append_basic(iter, (sizeof(void *) == 4) ? DBUS_TYPE_UINT32 : DBUS_TYPE_UINT64, &id); |
| 11146 | 668 break; |
| 669 default: /* no conversion implemented */ | |
| 11291 | 670 g_return_if_reached(); |
| 11146 | 671 } |
| 672 } | |
| 673 } | |
| 674 | |
| 675 #undef my_arg | |
| 676 | |
| 11903 | 677 void gaim_dbus_signal_emit_gaim(const char *name, int num_values, |
| 11146 | 678 GaimValue **values, va_list vargs) |
| 679 { | |
| 680 DBusMessage *signal; | |
| 681 DBusMessageIter iter; | |
| 682 char *newname; | |
| 683 | |
| 11903 | 684 #if 0 /* this is noisy with no dbus connection */ |
| 11171 | 685 g_return_if_fail(gaim_dbus_connection); |
| 11903 | 686 #else |
| 687 if (gaim_dbus_connection == NULL) | |
| 688 return; | |
| 689 #endif | |
| 690 | |
| 11146 | 691 |
| 11277 | 692 /* The test below is a hack that prevents our "dbus-method-called" |
| 693 signal from being propagated to dbus. What we really need is a | |
| 694 flag for each signal that states whether this signal is to be | |
| 695 dbus-propagated or not. */ | |
| 696 if (!strcmp(name, "dbus-method-called")) | |
| 697 return; | |
| 698 | |
| 11146 | 699 newname = gaim_dbus_convert_signal_name(name); |
| 700 signal = dbus_message_new_signal(DBUS_PATH_GAIM, DBUS_INTERFACE_GAIM, newname); | |
| 701 dbus_message_iter_init_append(signal, &iter); | |
| 702 | |
| 703 gaim_dbus_message_append_gaim_values(&iter, num_values, values, vargs); | |
| 704 | |
| 11171 | 705 dbus_connection_send(gaim_dbus_connection, signal, NULL); |
| 11146 | 706 |
| 707 g_free(newname); | |
| 708 dbus_message_unref(signal); | |
| 709 } | |
| 710 | |
| 711 | |
| 11080 | 712 |
| 713 | |
| 714 | |
| 715 | |
| 716 gboolean gaim_dbus_init(void) | |
| 717 { | |
| 11171 | 718 gaim_dbus_init_ids(); |
| 719 return gaim_dbus_dispatch_init() ; | |
| 11080 | 720 } |
| 11146 | 721 |
| 722 |
