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>
|
|
32
|
|
33 #include "account.h"
|
11067
|
34 #include "blist.h"
|
|
35 #include "conversation.h"
|
11055
|
36 #include "dbus-gaim.h"
|
|
37 #include "dbus-server.h"
|
|
38 #include "debug.h"
|
|
39 #include "core.h"
|
11067
|
40 #include "value.h"
|
11055
|
41
|
11067
|
42 /**************************************************************************/
|
|
43 /** @name Lots of GObject crap I don't understand */
|
|
44 /**************************************************************************/
|
11055
|
45
|
|
46 typedef struct GaimObject GaimObject;
|
|
47 typedef struct GaimObjectClass GaimObjectClass;
|
|
48
|
|
49 GType gaim_object_get_type(void);
|
|
50
|
|
51 struct GaimObject {
|
|
52 GObject parent;
|
|
53 };
|
|
54
|
|
55 struct GaimObjectClass {
|
|
56 GObjectClass parent;
|
|
57 };
|
|
58
|
|
59 #define GAIM_TYPE_OBJECT (gaim_object_get_type ())
|
|
60 #define GAIM_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), GAIM_TYPE_OBJECT, GaimObject))
|
|
61 #define GAIM_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GAIM_TYPE_OBJECT, GaimObjectClass))
|
|
62 #define GAIM_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), GAIM_TYPE_OBJECT))
|
|
63 #define GAIM_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GAIM_TYPE_OBJECT))
|
|
64 #define GAIM_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GAIM_TYPE_OBJECT, GaimObjectClass))
|
|
65
|
|
66 G_DEFINE_TYPE(GaimObject, gaim_object, G_TYPE_OBJECT)
|
|
67
|
|
68
|
|
69 static void
|
|
70 gaim_object_init(GaimObject *obj)
|
|
71 {
|
|
72 }
|
|
73
|
|
74 static void
|
|
75 gaim_object_class_init(GaimObjectClass *mobject_class)
|
|
76 {
|
|
77 }
|
|
78
|
|
79 static GObject *gaim_object;
|
11067
|
80 static GQuark gaim_object_error_quark;
|
|
81
|
11055
|
82
|
|
83
|
11067
|
84 /**************************************************************************/
|
|
85 /** @name Utility functions */
|
|
86 /**************************************************************************/
|
|
87
|
|
88 #define error_unless_1(condition, str, parameter) \
|
|
89 if (!(condition)) { \
|
|
90 g_set_error(error, gaim_object_error_quark, \
|
|
91 DBUS_ERROR_NOT_FOUND, \
|
|
92 str, parameter); \
|
|
93 return FALSE; \
|
|
94 }
|
|
95
|
|
96 #define error_unless_2(condition, str, a,b) \
|
|
97 if (!(condition)) { \
|
|
98 g_set_error(error, gaim_object_error_quark, \
|
|
99 DBUS_ERROR_NOT_FOUND, \
|
|
100 str, a,b); \
|
|
101 return FALSE; \
|
|
102 }
|
|
103
|
|
104 static const char* null_to_empty(const char *s) {
|
|
105 if (s)
|
|
106 return s;
|
|
107 else
|
|
108 return "";
|
|
109 }
|
|
110
|
|
111 typedef gboolean (*GaimNodeFilter)(GaimBlistNode *node, gpointer *user_data);
|
|
112
|
|
113 static gboolean
|
|
114 filter_is_buddy(GaimBlistNode *node, gpointer *user_data)
|
|
115 {
|
|
116 return GAIM_BLIST_NODE_IS_BUDDY(node);
|
|
117 }
|
11055
|
118
|
11067
|
119 static gboolean
|
|
120 filter_is_online_buddy(GaimBlistNode *node,
|
|
121 gpointer *user_data)
|
|
122 {
|
|
123 return GAIM_BLIST_NODE_IS_BUDDY(node) &&
|
|
124 GAIM_BUDDY_IS_ONLINE((GaimBuddy *)node);
|
|
125 }
|
|
126
|
|
127
|
|
128 static GList*
|
|
129 get_buddy_list (GList *created_list, /**< can be NULL */
|
|
130 GaimBlistNode *gaim_buddy_list, /**< can be NULL */
|
|
131 GaimNodeFilter filter,
|
|
132 gpointer user_data)
|
|
133 {
|
|
134 GaimBlistNode *node;
|
|
135
|
|
136 for(node = gaim_buddy_list; node; node = node->next)
|
|
137 {
|
|
138 if ((*filter)(node, user_data))
|
|
139 created_list = g_list_prepend(created_list, node);
|
|
140
|
|
141 created_list = get_buddy_list(created_list, node->child,
|
|
142 filter, user_data);
|
|
143 }
|
|
144
|
|
145 return created_list;
|
|
146 }
|
|
147
|
|
148
|
|
149
|
|
150 /**************************************************************************/
|
|
151 /** @name Implementations of remote DBUS calls */
|
|
152 /**************************************************************************/
|
11055
|
153
|
|
154 static gboolean
|
|
155 gaim_object_ping(GaimObject *obj, GError **error)
|
|
156 {
|
|
157 return TRUE;
|
|
158 }
|
|
159
|
|
160 static gboolean
|
|
161 gaim_object_quit(GaimObject *obj, GError **error)
|
|
162 {
|
|
163 g_timeout_add(0, gaim_core_quit_cb, NULL);
|
|
164 return TRUE;
|
|
165 }
|
|
166
|
|
167 static gboolean
|
|
168 gaim_object_connect_all(GaimObject *obj, GError **error)
|
|
169 {
|
|
170 GList *cur;
|
|
171
|
|
172 for (cur = gaim_accounts_get_all(); cur != NULL; cur = cur->next)
|
|
173 gaim_account_connect((GaimAccount*) cur->data);
|
|
174
|
|
175 return TRUE;
|
|
176 }
|
|
177
|
11067
|
178
|
|
179
|
|
180 static gboolean
|
|
181 gaim_object_get_buddy_list (GaimObject *obj, GArray **out_buddy_ids,
|
|
182 GError **error)
|
|
183 {
|
|
184 GList *node;
|
|
185 GList *buddy_list = get_buddy_list(NULL, gaim_get_blist()->root,
|
|
186 &filter_is_buddy, NULL);
|
|
187 GArray *buddy_ids = g_array_new(FALSE, TRUE, sizeof(gint32));;
|
|
188
|
|
189 buddy_list = g_list_reverse(buddy_list);
|
|
190
|
|
191 for (node = buddy_list; node; node = node->next) {
|
|
192 gint32 id = gaim_dbus_pointer_to_id(node->data);
|
|
193 g_array_append_val(buddy_ids, id);
|
|
194 }
|
|
195
|
|
196 g_list_free(buddy_list);
|
|
197 *out_buddy_ids = buddy_ids;
|
|
198
|
|
199 return TRUE;
|
|
200 }
|
|
201
|
|
202
|
|
203
|
|
204
|
|
205
|
|
206 static gboolean
|
|
207 gaim_object_find_account(GaimObject *unused,
|
|
208 const char *account_name, const char *protocol_name,
|
|
209 gint *account_id, GError **error)
|
|
210 {
|
|
211 GaimAccount *account = gaim_accounts_find(account_name, protocol_name);
|
|
212
|
|
213 error_unless_2(account, "Account '%s' with protocol '%s' not found",
|
|
214 account_name, protocol_name);
|
|
215
|
|
216 *account_id = gaim_dbus_pointer_to_id(account);
|
|
217 return TRUE;
|
|
218 }
|
|
219
|
|
220 static gboolean
|
|
221 gaim_object_find_buddy(GaimObject *unused, gint account_id, const char *buddy_name,
|
|
222 gint *buddy_id, GError **error)
|
|
223 {
|
|
224 GaimAccount *account;
|
|
225 GaimBuddy *buddy;
|
|
226
|
|
227 account = gaim_dbus_id_to_pointer(account_id, DBUS_POINTER_ACCOUNT);
|
|
228 error_unless_1(account, "Invalid account id: %i", account_id);
|
|
229
|
|
230 buddy = gaim_find_buddy(account, buddy_name);
|
|
231 error_unless_1(account, "Buddy '%s' not found.", buddy_name);
|
|
232
|
|
233 *buddy_id = gaim_dbus_pointer_to_id(buddy);
|
|
234 return TRUE;
|
|
235 }
|
|
236
|
|
237 static gboolean
|
|
238 gaim_object_start_im_conversation (GaimObject *obj, gint buddy_id, GError **error)
|
|
239 {
|
|
240 GaimBuddy *buddy = (GaimBuddy*) gaim_dbus_id_to_pointer(buddy_id,
|
|
241 DBUS_POINTER_BUDDY);
|
|
242
|
|
243 error_unless_1(buddy, "Invalid buddy id: %i", buddy_id);
|
|
244
|
|
245 gaim_conversation_new(GAIM_CONV_IM, buddy->account, buddy->name);
|
|
246
|
|
247 return TRUE;
|
|
248 }
|
|
249
|
|
250
|
|
251
|
|
252 /**************************************************************************/
|
|
253 /** @name Gaim DBUS property handling functions */
|
|
254 /**************************************************************************/
|
|
255
|
|
256
|
|
257 typedef struct _GaimDBusProperty {
|
|
258 char *name;
|
|
259 gint offset;
|
|
260 GaimType type;
|
|
261 } GaimDBusProperty;
|
|
262
|
|
263 /* change GAIM_TYPE into G_TYPE */
|
|
264
|
|
265 static gboolean
|
|
266 gaim_dbus_get_property(GaimDBusProperty *list, int list_len,
|
|
267 gpointer data, const char *name,
|
|
268 GValue *value, GError **error)
|
|
269 {
|
|
270 int i;
|
|
271
|
|
272 for(i=0; i<list_len; i++) {
|
|
273 if (!strcmp(list[i].name, name)) {
|
|
274 gpointer ptr = G_STRUCT_MEMBER_P(data, list[i].offset);
|
|
275 switch(list[i].type) {
|
|
276 case GAIM_TYPE_STRING:
|
|
277 g_value_init(value, G_TYPE_STRING);
|
|
278 g_value_set_string (value, g_strdup(null_to_empty(*(char **)ptr)));
|
|
279 break;
|
|
280
|
|
281 case GAIM_TYPE_INT:
|
|
282 g_value_init(value, G_TYPE_INT);
|
|
283 g_value_set_int (value, *(int*) ptr);
|
|
284 break;
|
|
285
|
|
286 case GAIM_TYPE_BOOLEAN:
|
|
287 g_value_init(value, G_TYPE_BOOLEAN);
|
|
288 g_value_set_int (value, *(gboolean*) ptr);
|
|
289 break;
|
|
290
|
|
291 case GAIM_TYPE_POINTER: /* registered pointers only! */
|
|
292 g_value_init(value, G_TYPE_INT);
|
|
293 g_value_set_int (value,
|
|
294 gaim_dbus_pointer_to_id (*(gpointer *)ptr));
|
|
295 break;
|
|
296 default:
|
|
297 g_assert_not_reached();
|
|
298 }
|
|
299 return TRUE;
|
|
300 }
|
|
301 }
|
|
302
|
|
303 g_value_init(value, G_TYPE_INT);
|
|
304 g_value_set_int(value, 0);
|
|
305 error_unless_1(FALSE, "Invalid property '%s'", name);
|
|
306 }
|
|
307
|
|
308
|
|
309 #define DECLARE_PROPERTY(maintype, name, type) {#name, G_STRUCT_OFFSET(maintype, name), type}
|
|
310
|
|
311 GaimDBusProperty buddy_properties [] = {
|
|
312 DECLARE_PROPERTY(GaimBuddy, name, GAIM_TYPE_STRING),
|
|
313 DECLARE_PROPERTY(GaimBuddy, alias, GAIM_TYPE_STRING),
|
|
314 DECLARE_PROPERTY(GaimBuddy, server_alias, GAIM_TYPE_STRING),
|
|
315 DECLARE_PROPERTY(GaimBuddy, account, GAIM_TYPE_POINTER)
|
|
316 };
|
|
317
|
|
318 GaimDBusProperty account_properties [] = {
|
|
319 DECLARE_PROPERTY(GaimAccount, username, GAIM_TYPE_STRING),
|
|
320 DECLARE_PROPERTY(GaimAccount, alias, GAIM_TYPE_STRING),
|
|
321 DECLARE_PROPERTY(GaimAccount, user_info, GAIM_TYPE_STRING),
|
|
322 DECLARE_PROPERTY(GaimAccount, protocol_id, GAIM_TYPE_STRING),
|
|
323 };
|
|
324
|
|
325 GaimDBusProperty contact_properties [] = {
|
|
326 DECLARE_PROPERTY(GaimContact, alias, GAIM_TYPE_STRING),
|
|
327 DECLARE_PROPERTY(GaimContact, totalsize, GAIM_TYPE_INT),
|
|
328 DECLARE_PROPERTY(GaimContact, currentsize, GAIM_TYPE_INT),
|
|
329 DECLARE_PROPERTY(GaimContact, online, GAIM_TYPE_INT),
|
|
330 DECLARE_PROPERTY(GaimContact, priority, GAIM_TYPE_POINTER),
|
|
331 DECLARE_PROPERTY(GaimContact, priority_valid, GAIM_TYPE_BOOLEAN),
|
|
332 };
|
|
333
|
|
334 GaimDBusProperty group_properties [] = {
|
|
335 DECLARE_PROPERTY(GaimGroup, name, GAIM_TYPE_STRING),
|
|
336 DECLARE_PROPERTY(GaimGroup, totalsize, GAIM_TYPE_INT),
|
|
337 DECLARE_PROPERTY(GaimGroup, currentsize, GAIM_TYPE_INT),
|
|
338 DECLARE_PROPERTY(GaimGroup, online, GAIM_TYPE_INT),
|
|
339 };
|
|
340
|
|
341 GaimDBusProperty chat_properties [] = {
|
|
342 DECLARE_PROPERTY(GaimChat, alias, GAIM_TYPE_STRING),
|
|
343 DECLARE_PROPERTY(GaimChat, account, GAIM_TYPE_POINTER),
|
|
344 };
|
|
345
|
|
346
|
|
347 #define DECLARE_PROPERTY_HANDLER(type, gaim_type) \
|
|
348 static gboolean \
|
|
349 gaim_object_get_##type##_property (GaimObject *unused, \
|
|
350 gint id, const char *property_name, \
|
|
351 GValue *value, GError **error) \
|
|
352 { \
|
|
353 gpointer object = gaim_dbus_id_to_pointer(id, gaim_type); \
|
|
354 \
|
|
355 error_unless_1(object, "Invalid " #type " id: %i", id); \
|
|
356 \
|
|
357 return gaim_dbus_get_property(type##_properties, \
|
|
358 G_N_ELEMENTS(type##_properties), \
|
|
359 object, property_name, value, error); \
|
|
360 }
|
|
361
|
|
362 DECLARE_PROPERTY_HANDLER(buddy, DBUS_POINTER_BUDDY)
|
|
363 DECLARE_PROPERTY_HANDLER(account, DBUS_POINTER_ACCOUNT)
|
11068
|
364 DECLARE_PROPERTY_HANDLER(contact, DBUS_POINTER_CONTACT)
|
|
365 DECLARE_PROPERTY_HANDLER(group, DBUS_POINTER_GROUP)
|
|
366 DECLARE_PROPERTY_HANDLER(chat, DBUS_POINTER_CHAT)
|
11067
|
367
|
11055
|
368 #include "dbus-server-bindings.c"
|
|
369
|
|
370
|
11067
|
371
|
|
372 /**************************************************************************/
|
|
373 /** @name Gaim DBUS pointer registration mechanism */
|
|
374 /**************************************************************************/
|
|
375
|
|
376 static GHashTable *map_id_node;
|
|
377 static GHashTable *map_id_type;
|
|
378 static GHashTable *map_node_id;
|
|
379
|
|
380 void gaim_dbus_init_ids(void) {
|
|
381 map_id_node = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
382 map_id_type = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
383 map_node_id = g_hash_table_new (g_direct_hash, g_direct_equal);
|
|
384 }
|
|
385
|
|
386 void gaim_dbus_register_pointer(gpointer node, GaimDBusPointerType type)
|
|
387 {
|
|
388 static gint last_id = 0;
|
|
389 g_assert(g_hash_table_lookup(map_node_id, node) == NULL);
|
|
390
|
|
391
|
|
392 last_id++;
|
|
393 g_hash_table_insert(map_node_id, node, GINT_TO_POINTER(last_id));
|
|
394 g_hash_table_insert(map_id_node, GINT_TO_POINTER(last_id), node);
|
|
395 g_hash_table_insert(map_id_type, GINT_TO_POINTER(last_id),
|
|
396 GINT_TO_POINTER(type));
|
|
397 }
|
|
398
|
|
399
|
|
400 void gaim_dbus_unregister_pointer(gpointer node) {
|
|
401 gpointer id = g_hash_table_lookup(map_node_id, node);
|
|
402
|
|
403 g_hash_table_remove(map_node_id, node);
|
|
404 g_hash_table_remove(map_id_node, GINT_TO_POINTER(id));
|
|
405 g_hash_table_remove(map_id_node, GINT_TO_POINTER(id));
|
|
406 }
|
|
407
|
|
408 gint gaim_dbus_pointer_to_id(gpointer node) {
|
|
409 gint id = GPOINTER_TO_INT(g_hash_table_lookup(map_node_id, node));
|
|
410 g_assert(id);
|
|
411 return id;
|
|
412 }
|
|
413
|
|
414 gpointer gaim_dbus_id_to_pointer(gint id, GaimDBusPointerType type) {
|
|
415 if (type != GPOINTER_TO_INT(g_hash_table_lookup(map_id_type,
|
|
416 GINT_TO_POINTER(id))))
|
|
417 return NULL;
|
|
418 return g_hash_table_lookup(map_id_node, GINT_TO_POINTER(id));
|
|
419 }
|
|
420
|
|
421
|
|
422
|
|
423 /**************************************************************************/
|
|
424 /** @name Gaim DBUS init function */
|
|
425 /**************************************************************************/
|
|
426
|
|
427
|
11055
|
428 gboolean
|
|
429 dbus_server_init(void)
|
|
430 {
|
|
431 DBusGConnection *connection;
|
11067
|
432 GError *error = NULL;
|
11055
|
433 DBusGProxy *driver_proxy;
|
|
434 guint32 request_name_ret;
|
|
435
|
11067
|
436 gaim_object_error_quark =
|
|
437 g_quark_from_static_string("org.gaim.GaimError");
|
|
438
|
11055
|
439 gaim_debug_misc("dbus", "launching dbus server\n");
|
11067
|
440
|
11055
|
441 /* Connect to the bus */
|
|
442
|
|
443 error = NULL;
|
|
444 connection = dbus_g_bus_get(DBUS_BUS_STARTER, &error);
|
|
445
|
|
446 if (connection == NULL) {
|
|
447 g_assert(error);
|
11067
|
448 gaim_debug_error("dbus", "Failed to open connection to bus: %s\n",
|
11055
|
449 error->message);
|
|
450 g_error_free (error);
|
|
451 return FALSE;
|
|
452 }
|
|
453
|
|
454 /* Instantiate the gaim dbus object and register it */
|
|
455
|
|
456 gaim_object = g_object_new (GAIM_TYPE_OBJECT, NULL);
|
|
457
|
11067
|
458
|
|
459 dbus_g_object_type_install_info (GAIM_TYPE_OBJECT,
|
11055
|
460 &dbus_glib_gaim_object_object_info);
|
|
461
|
11067
|
462 dbus_g_connection_register_g_object (connection, DBUS_PATH_GAIM,
|
11055
|
463 gaim_object);
|
|
464
|
|
465
|
|
466 /* Obtain a proxy for the DBus object */
|
|
467
|
|
468 driver_proxy = dbus_g_proxy_new_for_name (connection,
|
|
469 DBUS_SERVICE_DBUS,
|
|
470 DBUS_PATH_DBUS,
|
|
471 DBUS_INTERFACE_DBUS);
|
|
472
|
11067
|
473 g_assert(driver_proxy);
|
11055
|
474
|
|
475 /* Test whether the registration was successful */
|
|
476
|
|
477 org_freedesktop_DBus_request_name(driver_proxy, DBUS_SERVICE_GAIM,
|
11067
|
478 DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &request_name_ret, &error);
|
11055
|
479
|
|
480 if (error) {
|
|
481 gaim_debug_error("dbus", "Failed to get name: %s\n", error->message);
|
|
482 g_error_free (error);
|
|
483 return FALSE;
|
|
484 }
|
|
485
|
|
486 if (request_name_ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
|
|
487 {
|
11067
|
488 gaim_debug_error ("dbus", "Got result code %u from requesting name\n",
|
11055
|
489 request_name_ret);
|
|
490 return FALSE;
|
|
491 }
|
|
492
|
|
493 gaim_debug_misc ("dbus", "GLib test service has name '%s'\n",
|
|
494 DBUS_SERVICE_GAIM);
|
|
495 gaim_debug_misc ("dbus", "GLib test service entering main loop\n");
|
11067
|
496
|
11055
|
497 return TRUE;
|
|
498 }
|