Initial GDBus code-drop from GDBus-standalone repo
[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                        GHashTable *changed_properties,
60                        gpointer    user_data)
61 {
62   GHashTableIter iter;
63   const gchar *key;
64   GVariant *value;
65
66   g_print (" *** Properties Changed:\n");
67
68   g_hash_table_iter_init (&iter, changed_properties);
69   while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value))
70     {
71       gchar *value_str;
72       value_str = g_variant_print (value, TRUE);
73       g_print ("      %s -> %s\n", key, value_str);
74       g_free (value_str);
75     }
76 }
77
78 static void
79 on_signal (GDBusProxy *proxy,
80            gchar      *sender_name,
81            gchar      *signal_name,
82            GVariant   *parameters,
83            gpointer    user_data)
84 {
85   gchar *parameters_str;
86
87   parameters_str = g_variant_print (parameters, TRUE);
88   g_print (" *** Received Signal: %s: %s\n",
89            signal_name,
90            parameters_str);
91   g_free (parameters_str);
92 }
93
94 static void
95 on_proxy_appeared (GDBusConnection *connection,
96                    const gchar     *name,
97                    const gchar     *name_owner,
98                    GDBusProxy      *proxy,
99                    gpointer         user_data)
100 {
101   g_print ("+++ Acquired proxy object for remote object owned by %s\n"
102            "    bus:          %s\n"
103            "    name:         %s\n"
104            "    object path:  %s\n"
105            "    interface:    %s\n",
106            name_owner,
107            opt_system_bus ? "System Bus" : "Session Bus",
108            opt_name,
109            opt_object_path,
110            opt_interface);
111
112   print_properties (proxy);
113
114   g_signal_connect (proxy,
115                     "g-properties-changed",
116                     G_CALLBACK (on_properties_changed),
117                     NULL);
118
119   g_signal_connect (proxy,
120                     "g-signal",
121                     G_CALLBACK (on_signal),
122                     NULL);
123 }
124
125 static void
126 on_proxy_vanished (GDBusConnection *connection,
127                    const gchar     *name,
128                    gpointer         user_data)
129 {
130   g_print ("--- Cannot create proxy object for\n"
131            "    bus:          %s\n"
132            "    name:         %s\n"
133            "    object path:  %s\n"
134            "    interface:    %s\n",
135            opt_system_bus ? "System Bus" : "Session Bus",
136            opt_name,
137            opt_object_path,
138            opt_interface);
139 }
140
141 int
142 main (int argc, char *argv[])
143 {
144   guint watcher_id;
145   GMainLoop *loop;
146   GOptionContext *opt_context;
147   GError *error;
148   GBusNameWatcherFlags flags;
149   GDBusProxyFlags proxy_flags;
150
151   g_type_init ();
152
153   opt_context = g_option_context_new ("g_bus_watch_proxy() example");
154   g_option_context_set_summary (opt_context,
155                                 "Example: to watch the object of gdbus-example-server, use:\n"
156                                 "\n"
157                                 "  ./gdbus-example-watch-proxy -n org.gtk.GDBus.TestServer  \\\n"
158                                 "                              -o /org/gtk/GDBus/TestObject \\\n"
159                                 "                              -i org.gtk.GDBus.TestInterface");
160   g_option_context_add_main_entries (opt_context, opt_entries, NULL);
161   error = NULL;
162   if (!g_option_context_parse (opt_context, &argc, &argv, &error))
163     {
164       g_printerr ("Error parsing options: %s", error->message);
165       goto out;
166     }
167   if (opt_name == NULL || opt_object_path == NULL || opt_interface == NULL)
168     {
169       g_printerr ("Incorrect usage, try --help.\n");
170       goto out;
171     }
172
173   flags = G_BUS_NAME_WATCHER_FLAGS_NONE;
174   if (opt_auto_start)
175     flags |= G_BUS_NAME_WATCHER_FLAGS_AUTO_START;
176
177   proxy_flags = G_DBUS_PROXY_FLAGS_NONE;
178   if (opt_no_properties)
179     proxy_flags |= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
180
181   watcher_id = g_bus_watch_proxy (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
182                                   opt_name,
183                                   flags,
184                                   opt_object_path,
185                                   opt_interface,
186                                   G_TYPE_DBUS_PROXY,
187                                   proxy_flags,
188                                   on_proxy_appeared,
189                                   on_proxy_vanished,
190                                   NULL,
191                                   NULL);
192
193   loop = g_main_loop_new (NULL, FALSE);
194   g_main_loop_run (loop);
195
196   g_bus_unwatch_proxy (watcher_id);
197
198  out:
199   g_option_context_free (opt_context);
200   g_free (opt_name);
201   g_free (opt_object_path);
202   g_free (opt_interface);
203
204   return 0;
205 }