GDBus: Use GVariant instead of GHashTable for GDBusProxy::g-properties-changed
[platform/upstream/glib.git] / gio / tests / gdbus-example-watch-proxy.c
1 /*
2  * Copyright © 2010 Red Hat, Inc.
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; either version 2 of the licence or (at
7  * your option) any later version.
8  *
9  * See the included COPYING file for more information.
10  *
11  * Author: David Zeuthen <davidz@redhat.com>
12  */
13
14 #include <gio/gio.h>
15
16 static gchar *opt_name         = NULL;
17 static gchar *opt_object_path  = NULL;
18 static gchar *opt_interface    = NULL;
19 static gboolean opt_system_bus = FALSE;
20 static gboolean opt_auto_start = FALSE;
21 static gboolean opt_no_properties = FALSE;
22
23 static GOptionEntry opt_entries[] =
24 {
25   { "name", 'n', 0, G_OPTION_ARG_STRING, &opt_name, "Name of the remote object to watch", NULL },
26   { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_object_path, "Object path of the remote object", NULL },
27   { "interface", 'i', 0, G_OPTION_ARG_STRING, &opt_interface, "D-Bus interface of remote object", NULL },
28   { "system-bus", 's', 0, G_OPTION_ARG_NONE, &opt_system_bus, "Use the system-bus instead of the session-bus", NULL },
29   { "auto-start", 'a', 0, G_OPTION_ARG_NONE, &opt_auto_start, "Instruct the bus to launch an owner for the name", NULL},
30   { "no-properties", 'p', 0, G_OPTION_ARG_NONE, &opt_no_properties, "Do not load properties", NULL},
31   { NULL}
32 };
33
34 static void
35 print_properties (GDBusProxy *proxy)
36 {
37   gchar **property_names;
38   guint n;
39
40   g_print ("    properties:\n");
41
42   property_names = g_dbus_proxy_get_cached_property_names (proxy, NULL);
43   for (n = 0; property_names != NULL && property_names[n] != NULL; n++)
44     {
45       const gchar *key = property_names[n];
46       GVariant *value;
47       gchar *value_str;
48       value = g_dbus_proxy_get_cached_property (proxy, key, NULL);
49       value_str = g_variant_print (value, TRUE);
50       g_print ("      %s -> %s\n", key, value_str);
51       g_variant_unref (value);
52       g_free (value_str);
53     }
54   g_strfreev (property_names);
55 }
56
57 static void
58 on_properties_changed (GDBusProxy *proxy,
59                        GVariant   *changed_properties,
60                        gpointer    user_data)
61 {
62   GVariantIter *iter;
63   GVariant *item;
64
65   g_print (" *** Properties Changed:\n");
66
67   g_variant_get (changed_properties,
68                  "a{sv}",
69                  &iter);
70   while ((item = g_variant_iter_next_value (iter)))
71     {
72       const gchar *key;
73       GVariant *value;
74       gchar *value_str;
75
76       g_variant_get (item,
77                      "{sv}",
78                      &key,
79                      &value);
80
81       value_str = g_variant_print (value, TRUE);
82       g_print ("      %s -> %s\n", key, value_str);
83       g_free (value_str);
84     }
85 }
86
87 static void
88 on_signal (GDBusProxy *proxy,
89            gchar      *sender_name,
90            gchar      *signal_name,
91            GVariant   *parameters,
92            gpointer    user_data)
93 {
94   gchar *parameters_str;
95
96   parameters_str = g_variant_print (parameters, TRUE);
97   g_print (" *** Received Signal: %s: %s\n",
98            signal_name,
99            parameters_str);
100   g_free (parameters_str);
101 }
102
103 static void
104 on_proxy_appeared (GDBusConnection *connection,
105                    const gchar     *name,
106                    const gchar     *name_owner,
107                    GDBusProxy      *proxy,
108                    gpointer         user_data)
109 {
110   g_print ("+++ Acquired proxy object for remote object owned by %s\n"
111            "    bus:          %s\n"
112            "    name:         %s\n"
113            "    object path:  %s\n"
114            "    interface:    %s\n",
115            name_owner,
116            opt_system_bus ? "System Bus" : "Session Bus",
117            opt_name,
118            opt_object_path,
119            opt_interface);
120
121   print_properties (proxy);
122
123   g_signal_connect (proxy,
124                     "g-properties-changed",
125                     G_CALLBACK (on_properties_changed),
126                     NULL);
127
128   g_signal_connect (proxy,
129                     "g-signal",
130                     G_CALLBACK (on_signal),
131                     NULL);
132 }
133
134 static void
135 on_proxy_vanished (GDBusConnection *connection,
136                    const gchar     *name,
137                    gpointer         user_data)
138 {
139   g_print ("--- Cannot create proxy object for\n"
140            "    bus:          %s\n"
141            "    name:         %s\n"
142            "    object path:  %s\n"
143            "    interface:    %s\n",
144            opt_system_bus ? "System Bus" : "Session Bus",
145            opt_name,
146            opt_object_path,
147            opt_interface);
148 }
149
150 int
151 main (int argc, char *argv[])
152 {
153   guint watcher_id;
154   GMainLoop *loop;
155   GOptionContext *opt_context;
156   GError *error;
157   GBusNameWatcherFlags flags;
158   GDBusProxyFlags proxy_flags;
159
160   g_type_init ();
161
162   opt_context = g_option_context_new ("g_bus_watch_proxy() example");
163   g_option_context_set_summary (opt_context,
164                                 "Example: to watch the object of gdbus-example-server, use:\n"
165                                 "\n"
166                                 "  ./gdbus-example-watch-proxy -n org.gtk.GDBus.TestServer  \\\n"
167                                 "                              -o /org/gtk/GDBus/TestObject \\\n"
168                                 "                              -i org.gtk.GDBus.TestInterface");
169   g_option_context_add_main_entries (opt_context, opt_entries, NULL);
170   error = NULL;
171   if (!g_option_context_parse (opt_context, &argc, &argv, &error))
172     {
173       g_printerr ("Error parsing options: %s", error->message);
174       goto out;
175     }
176   if (opt_name == NULL || opt_object_path == NULL || opt_interface == NULL)
177     {
178       g_printerr ("Incorrect usage, try --help.\n");
179       goto out;
180     }
181
182   flags = G_BUS_NAME_WATCHER_FLAGS_NONE;
183   if (opt_auto_start)
184     flags |= G_BUS_NAME_WATCHER_FLAGS_AUTO_START;
185
186   proxy_flags = G_DBUS_PROXY_FLAGS_NONE;
187   if (opt_no_properties)
188     proxy_flags |= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
189
190   watcher_id = g_bus_watch_proxy (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
191                                   opt_name,
192                                   flags,
193                                   opt_object_path,
194                                   opt_interface,
195                                   G_TYPE_DBUS_PROXY,
196                                   proxy_flags,
197                                   on_proxy_appeared,
198                                   on_proxy_vanished,
199                                   NULL,
200                                   NULL);
201
202   loop = g_main_loop_new (NULL, FALSE);
203   g_main_loop_run (loop);
204
205   g_bus_unwatch_proxy (watcher_id);
206
207  out:
208   g_option_context_free (opt_context);
209   g_free (opt_name);
210   g_free (opt_object_path);
211   g_free (opt_interface);
212
213   return 0;
214 }