1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gobject.c Exporting a GObject remotely
4 * Copyright (C) 2003 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.0
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "dbus-glib.h"
26 #include "dbus-gtest.h"
27 #include "dbus-gutils.h"
28 #include "dbus-gvalue.h"
32 * @addtogroup DBusGLibInternals
36 static GStaticMutex info_hash_mutex = G_STATIC_MUTEX_INIT;
37 static GHashTable *info_hash = NULL;
40 wincaps_to_uscore (const char *caps)
45 str = g_string_new (NULL);
49 if (g_ascii_isupper (*p))
52 (str->len < 2 || str->str[str->len-2] != '_'))
53 g_string_append_c (str, '_');
54 g_string_append_c (str, g_ascii_tolower (*p));
58 g_string_append_c (str, *p);
63 return g_string_free (str, FALSE);
67 uscore_to_wincaps (const char *uscore)
71 gboolean last_was_uscore;
73 last_was_uscore = TRUE;
75 str = g_string_new (NULL);
79 if (*p == '-' || *p == '_')
81 last_was_uscore = TRUE;
87 g_string_append_c (str, g_ascii_toupper (*p));
88 last_was_uscore = FALSE;
91 g_string_append_c (str, *p);
96 return g_string_free (str, FALSE);
100 gobject_unregister_function (DBusConnection *connection,
105 object = G_OBJECT (user_data);
112 gtype_to_dbus_type (GType type)
118 return DBUS_TYPE_BYTE;
121 return DBUS_TYPE_BOOLEAN;
123 /* long gets cut to 32 bits so the remote API is consistent
124 * on all architectures
129 return DBUS_TYPE_INT32;
132 return DBUS_TYPE_UINT32;
135 return DBUS_TYPE_INT64;
138 return DBUS_TYPE_UINT64;
142 return DBUS_TYPE_DOUBLE;
145 return DBUS_TYPE_STRING;
148 return DBUS_TYPE_INVALID;
153 dbus_type_to_string (int type)
157 case DBUS_TYPE_INVALID:
161 case DBUS_TYPE_BOOLEAN:
163 case DBUS_TYPE_INT32:
165 case DBUS_TYPE_UINT32:
167 case DBUS_TYPE_DOUBLE:
169 case DBUS_TYPE_STRING:
171 case DBUS_TYPE_CUSTOM:
173 case DBUS_TYPE_ARRAY:
182 static DBusHandlerResult
183 handle_introspect (DBusConnection *connection,
184 DBusMessage *message,
189 unsigned int n_specs;
196 if (!dbus_message_get_path_decomposed (message, &path))
197 g_error ("Out of memory");
199 if (!dbus_connection_list_registered (connection, (const char**) path,
201 g_error ("Out of memory");
203 xml = g_string_new (NULL);
205 g_string_append (xml, "<node>\n");
207 last_type = G_TYPE_INVALID;
209 specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
215 GParamSpec *spec = specs[i];
221 dbus_type = gtype_to_dbus_type (G_PARAM_SPEC_VALUE_TYPE (spec));
222 if (dbus_type == DBUS_TYPE_INVALID)
225 if (spec->owner_type != last_type)
227 if (last_type != G_TYPE_INVALID)
228 g_string_append (xml, " </interface>\n");
231 /* FIXME what should the namespace on the interface be in
232 * general? should people be able to set it for their
236 g_string_append (xml, " <interface name=\"org.gtk.objects.");
237 g_string_append (xml, g_type_name (spec->owner_type));
238 g_string_append (xml, "\">\n");
240 last_type = spec->owner_type;
243 can_set = ((spec->flags & G_PARAM_WRITABLE) != 0 &&
244 (spec->flags & G_PARAM_CONSTRUCT_ONLY) == 0);
246 can_get = (spec->flags & G_PARAM_READABLE) != 0;
248 s = uscore_to_wincaps (spec->name);
252 g_string_append (xml, " <method name=\"set_");
253 g_string_append (xml, s);
254 g_string_append (xml, "\">\n");
256 g_string_append (xml, " <arg type=\"");
257 g_string_append (xml, dbus_type_to_string (dbus_type));
258 g_string_append (xml, "\"/>\n");
263 g_string_append (xml, " <method name=\"get_");
264 g_string_append (xml, s);
265 g_string_append (xml, "\">\n");
267 g_string_append (xml, " <arg type=\"");
268 g_string_append (xml, dbus_type_to_string (dbus_type));
269 g_string_append (xml, "\" direction=\"out\"/>\n");
278 if (last_type != G_TYPE_INVALID)
279 g_string_append (xml, " </interface>\n");
283 /* Append child nodes */
288 g_string_append_printf (xml, " <node name=\"%s\"/>\n",
293 /* Close the XML, and send it to the requesting app */
295 g_string_append (xml, "</node>\n");
297 ret = dbus_message_new_method_return (message);
299 g_error ("Out of memory");
301 dbus_message_append_args (message,
302 DBUS_TYPE_STRING, xml->str,
305 dbus_connection_send (connection, message, NULL);
306 dbus_message_unref (message);
308 g_string_free (xml, TRUE);
310 dbus_free_string_array (path);
311 dbus_free_string_array (children);
313 return DBUS_HANDLER_RESULT_HANDLED;
317 set_object_property (DBusConnection *connection,
318 DBusMessage *message,
322 GValue value = { 0, };
324 DBusMessageIter iter;
326 dbus_message_iter_init (message, &iter);
328 /* The g_object_set_property() will transform some types, e.g. it
329 * will let you use a uchar to set an int property etc. Note that
330 * any error in value range or value conversion will just
331 * g_warning(). These GObject skels are not for secure applications.
333 if (dbus_gvalue_demarshal (&iter, &value))
335 g_object_set_property (object,
339 g_value_unset (&value);
341 ret = dbus_message_new_method_return (message);
343 g_error ("out of memory");
347 ret = dbus_message_new_error (message,
348 DBUS_ERROR_INVALID_ARGS,
349 "Argument's D-BUS type can't be converted to a GType");
351 g_error ("out of memory");
358 get_object_property (DBusConnection *connection,
359 DBusMessage *message,
366 DBusMessageIter iter;
368 value_type = G_PARAM_SPEC_VALUE_TYPE (pspec);
370 ret = dbus_message_new_method_return (message);
372 g_error ("out of memory");
374 g_value_init (&value, value_type);
375 g_object_get_property (object, pspec->name, &value);
377 value_type = G_VALUE_TYPE (&value);
379 dbus_message_append_iter_init (message, &iter);
381 if (!dbus_gvalue_marshal (&iter, &value))
383 dbus_message_unref (ret);
384 ret = dbus_message_new_error (message,
385 DBUS_ERROR_UNKNOWN_METHOD,
386 "Can't convert GType of object property to a D-BUS type");
392 static DBusHandlerResult
393 gobject_message_function (DBusConnection *connection,
394 DBusMessage *message,
397 const DBusGObjectInfo *info;
405 object = G_OBJECT (user_data);
407 if (dbus_message_is_method_call (message,
408 DBUS_INTERFACE_ORG_FREEDESKTOP_INTROSPECTABLE,
410 return handle_introspect (connection, message, object);
412 member = dbus_message_get_member (message);
414 /* Try the metainfo, which lets us invoke methods */
416 g_static_mutex_lock (&info_hash_mutex);
417 /* FIXME this needs to walk up the inheritance tree, not
418 * just look at the most-derived class
420 info = g_hash_table_lookup (info_hash,
421 G_OBJECT_GET_CLASS (object));
422 g_static_mutex_unlock (&info_hash_mutex);
431 /* If no metainfo, we can still do properties and signals
432 * via standard GLib introspection
434 setter = (member[0] == 's' && member[1] == 'e' && member[2] == 't' && member[3] == '_');
435 getter = (member[0] == 'g' && member[1] == 'e' && member[2] == 't' && member[3] == '_');
437 if (!(setter || getter))
438 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
440 s = wincaps_to_uscore (&member[4]);
442 pspec = g_object_class_find_property (G_OBJECT_GET_CLASS (object),
453 ret = set_object_property (connection, message,
458 ret = get_object_property (connection, message,
463 g_assert_not_reached ();
467 g_assert (ret != NULL);
469 dbus_connection_send (connection, ret, NULL);
470 dbus_message_unref (ret);
471 return DBUS_HANDLER_RESULT_HANDLED;
474 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
477 static DBusObjectPathVTable gobject_dbus_vtable = {
478 gobject_unregister_function,
479 gobject_message_function,
483 /** @} */ /* end of internals */
486 * @addtogroup DBusGLib
491 * Install introspection information about the given object class
492 * sufficient to allow methods on the object to be invoked by name.
493 * The introspection information is normally generated by
494 * dbus-glib-tool, then this function is called in the
495 * class_init() for the object class.
497 * Once introspection information has been installed, instances of the
498 * object registered with dbus_connection_register_g_object() can have
499 * their methods invoked remotely.
501 * @param object_class class struct of the object
502 * @param info introspection data generated by dbus-glib-tool
505 dbus_g_object_class_install_info (GObjectClass *object_class,
506 const DBusGObjectInfo *info)
508 g_return_if_fail (G_IS_OBJECT_CLASS (object_class));
510 g_static_mutex_lock (&info_hash_mutex);
512 if (info_hash == NULL)
514 info_hash = g_hash_table_new (NULL, NULL); /* direct hash */
517 g_hash_table_replace (info_hash, object_class, (void*) info);
519 g_static_mutex_unlock (&info_hash_mutex);
523 * Registers a GObject at the given path. Properties, methods, and signals
524 * of the object can then be accessed remotely. Methods are only available
525 * if method introspection data has been added to the object's class
526 * with g_object_class_install_info().
528 * The registration will be cancelled if either the DBusConnection or
529 * the GObject gets finalized.
531 * @param connection the D-BUS connection
532 * @param at_path the path where the object will live (the object's name)
533 * @param object the object
536 dbus_connection_register_g_object (DBusConnection *connection,
542 g_return_if_fail (connection != NULL);
543 g_return_if_fail (at_path != NULL);
544 g_return_if_fail (G_IS_OBJECT (object));
546 split = _dbus_gutils_split_path (at_path);
548 if (!dbus_connection_register_object_path (connection,
549 (const char**) split,
550 &gobject_dbus_vtable,
552 g_error ("Failed to register GObject with DBusConnection");
556 /* FIXME set up memory management (so we break the
557 * registration if object or connection vanishes)
561 /** @} */ /* end of public API */
563 #ifdef DBUS_BUILD_TESTS
567 * @ingroup DBusGLibInternals
568 * Unit test for GLib GObject integration ("skeletons")
569 * @returns #TRUE on success.
572 _dbus_gobject_test (const char *test_data_dir)
575 static struct { const char *wincaps; const char *uscore; } name_pairs[] = {
576 { "SetFoo", "set_foo" },
578 { "GetFooBar", "get_foo_bar" },
581 /* Impossible-to-handle cases */
582 /* { "FrobateUIHandler", "frobate_ui_handler" } */
586 while (i < (int) G_N_ELEMENTS (name_pairs))
591 uscore = wincaps_to_uscore (name_pairs[i].wincaps);
592 wincaps = uscore_to_wincaps (name_pairs[i].uscore);
594 if (strcmp (uscore, name_pairs[i].uscore) != 0)
596 g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
597 name_pairs[i].wincaps, name_pairs[i].uscore,
602 if (strcmp (wincaps, name_pairs[i].wincaps) != 0)
604 g_printerr ("\"%s\" should have been converted to \"%s\" not \"%s\"\n",
605 name_pairs[i].uscore, name_pairs[i].wincaps,
619 #endif /* DBUS_BUILD_TESTS */