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
|
|
24 #include <dbus/dbus-glib.h>
|
|
25 #include <dbus/dbus-glib-bindings.h>
|
|
26 #include <stdio.h>
|
|
27 #include <stdlib.h>
|
|
28 #include <string.h>
|
|
29 #include <glib/gi18n.h>
|
|
30 #include <glib-object.h>
|
|
31 #include <glib/gquark.h>
|
11080
|
32 #include <glib.h>
|
11055
|
33
|
|
34 #include "account.h"
|
11067
|
35 #include "blist.h"
|
|
36 #include "conversation.h"
|
11055
|
37 #include "dbus-gaim.h"
|
|
38 #include "dbus-server.h"
|
|
39 #include "debug.h"
|
|
40 #include "core.h"
|
11067
|
41 #include "value.h"
|
11055
|
42
|
11080
|
43 static gint gaim_dbus_pointer_to_id(gpointer node);
|
|
44 static gpointer gaim_dbus_id_to_pointer(gint id, GaimDBusPointerType type);
|
|
45
|
|
46
|
11067
|
47 /**************************************************************************/
|
11080
|
48 /** @name Lots of GObject stuff I don't really understand */
|
|
49 /**************************************************************************/
|
11055
|
50
|
|
51 GType gaim_object_get_type(void);
|
|
52
|
11080
|
53 struct _GaimObject {
|
11055
|
54 GObject parent;
|
11080
|
55
|
|
56 int ping_signal_id;
|
11055
|
57 };
|
|
58
|
11080
|
59 typedef struct {
|
11055
|
60 GObjectClass parent;
|
11080
|
61 } GaimObjectClass;
|
|
62
|
|
63
|
11055
|
64
|
11080
|
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
|
11080
|
77 static const char* null_to_empty(const char *s) {
|
|
78 if (s)
|
|
79 return s;
|
|
80 else
|
|
81 return "";
|
11055
|
82 }
|
|
83
|
11080
|
84
|
|
85
|
|
86 static void gaim_object_class_init(GaimObjectClass *klass)
|
11055
|
87 {
|
|
88 }
|
|
89
|
11080
|
90
|
|
91
|
|
92 /**************************************************************************/
|
|
93 /** @name Signals */
|
|
94 /**************************************************************************/
|
|
95
|
|
96 /* used in #gaim_values_to_gvalues, undefined afterwards */
|
|
97 #define my_arg(type) (ptr != NULL ? * ((type *)ptr) : va_arg(data, type))
|
|
98
|
|
99 /**
|
|
100 Converts from a list of data into an GValue array.
|
|
101
|
|
102 @param gvalus Array of empty gvalues to be filled.
|
|
103 @param number The number of data items.
|
|
104 @param gaim_value Array of #number pointers to GaimValues.
|
|
105 The types of of these GaimValues determine the type
|
|
106 of data items. The values do not matter.
|
|
107 @param mainptr A pointer to a single data item. If this pointer is not #NULL,
|
|
108 then #number must be 1.
|
|
109 @param data A va_list containing data items. If
|
|
110
|
|
111 Exactly one of #mainptr and #data must be not #NULL. If #mainptr
|
|
112 is not #NULL, then there is a single piece of data at the address
|
|
113 pointed at by #mainptr. If #data is not #NULL, then there are
|
|
114 #number data items in the #va_list #data.
|
|
115 */
|
|
116 static void gaim_values_to_gvalues(GValue *gvalue, int number,
|
|
117 GaimValue **gaim_values, gpointer mainptr, va_list data)
|
|
118 {
|
|
119 int i;
|
|
120 gpointer ptr;
|
|
121
|
|
122 g_assert(mainptr == NULL || data == NULL);
|
|
123 g_assert(mainptr != NULL || data != NULL);
|
|
124 g_assert(number == 1 || data != NULL);
|
|
125
|
|
126 for(i=0; i<number; i++, gvalue++) {
|
|
127 ptr = mainptr;
|
|
128 if (gaim_value_is_outgoing(gaim_values[i])) {
|
|
129 ptr = my_arg(gpointer);
|
|
130 g_assert(ptr);
|
|
131 }
|
|
132
|
|
133 switch(gaim_values[i]->type) {
|
|
134 case GAIM_TYPE_CHAR:
|
|
135 g_value_init(gvalue, G_TYPE_CHAR);
|
|
136 g_value_set_char(gvalue, (char) my_arg(int));
|
|
137 break;
|
|
138 case GAIM_TYPE_INT:
|
|
139 g_value_init(gvalue, G_TYPE_INT);
|
|
140 g_value_set_int(gvalue, my_arg(gint));
|
|
141 break;
|
|
142 case GAIM_TYPE_UINT:
|
|
143 g_value_init(gvalue, G_TYPE_UINT);
|
|
144 g_value_set_uint(gvalue, my_arg(guint));
|
|
145 break;
|
|
146 case GAIM_TYPE_BOOLEAN:
|
|
147 g_value_init(gvalue, G_TYPE_BOOLEAN);
|
|
148 g_value_set_boolean(gvalue, my_arg(gboolean));
|
|
149 break;
|
|
150 case GAIM_TYPE_STRING:
|
|
151 g_value_init(gvalue, G_TYPE_STRING);
|
|
152 g_value_set_string(gvalue, null_to_empty(my_arg(char*)));
|
|
153 break;
|
|
154 case GAIM_TYPE_SUBTYPE: /* registered pointers only! */
|
|
155 g_value_init(gvalue, G_TYPE_INT);
|
|
156 g_value_set_int(gvalue,
|
|
157 gaim_dbus_pointer_to_id(my_arg(gpointer)));
|
|
158 break;
|
|
159 case GAIM_TYPE_POINTER:
|
|
160 case GAIM_TYPE_OBJECT:
|
|
161 case GAIM_TYPE_BOXED:
|
|
162 my_arg(gpointer); /* cannot pass general pointers */
|
|
163 g_value_init(gvalue, G_TYPE_INT);
|
|
164 g_value_set_int(gvalue, 0);
|
|
165 break;
|
|
166
|
|
167 default: /* no conversion implemented */
|
|
168 g_assert_not_reached();
|
|
169 }
|
|
170 }
|
|
171
|
|
172 if (data)
|
|
173 va_end(data);
|
|
174 }
|
|
175
|
|
176 #undef my_arg /* my_arg was only used in gaim_values_to_gvalues */
|
|
177
|
|
178
|
|
179
|
|
180 /**
|
|
181 Converts from GaimTypes to GTypes.
|
11067
|
182
|
11080
|
183 @param type A GaimType to be converted.
|
|
184 @result The result of the conversion (GType).
|
|
185 */
|
|
186 static GType gaim_type_to_g_type(GaimType type)
|
|
187 {
|
|
188 switch(type) {
|
|
189 case GAIM_TYPE_CHAR:
|
|
190 return G_TYPE_CHAR;
|
|
191 case GAIM_TYPE_INT:
|
|
192 return G_TYPE_INT;
|
|
193 case GAIM_TYPE_UINT:
|
|
194 return G_TYPE_UINT;
|
|
195 case GAIM_TYPE_BOOLEAN:
|
|
196 return G_TYPE_BOOLEAN;
|
|
197 case GAIM_TYPE_STRING:
|
|
198 return G_TYPE_STRING;
|
|
199 case GAIM_TYPE_SUBTYPE: /* registered pointers only! */
|
|
200 return G_TYPE_INT;
|
|
201 case GAIM_TYPE_POINTER:
|
|
202 case GAIM_TYPE_BOXED:
|
|
203 case GAIM_TYPE_OBJECT:
|
|
204 return G_TYPE_INT; /* always 0 */
|
|
205 default: /* no conversion implemented */
|
|
206 g_assert_not_reached();
|
|
207 }
|
|
208 }
|
|
209
|
|
210
|
|
211 static const char *gaim_dbus_convert_signal_name(const char *gaim_name)
|
|
212 {
|
|
213 int gaim_index, g_index;
|
|
214 char *g_name = g_new(char, strlen(gaim_name)+1);
|
|
215 gboolean capitalize_next = TRUE;
|
|
216
|
|
217 for(gaim_index = g_index = 0; gaim_name[gaim_index]; gaim_index++)
|
|
218 if (gaim_name[gaim_index] != '-' && gaim_name[gaim_index] != '_') {
|
|
219 if (capitalize_next)
|
|
220 g_name[g_index++] = g_ascii_toupper(gaim_name[gaim_index]);
|
|
221 else
|
|
222 g_name[g_index++] = gaim_name[gaim_index];
|
|
223 capitalize_next = FALSE;
|
|
224 } else
|
|
225 capitalize_next = TRUE;
|
|
226 g_name[g_index] = 0;
|
|
227
|
|
228 return g_name;
|
|
229 }
|
|
230
|
|
231 /* Public signal-related functions */
|
|
232
|
|
233
|
|
234 void gaim_dbus_invalid_marshaller(GClosure *closure,
|
|
235 GValue *return_value,
|
|
236 guint n_param_values,
|
|
237 const GValue *param_values,
|
|
238 gpointer invocation_hint,
|
|
239 gpointer marshal_data)
|
|
240 {
|
|
241 g_assert_not_reached();
|
|
242 }
|
|
243
|
|
244 int gaim_dbus_signal_register(GaimObject *object, const char *name,
|
|
245 GSignalCMarshaller marshaller,
|
|
246 int num_values, ...)
|
|
247 {
|
|
248 va_list args;
|
|
249
|
|
250 va_start(args, num_values);
|
|
251
|
|
252 return g_signal_new_valist(name, G_OBJECT_TYPE(object),
|
|
253 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
|
|
254 NULL, NULL, NULL, marshaller,
|
|
255 G_TYPE_NONE, num_values, args);
|
|
256 }
|
|
257
|
|
258 void gaim_dbus_signal_emit(GaimObject *object, int dbus_id, ...) {
|
|
259 va_list args;
|
|
260
|
|
261 va_start(args, dbus_id);
|
|
262
|
|
263 gaim_dbus_signal_emit_valist(object, dbus_id, args);
|
|
264 }
|
|
265
|
|
266 void gaim_dbus_signal_emit_valist(GaimObject *object, int dbus_id, va_list args) {
|
|
267 g_signal_emit_valist(object, dbus_id, 0, args);
|
|
268 }
|
|
269
|
|
270 int gaim_dbus_signal_register_gaim(GaimObject *object, const char *name,
|
|
271 GSignalCMarshaller marshaller,
|
|
272 int num_values, GaimValue **values)
|
|
273 {
|
|
274 int i;
|
|
275 int dbus_id;
|
|
276 GType *types;
|
|
277
|
|
278 types = g_new0(GType, num_values);
|
|
279
|
|
280 for(i=0; i<num_values; i++)
|
|
281 types[i] = gaim_type_to_g_type(values[i]->type);
|
|
282
|
|
283 dbus_id =
|
|
284 g_signal_newv(gaim_dbus_convert_signal_name(name),
|
|
285 G_OBJECT_TYPE(object),
|
|
286 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
|
|
287 NULL, NULL, NULL, marshaller,
|
|
288 G_TYPE_NONE, num_values, types);
|
|
289
|
|
290 g_free(types);
|
|
291
|
|
292 return dbus_id;
|
|
293 }
|
|
294
|
|
295
|
|
296 void gaim_dbus_signal_emit_gaim(GaimObject *object, int dbus_id, int num_values,
|
|
297 GaimValue **values, va_list vargs)
|
|
298 {
|
|
299 GValue *args;
|
|
300 int i;
|
|
301
|
|
302 g_return_if_fail(dbus_id);
|
|
303
|
|
304 args = g_new0(GValue, num_values + 1);
|
|
305
|
|
306 g_value_init(args + 0, G_OBJECT_TYPE(object));
|
|
307 g_value_set_object(args + 0, object);
|
|
308
|
|
309 gaim_values_to_gvalues(args + 1, num_values, values, NULL, vargs);
|
|
310
|
|
311 g_signal_emitv(args, dbus_id, 0, NULL);
|
|
312
|
|
313 for(i = 1; i <= num_values; i++)
|
|
314 g_value_unset(args + i);
|
|
315
|
|
316 g_free(args);
|
|
317 }
|
11055
|
318
|
|
319
|
11067
|
320 /**************************************************************************/
|
|
321 /** @name Utility functions */
|
|
322 /**************************************************************************/
|
|
323
|
|
324 #define error_unless_1(condition, str, parameter) \
|
|
325 if (!(condition)) { \
|
|
326 g_set_error(error, gaim_object_error_quark, \
|
|
327 DBUS_ERROR_NOT_FOUND, \
|
|
328 str, parameter); \
|
|
329 return FALSE; \
|
|
330 }
|
|
331
|
|
332 #define error_unless_2(condition, str, a,b) \
|
|
333 if (!(condition)) { \
|
|
334 g_set_error(error, gaim_object_error_quark, \
|
|
335 DBUS_ERROR_NOT_FOUND, \
|
|
336 str, a,b); \
|
|
337 return FALSE; \
|
|
338 }
|
|
339
|
|
340 typedef gboolean (*GaimNodeFilter)(GaimBlistNode *node, gpointer *user_data);
|
|
341
|
|
342 static gboolean
|
|
343 filter_is_buddy(GaimBlistNode *node, gpointer *user_data)
|
|
344 {
|
|
345 return GAIM_BLIST_NODE_IS_BUDDY(node);
|
|
346 }
|
11055
|
347
|
11067
|
348 static gboolean
|
|
349 filter_is_online_buddy(GaimBlistNode *node,
|
|
350 gpointer *user_data)
|
|
351 {
|
|
352 return GAIM_BLIST_NODE_IS_BUDDY(node) &&
|
|
353 GAIM_BUDDY_IS_ONLINE((GaimBuddy *)node);
|
|
354 }
|
|
355
|
|
356
|
|
357 static GList*
|
|
358 get_buddy_list (GList *created_list, /**< can be NULL */
|
|
359 GaimBlistNode *gaim_buddy_list, /**< can be NULL */
|
|
360 GaimNodeFilter filter,
|
|
361 gpointer user_data)
|
|
362 {
|
|
363 GaimBlistNode *node;
|
|
364
|
|
365 for(node = gaim_buddy_list; node; node = node->next)
|
|
366 {
|
|
367 if ((*filter)(node, user_data))
|
|
368 created_list = g_list_prepend(created_list, node);
|
|
369
|
|
370 created_list = get_buddy_list(created_list, node->child,
|
|
371 filter, user_data);
|
|
372 }
|
|
373
|
|
374 return created_list;
|
|
375 }
|
|
376
|
|
377
|
|
378
|
|
379 /**************************************************************************/
|
|
380 /** @name Implementations of remote DBUS calls */
|
|
381 /**************************************************************************/
|
11055
|
382
|
|
383 static gboolean
|
11080
|
384 gaim_object_ping(GaimObject *object, GError **error)
|
11055
|
385 {
|
11080
|
386 gaim_dbus_signal_emit(object, object->ping_signal_id, "Ping Pong!");
|
11055
|
387 return TRUE;
|
|
388 }
|
|
389
|
|
390 static gboolean
|
|
391 gaim_object_quit(GaimObject *obj, GError **error)
|
|
392 {
|
|
393 g_timeout_add(0, gaim_core_quit_cb, NULL);
|
|
394 return TRUE;
|
|
395 }
|
|
396
|
|
397 static gboolean
|
|
398 gaim_object_connect_all(GaimObject *obj, GError **error)
|
|
399 {
|
|
400 GList *cur;
|
|
401
|
|
402 for (cur = gaim_accounts_get_all(); cur != NULL; cur = cur->next)
|
|
403 gaim_account_connect((GaimAccount*) cur->data);
|
|
404
|
|
405 return TRUE;
|
|
406 }
|
|
407
|
11067
|
408
|
|
409
|
|
410 static gboolean
|
|
411 gaim_object_get_buddy_list (GaimObject *obj, GArray **out_buddy_ids,
|
|
412 GError **error)
|
|
413 {
|
|
414 GList *node;
|
|
415 GList *buddy_list = get_buddy_list(NULL, gaim_get_blist()->root,
|
|
416 &filter_is_buddy, NULL);
|
|
417 GArray *buddy_ids = g_array_new(FALSE, TRUE, sizeof(gint32));;
|
|
418
|
|
419 buddy_list = g_list_reverse(buddy_list);
|
|
420
|
|
421 for (node = buddy_list; node; node = node->next) {
|
|
422 gint32 id = gaim_dbus_pointer_to_id(node->data);
|
|
423 g_array_append_val(buddy_ids, id);
|
|
424 }
|
|
425
|
|
426 g_list_free(buddy_list);
|
|
427 *out_buddy_ids = buddy_ids;
|
|
428
|
|
429 return TRUE;
|
|
430 }
|
|
431
|
|
432
|
|
433
|
|
434
|
|
435
|
|
436 static gboolean
|
11080
|
437 gaim_object_find_account(GaimObject *object,
|
11067
|
438 const char *account_name, const char *protocol_name,
|
|
439 gint *account_id, GError **error)
|
|
440 {
|
|
441 GaimAccount *account = gaim_accounts_find(account_name, protocol_name);
|
|
442
|
|
443 error_unless_2(account, "Account '%s' with protocol '%s' not found",
|
|
444 account_name, protocol_name);
|
|
445
|
|
446 *account_id = gaim_dbus_pointer_to_id(account);
|
|
447 return TRUE;
|
|
448 }
|
|
449
|
|
450 static gboolean
|
11080
|
451 gaim_object_find_buddy(GaimObject *object, gint account_id, const char *buddy_name,
|
11067
|
452 gint *buddy_id, GError **error)
|
|
453 {
|
|
454 GaimAccount *account;
|
|
455 GaimBuddy *buddy;
|
|
456
|
|
457 account = gaim_dbus_id_to_pointer(account_id, DBUS_POINTER_ACCOUNT);
|
|
458 error_unless_1(account, "Invalid account id: %i", account_id);
|
|
459
|
|
460 buddy = gaim_find_buddy(account, buddy_name);
|
|
461 error_unless_1(account, "Buddy '%s' not found.", buddy_name);
|
|
462
|
|
463 *buddy_id = gaim_dbus_pointer_to_id(buddy);
|
|
464 return TRUE;
|
|
465 }
|
|
466
|
|
467 static gboolean
|
11080
|
468 gaim_object_start_im_conversation (GaimObject *object, gint buddy_id, GError **error)
|
11067
|
469 {
|
|
470 GaimBuddy *buddy = (GaimBuddy*) gaim_dbus_id_to_pointer(buddy_id,
|
|
471 DBUS_POINTER_BUDDY);
|
|
472
|
|
473 error_unless_1(buddy, "Invalid buddy id: %i", buddy_id);
|
|
474
|
|
475 gaim_conversation_new(GAIM_CONV_IM, buddy->account, buddy->name);
|
|
476
|
|
477 return TRUE;
|
|
478 }
|
|
479
|
|
480
|
|
481
|
|
482 /**************************************************************************/
|
|
483 /** @name Gaim DBUS property handling functions */
|
|
484 /**************************************************************************/
|
|
485
|
|
486
|
|
487 typedef struct _GaimDBusProperty {
|
|
488 char *name;
|
|
489 gint offset;
|
|
490 GaimType type;
|
|
491 } GaimDBusProperty;
|
|
492
|
|
493 /* change GAIM_TYPE into G_TYPE */
|
|
494
|
|
495 static gboolean
|
|
496 gaim_dbus_get_property(GaimDBusProperty *list, int list_len,
|
|
497 gpointer data, const char *name,
|
|
498 GValue *value, GError **error)
|
|
499 {
|
|
500 int i;
|
|
501
|
|
502 for(i=0; i<list_len; i++) {
|
|
503 if (!strcmp(list[i].name, name)) {
|
11080
|
504 gpointer ptr;
|
|
505 GaimValue gaim_value, *gaim_value_ptr;
|
11067
|
506
|
11080
|
507 ptr = G_STRUCT_MEMBER_P(data, list[i].offset);
|
|
508 gaim_value.type = list[i].type;
|
|
509 gaim_value.flags = 0;
|
|
510 gaim_value_ptr = &gaim_value;
|
|
511
|
|
512 gaim_values_to_gvalues(value, 1, &gaim_value_ptr,
|
|
513 ptr, NULL);
|
11067
|
514 return TRUE;
|
|
515 }
|
|
516 }
|
|
517
|
|
518 g_value_init(value, G_TYPE_INT);
|
|
519 g_value_set_int(value, 0);
|
|
520 error_unless_1(FALSE, "Invalid property '%s'", name);
|
|
521 }
|
|
522
|
|
523
|
|
524 #define DECLARE_PROPERTY(maintype, name, type) {#name, G_STRUCT_OFFSET(maintype, name), type}
|
|
525
|
|
526 GaimDBusProperty buddy_properties [] = {
|
|
527 DECLARE_PROPERTY(GaimBuddy, name, GAIM_TYPE_STRING),
|
|
528 DECLARE_PROPERTY(GaimBuddy, alias, GAIM_TYPE_STRING),
|
|
529 DECLARE_PROPERTY(GaimBuddy, server_alias, GAIM_TYPE_STRING),
|
11080
|
530 DECLARE_PROPERTY(GaimBuddy, account, GAIM_TYPE_SUBTYPE)
|
11067
|
531 };
|
|
532
|
|
533 GaimDBusProperty account_properties [] = {
|
|
534 DECLARE_PROPERTY(GaimAccount, username, GAIM_TYPE_STRING),
|
|
535 DECLARE_PROPERTY(GaimAccount, alias, GAIM_TYPE_STRING),
|
|
536 DECLARE_PROPERTY(GaimAccount, user_info, GAIM_TYPE_STRING),
|
|
537 DECLARE_PROPERTY(GaimAccount, protocol_id, GAIM_TYPE_STRING),
|
|
538 };
|
|
539
|
|
540 GaimDBusProperty contact_properties [] = {
|
|
541 DECLARE_PROPERTY(GaimContact, alias, GAIM_TYPE_STRING),
|
|
542 DECLARE_PROPERTY(GaimContact, totalsize, GAIM_TYPE_INT),
|
|
543 DECLARE_PROPERTY(GaimContact, currentsize, GAIM_TYPE_INT),
|
|
544 DECLARE_PROPERTY(GaimContact, online, GAIM_TYPE_INT),
|
11080
|
545 DECLARE_PROPERTY(GaimContact, priority, GAIM_TYPE_SUBTYPE),
|
11067
|
546 DECLARE_PROPERTY(GaimContact, priority_valid, GAIM_TYPE_BOOLEAN),
|
|
547 };
|
|
548
|
|
549 GaimDBusProperty group_properties [] = {
|
|
550 DECLARE_PROPERTY(GaimGroup, name, GAIM_TYPE_STRING),
|
|
551 DECLARE_PROPERTY(GaimGroup, totalsize, GAIM_TYPE_INT),
|
|
552 DECLARE_PROPERTY(GaimGroup, currentsize, GAIM_TYPE_INT),
|
|
553 DECLARE_PROPERTY(GaimGroup, online, GAIM_TYPE_INT),
|
|
554 };
|
|
555
|
|
556 GaimDBusProperty chat_properties [] = {
|
|
557 DECLARE_PROPERTY(GaimChat, alias, GAIM_TYPE_STRING),
|
11080
|
558 DECLARE_PROPERTY(GaimChat, account, GAIM_TYPE_SUBTYPE),
|
11067
|
559 };
|
|
560
|
|
561
|
|
562 #define DECLARE_PROPERTY_HANDLER(type, gaim_type) \
|
|
563 static gboolean \
|
11080
|
564 gaim_object_get_##type##_property (GaimObject *object, \
|
11067
|
565 gint id, const char *property_name, \
|
|
566 GValue *value, GError **error) \
|
|
567 { \
|
11080
|
568 gpointer ptr = gaim_dbus_id_to_pointer(id, gaim_type); \
|
11067
|
569 \
|
11080
|
570 error_unless_1(ptr, "Invalid " #type " id: %i", id); \
|
11067
|
571 \
|
|
572 return gaim_dbus_get_property(type##_properties, \
|
|
573 G_N_ELEMENTS(type##_properties), \
|
11080
|
574 ptr, property_name, value, error); \
|
11067
|
575 }
|
|
576
|
|
577 DECLARE_PROPERTY_HANDLER(buddy, DBUS_POINTER_BUDDY)
|
|
578 DECLARE_PROPERTY_HANDLER(account, DBUS_POINTER_ACCOUNT)
|
11068
|
579 DECLARE_PROPERTY_HANDLER(contact, DBUS_POINTER_CONTACT)
|
|
580 DECLARE_PROPERTY_HANDLER(group, DBUS_POINTER_GROUP)
|
|
581 DECLARE_PROPERTY_HANDLER(chat, DBUS_POINTER_CHAT)
|
11067
|
582
|
11055
|
583 #include "dbus-server-bindings.c"
|
|
584
|
|
585
|
11067
|
586
|
|
587 /**************************************************************************/
|
|
588 /** @name Gaim DBUS pointer registration mechanism */
|
|
589 /**************************************************************************/
|
|
590
|
|
591 static GHashTable *map_id_node;
|
|
592 static GHashTable *map_id_type;
|
|
593 static GHashTable *map_node_id;
|
|
594
|
|
595 void gaim_dbus_init_ids(void) {
|
11080
|
596
|
|
597
|
11067
|
598 map_id_node = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
599 map_id_type = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
600 map_node_id = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
601 }
|
|
602
|
|
603 void gaim_dbus_register_pointer(gpointer node, GaimDBusPointerType type)
|
|
604 {
|
|
605 static gint last_id = 0;
|
11080
|
606
|
|
607 g_assert(map_node_id);
|
11067
|
608 g_assert(g_hash_table_lookup(map_node_id, node) == NULL);
|
|
609
|
|
610
|
|
611 last_id++;
|
|
612 g_hash_table_insert(map_node_id, node, GINT_TO_POINTER(last_id));
|
|
613 g_hash_table_insert(map_id_node, GINT_TO_POINTER(last_id), node);
|
|
614 g_hash_table_insert(map_id_type, GINT_TO_POINTER(last_id),
|
|
615 GINT_TO_POINTER(type));
|
|
616 }
|
|
617
|
|
618
|
|
619 void gaim_dbus_unregister_pointer(gpointer node) {
|
|
620 gpointer id = g_hash_table_lookup(map_node_id, node);
|
|
621
|
|
622 g_hash_table_remove(map_node_id, node);
|
|
623 g_hash_table_remove(map_id_node, GINT_TO_POINTER(id));
|
|
624 g_hash_table_remove(map_id_node, GINT_TO_POINTER(id));
|
|
625 }
|
|
626
|
11080
|
627 static gint gaim_dbus_pointer_to_id(gpointer node) {
|
|
628 g_assert(map_node_id);
|
|
629
|
11067
|
630 gint id = GPOINTER_TO_INT(g_hash_table_lookup(map_node_id, node));
|
11080
|
631 g_return_val_if_fail(id, 0);
|
11067
|
632 return id;
|
|
633 }
|
|
634
|
11080
|
635 static gpointer gaim_dbus_id_to_pointer(gint id, GaimDBusPointerType type) {
|
11067
|
636 if (type != GPOINTER_TO_INT(g_hash_table_lookup(map_id_type,
|
|
637 GINT_TO_POINTER(id))))
|
|
638 return NULL;
|
|
639 return g_hash_table_lookup(map_id_node, GINT_TO_POINTER(id));
|
|
640 }
|
|
641
|
|
642
|
|
643
|
|
644 /**************************************************************************/
|
|
645 /** @name Gaim DBUS init function */
|
|
646 /**************************************************************************/
|
|
647
|
|
648
|
11080
|
649 gboolean gaim_dbus_connect(GaimObject *object)
|
11055
|
650 {
|
|
651 DBusGConnection *connection;
|
11067
|
652 GError *error = NULL;
|
11055
|
653 DBusGProxy *driver_proxy;
|
|
654 guint32 request_name_ret;
|
|
655
|
11067
|
656
|
11055
|
657 gaim_debug_misc("dbus", "launching dbus server\n");
|
11067
|
658
|
11055
|
659 /* Connect to the bus */
|
|
660
|
|
661 error = NULL;
|
|
662 connection = dbus_g_bus_get(DBUS_BUS_STARTER, &error);
|
|
663
|
|
664 if (connection == NULL) {
|
|
665 g_assert(error);
|
11067
|
666 gaim_debug_error("dbus", "Failed to open connection to bus: %s\n",
|
11055
|
667 error->message);
|
|
668 g_error_free (error);
|
|
669 return FALSE;
|
|
670 }
|
|
671
|
|
672 /* Instantiate the gaim dbus object and register it */
|
|
673
|
|
674
|
11067
|
675
|
11080
|
676
|
|
677 dbus_g_object_type_install_info (GAIM_DBUS_TYPE_OBJECT,
|
11055
|
678 &dbus_glib_gaim_object_object_info);
|
|
679
|
11067
|
680 dbus_g_connection_register_g_object (connection, DBUS_PATH_GAIM,
|
11080
|
681 (GObject*) object);
|
11055
|
682
|
|
683
|
|
684 /* Obtain a proxy for the DBus object */
|
|
685
|
|
686 driver_proxy = dbus_g_proxy_new_for_name (connection,
|
|
687 DBUS_SERVICE_DBUS,
|
|
688 DBUS_PATH_DBUS,
|
|
689 DBUS_INTERFACE_DBUS);
|
|
690
|
11067
|
691 g_assert(driver_proxy);
|
11055
|
692
|
|
693 /* Test whether the registration was successful */
|
|
694
|
|
695 org_freedesktop_DBus_request_name(driver_proxy, DBUS_SERVICE_GAIM,
|
11067
|
696 DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &request_name_ret, &error);
|
11055
|
697
|
|
698 if (error) {
|
|
699 gaim_debug_error("dbus", "Failed to get name: %s\n", error->message);
|
|
700 g_error_free (error);
|
|
701 return FALSE;
|
|
702 }
|
|
703
|
|
704 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
|
|
705 {
|
11067
|
706 gaim_debug_error ("dbus", "Got result code %u from requesting name\n",
|
11055
|
707 request_name_ret);
|
|
708 return FALSE;
|
|
709 }
|
|
710
|
|
711 gaim_debug_misc ("dbus", "GLib test service has name '%s'\n",
|
|
712 DBUS_SERVICE_GAIM);
|
|
713 gaim_debug_misc ("dbus", "GLib test service entering main loop\n");
|
11067
|
714
|
11055
|
715 return TRUE;
|
|
716 }
|
11080
|
717
|
|
718
|
|
719 static void gaim_object_init(GaimObject *object)
|
|
720 {
|
|
721
|
|
722 object->ping_signal_id =
|
|
723 gaim_dbus_signal_register(object, "PingSignal",
|
|
724 g_cclosure_marshal_VOID__STRING,
|
|
725 1, G_TYPE_STRING);
|
|
726 }
|
|
727
|
|
728
|
|
729 gboolean gaim_dbus_init(void)
|
|
730 {
|
|
731 gaim_dbus_init_ids();
|
|
732 gaim_object_error_quark =
|
|
733 g_quark_from_static_string("org.gaim.GaimError");
|
|
734
|
|
735 gaim_dbus_object = GAIM_DBUS_OBJECT(g_object_new (GAIM_DBUS_TYPE_OBJECT, NULL));
|
|
736
|
|
737 return TRUE;
|
|
738 }
|