Add some more cases to the app-id unit tests
[glib.git] / gio / tests / gdbus-example-objectmanager-client.c
blobb235ca140d36a3a26c4252a0fa4289317a0e4a44
2 #include "gdbus-object-manager-example/gdbus-example-objectmanager-generated.h"
4 /* ---------------------------------------------------------------------------------------------------- */
6 static void
7 print_objects (GDBusObjectManager *manager)
9 GList *objects;
10 GList *l;
12 g_print ("Object manager at %s\n", g_dbus_object_manager_get_object_path (manager));
13 objects = g_dbus_object_manager_get_objects (manager);
14 for (l = objects; l != NULL; l = l->next)
16 ExampleObject *object = EXAMPLE_OBJECT (l->data);
17 GList *interfaces;
18 GList *ll;
19 g_print (" - Object at %s\n", g_dbus_object_get_object_path (G_DBUS_OBJECT (object)));
21 interfaces = g_dbus_object_get_interfaces (G_DBUS_OBJECT (object));
22 for (ll = interfaces; ll != NULL; ll = ll->next)
24 GDBusInterface *interface = G_DBUS_INTERFACE (ll->data);
25 g_print (" - Interface %s\n", g_dbus_interface_get_info (interface)->name);
27 /* Note that @interface is really a GDBusProxy instance - and additionally also
28 * an ExampleAnimal or ExampleCat instance - either of these can be used to
29 * invoke methods on the remote object. For example, the generated function
31 * void example_animal_call_poke_sync (ExampleAnimal *proxy,
32 * gboolean make_sad,
33 * gboolean make_happy,
34 * GCancellable *cancellable,
35 * GError **error);
37 * can be used to call the Poke() D-Bus method on the .Animal interface.
38 * Additionally, the generated function
40 * const gchar *example_animal_get_mood (ExampleAnimal *object);
42 * can be used to get the value of the :Mood property.
45 g_list_free_full (interfaces, g_object_unref);
47 g_list_free_full (objects, g_object_unref);
50 static void
51 on_object_added (GDBusObjectManager *manager,
52 GDBusObject *object,
53 gpointer user_data)
55 gchar *owner;
56 owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (manager));
57 g_print ("Added object at %s (owner %s)\n", g_dbus_object_get_object_path (object), owner);
58 g_free (owner);
61 static void
62 on_object_removed (GDBusObjectManager *manager,
63 GDBusObject *object,
64 gpointer user_data)
66 gchar *owner;
67 owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (manager));
68 g_print ("Removed object at %s (owner %s)\n", g_dbus_object_get_object_path (object), owner);
69 g_free (owner);
72 static void
73 on_notify_name_owner (GObject *object,
74 GParamSpec *pspec,
75 gpointer user_data)
77 GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (object);
78 gchar *name_owner;
80 name_owner = g_dbus_object_manager_client_get_name_owner (manager);
81 g_print ("name-owner: %s\n", name_owner);
82 g_free (name_owner);
85 static void
86 on_interface_proxy_properties_changed (GDBusObjectManagerClient *manager,
87 GDBusObjectProxy *object_proxy,
88 GDBusProxy *interface_proxy,
89 GVariant *changed_properties,
90 const gchar *const *invalidated_properties,
91 gpointer user_data)
93 GVariantIter iter;
94 const gchar *key;
95 GVariant *value;
96 gchar *s;
98 g_print ("Properties Changed on %s:\n", g_dbus_object_get_object_path (G_DBUS_OBJECT (object_proxy)));
99 g_variant_iter_init (&iter, changed_properties);
100 while (g_variant_iter_next (&iter, "{&sv}", &key, &value))
102 s = g_variant_print (value, TRUE);
103 g_print (" %s -> %s\n", key, s);
104 g_variant_unref (value);
105 g_free (s);
109 gint
110 main (gint argc, gchar *argv[])
112 GDBusObjectManager *manager;
113 GMainLoop *loop;
114 GError *error;
115 gchar *name_owner;
117 manager = NULL;
118 loop = NULL;
120 loop = g_main_loop_new (NULL, FALSE);
122 error = NULL;
123 manager = example_object_manager_client_new_for_bus_sync (G_BUS_TYPE_SESSION,
124 G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE,
125 "org.gtk.GDBus.Examples.ObjectManager",
126 "/example/Animals",
127 NULL, /* GCancellable */
128 &error);
129 if (manager == NULL)
131 g_printerr ("Error getting object manager client: %s", error->message);
132 g_error_free (error);
133 goto out;
136 name_owner = g_dbus_object_manager_client_get_name_owner (G_DBUS_OBJECT_MANAGER_CLIENT (manager));
137 g_print ("name-owner: %s\n", name_owner);
138 g_free (name_owner);
140 print_objects (manager);
142 g_signal_connect (manager,
143 "notify::name-owner",
144 G_CALLBACK (on_notify_name_owner),
145 NULL);
146 g_signal_connect (manager,
147 "object-added",
148 G_CALLBACK (on_object_added),
149 NULL);
150 g_signal_connect (manager,
151 "object-removed",
152 G_CALLBACK (on_object_removed),
153 NULL);
154 g_signal_connect (manager,
155 "interface-proxy-properties-changed",
156 G_CALLBACK (on_interface_proxy_properties_changed),
157 NULL);
159 g_main_loop_run (loop);
161 out:
162 if (manager != NULL)
163 g_object_unref (manager);
164 if (loop != NULL)
165 g_main_loop_unref (loop);
167 return 0;