2 * Copyright © 2010 Red Hat, Inc.
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.
9 * See the included COPYING file for more information.
11 * Author: David Zeuthen <davidz@redhat.com>
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;
23 static GOptionEntry opt_entries[] =
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},
35 print_properties (GDBusProxy *proxy)
37 gchar **property_names;
40 g_print (" properties:\n");
42 property_names = g_dbus_proxy_get_cached_property_names (proxy, NULL);
43 for (n = 0; property_names != NULL && property_names[n] != NULL; n++)
45 const gchar *key = property_names[n];
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);
54 g_strfreev (property_names);
58 on_properties_changed (GDBusProxy *proxy,
59 GHashTable *changed_properties,
66 g_print (" *** Properties Changed:\n");
68 g_hash_table_iter_init (&iter, changed_properties);
69 while (g_hash_table_iter_next (&iter, (gpointer) &key, (gpointer) &value))
72 value_str = g_variant_print (value, TRUE);
73 g_print (" %s -> %s\n", key, value_str);
79 on_signal (GDBusProxy *proxy,
85 gchar *parameters_str;
87 parameters_str = g_variant_print (parameters, TRUE);
88 g_print (" *** Received Signal: %s: %s\n",
91 g_free (parameters_str);
95 on_proxy_appeared (GDBusConnection *connection,
97 const gchar *name_owner,
101 g_print ("+++ Acquired proxy object for remote object owned by %s\n"
107 opt_system_bus ? "System Bus" : "Session Bus",
112 print_properties (proxy);
114 g_signal_connect (proxy,
115 "g-properties-changed",
116 G_CALLBACK (on_properties_changed),
119 g_signal_connect (proxy,
121 G_CALLBACK (on_signal),
126 on_proxy_vanished (GDBusConnection *connection,
130 g_print ("--- Cannot create proxy object for\n"
135 opt_system_bus ? "System Bus" : "Session Bus",
142 main (int argc, char *argv[])
146 GOptionContext *opt_context;
148 GBusNameWatcherFlags flags;
149 GDBusProxyFlags proxy_flags;
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"
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);
162 if (!g_option_context_parse (opt_context, &argc, &argv, &error))
164 g_printerr ("Error parsing options: %s", error->message);
167 if (opt_name == NULL || opt_object_path == NULL || opt_interface == NULL)
169 g_printerr ("Incorrect usage, try --help.\n");
173 flags = G_BUS_NAME_WATCHER_FLAGS_NONE;
175 flags |= G_BUS_NAME_WATCHER_FLAGS_AUTO_START;
177 proxy_flags = G_DBUS_PROXY_FLAGS_NONE;
178 if (opt_no_properties)
179 proxy_flags |= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
181 watcher_id = g_bus_watch_proxy (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
193 loop = g_main_loop_new (NULL, FALSE);
194 g_main_loop_run (loop);
196 g_bus_unwatch_proxy (watcher_id);
199 g_option_context_free (opt_context);
201 g_free (opt_object_path);
202 g_free (opt_interface);