| 1 |
|
| 2 |
#include "gdbus-object-manager-example/objectmanager-gen.h" |
| 3 |
|
| 4 |
/* ---------------------------------------------------------------------------------------------------- */ |
| 5 |
|
| 6 |
static GDBusObjectManagerServer *manager = NULL; |
| 7 |
|
| 8 |
static gboolean |
| 9 |
on_animal_poke (ExampleAnimal *animal, |
| 10 |
GDBusMethodInvocation *invocation, |
| 11 |
gboolean make_sad, |
| 12 |
gboolean make_happy, |
| 13 |
gpointer user_data) |
| 14 |
{ |
| 15 |
if ((make_sad && make_happy) || (!make_sad && !make_happy)) |
| 16 |
{ |
| 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; |
| 21 |
} |
| 22 |
|
| 23 |
if (make_sad) |
| 24 |
{ |
| 25 |
if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0) |
| 26 |
{ |
| 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; |
| 31 |
} |
| 32 |
|
| 33 |
example_animal_set_mood (animal, "Sad"); |
| 34 |
example_animal_complete_poke (animal, invocation); |
| 35 |
goto out; |
| 36 |
} |
| 37 |
|
| 38 |
if (make_happy) |
| 39 |
{ |
| 40 |
if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0) |
| 41 |
{ |
| 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; |
| 46 |
} |
| 47 |
|
| 48 |
example_animal_set_mood (animal, "Happy"); |
| 49 |
example_animal_complete_poke (animal, invocation); |
| 50 |
goto out; |
| 51 |
} |
| 52 |
|
| 53 |
g_assert_not_reached (); |
| 54 |
|
| 55 |
out: |
| 56 |
return G_DBUS_METHOD_INVOCATION_HANDLED; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
static void |
| 61 |
on_bus_acquired (GDBusConnection *connection, |
| 62 |
const gchar *name, |
| 63 |
gpointer user_data) |
| 64 |
{ |
| 65 |
ExampleObjectSkeleton *object; |
| 66 |
guint n; |
| 67 |
|
| 68 |
g_print ("Acquired a message bus connection\n"); |
| 69 |
|
| 70 |
/* Create a new org.freedesktop.DBus.ObjectManager rooted at /example/Animals */ |
| 71 |
manager = g_dbus_object_manager_server_new ("/example/Animals"); |
| 72 |
|
| 73 |
for (n = 0; n < 10; n++) |
| 74 |
{ |
| 75 |
gchar *s; |
| 76 |
ExampleAnimal *animal; |
| 77 |
|
| 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); |
| 82 |
|
| 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). |
| 86 |
*/ |
| 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); |
| 91 |
|
| 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 |
| 95 |
*/ |
| 96 |
if (n % 2 == 1) |
| 97 |
{ |
| 98 |
ExampleCat *cat; |
| 99 |
cat = example_cat_skeleton_new (); |
| 100 |
example_object_skeleton_set_cat (object, cat); |
| 101 |
g_object_unref (cat); |
| 102 |
} |
| 103 |
|
| 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 */ |
| 109 |
|
| 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); |
| 113 |
} |
| 114 |
|
| 115 |
/* Export all objects */ |
| 116 |
g_dbus_object_manager_server_set_connection (manager, connection); |
| 117 |
} |
| 118 |
|
| 119 |
static void |
| 120 |
on_name_acquired (GDBusConnection *connection, |
| 121 |
const gchar *name, |
| 122 |
gpointer user_data) |
| 123 |
{ |
| 124 |
g_print ("Acquired the name %s\n", name); |
| 125 |
} |
| 126 |
|
| 127 |
static void |
| 128 |
on_name_lost (GDBusConnection *connection, |
| 129 |
const gchar *name, |
| 130 |
gpointer user_data) |
| 131 |
{ |
| 132 |
g_print ("Lost the name %s\n", name); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
gint |
| 137 |
main (gint argc, gchar *argv[]) |
| 138 |
{ |
| 139 |
GMainLoop *loop; |
| 140 |
guint id; |
| 141 |
|
| 142 |
loop = g_main_loop_new (NULL, FALSE); |
| 143 |
|
| 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); |
| 153 |
|
| 154 |
g_main_loop_run (loop); |
| 155 |
|
| 156 |
g_bus_unown_name (id); |
| 157 |
g_main_loop_unref (loop); |
| 158 |
|
| 159 |
return 0; |
| 160 |
} |