2 #include "gdbus-object-manager-example/gdbus-example-objectmanager-generated.h"
4 /* ---------------------------------------------------------------------------------------------------- */
6 static GDBusObjectManagerServer
*manager
= NULL
;
9 on_animal_poke (ExampleAnimal
*animal
,
10 GDBusMethodInvocation
*invocation
,
15 if ((make_sad
&& make_happy
) || (!make_sad
&& !make_happy
))
17 g_dbus_method_invocation_return_dbus_error (invocation
,
18 "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
19 "Exactly one of make_sad or make_happy must be TRUE");
25 if (g_strcmp0 (example_animal_get_mood (animal
), "Sad") == 0)
27 g_dbus_method_invocation_return_dbus_error (invocation
,
28 "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
29 "Sad animal is already sad");
33 example_animal_set_mood (animal
, "Sad");
34 example_animal_complete_poke (animal
, invocation
);
40 if (g_strcmp0 (example_animal_get_mood (animal
), "Happy") == 0)
42 g_dbus_method_invocation_return_dbus_error (invocation
,
43 "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
44 "Happy animal is already happy");
48 example_animal_set_mood (animal
, "Happy");
49 example_animal_complete_poke (animal
, invocation
);
53 g_assert_not_reached ();
56 return TRUE
; /* to indicate that the method was handled */
61 on_bus_acquired (GDBusConnection
*connection
,
65 ExampleObjectSkeleton
*object
;
68 g_print ("Acquired a message bus connection\n");
70 /* Create a new org.freedesktop.DBus.ObjectManager rooted at /example/Animals */
71 manager
= g_dbus_object_manager_server_new ("/example/Animals");
73 for (n
= 0; n
< 10; n
++)
76 ExampleAnimal
*animal
;
78 /* Create a new D-Bus object at the path /example/Animals/N where N is 000..009 */
79 s
= g_strdup_printf ("/example/Animals/%03d", n
);
80 object
= example_object_skeleton_new (s
);
83 /* Make the newly created object export the interface
84 * org.gtk.GDBus.Example.ObjectManager.Animal (note
85 * that @object takes its own reference to @animal).
87 animal
= example_animal_skeleton_new ();
88 example_animal_set_mood (animal
, "Happy");
89 example_object_skeleton_set_animal (object
, animal
);
90 g_object_unref (animal
);
92 /* Cats are odd animals - so some of our objects implement the
93 * org.gtk.GDBus.Example.ObjectManager.Cat interface in addition
94 * to the .Animal interface
99 cat
= example_cat_skeleton_new ();
100 example_object_skeleton_set_cat (object
, cat
);
101 g_object_unref (cat
);
104 /* Handle Poke() D-Bus method invocations on the .Animal interface */
105 g_signal_connect (animal
,
107 G_CALLBACK (on_animal_poke
),
108 NULL
); /* user_data */
110 /* Export the object (@manager takes its own reference to @object) */
111 g_dbus_object_manager_server_export (manager
, G_DBUS_OBJECT_SKELETON (object
));
112 g_object_unref (object
);
115 /* Export all objects */
116 g_dbus_object_manager_server_set_connection (manager
, connection
);
120 on_name_acquired (GDBusConnection
*connection
,
124 g_print ("Acquired the name %s\n", name
);
128 on_name_lost (GDBusConnection
*connection
,
132 g_print ("Lost the name %s\n", name
);
137 main (gint argc
, gchar
*argv
[])
142 loop
= g_main_loop_new (NULL
, FALSE
);
144 id
= g_bus_own_name (G_BUS_TYPE_SESSION
,
145 "org.gtk.GDBus.Examples.ObjectManager",
146 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT
|
147 G_BUS_NAME_OWNER_FLAGS_REPLACE
,
154 g_main_loop_run (loop
);
156 g_bus_unown_name (id
);
157 g_main_loop_unref (loop
);