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 <dbus/dbus-glib.h>
|
11146
|
27 #include <dbus/dbus-glib-lowlevel.h>
|
11055
|
28 #include <dbus/dbus-glib-bindings.h>
|
|
29 #include <stdio.h>
|
|
30 #include <stdlib.h>
|
|
31 #include <string.h>
|
|
32 #include <glib/gi18n.h>
|
|
33 #include <glib-object.h>
|
|
34 #include <glib/gquark.h>
|
11080
|
35 #include <glib.h>
|
11055
|
36
|
|
37 #include "account.h"
|
11067
|
38 #include "blist.h"
|
|
39 #include "conversation.h"
|
11055
|
40 #include "dbus-gaim.h"
|
|
41 #include "dbus-server.h"
|
11146
|
42 #include "dbus-useful.h"
|
11055
|
43 #include "debug.h"
|
|
44 #include "core.h"
|
11067
|
45 #include "value.h"
|
11055
|
46
|
11080
|
47
|
|
48
|
11067
|
49 /**************************************************************************/
|
11080
|
50 /** @name Lots of GObject stuff I don't really understand */
|
|
51 /**************************************************************************/
|
11055
|
52
|
|
53 GType gaim_object_get_type(void);
|
|
54
|
11080
|
55 struct _GaimObject {
|
11146
|
56 GObject parent;
|
|
57 DBusGProxy *proxy;
|
11055
|
58 };
|
|
59
|
11080
|
60 typedef struct {
|
11055
|
61 GObjectClass parent;
|
11080
|
62 } GaimObjectClass;
|
|
63
|
|
64
|
|
65 #define GAIM_DBUS_TYPE_OBJECT (gaim_object_get_type ())
|
|
66 #define GAIM_DBUS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GAIM_DBUS_TYPE_OBJECT, GaimObject))
|
|
67 #define GAIM_DBUS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAIM_DBUS_TYPE_OBJECT, GaimObjectClass))
|
|
68 #define GAIM_DBUS_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GAIM_DBUS_TYPE_OBJECT))
|
|
69 #define GAIM_DBUS_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAIM_DBUS_TYPE_OBJECT))
|
|
70 #define GAIM_DBUS_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAIM_DBUS_TYPE_OBJECT, GaimObjectClass))
|
11055
|
71
|
|
72 G_DEFINE_TYPE(GaimObject, gaim_object, G_TYPE_OBJECT)
|
|
73
|
11080
|
74 GaimObject *gaim_dbus_object;
|
|
75 static GQuark gaim_object_error_quark;
|
11055
|
76
|
11146
|
77 #define NULLIFY(id) id = empty_to_null(id)
|
|
78
|
|
79 static const char* empty_to_null(const char *str) {
|
|
80 if (str == NULL || str[0] == 0)
|
|
81 return NULL;
|
|
82 else
|
|
83 return str;
|
|
84 }
|
|
85
|
11080
|
86 static const char* null_to_empty(const char *s) {
|
|
87 if (s)
|
|
88 return s;
|
|
89 else
|
|
90 return "";
|
11055
|
91 }
|
|
92
|
11080
|
93
|
|
94
|
|
95 static void gaim_object_class_init(GaimObjectClass *klass)
|
11055
|
96 {
|
|
97 }
|
|
98
|
11146
|
99 static void gaim_object_init(GaimObject *object)
|
11080
|
100 {
|
|
101 }
|
11055
|
102
|
11067
|
103 /**************************************************************************/
|
|
104 /** @name Gaim DBUS pointer registration mechanism */
|
|
105 /**************************************************************************/
|
|
106
|
11146
|
107 GaimDBusPointerType dbus_type_parent[DBUS_POINTER_LASTTYPE];
|
|
108
|
11067
|
109 static GHashTable *map_id_node;
|
|
110 static GHashTable *map_id_type;
|
|
111 static GHashTable *map_node_id;
|
|
112
|
11146
|
113
|
|
114 #define DECLARE_TYPE(type, parent) \
|
|
115 dbus_type_parent[DBUS_POINTER_##type] = DBUS_POINTER_##parent
|
|
116
|
11067
|
117 void gaim_dbus_init_ids(void) {
|
11146
|
118 int i;
|
11080
|
119
|
11067
|
120 map_id_node = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
121 map_id_type = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
122 map_node_id = g_hash_table_new (g_direct_hash, g_direct_equal);
|
11146
|
123
|
|
124 for (i = 0; i < DBUS_POINTER_LASTTYPE; i++)
|
|
125 dbus_type_parent[i] = DBUS_POINTER_NONE;
|
|
126
|
|
127 /* some manual corrections */
|
|
128 DECLARE_TYPE(GaimBuddy, GaimBlistNode);
|
|
129 DECLARE_TYPE(GaimContact, GaimBlistNode);
|
|
130 DECLARE_TYPE(GaimChat, GaimBlistNode);
|
|
131 DECLARE_TYPE(GaimGroup, GaimBlistNode);
|
11067
|
132 }
|
|
133
|
|
134 void gaim_dbus_register_pointer(gpointer node, GaimDBusPointerType type)
|
|
135 {
|
|
136 static gint last_id = 0;
|
11080
|
137
|
|
138 g_assert(map_node_id);
|
11067
|
139 g_assert(g_hash_table_lookup(map_node_id, node) == NULL);
|
|
140
|
|
141
|
|
142 last_id++;
|
|
143 g_hash_table_insert(map_node_id, node, GINT_TO_POINTER(last_id));
|
|
144 g_hash_table_insert(map_id_node, GINT_TO_POINTER(last_id), node);
|
|
145 g_hash_table_insert(map_id_type, GINT_TO_POINTER(last_id),
|
|
146 GINT_TO_POINTER(type));
|
|
147 }
|
|
148
|
|
149
|
|
150 void gaim_dbus_unregister_pointer(gpointer node) {
|
|
151 gpointer id = g_hash_table_lookup(map_node_id, node);
|
|
152
|
|
153 g_hash_table_remove(map_node_id, node);
|
|
154 g_hash_table_remove(map_id_node, GINT_TO_POINTER(id));
|
|
155 g_hash_table_remove(map_id_node, GINT_TO_POINTER(id));
|
|
156 }
|
|
157
|
11080
|
158 static gint gaim_dbus_pointer_to_id(gpointer node) {
|
11067
|
159 gint id = GPOINTER_TO_INT(g_hash_table_lookup(map_node_id, node));
|
11080
|
160 g_return_val_if_fail(id, 0);
|
11067
|
161 return id;
|
|
162 }
|
|
163
|
11080
|
164 static gpointer gaim_dbus_id_to_pointer(gint id, GaimDBusPointerType type) {
|
11146
|
165 GaimDBusPointerType objtype =
|
|
166 GPOINTER_TO_INT(g_hash_table_lookup(map_id_type,
|
|
167 GINT_TO_POINTER(id)));
|
|
168
|
|
169 while (objtype != type && objtype != DBUS_POINTER_NONE)
|
|
170 objtype = dbus_type_parent[objtype];
|
|
171
|
|
172 if (objtype == type)
|
|
173 return g_hash_table_lookup(map_id_node, GINT_TO_POINTER(id));
|
|
174 else
|
11067
|
175 return NULL;
|
|
176 }
|
|
177
|
11146
|
178 /**************************************************************************/
|
|
179 /** @name Useful functions */
|
|
180 /**************************************************************************/
|
|
181
|
|
182
|
|
183 #define error_unless_1(condition, str, parameter) \
|
|
184 if (!(condition)) { \
|
|
185 g_set_error(error, gaim_object_error_quark, \
|
|
186 DBUS_ERROR_NOT_FOUND, \
|
|
187 str, parameter); \
|
|
188 return FALSE; \
|
|
189 }
|
|
190
|
|
191 #define error_unless_2(condition, str, a,b) \
|
|
192 if (!(condition)) { \
|
|
193 g_set_error(error, gaim_object_error_quark, \
|
|
194 DBUS_ERROR_NOT_FOUND, \
|
|
195 str, a,b); \
|
|
196 return FALSE; \
|
|
197 }
|
|
198
|
|
199 #define GAIM_DBUS_ID_TO_POINTER(ptr, id, type) \
|
|
200 G_STMT_START { \
|
|
201 ptr = ((type*) gaim_dbus_id_to_pointer \
|
|
202 (id, DBUS_POINTER_##type)); \
|
|
203 error_unless_2(ptr != NULL || id == 0, \
|
|
204 "%s object with ID = %i not found", \
|
|
205 #type, id); \
|
|
206 } G_STMT_END
|
|
207
|
|
208
|
|
209 #define GAIM_DBUS_POINTER_TO_ID(id, ptr) \
|
|
210 G_STMT_START { \
|
|
211 gpointer _ptr = ptr; \
|
|
212 id = gaim_dbus_pointer_to_id(_ptr); \
|
|
213 error_unless_1(ptr == NULL || id != 0, \
|
|
214 "Result object not registered (%i)", \
|
|
215 id); \
|
|
216 } G_STMT_END
|
|
217
|
|
218
|
|
219 /**************************************************************************/
|
|
220 /** @name Signals */
|
|
221 /**************************************************************************/
|
|
222
|
|
223
|
|
224
|
|
225 static char *gaim_dbus_convert_signal_name(const char *gaim_name)
|
|
226 {
|
|
227 int gaim_index, g_index;
|
|
228 char *g_name = g_new(char, strlen(gaim_name)+1);
|
|
229 gboolean capitalize_next = TRUE;
|
|
230
|
|
231 for(gaim_index = g_index = 0; gaim_name[gaim_index]; gaim_index++)
|
|
232 if (gaim_name[gaim_index] != '-' && gaim_name[gaim_index] != '_') {
|
|
233 if (capitalize_next)
|
|
234 g_name[g_index++] = g_ascii_toupper(gaim_name[gaim_index]);
|
|
235 else
|
|
236 g_name[g_index++] = gaim_name[gaim_index];
|
|
237 capitalize_next = FALSE;
|
|
238 } else
|
|
239 capitalize_next = TRUE;
|
|
240 g_name[g_index] = 0;
|
|
241
|
|
242 return g_name;
|
|
243 }
|
|
244
|
|
245 #define my_arg(type) (ptr != NULL ? * ((type *)ptr) : va_arg(data, type))
|
|
246
|
|
247 static void gaim_dbus_message_append_gaim_values(DBusMessageIter *iter,
|
|
248 int number,
|
|
249 GaimValue **gaim_values,
|
|
250 va_list data)
|
|
251 {
|
|
252 int i;
|
|
253
|
|
254 for(i=0; i<number; i++) {
|
|
255 const char *str;
|
|
256 int id;
|
|
257 gint xint;
|
|
258 guint xuint;
|
|
259 gboolean xboolean;
|
|
260 gpointer ptr = NULL;
|
|
261 if (gaim_value_is_outgoing(gaim_values[i])) {
|
|
262 ptr = my_arg(gpointer);
|
|
263 g_assert(ptr);
|
|
264 }
|
|
265
|
|
266 switch(gaim_values[i]->type) {
|
|
267 case GAIM_TYPE_INT:
|
|
268 g_print("appending int\n");
|
|
269 xint = my_arg(gint);
|
|
270 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &xint);
|
|
271 break;
|
|
272 case GAIM_TYPE_UINT:
|
|
273 xuint = my_arg(guint);
|
|
274 g_print("appending uint\n");
|
|
275 dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &xuint);
|
|
276 break;
|
|
277 case GAIM_TYPE_BOOLEAN:
|
|
278 g_print("appending boolean\n");
|
|
279 xboolean = my_arg(gboolean);
|
|
280 dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &xboolean);
|
|
281 break;
|
|
282 case GAIM_TYPE_STRING:
|
|
283 g_print("appending string\n");
|
|
284 str = null_to_empty(my_arg(char*));
|
|
285 dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str);
|
|
286 break;
|
|
287 case GAIM_TYPE_SUBTYPE: /* registered pointers only! */
|
|
288 case GAIM_TYPE_POINTER:
|
|
289 case GAIM_TYPE_OBJECT:
|
|
290 case GAIM_TYPE_BOXED:
|
|
291 g_print("appending obj\n");
|
|
292 id = gaim_dbus_pointer_to_id(my_arg(gpointer));
|
|
293 dbus_message_iter_append_basic(iter, DBUS_TYPE_INT32, &id);
|
|
294 break;
|
|
295 default: /* no conversion implemented */
|
|
296 g_assert_not_reached();
|
|
297 }
|
|
298 }
|
|
299 }
|
|
300
|
|
301 #undef my_arg
|
|
302
|
|
303 void gaim_dbus_signal_emit_gaim(GaimObject *object, char *name, int num_values,
|
|
304 GaimValue **values, va_list vargs)
|
|
305 {
|
|
306 /* pass name */
|
|
307 DBusMessage *signal;
|
|
308 DBusMessageIter iter;
|
|
309 char *newname;
|
|
310
|
|
311 g_print("Emitting %s\n", name);
|
|
312 g_return_if_fail(object->proxy);
|
|
313
|
|
314 newname = gaim_dbus_convert_signal_name(name);
|
|
315 signal = dbus_message_new_signal(DBUS_PATH_GAIM, DBUS_INTERFACE_GAIM, newname);
|
|
316 dbus_message_iter_init_append(signal, &iter);
|
|
317
|
|
318 gaim_dbus_message_append_gaim_values(&iter, num_values, values, vargs);
|
|
319
|
|
320 dbus_g_proxy_send(object->proxy, signal, NULL);
|
|
321
|
|
322 g_free(newname);
|
|
323 dbus_message_unref(signal);
|
|
324 }
|
|
325
|
|
326
|
|
327 /**************************************************************/
|
|
328 /* DBus bindings ... */
|
|
329 /**************************************************************/
|
|
330
|
|
331
|
|
332
|
|
333 GArray* gaim_dbusify_GList(GList *list, gboolean free_memory) {
|
|
334 GArray *array;
|
|
335 GList *elem;
|
|
336
|
|
337 array = g_array_new (FALSE, TRUE, sizeof (guint32));
|
|
338 for(elem = list; elem != NULL; elem = elem->next) {
|
|
339 int objectid;
|
|
340
|
|
341 objectid = gaim_dbus_pointer_to_id(elem->data);
|
|
342 g_array_append_val(array, objectid);
|
|
343 }
|
|
344
|
|
345 if (free_memory)
|
|
346 g_list_free(list);
|
|
347
|
|
348 return array;
|
|
349 }
|
|
350
|
|
351 GArray* gaim_dbusify_GSList(GSList *list, gboolean free_memory) {
|
|
352 GArray *array;
|
|
353 GSList *elem;
|
|
354
|
|
355 array = g_array_new (FALSE, TRUE, sizeof (guint32));
|
|
356 for(elem = list; elem != NULL; elem = elem->next) {
|
|
357 int objectid;
|
|
358
|
|
359 objectid = gaim_dbus_pointer_to_id(elem->data);
|
|
360 g_array_append_val(array, objectid);
|
|
361 }
|
|
362
|
|
363 if (free_memory)
|
|
364 g_slist_free(list);
|
|
365 return array;
|
|
366 }
|
|
367
|
|
368 #include "dbus-generated-code.c"
|
|
369
|
|
370 #include "dbus-server-bindings.c"
|
|
371
|
|
372
|
|
373
|
11067
|
374
|
|
375
|
|
376 /**************************************************************************/
|
|
377 /** @name Gaim DBUS init function */
|
|
378 /**************************************************************************/
|
|
379
|
|
380
|
11146
|
381 /* fixme: why do we need two functions here instead of one? */
|
|
382
|
11080
|
383 gboolean gaim_dbus_connect(GaimObject *object)
|
11055
|
384 {
|
|
385 DBusGConnection *connection;
|
11067
|
386 GError *error = NULL;
|
11055
|
387 DBusGProxy *driver_proxy;
|
|
388 guint32 request_name_ret;
|
|
389
|
|
390 gaim_debug_misc("dbus", "launching dbus server\n");
|
11067
|
391
|
11146
|
392 dbus_g_object_type_install_info (GAIM_DBUS_TYPE_OBJECT,
|
|
393 &dbus_glib_gaim_object_object_info); /* Connect to the bus */
|
11055
|
394
|
|
395 error = NULL;
|
|
396 connection = dbus_g_bus_get(DBUS_BUS_STARTER, &error);
|
|
397
|
|
398 if (connection == NULL) {
|
|
399 g_assert(error);
|
11067
|
400 gaim_debug_error("dbus", "Failed to open connection to bus: %s\n",
|
11055
|
401 error->message);
|
|
402 g_error_free (error);
|
|
403 return FALSE;
|
|
404 }
|
|
405
|
|
406 /* Instantiate the gaim dbus object and register it */
|
|
407
|
|
408
|
11067
|
409
|
11080
|
410
|
11055
|
411 /* Obtain a proxy for the DBus object */
|
|
412
|
|
413 driver_proxy = dbus_g_proxy_new_for_name (connection,
|
|
414 DBUS_SERVICE_DBUS,
|
|
415 DBUS_PATH_DBUS,
|
|
416 DBUS_INTERFACE_DBUS);
|
|
417
|
11067
|
418 g_assert(driver_proxy);
|
11055
|
419
|
|
420 /* Test whether the registration was successful */
|
|
421
|
|
422 org_freedesktop_DBus_request_name(driver_proxy, DBUS_SERVICE_GAIM,
|
11067
|
423 DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &request_name_ret, &error);
|
11055
|
424
|
|
425 if (error) {
|
|
426 gaim_debug_error("dbus", "Failed to get name: %s\n", error->message);
|
|
427 g_error_free (error);
|
|
428 return FALSE;
|
|
429 }
|
|
430
|
|
431 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
|
|
432 {
|
11067
|
433 gaim_debug_error ("dbus", "Got result code %u from requesting name\n",
|
11055
|
434 request_name_ret);
|
|
435 return FALSE;
|
|
436 }
|
|
437
|
|
438 gaim_debug_misc ("dbus", "GLib test service has name '%s'\n",
|
|
439 DBUS_SERVICE_GAIM);
|
11146
|
440
|
|
441
|
|
442
|
|
443 dbus_g_connection_register_g_object (connection, DBUS_PATH_GAIM,
|
|
444 (GObject*) object);
|
|
445
|
|
446 object->proxy = dbus_g_proxy_new_for_name (connection,
|
|
447 DBUS_SERVICE_GAIM,
|
|
448 DBUS_PATH_GAIM,
|
|
449 DBUS_INTERFACE_GAIM);
|
|
450
|
11055
|
451 gaim_debug_misc ("dbus", "GLib test service entering main loop\n");
|
11067
|
452
|
11055
|
453 return TRUE;
|
|
454 }
|
11080
|
455
|
|
456
|
|
457
|
|
458
|
|
459 gboolean gaim_dbus_init(void)
|
|
460 {
|
11146
|
461 gaim_dbus_init_ids();
|
|
462 gaim_object_error_quark =
|
|
463 g_quark_from_static_string("org.gaim.GaimError");
|
|
464
|
|
465
|
11080
|
466
|
|
467 gaim_dbus_object = GAIM_DBUS_OBJECT(g_object_new (GAIM_DBUS_TYPE_OBJECT, NULL));
|
|
468
|
11146
|
469 gaim_dbus_object->proxy = NULL;
|
11080
|
470 return TRUE;
|
|
471 }
|
11146
|
472
|
|
473
|