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