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