Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-example-objectmanager-server.c
blob2a7bf63b623e4836151316319e37330cac5cc922
2 #include "gdbus-object-manager-example/gdbus-example-objectmanager-generated.h"
4 /* ---------------------------------------------------------------------------------------------------- */
6 static GDBusObjectManagerServer *manager = NULL;
8 static gboolean
9 on_animal_poke (ExampleAnimal *animal,
10 GDBusMethodInvocation *invocation,
11 gboolean make_sad,
12 gboolean make_happy,
13 gpointer user_data)
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");
20 goto out;
23 if (make_sad)
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");
30 goto out;
33 example_animal_set_mood (animal, "Sad");
34 example_animal_complete_poke (animal, invocation);
35 goto out;
38 if (make_happy)
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");
45 goto out;
48 example_animal_set_mood (animal, "Happy");
49 example_animal_complete_poke (animal, invocation);
50 goto out;
53 g_assert_not_reached ();
55 out:
56 return TRUE; /* to indicate that the method was handled */
60 static void
61 on_bus_acquired (GDBusConnection *connection,
62 const gchar *name,
63 gpointer user_data)
65 ExampleObjectSkeleton *object;
66 guint n;
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++)
75 gchar *s;
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);
81 g_free (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
96 if (n % 2 == 1)
98 ExampleCat *cat;
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,
106 "handle-poke",
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);
119 static void
120 on_name_acquired (GDBusConnection *connection,
121 const gchar *name,
122 gpointer user_data)
124 g_print ("Acquired the name %s\n", name);
127 static void
128 on_name_lost (GDBusConnection *connection,
129 const gchar *name,
130 gpointer user_data)
132 g_print ("Lost the name %s\n", name);
136 gint
137 main (gint argc, gchar *argv[])
139 GMainLoop *loop;
140 guint id;
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,
148 on_bus_acquired,
149 on_name_acquired,
150 on_name_lost,
151 loop,
152 NULL);
154 g_main_loop_run (loop);
156 g_bus_unown_name (id);
157 g_main_loop_unref (loop);
159 return 0;