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 */
78 /* ---------------------------------------------------------------------------------------------------- */
81 block_method_call (GDBusConnection *connection,
83 const gchar *object_path,
84 const gchar *interface_name,
85 const gchar *method_name,
87 GDBusMethodInvocation *invocation,
90 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.Example.Block");
92 if (g_strcmp0 (method_name, "Hello") == 0)
94 const gchar *greeting;
97 g_variant_get (parameters, "(&s)", &greeting);
99 response = g_strdup_printf ("Method %s.%s with user_data `%s' on object path %s called with arg '%s'",
102 (const gchar *) user_data,
105 g_dbus_method_invocation_return_value (invocation,
106 g_variant_new ("(s)", response));
109 else if (g_strcmp0 (method_name, "DoStuff") == 0)
111 g_dbus_method_invocation_return_dbus_error (invocation,
112 "org.gtk.GDBus.TestSubtree.Error.Failed",
113 "This method intentionally always fails");
117 g_assert_not_reached ();
122 block_get_property (GDBusConnection *connection,
124 const gchar *object_path,
125 const gchar *interface_name,
126 const gchar *property_name,
135 node = strrchr (object_path, '/') + 1;
136 if (g_str_has_prefix (node, "sda"))
140 if (strlen (node) == 4)
141 minor = node[3] - '0';
146 if (g_strcmp0 (property_name, "Major") == 0)
148 ret = g_variant_new_int32 (major);
150 else if (g_strcmp0 (property_name, "Minor") == 0)
152 ret = g_variant_new_int32 (minor);
154 else if (g_strcmp0 (property_name, "Notes") == 0)
159 "Hello %s. I thought I said reading this property "
160 "always results in an error. kthxbye",
165 g_assert_not_reached ();
172 block_set_property (GDBusConnection *connection,
174 const gchar *object_path,
175 const gchar *interface_name,
176 const gchar *property_name,
182 g_assert_not_reached ();
185 const GDBusInterfaceVTable block_vtable =
192 /* ---------------------------------------------------------------------------------------------------- */
195 partition_method_call (GDBusConnection *connection,
197 const gchar *object_path,
198 const gchar *interface_name,
199 const gchar *method_name,
200 GVariant *parameters,
201 GDBusMethodInvocation *invocation,
204 const gchar *greeting;
207 g_assert_cmpstr (interface_name, ==, "org.gtk.GDBus.Example.Partition");
208 g_assert_cmpstr (method_name, ==, "Hello");
210 g_variant_get (parameters, "(&s)", &greeting);
212 response = g_strdup_printf ("Method %s.%s with user_data `%s' on object path %s called with arg '%s'",
215 (const gchar *) user_data,
218 g_dbus_method_invocation_return_value (invocation,
219 g_variant_new ("(s)", response));
223 const GDBusInterfaceVTable partition_vtable =
225 partition_method_call,
226 //partition_get_property,
227 //partition_set_property
230 /* ---------------------------------------------------------------------------------------------------- */
233 subtree_enumerate (GDBusConnection *connection,
235 const gchar *object_path,
241 p = g_ptr_array_new ();
242 g_ptr_array_add (p, g_strdup ("sda"));
243 g_ptr_array_add (p, g_strdup ("sda1"));
244 g_ptr_array_add (p, g_strdup ("sda2"));
245 g_ptr_array_add (p, g_strdup ("sda3"));
246 g_ptr_array_add (p, g_strdup ("sdb"));
247 g_ptr_array_add (p, g_strdup ("sdb1"));
248 g_ptr_array_add (p, g_strdup ("sdc"));
249 g_ptr_array_add (p, g_strdup ("sdc1"));
250 g_ptr_array_add (p, NULL);
251 nodes = (gchar **) g_ptr_array_free (p, FALSE);
256 static GDBusInterfaceInfo **
257 subtree_introspect (GDBusConnection *connection,
259 const gchar *object_path,
265 p = g_ptr_array_new ();
268 g_ptr_array_add (p, g_dbus_interface_info_ref (manager_interface_info));
272 g_ptr_array_add (p, g_dbus_interface_info_ref (block_interface_info));
273 if (strlen (node) == 4)
275 g_dbus_interface_info_ref (partition_interface_info));
278 g_ptr_array_add (p, NULL);
280 return (GDBusInterfaceInfo **) g_ptr_array_free (p, FALSE);
283 static const GDBusInterfaceVTable *
284 subtree_dispatch (GDBusConnection *connection,
286 const gchar *object_path,
287 const gchar *interface_name,
289 gpointer *out_user_data,
292 const GDBusInterfaceVTable *vtable_to_return;
293 gpointer user_data_to_return;
295 if (g_strcmp0 (interface_name, "org.gtk.GDBus.Example.Manager") == 0)
297 user_data_to_return = "The Root";
298 vtable_to_return = &manager_vtable;
302 if (strlen (node) == 4)
303 user_data_to_return = "A partition";
305 user_data_to_return = "A block device";
307 if (g_strcmp0 (interface_name, "org.gtk.GDBus.Example.Block") == 0)
308 vtable_to_return = &block_vtable;
309 else if (g_strcmp0 (interface_name, "org.gtk.GDBus.Example.Partition") == 0)
310 vtable_to_return = &partition_vtable;
312 g_assert_not_reached ();
315 *out_user_data = user_data_to_return;
317 return vtable_to_return;
320 const GDBusSubtreeVTable subtree_vtable =
327 /* ---------------------------------------------------------------------------------------------------- */
330 on_bus_acquired (GDBusConnection *connection,
334 guint registration_id;
336 registration_id = g_dbus_connection_register_subtree (connection,
337 "/org/gtk/GDBus/TestSubtree/Devices",
339 G_DBUS_SUBTREE_FLAGS_NONE,
340 NULL, /* user_data */
341 NULL, /* user_data_free_func */
342 NULL); /* GError** */
343 g_assert (registration_id > 0);
347 on_name_acquired (GDBusConnection *connection,
354 on_name_lost (GDBusConnection *connection,
362 main (int argc, char *argv[])
369 /* We are lazy here - we don't want to manually provide
370 * the introspection data structures - so we just build
373 introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
374 g_assert (introspection_data != NULL);
376 manager_interface_info = g_dbus_node_info_lookup_interface (introspection_data, "org.gtk.GDBus.Example.Manager");
377 block_interface_info = g_dbus_node_info_lookup_interface (introspection_data, "org.gtk.GDBus.Example.Block");
378 partition_interface_info = g_dbus_node_info_lookup_interface (introspection_data, "org.gtk.GDBus.Example.Partition");
379 g_assert (manager_interface_info != NULL);
380 g_assert (block_interface_info != NULL);
381 g_assert (partition_interface_info != NULL);
383 owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
384 "org.gtk.GDBus.TestSubtree",
385 G_BUS_NAME_OWNER_FLAGS_NONE,
392 loop = g_main_loop_new (NULL, FALSE);
393 g_main_loop_run (loop);
395 g_bus_unown_name (owner_id);
397 g_dbus_node_info_unref (introspection_data);