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_no_auto_start = FALSE;
8 static gboolean opt_no_properties = FALSE;
10 static GOptionEntry opt_entries[] =
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 { "no-auto-start", 'a', 0, G_OPTION_ARG_NONE, &opt_no_auto_start, "Don't 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},
21 static GMainLoop *loop = NULL;
24 print_properties (GDBusProxy *proxy)
26 gchar **property_names;
29 g_print (" properties:\n");
31 property_names = g_dbus_proxy_get_cached_property_names (proxy);
32 for (n = 0; property_names != NULL && property_names[n] != NULL; n++)
34 const gchar *key = property_names[n];
37 value = g_dbus_proxy_get_cached_property (proxy, key);
38 value_str = g_variant_print (value, TRUE);
39 g_print (" %s -> %s\n", key, value_str);
40 g_variant_unref (value);
43 g_strfreev (property_names);
47 on_properties_changed (GDBusProxy *proxy,
48 GVariant *changed_properties,
49 const gchar* const *invalidated_properties,
52 /* Note that we are guaranteed that changed_properties and
53 * invalidated_properties are never NULL
56 if (g_variant_n_children (changed_properties) > 0)
62 g_print (" *** Properties Changed:\n");
63 g_variant_get (changed_properties,
66 while (g_variant_iter_loop (iter, "{&sv}", &key, &value))
69 value_str = g_variant_print (value, TRUE);
70 g_print (" %s -> %s\n", key, value_str);
73 g_variant_iter_free (iter);
76 if (g_strv_length ((GStrv) invalidated_properties) > 0)
79 g_print (" *** Properties Invalidated:\n");
80 for (n = 0; invalidated_properties[n] != NULL; n++)
82 const gchar *key = invalidated_properties[n];
83 g_print (" %s\n", key);
89 on_signal (GDBusProxy *proxy,
95 gchar *parameters_str;
97 parameters_str = g_variant_print (parameters, TRUE);
98 g_print (" *** Received Signal: %s: %s\n",
101 g_free (parameters_str);
105 print_proxy (GDBusProxy *proxy)
109 name_owner = g_dbus_proxy_get_name_owner (proxy);
110 if (name_owner != NULL)
112 g_print ("+++ Proxy object points to remote object owned by %s\n"
118 opt_system_bus ? "System Bus" : "Session Bus",
122 print_properties (proxy);
126 g_print ("--- Proxy object is inert - there is no name owner for the name\n"
131 opt_system_bus ? "System Bus" : "Session Bus",
140 on_name_owner_notify (GObject *object,
144 GDBusProxy *proxy = G_DBUS_PROXY (object);
149 main (int argc, char *argv[])
151 GOptionContext *opt_context;
153 GDBusProxyFlags flags;
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"
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);
170 if (!g_option_context_parse (opt_context, &argc, &argv, &error))
172 g_printerr ("Error parsing options: %s\n", error->message);
175 if (opt_name == NULL || opt_object_path == NULL || opt_interface == NULL)
177 g_printerr ("Incorrect usage, try --help.\n");
181 flags = G_DBUS_PROXY_FLAGS_NONE;
182 if (opt_no_properties)
183 flags |= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
184 if (opt_no_auto_start)
185 flags |= G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
187 loop = g_main_loop_new (NULL, FALSE);
190 proxy = g_dbus_proxy_new_for_bus_sync (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
192 NULL, /* GDBusInterfaceInfo */
196 NULL, /* GCancellable */
200 g_printerr ("Error creating proxy: %s\n", error->message);
201 g_error_free (error);
205 g_signal_connect (proxy,
206 "g-properties-changed",
207 G_CALLBACK (on_properties_changed),
209 g_signal_connect (proxy,
211 G_CALLBACK (on_signal),
213 g_signal_connect (proxy,
214 "notify::g-name-owner",
215 G_CALLBACK (on_name_owner_notify),
219 g_main_loop_run (loop);
223 g_object_unref (proxy);
225 g_main_loop_unref (loop);
226 g_option_context_free (opt_context);
228 g_free (opt_object_path);
229 g_free (opt_interface);