5 /* ---------------------------------------------------------------------------------------------------- */
7 static GDBusNodeInfo *introspection_data = NULL;
8 static GDBusInterfaceInfo *manager_interface_info = NULL;
9 static GDBusInterfaceInfo *block_interface_info = NULL;
10 static GDBusInterfaceInfo *partition_interface_info = NULL;
12 /* Introspection data for the service we are exporting */
13 static const gchar introspection_xml[] =
15 " <interface name='org.gtk.GDBus.Example.Manager'>"
16 " <method name='Hello'>"
17 " <arg type='s' name='greeting' direction='in'/>"
18 " <arg type='s' name='response' direction='out'/>"
21 " <interface name='org.gtk.GDBus.Example.Block'>"
22 " <method name='Hello'>"
23 " <arg type='s' name='greeting' direction='in'/>"
24 " <arg type='s' name='response' direction='out'/>"
26 " <property type='i' name='Major' access='read'/>"
27 " <property type='i' name='Minor' access='read'/>"
28 " <property type='s' name='Notes' access='readwrite'/>"
30 " <interface name='org.gtk.GDBus.Example.Partition'>"
31 " <method name='Hello'>"
32 " <arg type='s' name='greeting' direction='in'/>"
33 " <arg type='s' name='response' direction='out'/>"
35 " <property type='i' name='PartitionNumber' access='read'/>"
36 " <property type='s' name='Notes' access='readwrite'/>"
40 /* ---------------------------------------------------------------------------------------------------- */
43 manager_method_call (GDBusConnection *connection,
45 const gchar *object_path,
46 const gchar *interface_name,
47 const gchar *method_name,
49 GDBusMethodInvocation *invocation,
52 const gchar *greeting;
55 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.Example.Manager");
56 g_assert_cmpstr (method_name, ==, "Hello");
58 g_variant_get (parameters, "(&s)", &greeting);
60 response = g_strdup_printf ("Method %s.%s with user_data '%s' on object path %s called with arg '%s'",
63 (const gchar *) user_data,
66 g_dbus_method_invocation_return_value (invocation,
67 g_variant_new ("(s)", response));
71 const GDBusInterfaceVTable manager_vtable =
74 NULL, /* get_property */
75 NULL, /* set_property */
79 /* ---------------------------------------------------------------------------------------------------- */
82 block_method_call (GDBusConnection *connection,
84 const gchar *object_path,
85 const gchar *interface_name,
86 const gchar *method_name,
88 GDBusMethodInvocation *invocation,
91 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.Example.Block");
93 if (g_strcmp0 (method_name, "Hello") == 0)
95 const gchar *greeting;
98 g_variant_get (parameters, "(&s)", &greeting);
100 response = g_strdup_printf ("Method %s.%s with user_data '%s' on object path %s called with arg '%s'",
103 (const gchar *) user_data,
106 g_dbus_method_invocation_return_value (invocation,
107 g_variant_new ("(s)", response));
110 else if (g_strcmp0 (method_name, "DoStuff") == 0)
112 g_dbus_method_invocation_return_dbus_error (invocation,
113 "org.gtk.GDBus.TestSubtree.Error.Failed",
114 "This method intentionally always fails");
118 g_assert_not_reached ();
123 block_get_property (GDBusConnection *connection,
125 const gchar *object_path,
126 const gchar *interface_name,
127 const gchar *property_name,
136 node = strrchr (object_path, '/') + 1;
137 if (g_str_has_prefix (node, "sda"))
141 if (strlen (node) == 4)
142 minor = node[3] - '0';
147 if (g_strcmp0 (property_name, "Major") == 0)
149 ret = g_variant_new_int32 (major);
151 else if (g_strcmp0 (property_name, "Minor") == 0)
153 ret = g_variant_new_int32 (minor);
155 else if (g_strcmp0 (property_name, "Notes") == 0)
160 "Hello %s. I thought I said reading this property "
161 "always results in an error. kthxbye",
166 g_assert_not_reached ();
173 block_set_property (GDBusConnection *connection,
175 const gchar *object_path,
176 const gchar *interface_name,
177 const gchar *property_name,
183 g_assert_not_reached ();
186 const GDBusInterfaceVTable block_vtable =
194 /* ---------------------------------------------------------------------------------------------------- */
197 partition_method_call (GDBusConnection *connection,
199 const gchar *object_path,
200 const gchar *interface_name,
201 const gchar *method_name,
202 GVariant *parameters,
203 GDBusMethodInvocation *invocation,
206 const gchar *greeting;
209 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.Example.Partition");
210 g_assert_cmpstr (method_name, ==, "Hello");
212 g_variant_get (parameters, "(&s)", &greeting);
214 response = g_strdup_printf ("Method %s.%s with user_data '%s' on object path %s called with arg '%s'",
217 (const gchar *) user_data,
220 g_dbus_method_invocation_return_value (invocation,
221 g_variant_new ("(s)", response));
225 const GDBusInterfaceVTable partition_vtable =
227 partition_method_call,
233 /* ---------------------------------------------------------------------------------------------------- */
236 subtree_enumerate (GDBusConnection *connection,
238 const gchar *object_path,
244 p = g_ptr_array_new ();
245 g_ptr_array_add (p, g_strdup ("sda"));
246 g_ptr_array_add (p, g_strdup ("sda1"));
247 g_ptr_array_add (p, g_strdup ("sda2"));
248 g_ptr_array_add (p, g_strdup ("sda3"));
249 g_ptr_array_add (p, g_strdup ("sdb"));
250 g_ptr_array_add (p, g_strdup ("sdb1"));
251 g_ptr_array_add (p, g_strdup ("sdc"));
252 g_ptr_array_add (p, g_strdup ("sdc1"));
253 g_ptr_array_add (p, NULL);
254 nodes = (gchar **) g_ptr_array_free (p, FALSE);
259 static GDBusInterfaceInfo **
260 subtree_introspect (GDBusConnection *connection,
262 const gchar *object_path,
268 p = g_ptr_array_new ();
271 g_ptr_array_add (p, g_dbus_interface_info_ref (manager_interface_info));
275 g_ptr_array_add (p, g_dbus_interface_info_ref (block_interface_info));
276 if (strlen (node) == 4)
278 g_dbus_interface_info_ref (partition_interface_info));
281 g_ptr_array_add (p, NULL);
283 return (GDBusInterfaceInfo **) g_ptr_array_free (p, FALSE);
286 static const GDBusInterfaceVTable *
287 subtree_dispatch (GDBusConnection *connection,
289 const gchar *object_path,
290 const gchar *interface_name,
292 gpointer *out_user_data,
295 const GDBusInterfaceVTable *vtable_to_return;
296 gpointer user_data_to_return;
298 if (g_strcmp0 (interface_name, "org.gtk.GDBus.Example.Manager") == 0)
300 user_data_to_return = "The Root";
301 vtable_to_return = &manager_vtable;
305 if (strlen (node) == 4)
306 user_data_to_return = "A partition";
308 user_data_to_return = "A block device";
310 if (g_strcmp0 (interface_name, "org.gtk.GDBus.Example.Block") == 0)
311 vtable_to_return = &block_vtable;
312 else if (g_strcmp0 (interface_name, "org.gtk.GDBus.Example.Partition") == 0)
313 vtable_to_return = &partition_vtable;
315 g_assert_not_reached ();
318 *out_user_data = user_data_to_return;
320 return vtable_to_return;
323 const GDBusSubtreeVTable subtree_vtable =
331 /* ---------------------------------------------------------------------------------------------------- */
334 on_bus_acquired (GDBusConnection *connection,
338 guint registration_id;
340 registration_id = g_dbus_connection_register_subtree (connection,
341 "/org/gtk/GDBus/TestSubtree/Devices",
343 G_DBUS_SUBTREE_FLAGS_NONE,
344 NULL, /* user_data */
345 NULL, /* user_data_free_func */
346 NULL); /* GError** */
347 g_assert (registration_id > 0);
351 on_name_acquired (GDBusConnection *connection,
358 on_name_lost (GDBusConnection *connection,
366 main (int argc, char *argv[])
371 /* We are lazy here - we don't want to manually provide
372 * the introspection data structures - so we just build
375 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
376 g_assert (introspection_data != NULL);
378 manager_interface_info = g_dbus_node_info_lookup_interface (introspection_data, "org.gtk.GDBus.Example.Manager");
379 block_interface_info = g_dbus_node_info_lookup_interface (introspection_data, "org.gtk.GDBus.Example.Block");
380 partition_interface_info = g_dbus_node_info_lookup_interface (introspection_data, "org.gtk.GDBus.Example.Partition");
381 g_assert (manager_interface_info != NULL);
382 g_assert (block_interface_info != NULL);
383 g_assert (partition_interface_info != NULL);
385 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
386 "org.gtk.GDBus.TestSubtree",
387 G_BUS_NAME_OWNER_FLAGS_NONE,
394 loop = g_main_loop_new (NULL, FALSE);
395 g_main_loop_run (loop);
397 g_bus_unown_name (owner_id);
399 g_dbus_node_info_unref (introspection_data);