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
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"
54 /**************************************************************************/
55 /** @name 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 /** @name 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 /** @name Useful functions */
283 /**************************************************************************/
285 const char *empty_to_null(const char *str
)
287 if (str
== NULL
|| str
[0] == 0)
294 null_to_empty(const char *s
)
303 purple_dbusify_GList(GList
*list
, gboolean free_memory
, 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
);
321 purple_dbusify_GSList(GSList
*list
, gboolean free_memory
, dbus_int32_t
*len
)
327 *len
= g_slist_length(list
);
328 array
= g_new0(dbus_int32_t
, *len
);
329 for (i
= 0, elem
= list
; elem
!= NULL
; elem
= elem
->next
, i
++)
330 array
[i
] = purple_dbus_pointer_to_id(elem
->data
);
339 purple_GList_to_array(GList
*list
, gboolean free_memory
, dbus_int32_t
*len
)
345 *len
= g_list_length(list
);
346 array
= g_new0(gpointer
, *len
);
347 for (i
= 0, elem
= list
; elem
!= NULL
; elem
= elem
->next
, i
++)
348 array
[i
] = elem
->data
;
357 purple_GSList_to_array(GSList
*list
, gboolean free_memory
, dbus_int32_t
*len
)
363 *len
= g_slist_length(list
);
364 array
= g_new0(gpointer
, *len
);
365 for (i
= 0, elem
= list
; elem
!= NULL
; elem
= elem
->next
, i
++)
366 array
[i
] = elem
->data
;
375 purple_dbus_iter_hash_table(DBusMessageIter
*iter
, DBusError
*error
)
379 /* we do not need to destroy strings because they are part of the message */
380 hash
= g_hash_table_new(g_str_hash
, g_str_equal
);
384 DBusMessageIter subiter
;
386 if (dbus_message_iter_get_arg_type(iter
) != DBUS_TYPE_DICT_ENTRY
)
388 /* With all due respect to Dijkstra,
389 * this goto is for exception
390 * handling, and it is ok because it
391 * avoids duplication of the code
392 * responsible for destroying the hash
393 * table. Exceptional instructions
394 * for exceptional situations.
397 dbus_message_iter_recurse(iter
, &subiter
);
398 if (!purple_dbus_message_iter_get_args(&subiter
, error
,
399 DBUS_TYPE_STRING
, &key
,
400 DBUS_TYPE_STRING
, &value
,
402 goto error
; /* same here */
404 g_hash_table_insert(hash
, key
, value
);
405 } while (dbus_message_iter_next(iter
));
410 g_hash_table_destroy(hash
);
414 /**************************************************************/
415 /* DBus bindings ... */
416 /**************************************************************/
418 static DBusConnection
*purple_dbus_connection
;
421 purple_dbus_get_connection(void)
423 return purple_dbus_connection
;
426 #include "dbus-bindings.c"
427 #include "dbus-signals.c"
430 purple_dbus_dispatch_cb(DBusConnection
*connection
,
431 DBusMessage
*message
, void *user_data
)
434 PurpleDBusBinding
*bindings
;
437 bindings
= (PurpleDBusBinding
*) user_data
;
439 if (!dbus_message_has_path(message
, DBUS_PATH_PURPLE
))
442 name
= dbus_message_get_member(message
);
447 if (dbus_message_get_type(message
) != DBUS_MESSAGE_TYPE_METHOD_CALL
)
450 for (i
= 0; bindings
[i
].name
; i
++)
451 if (!strcmp(name
, bindings
[i
].name
))
456 dbus_error_init(&error
);
458 reply
= bindings
[i
].handler(message
, &error
);
460 if (reply
== NULL
&& dbus_error_is_set(&error
))
461 reply
= dbus_message_new_error (message
,
462 error
.name
, error
.message
);
466 dbus_connection_send(connection
, reply
, NULL
);
467 dbus_message_unref(reply
);
470 return TRUE
; /* return reply! */
478 dbus_gettext(const char **ptr
)
480 const char *text
= *ptr
;
481 *ptr
+= strlen(text
) + 1;
486 purple_dbus_introspect_cb(GList
**bindings_list
, void *bindings
)
488 *bindings_list
= g_list_prepend(*bindings_list
, bindings
);
491 static DBusMessage
*purple_dbus_introspect(DBusMessage
*message
)
495 GList
*bindings_list
, *node
;
498 const char *pointer_type
;
500 str
= g_string_sized_new(0x1000); /* TODO: why this size? */
502 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");
503 g_string_append_printf(str
, "<node name='%s'>\n", DBUS_PATH_PURPLE
);
504 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");
506 g_string_append_printf(str
, " <interface name='%s'>\n", DBUS_INTERFACE_PURPLE
);
508 bindings_list
= NULL
;
509 purple_signal_emit(purple_dbus_get_handle(), "dbus-introspect", &bindings_list
);
511 for (node
= bindings_list
; node
; node
= node
->next
)
513 PurpleDBusBinding
*bindings
;
516 bindings
= (PurpleDBusBinding
*)node
->data
;
518 for (i
= 0; bindings
[i
].name
; i
++)
522 g_string_append_printf(str
, " <method name='%s'>\n", bindings
[i
].name
);
524 text
= bindings
[i
].parameters
;
527 const char *name
, *direction
, *type
;
529 direction
= dbus_gettext(&text
);
530 type
= dbus_gettext(&text
);
531 name
= dbus_gettext(&text
);
533 g_string_append_printf(str
,
534 " <arg name='%s' type='%s' direction='%s'/>\n",
535 name
, type
, direction
);
537 g_string_append(str
, " </method>\n");
541 if (sizeof(int) == sizeof(dbus_int32_t
))
542 pointer_type
= "type='i'";
544 pointer_type
= "type='x'";
546 signals
= dbus_signals
;
547 while ((type
= strstr(signals
, "type='p'")) != NULL
) {
548 g_string_append_len(str
, signals
, type
- signals
);
549 g_string_append(str
, pointer_type
);
550 signals
= type
+ sizeof("type='p'") - 1;
552 g_string_append(str
, signals
);
554 g_string_append(str
, " </interface>\n</node>\n");
556 reply
= dbus_message_new_method_return(message
);
557 dbus_message_append_args(reply
, DBUS_TYPE_STRING
, &(str
->str
),
559 g_string_free(str
, TRUE
);
560 g_list_free(bindings_list
);
565 static DBusHandlerResult
566 purple_dbus_dispatch(DBusConnection
*connection
,
567 DBusMessage
*message
, void *user_data
)
569 if (purple_signal_emit_return_1(purple_dbus_get_handle(),
570 "dbus-method-called", connection
, message
))
571 return DBUS_HANDLER_RESULT_HANDLED
;
573 if (dbus_message_is_method_call(message
, DBUS_INTERFACE_INTROSPECTABLE
, "Introspect") &&
574 dbus_message_has_path(message
, DBUS_PATH_PURPLE
))
577 reply
= purple_dbus_introspect(message
);
578 dbus_connection_send (connection
, reply
, NULL
);
579 dbus_message_unref(reply
);
580 return DBUS_HANDLER_RESULT_HANDLED
;
583 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
587 purple_dbus_register_bindings(void *handle
, PurpleDBusBinding
*bindings
)
589 purple_signal_connect(purple_dbus_get_handle(), "dbus-method-called",
591 PURPLE_CALLBACK(purple_dbus_dispatch_cb
),
593 purple_signal_connect(purple_dbus_get_handle(), "dbus-introspect",
595 PURPLE_CALLBACK(purple_dbus_introspect_cb
),
600 purple_dbus_dispatch_init(void)
602 static DBusObjectPathVTable vtable
= {NULL
, &purple_dbus_dispatch
, NULL
, NULL
, NULL
, NULL
};
605 dbus_error_init(&error
);
606 purple_dbus_connection
= dbus_bus_get(DBUS_BUS_STARTER
, &error
);
608 if (purple_dbus_connection
== NULL
)
610 init_error
= g_strdup_printf(N_("Failed to get connection: %s"), error
.message
);
611 dbus_error_free(&error
);
615 /* Do not allow libdbus to exit on connection failure (This may
616 work around random exit(1) on SIGPIPE errors) */
617 dbus_connection_set_exit_on_disconnect (purple_dbus_connection
, FALSE
);
619 if (!dbus_connection_register_object_path(purple_dbus_connection
,
620 DBUS_PATH_PURPLE
, &vtable
, NULL
))
622 init_error
= g_strdup_printf(N_("Failed to get name: %s"), error
.name
);
623 dbus_error_free(&error
);
627 dbus_request_name_reply
= dbus_bus_request_name(purple_dbus_connection
,
628 DBUS_SERVICE_PURPLE
, 0, &error
);
630 if (dbus_error_is_set(&error
))
632 dbus_connection_unref(purple_dbus_connection
);
633 purple_dbus_connection
= NULL
;
634 init_error
= g_strdup_printf(N_("Failed to get serv name: %s"), error
.name
);
635 dbus_error_free(&error
);
639 dbus_connection_setup_with_g_main(purple_dbus_connection
, NULL
);
641 purple_debug_misc("dbus", "okkk\n");
643 purple_signal_register(purple_dbus_get_handle(), "dbus-method-called",
644 purple_marshal_BOOLEAN__POINTER_POINTER
,
645 purple_value_new(PURPLE_TYPE_BOOLEAN
), 2,
646 purple_value_new(PURPLE_TYPE_POINTER
),
647 purple_value_new(PURPLE_TYPE_POINTER
));
649 purple_signal_register(purple_dbus_get_handle(), "dbus-introspect",
650 purple_marshal_VOID__POINTER
, NULL
, 1,
651 purple_value_new_outgoing(PURPLE_TYPE_POINTER
));
653 PURPLE_DBUS_REGISTER_BINDINGS(purple_dbus_get_handle());
658 /**************************************************************************/
660 /**************************************************************************/
665 purple_dbus_convert_signal_name(const char *purple_name
)
667 int purple_index
, g_index
;
668 char *g_name
= g_new(char, strlen(purple_name
) + 1);
669 gboolean capitalize_next
= TRUE
;
671 for (purple_index
= g_index
= 0; purple_name
[purple_index
]; purple_index
++)
672 if (purple_name
[purple_index
] != '-' && purple_name
[purple_index
] != '_')
675 g_name
[g_index
++] = g_ascii_toupper(purple_name
[purple_index
]);
677 g_name
[g_index
++] = purple_name
[purple_index
];
678 capitalize_next
= FALSE
;
680 capitalize_next
= TRUE
;
687 #define my_arg(type) (ptr != NULL ? * ((type *)ptr) : va_arg(data, type))
690 purple_dbus_message_append_purple_values(DBusMessageIter
*iter
,
691 int number
, PurpleValue
**purple_values
, va_list data
)
694 gboolean error
= FALSE
;
696 for (i
= 0; i
< number
; i
++)
708 if (purple_value_is_outgoing(purple_values
[i
]))
710 ptr
= my_arg(gpointer
);
711 g_return_val_if_fail(ptr
, TRUE
);
714 switch (purple_values
[i
]->type
)
716 case PURPLE_TYPE_INT
:
717 case PURPLE_TYPE_ENUM
:
719 dbus_message_iter_append_basic(iter
, DBUS_TYPE_INT32
, &xint
);
721 case PURPLE_TYPE_UINT
:
722 xuint
= my_arg(guint
);
723 dbus_message_iter_append_basic(iter
, DBUS_TYPE_UINT32
, &xuint
);
725 case PURPLE_TYPE_INT64
:
726 xint64
= my_arg(gint64
);
727 dbus_message_iter_append_basic(iter
, DBUS_TYPE_INT64
, &xint64
);
729 case PURPLE_TYPE_UINT64
:
730 xuint64
= my_arg(guint64
);
731 dbus_message_iter_append_basic(iter
, DBUS_TYPE_UINT64
, &xuint64
);
733 case PURPLE_TYPE_BOOLEAN
:
734 xboolean
= my_arg(gboolean
);
735 dbus_message_iter_append_basic(iter
, DBUS_TYPE_BOOLEAN
, &xboolean
);
737 case PURPLE_TYPE_STRING
:
738 str
= null_to_empty(my_arg(char*));
739 if (!g_utf8_validate(str
, -1, NULL
)) {
741 purple_debug_error("dbus", "Invalid UTF-8 string passed to signal, emitting salvaged string!\n");
742 tmp
= purple_utf8_salvage(str
);
743 dbus_message_iter_append_basic(iter
, DBUS_TYPE_STRING
, &tmp
);
746 dbus_message_iter_append_basic(iter
, DBUS_TYPE_STRING
, &str
);
749 case PURPLE_TYPE_SUBTYPE
: /* registered pointers only! */
750 case PURPLE_TYPE_POINTER
:
751 case PURPLE_TYPE_OBJECT
:
752 case PURPLE_TYPE_BOXED
:
753 val
= my_arg(gpointer
);
754 id
= purple_dbus_pointer_to_id(val
);
755 if (id
== 0 && val
!= NULL
)
756 error
= TRUE
; /* Some error happened. */
757 dbus_message_iter_append_basic(iter
,
758 (sizeof(id
) == sizeof(dbus_int32_t
)) ? DBUS_TYPE_INT32
: DBUS_TYPE_INT64
, &id
);
760 default: /* no conversion implemented */
761 g_return_val_if_reached(TRUE
);
770 purple_dbus_signal_emit_purple(const char *name
, int num_values
,
771 PurpleValue
**values
, va_list vargs
)
774 DBusMessageIter iter
;
777 #if 0 /* this is noisy with no dbus connection */
778 g_return_if_fail(purple_dbus_connection
);
780 if (purple_dbus_connection
== NULL
)
786 * The test below is a hack that prevents our "dbus-method-called"
787 * signal from being propagated to dbus. What we really need is a
788 * flag for each signal that states whether this signal is to be
789 * dbus-propagated or not.
791 if (!strcmp(name
, "dbus-method-called"))
794 newname
= purple_dbus_convert_signal_name(name
);
795 signal
= dbus_message_new_signal(DBUS_PATH_PURPLE
, DBUS_INTERFACE_PURPLE
, newname
);
796 dbus_message_iter_init_append(signal
, &iter
);
798 if (purple_dbus_message_append_purple_values(&iter
, num_values
, values
, vargs
))
799 if (purple_debug_is_verbose())
800 purple_debug_warning("dbus",
801 "The signal \"%s\" caused some dbus error."
802 " (If you are not a developer, please ignore this message.)\n",
805 dbus_connection_send(purple_dbus_connection
, signal
, NULL
);
808 dbus_message_unref(signal
);
812 purple_dbus_get_init_error(void)
818 purple_dbus_get_handle(void)
826 purple_dbus_init(void)
828 if (g_thread_supported())
829 dbus_g_thread_init();
831 purple_dbus_init_ids();
835 purple_dbus_dispatch_init();
836 if (init_error
!= NULL
)
837 purple_debug_error("dbus", "%s\n", init_error
);
841 purple_dbus_uninit(void)
844 if (!purple_dbus_connection
)
847 dbus_error_init(&error
);
848 dbus_connection_unregister_object_path(purple_dbus_connection
, DBUS_PATH_PURPLE
);
849 dbus_bus_release_name(purple_dbus_connection
, DBUS_SERVICE_PURPLE
, &error
);
850 dbus_error_free(&error
);
851 dbus_connection_unref(purple_dbus_connection
);
852 purple_dbus_connection
= NULL
;
853 purple_signals_disconnect_by_handle(purple_dbus_get_handle());