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;
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 { "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},
22 print_properties (GDBusProxy *proxy)
24 gchar **property_names;
27 g_print (" properties:\n");
29 property_names = g_dbus_proxy_get_cached_property_names (proxy, NULL);
30 for (n = 0; property_names != NULL && property_names[n] != NULL; n++)
32 const gchar *key = property_names[n];
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);
41 g_strfreev (property_names);
45 on_properties_changed (GDBusProxy *proxy,
46 GVariant *changed_properties,
52 g_print (" *** Properties Changed:\n");
54 g_variant_get (changed_properties,
57 while ((item = g_variant_iter_next_value (iter)))
68 value_str = g_variant_print (value, TRUE);
69 g_print (" %s -> %s\n", key, value_str);
75 on_signal (GDBusProxy *proxy,
81 gchar *parameters_str;
83 parameters_str = g_variant_print (parameters, TRUE);
84 g_print (" *** Received Signal: %s: %s\n",
87 g_free (parameters_str);
91 on_proxy_appeared (GDBusConnection *connection,
93 const gchar *name_owner,
97 g_print ("+++ Acquired proxy object for remote object owned by %s\n"
103 opt_system_bus ? "System Bus" : "Session Bus",
108 print_properties (proxy);
110 g_signal_connect (proxy,
111 "g-properties-changed",
112 G_CALLBACK (on_properties_changed),
115 g_signal_connect (proxy,
117 G_CALLBACK (on_signal),
122 on_proxy_vanished (GDBusConnection *connection,
126 g_print ("--- Cannot create proxy object for\n"
131 opt_system_bus ? "System Bus" : "Session Bus",
138 main (int argc, char *argv[])
142 GOptionContext *opt_context;
144 GBusNameWatcherFlags flags;
145 GDBusProxyFlags proxy_flags;
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"
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);
158 if (!g_option_context_parse (opt_context, &argc, &argv, &error))
160 g_printerr ("Error parsing options: %s", error->message);
163 if (opt_name == NULL || opt_object_path == NULL || opt_interface == NULL)
165 g_printerr ("Incorrect usage, try --help.\n");
169 flags = G_BUS_NAME_WATCHER_FLAGS_NONE;
171 flags |= G_BUS_NAME_WATCHER_FLAGS_AUTO_START;
173 proxy_flags = G_DBUS_PROXY_FLAGS_NONE;
174 if (opt_no_properties)
175 proxy_flags |= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
177 watcher_id = g_bus_watch_proxy (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
189 loop = g_main_loop_new (NULL, FALSE);
190 g_main_loop_run (loop);
192 g_bus_unwatch_proxy (watcher_id);
195 g_option_context_free (opt_context);
197 g_free (opt_object_path);
198 g_free (opt_interface);