4 * Purple 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
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
24 #ifndef DBUS_API_SUBJECT_TO_CHANGE
25 #define DBUS_API_SUBJECT_TO_CHANGE
28 /* Allow the code below to see deprecated functions, so we can continue to
29 * export them via DBus. */
30 #undef PURPLE_DISABLE_DEPRECATED
39 #include "buddylist.h"
40 #include "conversation.h"
41 #include "dbus-purple.h"
42 #include "dbus-server.h"
43 #include "dbus-useful.h"
44 #include "dbus-bindings.h"
47 #include "savedstatuses.h"
49 #include "smiley-list.h"
54 /**************************************************************************/
55 /* Purple DBUS pointer registration mechanism */
56 /**************************************************************************/
59 * Here we include the list of #PURPLE_DBUS_DEFINE_TYPE statements for
60 * all structs defined in purple. This file has been generated by the
61 * #dbus-analyze-types.py script.
64 #include "dbus-types.c"
67 * The following three hashtables map are used to translate between
68 * pointers (nodes) and the corresponding handles (ids).
71 static GHashTable
*map_node_id
;
72 static GHashTable
*map_id_node
;
73 static GHashTable
*map_id_type
;
75 static gchar
*init_error
;
76 static int dbus_request_name_reply
= DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
;
78 gboolean
purple_dbus_is_owner(void)
80 return(DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER
== dbus_request_name_reply
);
84 * This function initializes the pointer-id traslation system. It
85 * creates the three above hashtables and defines parents of some types.
88 purple_dbus_init_ids(void)
90 map_id_node
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
91 map_id_type
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
92 map_node_id
= g_hash_table_new(g_direct_hash
, g_direct_equal
);
94 PURPLE_DBUS_TYPE(PurpleBuddy
)->parent
= PURPLE_DBUS_TYPE(PurpleBlistNode
);
95 PURPLE_DBUS_TYPE(PurpleContact
)->parent
= PURPLE_DBUS_TYPE(PurpleBlistNode
);
96 PURPLE_DBUS_TYPE(PurpleChat
)->parent
= PURPLE_DBUS_TYPE(PurpleBlistNode
);
97 PURPLE_DBUS_TYPE(PurpleGroup
)->parent
= PURPLE_DBUS_TYPE(PurpleBlistNode
);
101 purple_dbus_register_pointer(gpointer node
, PurpleDBusType
*type
)
103 static gint last_id
= 0;
105 g_return_if_fail(map_node_id
);
106 g_return_if_fail(g_hash_table_lookup(map_node_id
, node
) == NULL
);
109 g_hash_table_insert(map_node_id
, node
, GINT_TO_POINTER(last_id
));
110 g_hash_table_insert(map_id_node
, GINT_TO_POINTER(last_id
), node
);
111 g_hash_table_insert(map_id_type
, GINT_TO_POINTER(last_id
), type
);
115 purple_dbus_unregister_pointer(gpointer node
)
117 gpointer id
= g_hash_table_lookup(map_node_id
, node
);
119 g_hash_table_remove(map_node_id
, node
);
120 g_hash_table_remove(map_id_node
, GINT_TO_POINTER(id
));
121 g_hash_table_remove(map_id_type
, GINT_TO_POINTER(id
));
125 purple_dbus_pointer_to_id(gconstpointer node
)
127 gint id
= GPOINTER_TO_INT(g_hash_table_lookup(map_node_id
, node
));
128 if ((id
== 0) && (node
!= NULL
))
130 if (purple_debug_is_verbose())
131 purple_debug_warning("dbus",
132 "Need to register an object with the dbus subsystem."
133 " (If you are not a developer, please ignore this message.)\n");
140 purple_dbus_id_to_pointer(gint id
, PurpleDBusType
*type
)
142 PurpleDBusType
*objtype
;
144 objtype
= (PurpleDBusType
*)g_hash_table_lookup(map_id_type
,
145 GINT_TO_POINTER(id
));
147 while (objtype
!= type
&& objtype
!= NULL
)
148 objtype
= objtype
->parent
;
151 return g_hash_table_lookup(map_id_node
, GINT_TO_POINTER(id
));
157 purple_dbus_pointer_to_id_error(gconstpointer ptr
, DBusError
*error
)
159 gint id
= purple_dbus_pointer_to_id(ptr
);
161 if (ptr
!= NULL
&& id
== 0)
162 dbus_set_error(error
, "im.pidgin.purple.ObjectNotFound",
163 "The return object is not mapped (this is a Purple error)");
169 purple_dbus_id_to_pointer_error(gint id
, PurpleDBusType
*type
,
170 const char *typename
, DBusError
*error
)
172 gpointer ptr
= purple_dbus_id_to_pointer(id
, type
);
174 if (ptr
== NULL
&& id
!= 0)
175 dbus_set_error(error
, "im.pidgin.purple.InvalidHandle",
176 "%s object with ID = %i not found", typename
, id
);
182 /**************************************************************************/
183 /* Modified versions of some DBus functions */
184 /**************************************************************************/
187 purple_dbus_message_get_args(DBusMessage
*message
,
188 DBusError
*error
, int first_arg_type
, ...)
193 va_start(var_args
, first_arg_type
);
194 retval
= purple_dbus_message_get_args_valist(message
, error
, first_arg_type
, var_args
);
201 purple_dbus_message_get_args_valist(DBusMessage
*message
,
202 DBusError
*error
, int first_arg_type
, va_list var_args
)
204 DBusMessageIter iter
;
206 dbus_message_iter_init(message
, &iter
);
207 return purple_dbus_message_iter_get_args_valist(&iter
, error
, first_arg_type
, var_args
);
211 purple_dbus_message_iter_get_args(DBusMessageIter
*iter
,
212 DBusError
*error
, int first_arg_type
, ...)
217 va_start(var_args
, first_arg_type
);
218 retval
= purple_dbus_message_iter_get_args_valist(iter
, error
, first_arg_type
, var_args
);
224 #define TYPE_IS_CONTAINER(typecode) \
225 ((typecode) == DBUS_TYPE_STRUCT || \
226 (typecode) == DBUS_TYPE_DICT_ENTRY || \
227 (typecode) == DBUS_TYPE_VARIANT || \
228 (typecode) == DBUS_TYPE_ARRAY)
232 purple_dbus_message_iter_get_args_valist(DBusMessageIter
*iter
,
233 DBusError
*error
, int first_arg_type
, va_list var_args
)
235 int spec_type
, msg_type
, i
;
237 spec_type
= first_arg_type
;
239 for (i
= 0; spec_type
!= DBUS_TYPE_INVALID
; i
++)
241 msg_type
= dbus_message_iter_get_arg_type(iter
);
243 if (msg_type
!= spec_type
)
245 dbus_set_error(error
, DBUS_ERROR_INVALID_ARGS
,
246 "Argument %d is specified to be of type \"%i\", but "
247 "is actually of type \"%i\"\n", i
,
248 spec_type
, msg_type
);
252 if (!TYPE_IS_CONTAINER(spec_type
))
255 ptr
= va_arg (var_args
, gpointer
);
256 dbus_message_iter_get_basic(iter
, ptr
);
260 DBusMessageIter
*sub
;
261 sub
= va_arg (var_args
, DBusMessageIter
*);
262 dbus_message_iter_recurse(iter
, sub
);
263 purple_debug_info("dbus", "subiter %p:%p\n", sub
, * (gpointer
*) sub
);
264 break; /* for testing only! */
267 spec_type
= va_arg(var_args
, int);
268 if (!dbus_message_iter_next(iter
) && spec_type
!= DBUS_TYPE_INVALID
)
270 dbus_set_error (error
, DBUS_ERROR_INVALID_ARGS
,
271 "Message has only %d arguments, but more were expected", i
);
281 /**************************************************************************/
282 /* Useful functions */
283 /**************************************************************************/
285 const char *purple_emptystr_to_null(const char *str
)
287 if (str
== NULL
|| str
[0] == 0)
294 purple_null_to_emptystr(const char *s
)
303 purple_dbusify_GList(GList
*list
, dbus_int32_t
*len
)
309 *len
= g_list_length(list
);
310 array
= g_new0(dbus_int32_t
, *len
);
311 for (i
= 0, elem
= list
; elem
!= NULL
; elem
= elem
->next
, i
++)
312 array
[i
] = purple_dbus_pointer_to_id(elem
->data
);
318 purple_dbusify_GSList(GSList
*list
, dbus_int32_t
*len
)
324 *len
= g_slist_length(list
);
325 array
= g_new0(dbus_int32_t
, *len
);
326 for (i
= 0, elem
= list
; elem
!= NULL
; elem
= elem
->next
, i
++)
327 array
[i
] = purple_dbus_pointer_to_id(elem
->data
);
333 purple_GList_to_array(GList
*list
, dbus_int32_t
*len
)
339 *len
= g_list_length(list
);
340 array
= g_new0(gpointer
, *len
);
341 for (i
= 0, elem
= list
; elem
!= NULL
; elem
= elem
->next
, i
++)
342 array
[i
] = elem
->data
;
348 purple_GSList_to_array(GSList
*list
, dbus_int32_t
*len
)
354 *len
= g_slist_length(list
);
355 array
= g_new0(gpointer
, *len
);
356 for (i
= 0, elem
= list
; elem
!= NULL
; elem
= elem
->next
, i
++)
357 array
[i
] = elem
->data
;
363 purple_dbus_iter_hash_table(DBusMessageIter
*iter
, DBusError
*error
)
367 /* we do not need to destroy strings because they are part of the message */
368 hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
372 DBusMessageIter subiter
;
374 if (dbus_message_iter_get_arg_type(iter
) != DBUS_TYPE_DICT_ENTRY
)
376 /* With all due respect to Dijkstra,
377 * this goto is for exception
378 * handling, and it is ok because it
379 * avoids duplication of the code
380 * responsible for destroying the hash
381 * table. Exceptional instructions
382 * for exceptional situations.
385 dbus_message_iter_recurse(iter
, &subiter
);
386 if (!purple_dbus_message_iter_get_args(&subiter
, error
,
387 DBUS_TYPE_STRING
, &key
,
388 DBUS_TYPE_STRING
, &value
,
390 goto error
; /* same here */
392 g_hash_table_insert(hash
, key
, value
);
393 } while (dbus_message_iter_next(iter
));
398 g_hash_table_destroy(hash
);
402 /**************************************************************/
403 /* DBus bindings ... */
404 /**************************************************************/
406 static DBusConnection
*purple_dbus_connection
;
409 purple_dbus_get_connection(void)
411 return purple_dbus_connection
;
414 #include "dbus-bindings.c"
415 #include "dbus-signals.c"
418 purple_dbus_dispatch_cb(DBusConnection
*connection
,
419 DBusMessage
*message
, void *user_data
)
422 PurpleDBusBinding
*bindings
;
425 bindings
= (PurpleDBusBinding
*) user_data
;
427 if (!dbus_message_has_path(message
, PURPLE_DBUS_PATH
))
430 name
= dbus_message_get_member(message
);
435 if (dbus_message_get_type(message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
438 for (i
= 0; bindings
[i
].name
; i
++)
439 if (!strcmp(name
, bindings
[i
].name
))
444 dbus_error_init(&error
);
446 reply
= bindings
[i
].handler(message
, &error
);
448 if (reply
== NULL
&& dbus_error_is_set(&error
))
449 reply
= dbus_message_new_error (message
,
450 error
.name
, error
.message
);
454 dbus_connection_send(connection
, reply
, NULL
);
455 dbus_message_unref(reply
);
458 return TRUE
; /* return reply! */
466 dbus_gettext(const char **ptr
)
468 const char *text
= *ptr
;
469 *ptr
+= strlen(text
) + 1;
474 purple_dbus_introspect_cb(GList
**bindings_list
, void *bindings
)
476 *bindings_list
= g_list_prepend(*bindings_list
, bindings
);
479 static DBusMessage
*purple_dbus_introspect(DBusMessage
*message
)
483 GList
*bindings_list
, *node
;
486 const char *pointer_type
;
488 str
= g_string_sized_new(0x1000); /* TODO: why this size? */
490 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");
491 g_string_append_printf(str
, "<node name='%s'>\n", PURPLE_DBUS_PATH
);
492 g_string_append(str
, " <interface name='org.freedesktop.DBus.Introspectable'>\n <method name='Introspect'>\n <arg name='data' direction='out' type='s'/>\n </method>\n </interface>\n\n");
494 g_string_append_printf(str
, " <interface name='%s'>\n", PURPLE_DBUS_INTERFACE
);
496 bindings_list
= NULL
;
497 purple_signal_emit(purple_dbus_get_handle(), "dbus-introspect", &bindings_list
);
499 for (node
= bindings_list
; node
; node
= node
->next
)
501 PurpleDBusBinding
*bindings
;
504 bindings
= (PurpleDBusBinding
*)node
->data
;
506 for (i
= 0; bindings
[i
].name
; i
++)
510 g_string_append_printf(str
, " <method name='%s'>\n", bindings
[i
].name
);
512 text
= bindings
[i
].parameters
;
515 const char *name
, *direction
, *type
;
517 direction
= dbus_gettext(&text
);
518 type
= dbus_gettext(&text
);
519 name
= dbus_gettext(&text
);
521 g_string_append_printf(str
,
522 " <arg name='%s' type='%s' direction='%s'/>\n",
523 name
, type
, direction
);
525 g_string_append(str
, " </method>\n");
529 if (sizeof(int) == sizeof(dbus_int32_t
))
530 pointer_type
= "type='i'";
532 pointer_type
= "type='x'";
534 signals
= dbus_signals
;
535 while ((type
= strstr(signals
, "type='p'")) != NULL
) {
536 g_string_append_len(str
, signals
, type
- signals
);
537 g_string_append(str
, pointer_type
);
538 signals
= type
+ sizeof("type='p'") - 1;
540 g_string_append(str
, signals
);
542 g_string_append(str
, " </interface>\n</node>\n");
544 reply
= dbus_message_new_method_return(message
);
545 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &(str
->str
),
547 g_string_free(str
, TRUE
);
548 g_list_free(bindings_list
);
553 static DBusHandlerResult
554 purple_dbus_dispatch(DBusConnection
*connection
,
555 DBusMessage
*message
, void *user_data
)
557 if (purple_signal_emit_return_1(purple_dbus_get_handle(),
558 "dbus-method-called", connection
, message
))
559 return DBUS_HANDLER_RESULT_HANDLED
;
561 if (dbus_message_is_method_call(message
, DBUS_INTERFACE_INTROSPECTABLE
, "Introspect") &&
562 dbus_message_has_path(message
, PURPLE_DBUS_PATH
))
565 reply
= purple_dbus_introspect(message
);
566 dbus_connection_send (connection
, reply
, NULL
);
567 dbus_message_unref(reply
);
568 return DBUS_HANDLER_RESULT_HANDLED
;
571 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
575 purple_dbus_register_bindings(void *handle
, PurpleDBusBinding
*bindings
)
577 purple_signal_connect(purple_dbus_get_handle(), "dbus-method-called",
579 PURPLE_CALLBACK(purple_dbus_dispatch_cb
),
581 purple_signal_connect(purple_dbus_get_handle(), "dbus-introspect",
583 PURPLE_CALLBACK(purple_dbus_introspect_cb
),
588 purple_dbus_dispatch_init(void)
590 static DBusObjectPathVTable vtable
= {NULL
, &purple_dbus_dispatch
, NULL
, NULL
, NULL
, NULL
};
593 dbus_error_init(&error
);
594 purple_dbus_connection
= dbus_bus_get(DBUS_BUS_STARTER
, &error
);
596 if (purple_dbus_connection
== NULL
)
598 init_error
= g_strdup_printf(N_("Failed to get connection: %s"), error
.message
);
599 dbus_error_free(&error
);
603 /* Do not allow libdbus to exit on connection failure (This may
604 work around random exit(1) on SIGPIPE errors) */
605 dbus_connection_set_exit_on_disconnect (purple_dbus_connection
, FALSE
);
607 if (!dbus_connection_register_object_path(purple_dbus_connection
,
608 PURPLE_DBUS_PATH
, &vtable
, NULL
))
610 init_error
= g_strdup_printf(N_("Failed to get name: %s"), error
.name
);
611 dbus_error_free(&error
);
615 dbus_request_name_reply
= dbus_bus_request_name(purple_dbus_connection
,
616 PURPLE_DBUS_SERVICE
, 0, &error
);
618 if (dbus_error_is_set(&error
))
620 dbus_connection_unref(purple_dbus_connection
);
621 purple_dbus_connection
= NULL
;
622 init_error
= g_strdup_printf(N_("Failed to get serv name: %s"), error
.name
);
623 dbus_error_free(&error
);
627 dbus_connection_setup_with_g_main(purple_dbus_connection
, NULL
);
629 purple_signal_register(purple_dbus_get_handle(), "dbus-method-called",
630 purple_marshal_BOOLEAN__POINTER_POINTER
,
631 G_TYPE_BOOLEAN
, 2, G_TYPE_POINTER
, G_TYPE_POINTER
);
633 purple_signal_register(purple_dbus_get_handle(), "dbus-introspect",
634 purple_marshal_VOID__POINTER
, G_TYPE_NONE
, 1,
635 G_TYPE_POINTER
); /* pointer to a pointer */
637 PURPLE_DBUS_REGISTER_BINDINGS(purple_dbus_get_handle());
639 if (purple_debug_is_verbose())
640 purple_debug_misc("dbus", "initialized");
645 /**************************************************************************/
647 /**************************************************************************/
652 purple_dbus_convert_signal_name(const char *purple_name
)
654 int purple_index
, g_index
;
655 char *g_name
= g_new(char, strlen(purple_name
) + 1);
656 gboolean capitalize_next
= TRUE
;
658 for (purple_index
= g_index
= 0; purple_name
[purple_index
]; purple_index
++)
659 if (purple_name
[purple_index
] != '-' && purple_name
[purple_index
] != '_')
662 g_name
[g_index
++] = g_ascii_toupper(purple_name
[purple_index
]);
664 g_name
[g_index
++] = purple_name
[purple_index
];
665 capitalize_next
= FALSE
;
667 capitalize_next
= TRUE
;
674 #define my_arg(type) (ptr != NULL ? * ((type *)ptr) : va_arg(data, type))
677 purple_dbus_message_append_values(DBusMessageIter
*iter
,
678 int number
, GType
*types
, va_list data
)
681 gboolean error
= FALSE
;
683 for (i
= 0; i
< number
; i
++)
695 if (purple_value_is_outgoing(purple_values
[i
]))
697 ptr
= my_arg(gpointer
);
698 g_return_val_if_fail(ptr
, TRUE
);
705 dbus_message_iter_append_basic(iter
, DBUS_TYPE_INT32
, &xint
);
708 xuint
= my_arg(guint
);
709 dbus_message_iter_append_basic(iter
, DBUS_TYPE_UINT32
, &xuint
);
712 xint64
= my_arg(gint64
);
713 dbus_message_iter_append_basic(iter
, DBUS_TYPE_INT64
, &xint64
);
716 xuint64
= my_arg(guint64
);
717 dbus_message_iter_append_basic(iter
, DBUS_TYPE_UINT64
, &xuint64
);
720 xboolean
= my_arg(gboolean
);
721 dbus_message_iter_append_basic(iter
, DBUS_TYPE_BOOLEAN
, &xboolean
);
724 str
= purple_null_to_emptystr(my_arg(char*));
725 if (!g_utf8_validate(str
, -1, NULL
)) {
727 purple_debug_error("dbus", "Invalid UTF-8 string passed to signal, emitting salvaged string!\n");
728 tmp
= purple_utf8_salvage(str
);
729 dbus_message_iter_append_basic(iter
, DBUS_TYPE_STRING
, &tmp
);
732 dbus_message_iter_append_basic(iter
, DBUS_TYPE_STRING
, &str
);
736 if (G_TYPE_IS_OBJECT(types
[i
]) ||
737 G_TYPE_IS_BOXED(types
[i
]) ||
738 types
[i
] == G_TYPE_POINTER
)
740 val
= my_arg(gpointer
);
741 id
= purple_dbus_pointer_to_id(val
);
742 if (id
== 0 && val
!= NULL
)
743 error
= TRUE
; /* Some error happened. */
744 dbus_message_iter_append_basic(iter
,
745 (sizeof(id
) == sizeof(dbus_int32_t
)) ? DBUS_TYPE_INT32
: DBUS_TYPE_INT64
, &id
);
747 else if (G_TYPE_IS_ENUM(types
[i
]))
750 dbus_message_iter_append_basic(iter
, DBUS_TYPE_INT32
, &xint
);
752 else /* no conversion implemented */
754 g_return_val_if_reached(TRUE
);
764 purple_dbus_signal_emit_purple(const char *name
, int num_values
,
765 GType
*types
, va_list vargs
)
768 DBusMessageIter iter
;
771 #if 0 /* this is noisy with no dbus connection */
772 g_return_if_fail(purple_dbus_connection
);
774 if (purple_dbus_connection
== NULL
)
780 * The test below is a hack that prevents our "dbus-method-called"
781 * signal from being propagated to dbus. What we really need is a
782 * flag for each signal that states whether this signal is to be
783 * dbus-propagated or not.
785 if (!strcmp(name
, "dbus-method-called"))
788 newname
= purple_dbus_convert_signal_name(name
);
789 signal
= dbus_message_new_signal(PURPLE_DBUS_PATH
, PURPLE_DBUS_INTERFACE
, newname
);
790 dbus_message_iter_init_append(signal
, &iter
);
792 if (purple_dbus_message_append_values(&iter
, num_values
, types
, vargs
))
793 if (purple_debug_is_verbose())
794 purple_debug_warning("dbus",
795 "The signal \"%s\" caused some dbus error."
796 " (If you are not a developer, please ignore this message.)\n",
799 dbus_connection_send(purple_dbus_connection
, signal
, NULL
);
802 dbus_message_unref(signal
);
806 purple_dbus_get_init_error(void)
812 purple_dbus_get_handle(void)
820 purple_dbus_init(void)
822 if (g_thread_supported())
823 dbus_g_thread_init();
825 purple_dbus_init_ids();
829 purple_dbus_dispatch_init();
830 if (init_error
!= NULL
)
831 purple_debug_error("dbus", "%s\n", init_error
);
835 purple_dbus_uninit(void)
838 if (!purple_dbus_connection
)
841 dbus_error_init(&error
);
842 dbus_connection_unregister_object_path(purple_dbus_connection
, PURPLE_DBUS_PATH
);
843 dbus_bus_release_name(purple_dbus_connection
, PURPLE_DBUS_SERVICE
, &error
);
844 dbus_error_free(&error
);
845 dbus_connection_unref(purple_dbus_connection
);
846 purple_dbus_connection
= NULL
;
847 purple_signals_disconnect_by_handle(purple_dbus_get_handle());