[kdbus] sync with kdbus (kdbus.h - commit: b024fb43c66b)
[platform/upstream/glib.git] / gio / tests / gdbus-example-objectmanager-client.c
1
2 #include "gdbus-object-manager-example/gdbus-example-objectmanager-generated.h"
3
4 /* ---------------------------------------------------------------------------------------------------- */
5
6 static void
7 print_objects (GDBusObjectManager *manager)
8 {
9   GList *objects;
10   GList *l;
11
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)
15     {
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)));
20
21       interfaces = g_dbus_object_get_interfaces (G_DBUS_OBJECT (object));
22       for (ll = interfaces; ll != NULL; ll = ll->next)
23         {
24           GDBusInterface *interface = G_DBUS_INTERFACE (ll->data);
25           g_print ("   - Interface %s\n", g_dbus_interface_get_info (interface)->name);
26
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
30            *
31            *  void example_animal_call_poke_sync (ExampleAnimal  *proxy,
32            *                                      gboolean        make_sad,
33            *                                      gboolean        make_happy,
34            *                                      GCancellable   *cancellable,
35            *                                      GError        **error);
36            *
37            * can be used to call the Poke() D-Bus method on the .Animal interface.
38            * Additionally, the generated function
39            *
40            *  const gchar *example_animal_get_mood (ExampleAnimal *object);
41            *
42            * can be used to get the value of the :Mood property.
43            */
44         }
45       g_list_free_full (interfaces, g_object_unref);
46     }
47   g_list_free_full (objects, g_object_unref);
48 }
49
50 static void
51 on_object_added (GDBusObjectManager *manager,
52                  GDBusObject        *object,
53                  gpointer            user_data)
54 {
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);
59 }
60
61 static void
62 on_object_removed (GDBusObjectManager *manager,
63                    GDBusObject        *object,
64                    gpointer            user_data)
65 {
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);
70 }
71
72 static void
73 on_notify_name_owner (GObject    *object,
74                       GParamSpec *pspec,
75                       gpointer    user_data)
76 {
77   GDBusObjectManagerClient *manager = G_DBUS_OBJECT_MANAGER_CLIENT (object);
78   gchar *name_owner;
79
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);
83 }
84
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)
92 {
93   GVariantIter iter;
94   const gchar *key;
95   GVariant *value;
96   gchar *s;
97
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))
101     {
102       s = g_variant_print (value, TRUE);
103       g_print ("  %s -> %s\n", key, s);
104       g_variant_unref (value);
105       g_free (s);
106     }
107 }
108
109 gint
110 main (gint argc, gchar *argv[])
111 {
112   GDBusObjectManager *manager;
113   GMainLoop *loop;
114   GError *error;
115   gchar *name_owner;
116
117   manager = NULL;
118   loop = NULL;
119
120   loop = g_main_loop_new (NULL, FALSE);
121
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)
130     {
131       g_printerr ("Error getting object manager client: %s", error->message);
132       g_error_free (error);
133       goto out;
134     }
135
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);
139
140   print_objects (manager);
141
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);
158
159   g_main_loop_run (loop);
160
161  out:
162   if (manager != NULL)
163     g_object_unref (manager);
164   if (loop != NULL)
165     g_main_loop_unref (loop);
166
167   return 0;
168 }