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
|
|
80 g_assert(map_node_id);
|
|
81 g_assert(g_hash_table_lookup(map_node_id, node) == NULL);
|
|
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));
|
|
99 g_return_val_if_fail(id, 0);
|
|
100 return id;
|
|
101 }
|
|
102
|
|
103 gpointer gaim_dbus_id_to_pointer(gint id, GaimDBusType *type) {
|
|
104 GaimDBusType *objtype =
|
|
105 (GaimDBusType*) g_hash_table_lookup(map_id_type,
|
|
106 GINT_TO_POINTER(id));
|
|
107
|
|
108 while (objtype != type && objtype != NULL)
|
|
109 objtype = objtype->parent;
|
|
110
|
|
111 if (objtype == type)
|
|
112 return g_hash_table_lookup(map_id_node, GINT_TO_POINTER(id));
|
|
113 else
|
|
114 return NULL;
|
|
115 }
|
|
116
|
|
117 gint gaim_dbus_pointer_to_id_error(gpointer ptr, DBusError *error)
|
|
118 {
|
|
119 gint id = gaim_dbus_pointer_to_id(ptr);
|
11055
|
120
|
11171
|
121 if (ptr != NULL && id == 0)
|
|
122 dbus_set_error(error, "org.gaim.ObjectNotFound",
|
|
123 "The return object is not mapped (this is a Gaim error)");
|
|
124
|
|
125 return id;
|
|
126 }
|
|
127
|
|
128 gpointer gaim_dbus_id_to_pointer_error(gint id, GaimDBusType *type,
|
|
129 const char *typename, DBusError *error)
|
|
130 {
|
|
131 gpointer ptr = gaim_dbus_id_to_pointer(id, type);
|
11055
|
132
|
11171
|
133 if (ptr == NULL && id != 0)
|
|
134 dbus_set_error(error, "org.gaim.InvalidHandle",
|
|
135 "%s object with ID = %i not found", typename, id);
|
|
136
|
|
137 return ptr;
|
|
138 }
|
|
139
|
|
140 /**************************************************************************/
|
|
141 /** @name Useful functions */
|
|
142 /**************************************************************************/
|
11146
|
143
|
11171
|
144 const char* empty_to_null(const char *str) {
|
11146
|
145 if (str == NULL || str[0] == 0)
|
|
146 return NULL;
|
|
147 else
|
|
148 return str;
|
|
149 }
|
|
150
|
11171
|
151 const char* null_to_empty(const char *s) {
|
11080
|
152 if (s)
|
|
153 return s;
|
|
154 else
|
|
155 return "";
|
11055
|
156 }
|
|
157
|
11080
|
158
|
|
159
|
11171
|
160
|
|
161 dbus_int32_t* gaim_dbusify_GList(GList *list, gboolean free_memory,
|
|
162 dbus_int32_t *len)
|
|
163 {
|
|
164 dbus_int32_t *array;
|
|
165 int i;
|
|
166 GList *elem;
|
11055
|
167
|
11171
|
168 *len = g_list_length(list);
|
|
169 array = g_new0(dbus_int32_t, g_list_length(list));
|
|
170 for(i = 0, elem = list; elem != NULL; elem = elem->next, i++)
|
|
171 array[i] = gaim_dbus_pointer_to_id(elem->data);
|
|
172
|
|
173 if (free_memory)
|
|
174 g_list_free(list);
|
|
175
|
|
176 return array;
|
11080
|
177 }
|
11055
|
178
|
11171
|
179 dbus_int32_t* gaim_dbusify_GSList(GSList *list, gboolean free_memory,
|
|
180 dbus_int32_t *len)
|
|
181 {
|
|
182 dbus_int32_t *array;
|
11146
|
183 int i;
|
11171
|
184 GSList *elem;
|
11146
|
185
|
11171
|
186 *len = g_slist_length(list);
|
|
187 array = g_new0(dbus_int32_t, g_slist_length(list));
|
|
188 for(i = 0, elem = list; elem != NULL; elem = elem->next, i++)
|
|
189 array[i] = gaim_dbus_pointer_to_id(elem->data);
|
11067
|
190
|
11171
|
191 if (free_memory)
|
|
192 g_slist_free(list);
|
11080
|
193
|
11171
|
194 return array;
|
11067
|
195 }
|
|
196
|
11171
|
197 /**************************************************************/
|
|
198 /* DBus bindings ... */
|
|
199 /**************************************************************/
|
|
200
|
|
201 static DBusConnection *gaim_dbus_connection;
|
|
202
|
|
203 DBusConnection *gaim_dbus_get_connection(void) {
|
|
204 return gaim_dbus_connection;
|
|
205 }
|
|
206
|
|
207 #include "dbus-bindings.c"
|
|
208
|
|
209 void *gaim_dbus_get_handle(void) {
|
|
210 static int handle;
|
|
211
|
|
212 return &handle;
|
11067
|
213 }
|
|
214
|
11171
|
215 static gboolean
|
|
216 gaim_dbus_dispatch_cb(DBusConnection *connection,
|
|
217 DBusMessage *message,
|
|
218 void *user_data)
|
|
219 {
|
|
220 const char *name;
|
|
221 GaimDBusBinding *bindings;
|
|
222 int i;
|
|
223
|
|
224 bindings = (GaimDBusBinding*) user_data;
|
|
225
|
11175
|
226 if (!dbus_message_has_path(message, DBUS_PATH_GAIM))
|
11171
|
227 return FALSE;
|
|
228
|
|
229 name = dbus_message_get_member(message);
|
|
230
|
11175
|
231 if (name == NULL)
|
|
232 return FALSE;
|
|
233
|
|
234 if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
|
11171
|
235 return FALSE;
|
|
236
|
|
237 for(i=0; bindings[i].name; i++)
|
|
238 if (!strcmp(name, bindings[i].name)) {
|
|
239 DBusMessage *reply;
|
|
240 DBusError error;
|
|
241
|
|
242 dbus_error_init(&error);
|
|
243
|
|
244 reply = bindings[i].handler(message, &error);
|
|
245
|
|
246 if (reply == NULL && dbus_error_is_set(&error))
|
|
247 reply = dbus_message_new_error (message,
|
|
248 error.name,
|
|
249 error.message);
|
|
250
|
|
251 if (reply != NULL) {
|
|
252 dbus_connection_send (connection, reply, NULL);
|
|
253 dbus_message_unref(reply);
|
|
254 }
|
|
255
|
|
256 return TRUE; /* return reply! */
|
|
257 }
|
|
258
|
|
259 return FALSE;
|
11067
|
260 }
|
11146
|
261
|
11175
|
262
|
|
263 /* Introspection */
|
|
264
|
|
265 static const char *gettext(const char **ptr) {
|
|
266 const char *text = *ptr;
|
|
267 *ptr += strlen(text) + 1;
|
|
268 return text;
|
|
269 }
|
|
270
|
|
271 static void
|
|
272 gaim_dbus_introspect_cb(GList **bindings_list, void *bindings) {
|
|
273 *bindings_list = g_list_prepend(*bindings_list, bindings);
|
|
274 }
|
|
275
|
|
276 static DBusMessage *gaim_dbus_introspect(DBusMessage *message)
|
|
277 {
|
|
278 DBusMessage *reply;
|
|
279 GString *str;
|
|
280 GList *bindings_list, *node;
|
|
281
|
|
282 str = g_string_sized_new(0x1000); /* fixme: why this size? */
|
|
283
|
|
284 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");
|
|
285 g_string_append_printf(str, "<node name='%s'>\n", DBUS_PATH_GAIM);
|
|
286 g_string_append_printf(str, "<interface name='%s'>\n", DBUS_INTERFACE_GAIM);
|
|
287
|
|
288 bindings_list = NULL;
|
|
289 gaim_signal_emit(gaim_dbus_get_handle(), "dbus-introspect", &bindings_list);
|
|
290
|
|
291 for(node = bindings_list; node; node = node->next) {
|
|
292 GaimDBusBinding *bindings;
|
|
293 int i;
|
|
294
|
|
295 bindings = (GaimDBusBinding*) node->data;
|
|
296
|
|
297 for(i=0; bindings[i].name; i++) {
|
|
298 const char *text;
|
|
299
|
|
300 g_string_append_printf(str, "<method name='%s'>\n", bindings[i].name);
|
|
301
|
|
302 text = bindings[i].parameters;
|
|
303 while (*text) {
|
|
304 const char *name, *direction, *type;
|
|
305
|
|
306 direction = gettext(&text);
|
|
307 type = gettext(&text);
|
|
308 name = gettext(&text);
|
|
309
|
|
310 g_string_append_printf(str,
|
|
311 "<arg name='%s' type='%s' direction='%s'/>\n",
|
|
312 name, type, direction);
|
|
313 }
|
|
314 g_string_append(str, "</method>\n");
|
|
315 }
|
|
316 }
|
|
317
|
|
318 g_string_append(str, "</interface>\n</node>\n");
|
|
319
|
|
320 reply = dbus_message_new_method_return (message);
|
|
321 dbus_message_append_args(reply, DBUS_TYPE_STRING, &(str->str),
|
|
322 DBUS_TYPE_INVALID);
|
|
323 g_string_free(str, TRUE);
|
|
324 g_list_free(bindings_list);
|
|
325
|
|
326 return reply;
|
|
327
|
|
328 }
|
|
329
|
11171
|
330 static DBusHandlerResult gaim_dbus_dispatch(DBusConnection *connection,
|
|
331 DBusMessage *message,
|
|
332 void *user_data)
|
|
333 {
|
|
334 if (gaim_signal_emit_return_1(gaim_dbus_get_handle(),
|
|
335 "dbus-method-called",
|
|
336 connection, message))
|
|
337 return DBUS_HANDLER_RESULT_HANDLED;
|
11175
|
338
|
|
339 if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_CALL &&
|
|
340 dbus_message_has_path(message, DBUS_PATH_GAIM) &&
|
|
341 dbus_message_has_interface(message, DBUS_INTERFACE_INTROSPECTABLE) &&
|
|
342 dbus_message_has_member(message, "Introspect"))
|
|
343 {
|
|
344 DBusMessage *reply;
|
|
345 reply = gaim_dbus_introspect(message);
|
|
346 dbus_connection_send (connection, reply, NULL);
|
|
347 dbus_message_unref(reply);
|
|
348 return DBUS_HANDLER_RESULT_HANDLED;
|
|
349 }
|
|
350
|
|
351 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
11067
|
352 }
|
11171
|
353
|
|
354 void gaim_dbus_register_bindings(void *handle, GaimDBusBinding *bindings) {
|
|
355 gaim_signal_connect(gaim_dbus_get_handle(), "dbus-method-called",
|
|
356 handle,
|
|
357 GAIM_CALLBACK(gaim_dbus_dispatch_cb),
|
|
358 bindings);
|
11175
|
359 gaim_signal_connect(gaim_dbus_get_handle(), "dbus-introspect",
|
|
360 handle,
|
|
361 GAIM_CALLBACK(gaim_dbus_introspect_cb),
|
|
362 bindings);
|
11171
|
363 }
|
11146
|
364
|
|
365
|
11171
|
366
|
|
367 gboolean gaim_dbus_dispatch_init(void)
|
|
368 {
|
|
369 static DBusObjectPathVTable vtable = {NULL, &gaim_dbus_dispatch};
|
|
370
|
|
371 DBusError error;
|
|
372 int result;
|
|
373
|
|
374 dbus_error_init (&error);
|
|
375 gaim_dbus_connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
|
11146
|
376
|
11171
|
377 if (gaim_dbus_connection == NULL) {
|
|
378 gaim_debug_error("dbus", "Failed to get connection\n");
|
|
379 dbus_error_free(&error);
|
|
380 return FALSE;
|
|
381 }
|
|
382
|
|
383 if (!dbus_connection_register_object_path (gaim_dbus_connection,
|
|
384 DBUS_PATH_GAIM, &vtable, NULL))
|
|
385 {
|
|
386 gaim_debug_error("dbus", "Failed to get name: %s\n", error.name);
|
|
387 dbus_error_free(&error);
|
|
388 return FALSE;
|
|
389 }
|
|
390
|
11146
|
391
|
11171
|
392 result = dbus_bus_request_name (gaim_dbus_connection, DBUS_SERVICE_GAIM,
|
|
393 0, &error);
|
|
394
|
|
395 if (dbus_error_is_set (&error)) {
|
|
396 dbus_connection_unref(gaim_dbus_connection);
|
|
397 dbus_error_free(&error);
|
|
398 gaim_debug_error("dbus", "Failed to get serv name: %s\n", error.name);
|
|
399 return FALSE;
|
|
400 }
|
|
401
|
|
402 dbus_connection_setup_with_g_main(gaim_dbus_connection, NULL);
|
11146
|
403
|
11171
|
404 gaim_debug_misc ("dbus", "okkk\n");
|
|
405
|
|
406 gaim_signal_register(gaim_dbus_get_handle(), "dbus-method-called",
|
|
407 gaim_marshal_BOOLEAN__POINTER_POINTER,
|
|
408 gaim_value_new(GAIM_TYPE_BOOLEAN), 2,
|
|
409 gaim_value_new(GAIM_TYPE_POINTER),
|
|
410 gaim_value_new(GAIM_TYPE_POINTER));
|
|
411
|
11175
|
412 gaim_signal_register(gaim_dbus_get_handle(), "dbus-introspect",
|
|
413 gaim_marshal_VOID__POINTER, NULL, 1,
|
|
414 gaim_value_new_outgoing(GAIM_TYPE_POINTER));
|
|
415
|
11171
|
416 GAIM_DBUS_REGISTER_BINDINGS(gaim_dbus_get_handle());
|
|
417
|
|
418 return TRUE;
|
|
419 }
|
|
420
|
11146
|
421
|
|
422
|
|
423 /**************************************************************************/
|
|
424 /** @name Signals */
|
|
425 /**************************************************************************/
|
|
426
|
|
427
|
|
428
|
|
429 static char *gaim_dbus_convert_signal_name(const char *gaim_name)
|
|
430 {
|
|
431 int gaim_index, g_index;
|
|
432 char *g_name = g_new(char, strlen(gaim_name)+1);
|
|
433 gboolean capitalize_next = TRUE;
|
|
434
|
|
435 for(gaim_index = g_index = 0; gaim_name[gaim_index]; gaim_index++)
|
|
436 if (gaim_name[gaim_index] != '-' && gaim_name[gaim_index] != '_') {
|
|
437 if (capitalize_next)
|
|
438 g_name[g_index++] = g_ascii_toupper(gaim_name[gaim_index]);
|
|
439 else
|
|
440 g_name[g_index++] = gaim_name[gaim_index];
|
|
441 capitalize_next = FALSE;
|
|
442 } else
|
|
443 capitalize_next = TRUE;
|
|
444 g_name[g_index] = 0;
|
|
445
|
|
446 return g_name;
|
|
447 }
|
|
448
|
|
449 #define my_arg(type) (ptr != NULL ? * ((type *)ptr) : va_arg(data, type))
|
|
450
|
|
451 static void gaim_dbus_message_append_gaim_values(DBusMessageIter *iter,
|
|
452 int number,
|
|
453 GaimValue **gaim_values,
|
|
454 va_list data)
|
|
455 {
|
|
456 int i;
|
|
457
|
|
458 for(i=0; i<number; i++) {
|
|
459 const char *str;
|
|
460 int id;
|
|
461 gint xint;
|
|
462 guint xuint;
|
|
463 gboolean xboolean;
|
|
464 gpointer ptr = NULL;
|
|
465 if (gaim_value_is_outgoing(gaim_values[i])) {
|
|
466 ptr = my_arg(gpointer);
|
|
467 g_assert(ptr);
|
|
468 }
|
|
469
|
|
470 switch(gaim_values[i]->type) {
|
|
471 case GAIM_TYPE_INT:
|
|
472 xint = my_arg(gint);
|
|
473 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &xint);
|
|
474 break;
|
|
475 case GAIM_TYPE_UINT:
|
|
476 xuint = my_arg(guint);
|
|
477 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &xuint);
|
|
478 break;
|
|
479 case GAIM_TYPE_BOOLEAN:
|
|
480 xboolean = my_arg(gboolean);
|
|
481 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &xboolean);
|
|
482 break;
|
|
483 case GAIM_TYPE_STRING:
|
|
484 str = null_to_empty(my_arg(char*));
|
|
485 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str);
|
|
486 break;
|
|
487 case GAIM_TYPE_SUBTYPE: /* registered pointers only! */
|
|
488 case GAIM_TYPE_POINTER:
|
|
489 case GAIM_TYPE_OBJECT:
|
|
490 case GAIM_TYPE_BOXED:
|
|
491 id = gaim_dbus_pointer_to_id(my_arg(gpointer));
|
|
492 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &id);
|
|
493 break;
|
|
494 default: /* no conversion implemented */
|
|
495 g_assert_not_reached();
|
|
496 }
|
|
497 }
|
|
498 }
|
|
499
|
|
500 #undef my_arg
|
|
501
|
11171
|
502 void gaim_dbus_signal_emit_gaim(char *name, int num_values,
|
11146
|
503 GaimValue **values, va_list vargs)
|
|
504 {
|
|
505 /* pass name */
|
|
506 DBusMessage *signal;
|
|
507 DBusMessageIter iter;
|
|
508 char *newname;
|
|
509
|
11171
|
510 g_return_if_fail(gaim_dbus_connection);
|
11146
|
511
|
|
512 newname = gaim_dbus_convert_signal_name(name);
|
|
513 signal = dbus_message_new_signal(DBUS_PATH_GAIM, DBUS_INTERFACE_GAIM, newname);
|
|
514 dbus_message_iter_init_append(signal, &iter);
|
|
515
|
|
516 gaim_dbus_message_append_gaim_values(&iter, num_values, values, vargs);
|
|
517
|
11171
|
518 dbus_connection_send(gaim_dbus_connection, signal, NULL);
|
11146
|
519
|
|
520 g_free(newname);
|
|
521 dbus_message_unref(signal);
|
|
522 }
|
|
523
|
|
524
|
11080
|
525
|
|
526
|
|
527
|
|
528
|
|
529 gboolean gaim_dbus_init(void)
|
|
530 {
|
11171
|
531 gaim_debug_register_category("dbus");
|
11146
|
532
|
11171
|
533 gaim_dbus_init_ids();
|
|
534 return gaim_dbus_dispatch_init() ;
|
11080
|
535 }
|
11146
|
536
|
|
537
|
11175
|
538 /* Introspection support */
|
|
539
|
|
540
|
|
541
|