| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | #include <gio/gio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static gchar *opt_name         = NULL; | 
					
						
							|  |  |  | static gchar *opt_object_path  = NULL; | 
					
						
							|  |  |  | static gchar *opt_interface    = NULL; | 
					
						
							|  |  |  | static gboolean opt_system_bus = FALSE; | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  | static gboolean opt_no_auto_start = FALSE; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | static gboolean opt_no_properties = FALSE; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static GOptionEntry opt_entries[] = | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   { "name", 'n', 0, G_OPTION_ARG_STRING, &opt_name, "Name of the remote object to watch", NULL }, | 
					
						
							|  |  |  |   { "object-path", 'o', 0, G_OPTION_ARG_STRING, &opt_object_path, "Object path of the remote object", NULL }, | 
					
						
							|  |  |  |   { "interface", 'i', 0, G_OPTION_ARG_STRING, &opt_interface, "D-Bus interface of remote object", NULL }, | 
					
						
							|  |  |  |   { "system-bus", 's', 0, G_OPTION_ARG_NONE, &opt_system_bus, "Use the system-bus instead of the session-bus", NULL }, | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   { "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}, | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |   { "no-properties", 'p', 0, G_OPTION_ARG_NONE, &opt_no_properties, "Do not load properties", NULL}, | 
					
						
							|  |  |  |   { NULL} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  | static GMainLoop *loop = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | static void | 
					
						
							|  |  |  | print_properties (GDBusProxy *proxy) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   gchar **property_names; | 
					
						
							|  |  |  |   guint n; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_print ("    properties:\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 17:10:15 -04:00
										 |  |  |   property_names = g_dbus_proxy_get_cached_property_names (proxy); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |   for (n = 0; property_names != NULL && property_names[n] != NULL; n++) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       const gchar *key = property_names[n]; | 
					
						
							|  |  |  |       GVariant *value; | 
					
						
							|  |  |  |       gchar *value_str; | 
					
						
							| 
									
										
										
										
											2010-05-12 20:43:40 -04:00
										 |  |  |       value = g_dbus_proxy_get_cached_property (proxy, key); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |       value_str = g_variant_print (value, TRUE); | 
					
						
							|  |  |  |       g_print ("      %s -> %s\n", key, value_str); | 
					
						
							|  |  |  |       g_variant_unref (value); | 
					
						
							|  |  |  |       g_free (value_str); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   g_strfreev (property_names); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2010-05-13 11:56:15 -04:00
										 |  |  | on_properties_changed (GDBusProxy          *proxy, | 
					
						
							|  |  |  |                        GVariant            *changed_properties, | 
					
						
							|  |  |  |                        const gchar* const  *invalidated_properties, | 
					
						
							|  |  |  |                        gpointer             user_data) | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-05-13 16:57:29 -04:00
										 |  |  |   /* Note that we are guaranteed that changed_properties and
 | 
					
						
							|  |  |  |    * invalidated_properties are never NULL | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 16:57:29 -04:00
										 |  |  |   if (g_variant_n_children (changed_properties) > 0) | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-05-13 11:56:15 -04:00
										 |  |  |       GVariantIter *iter; | 
					
						
							| 
									
										
										
										
											2010-05-14 20:07:15 +02:00
										 |  |  |       const gchar *key; | 
					
						
							|  |  |  |       GVariant *value; | 
					
						
							| 
									
										
										
										
											2010-05-10 13:31:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 11:56:15 -04:00
										 |  |  |       g_print (" *** Properties Changed:\n"); | 
					
						
							|  |  |  |       g_variant_get (changed_properties, | 
					
						
							|  |  |  |                      "a{sv}", | 
					
						
							|  |  |  |                      &iter); | 
					
						
							| 
									
										
										
										
											2010-05-14 20:07:15 +02:00
										 |  |  |       while (g_variant_iter_loop (iter, "{&sv}", &key, &value)) | 
					
						
							| 
									
										
										
										
											2010-05-13 11:56:15 -04:00
										 |  |  |         { | 
					
						
							|  |  |  |           gchar *value_str; | 
					
						
							|  |  |  |           value_str = g_variant_print (value, TRUE); | 
					
						
							|  |  |  |           g_print ("      %s -> %s\n", key, value_str); | 
					
						
							|  |  |  |           g_free (value_str); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-14 20:07:15 +02:00
										 |  |  |       g_variant_iter_free (iter); | 
					
						
							| 
									
										
										
										
											2010-05-13 11:56:15 -04:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2010-05-10 13:31:54 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-13 16:57:29 -04:00
										 |  |  |   if (g_strv_length ((GStrv) invalidated_properties) > 0) | 
					
						
							| 
									
										
										
										
											2010-05-13 11:56:15 -04:00
										 |  |  |     { | 
					
						
							|  |  |  |       guint n; | 
					
						
							|  |  |  |       g_print (" *** Properties Invalidated:\n"); | 
					
						
							|  |  |  |       for (n = 0; invalidated_properties[n] != NULL; n++) | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           const gchar *key = invalidated_properties[n]; | 
					
						
							|  |  |  |           g_print ("      %s\n", key); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | on_signal (GDBusProxy *proxy, | 
					
						
							|  |  |  |            gchar      *sender_name, | 
					
						
							|  |  |  |            gchar      *signal_name, | 
					
						
							|  |  |  |            GVariant   *parameters, | 
					
						
							|  |  |  |            gpointer    user_data) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   gchar *parameters_str; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   parameters_str = g_variant_print (parameters, TRUE); | 
					
						
							|  |  |  |   g_print (" *** Received Signal: %s: %s\n", | 
					
						
							|  |  |  |            signal_name, | 
					
						
							|  |  |  |            parameters_str); | 
					
						
							|  |  |  |   g_free (parameters_str); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  | print_proxy (GDBusProxy *proxy) | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   gchar *name_owner; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   name_owner = g_dbus_proxy_get_name_owner (proxy); | 
					
						
							|  |  |  |   if (name_owner != NULL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_print ("+++ Proxy object points to remote object owned by %s\n" | 
					
						
							|  |  |  |                "    bus:          %s\n" | 
					
						
							|  |  |  |                "    name:         %s\n" | 
					
						
							|  |  |  |                "    object path:  %s\n" | 
					
						
							|  |  |  |                "    interface:    %s\n", | 
					
						
							|  |  |  |                name_owner, | 
					
						
							|  |  |  |                opt_system_bus ? "System Bus" : "Session Bus", | 
					
						
							|  |  |  |                opt_name, | 
					
						
							|  |  |  |                opt_object_path, | 
					
						
							|  |  |  |                opt_interface); | 
					
						
							|  |  |  |       print_properties (proxy); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   else | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_print ("--- Proxy object is inert - there is no name owner for the name\n" | 
					
						
							|  |  |  |                "    bus:          %s\n" | 
					
						
							|  |  |  |                "    name:         %s\n" | 
					
						
							|  |  |  |                "    object path:  %s\n" | 
					
						
							|  |  |  |                "    interface:    %s\n", | 
					
						
							|  |  |  |                opt_system_bus ? "System Bus" : "Session Bus", | 
					
						
							|  |  |  |                opt_name, | 
					
						
							|  |  |  |                opt_object_path, | 
					
						
							|  |  |  |                opt_interface); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   g_free (name_owner); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  | on_name_owner_notify (GObject    *object, | 
					
						
							|  |  |  |                       GParamSpec *pspec, | 
					
						
							|  |  |  |                       gpointer    user_data) | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   GDBusProxy *proxy = G_DBUS_PROXY (object); | 
					
						
							|  |  |  |   print_proxy (proxy); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | main (int argc, char *argv[]) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   GOptionContext *opt_context; | 
					
						
							|  |  |  |   GError *error; | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   GDBusProxyFlags flags; | 
					
						
							|  |  |  |   GDBusProxy *proxy; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   g_type_init (); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   loop = NULL; | 
					
						
							|  |  |  |   proxy = NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |   opt_context = g_option_context_new ("g_bus_watch_proxy() example"); | 
					
						
							|  |  |  |   g_option_context_set_summary (opt_context, | 
					
						
							|  |  |  |                                 "Example: to watch the object of gdbus-example-server, use:\n" | 
					
						
							|  |  |  |                                 "\n" | 
					
						
							|  |  |  |                                 "  ./gdbus-example-watch-proxy -n org.gtk.GDBus.TestServer  \\\n" | 
					
						
							|  |  |  |                                 "                              -o /org/gtk/GDBus/TestObject \\\n" | 
					
						
							|  |  |  |                                 "                              -i org.gtk.GDBus.TestInterface"); | 
					
						
							|  |  |  |   g_option_context_add_main_entries (opt_context, opt_entries, NULL); | 
					
						
							|  |  |  |   error = NULL; | 
					
						
							|  |  |  |   if (!g_option_context_parse (opt_context, &argc, &argv, &error)) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |       g_printerr ("Error parsing options: %s\n", error->message); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |       goto out; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   if (opt_name == NULL || opt_object_path == NULL || opt_interface == NULL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_printerr ("Incorrect usage, try --help.\n"); | 
					
						
							|  |  |  |       goto out; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   flags = G_DBUS_PROXY_FLAGS_NONE; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |   if (opt_no_properties) | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |     flags |= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES; | 
					
						
							|  |  |  |   if (opt_no_auto_start) | 
					
						
							|  |  |  |     flags |= G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START; | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |   loop = g_main_loop_new (NULL, FALSE); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   error = NULL; | 
					
						
							|  |  |  |   proxy = g_dbus_proxy_new_for_bus_sync (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION, | 
					
						
							|  |  |  |                                          flags, | 
					
						
							|  |  |  |                                          NULL, /* GDBusInterfaceInfo */ | 
					
						
							|  |  |  |                                          opt_name, | 
					
						
							|  |  |  |                                          opt_object_path, | 
					
						
							|  |  |  |                                          opt_interface, | 
					
						
							|  |  |  |                                          NULL, /* GCancellable */ | 
					
						
							|  |  |  |                                          &error); | 
					
						
							|  |  |  |   if (proxy == NULL) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       g_printerr ("Error creating proxy: %s\n", error->message); | 
					
						
							|  |  |  |       g_error_free (error); | 
					
						
							|  |  |  |       goto out; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_signal_connect (proxy, | 
					
						
							|  |  |  |                     "g-properties-changed", | 
					
						
							|  |  |  |                     G_CALLBACK (on_properties_changed), | 
					
						
							|  |  |  |                     NULL); | 
					
						
							|  |  |  |   g_signal_connect (proxy, | 
					
						
							|  |  |  |                     "g-signal", | 
					
						
							|  |  |  |                     G_CALLBACK (on_signal), | 
					
						
							|  |  |  |                     NULL); | 
					
						
							|  |  |  |   g_signal_connect (proxy, | 
					
						
							|  |  |  |                     "notify::g-name-owner", | 
					
						
							|  |  |  |                     G_CALLBACK (on_name_owner_notify), | 
					
						
							|  |  |  |                     NULL); | 
					
						
							|  |  |  |   print_proxy (proxy); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   g_main_loop_run (loop); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |  out: | 
					
						
							| 
									
										
										
										
											2010-06-11 15:45:18 -04:00
										 |  |  |   if (proxy != NULL) | 
					
						
							|  |  |  |     g_object_unref (proxy); | 
					
						
							|  |  |  |   if (loop != NULL) | 
					
						
							|  |  |  |     g_main_loop_unref (loop); | 
					
						
							| 
									
										
										
										
											2010-05-06 14:13:59 -04:00
										 |  |  |   g_option_context_free (opt_context); | 
					
						
							|  |  |  |   g_free (opt_name); | 
					
						
							|  |  |  |   g_free (opt_object_path); | 
					
						
							|  |  |  |   g_free (opt_interface); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return 0; | 
					
						
							|  |  |  | } |