LCOV - code coverage report
Current view: top level - glib/gio/tests - gdbus-example-watch-proxy.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 0 86 0.0 %
Date: 2024-04-23 05:16:05 Functions: 0 6 0.0 %
Branches: 0 38 0.0 %

           Branch data     Line data    Source code
       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_no_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                 :            :   { "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},
      18                 :            :   G_OPTION_ENTRY_NULL
      19                 :            : };
      20                 :            : 
      21                 :            : static GMainLoop *loop = NULL;
      22                 :            : 
      23                 :            : static void
      24                 :          0 : print_properties (GDBusProxy *proxy)
      25                 :            : {
      26                 :            :   gchar **property_names;
      27                 :            :   guint n;
      28                 :            : 
      29                 :          0 :   g_print ("    properties:\n");
      30                 :            : 
      31                 :          0 :   property_names = g_dbus_proxy_get_cached_property_names (proxy);
      32   [ #  #  #  # ]:          0 :   for (n = 0; property_names != NULL && property_names[n] != NULL; n++)
      33                 :            :     {
      34                 :          0 :       const gchar *key = property_names[n];
      35                 :            :       GVariant *value;
      36                 :            :       gchar *value_str;
      37                 :          0 :       value = g_dbus_proxy_get_cached_property (proxy, key);
      38                 :          0 :       value_str = g_variant_print (value, TRUE);
      39                 :          0 :       g_print ("      %s -> %s\n", key, value_str);
      40                 :          0 :       g_variant_unref (value);
      41                 :          0 :       g_free (value_str);
      42                 :            :     }
      43                 :          0 :   g_strfreev (property_names);
      44                 :          0 : }
      45                 :            : 
      46                 :            : static void
      47                 :          0 : on_properties_changed (GDBusProxy          *proxy,
      48                 :            :                        GVariant            *changed_properties,
      49                 :            :                        const gchar* const  *invalidated_properties,
      50                 :            :                        gpointer             user_data)
      51                 :            : {
      52                 :            :   /* Note that we are guaranteed that changed_properties and
      53                 :            :    * invalidated_properties are never NULL
      54                 :            :    */
      55                 :            : 
      56         [ #  # ]:          0 :   if (g_variant_n_children (changed_properties) > 0)
      57                 :            :     {
      58                 :            :       GVariantIter *iter;
      59                 :            :       const gchar *key;
      60                 :            :       GVariant *value;
      61                 :            : 
      62                 :          0 :       g_print (" *** Properties Changed:\n");
      63                 :          0 :       g_variant_get (changed_properties,
      64                 :            :                      "a{sv}",
      65                 :            :                      &iter);
      66         [ #  # ]:          0 :       while (g_variant_iter_loop (iter, "{&sv}", &key, &value))
      67                 :            :         {
      68                 :            :           gchar *value_str;
      69                 :          0 :           value_str = g_variant_print (value, TRUE);
      70                 :          0 :           g_print ("      %s -> %s\n", key, value_str);
      71                 :          0 :           g_free (value_str);
      72                 :            :         }
      73                 :          0 :       g_variant_iter_free (iter);
      74                 :            :     }
      75                 :            : 
      76         [ #  # ]:          0 :   if (g_strv_length ((GStrv) invalidated_properties) > 0)
      77                 :            :     {
      78                 :            :       guint n;
      79                 :          0 :       g_print (" *** Properties Invalidated:\n");
      80         [ #  # ]:          0 :       for (n = 0; invalidated_properties[n] != NULL; n++)
      81                 :            :         {
      82                 :          0 :           const gchar *key = invalidated_properties[n];
      83                 :          0 :           g_print ("      %s\n", key);
      84                 :            :         }
      85                 :            :     }
      86                 :          0 : }
      87                 :            : 
      88                 :            : static void
      89                 :          0 : on_signal (GDBusProxy *proxy,
      90                 :            :            gchar      *sender_name,
      91                 :            :            gchar      *signal_name,
      92                 :            :            GVariant   *parameters,
      93                 :            :            gpointer    user_data)
      94                 :            : {
      95                 :            :   gchar *parameters_str;
      96                 :            : 
      97                 :          0 :   parameters_str = g_variant_print (parameters, TRUE);
      98                 :          0 :   g_print (" *** Received Signal: %s: %s\n",
      99                 :            :            signal_name,
     100                 :            :            parameters_str);
     101                 :          0 :   g_free (parameters_str);
     102                 :          0 : }
     103                 :            : 
     104                 :            : static void
     105                 :          0 : print_proxy (GDBusProxy *proxy)
     106                 :            : {
     107                 :            :   gchar *name_owner;
     108                 :            : 
     109                 :          0 :   name_owner = g_dbus_proxy_get_name_owner (proxy);
     110         [ #  # ]:          0 :   if (name_owner != NULL)
     111                 :            :     {
     112                 :          0 :       g_print ("+++ Proxy object points to remote object owned by %s\n"
     113                 :            :                "    bus:          %s\n"
     114                 :            :                "    name:         %s\n"
     115                 :            :                "    object path:  %s\n"
     116                 :            :                "    interface:    %s\n",
     117                 :            :                name_owner,
     118         [ #  # ]:          0 :                opt_system_bus ? "System Bus" : "Session Bus",
     119                 :            :                opt_name,
     120                 :            :                opt_object_path,
     121                 :            :                opt_interface);
     122                 :          0 :       print_properties (proxy);
     123                 :            :     }
     124                 :            :   else
     125                 :            :     {
     126                 :          0 :       g_print ("--- Proxy object is inert - there is no name owner for the name\n"
     127                 :            :                "    bus:          %s\n"
     128                 :            :                "    name:         %s\n"
     129                 :            :                "    object path:  %s\n"
     130                 :            :                "    interface:    %s\n",
     131         [ #  # ]:          0 :                opt_system_bus ? "System Bus" : "Session Bus",
     132                 :            :                opt_name,
     133                 :            :                opt_object_path,
     134                 :            :                opt_interface);
     135                 :            :     }
     136                 :          0 :   g_free (name_owner);
     137                 :          0 : }
     138                 :            : 
     139                 :            : static void
     140                 :          0 : on_name_owner_notify (GObject    *object,
     141                 :            :                       GParamSpec *pspec,
     142                 :            :                       gpointer    user_data)
     143                 :            : {
     144                 :          0 :   GDBusProxy *proxy = G_DBUS_PROXY (object);
     145                 :          0 :   print_proxy (proxy);
     146                 :          0 : }
     147                 :            : 
     148                 :            : int
     149                 :          0 : main (int argc, char *argv[])
     150                 :            : {
     151                 :            :   GOptionContext *opt_context;
     152                 :            :   GError *error;
     153                 :            :   GDBusProxyFlags flags;
     154                 :            :   GDBusProxy *proxy;
     155                 :            : 
     156                 :          0 :   loop = NULL;
     157                 :          0 :   proxy = NULL;
     158                 :            : 
     159                 :          0 :   opt_context = g_option_context_new ("g_bus_watch_proxy() example");
     160                 :          0 :   g_option_context_set_summary (opt_context,
     161                 :            :                                 "Example: to watch the object of gdbus-example-server, use:\n"
     162                 :            :                                 "\n"
     163                 :            :                                 "  ./gdbus-example-watch-proxy -n org.gtk.GDBus.TestServer  \\\n"
     164                 :            :                                 "                              -o /org/gtk/GDBus/TestObject \\\n"
     165                 :            :                                 "                              -i org.gtk.GDBus.TestInterface");
     166                 :          0 :   g_option_context_add_main_entries (opt_context, opt_entries, NULL);
     167                 :          0 :   error = NULL;
     168         [ #  # ]:          0 :   if (!g_option_context_parse (opt_context, &argc, &argv, &error))
     169                 :            :     {
     170                 :          0 :       g_printerr ("Error parsing options: %s\n", error->message);
     171                 :          0 :       goto out;
     172                 :            :     }
     173   [ #  #  #  #  :          0 :   if (opt_name == NULL || opt_object_path == NULL || opt_interface == NULL)
                   #  # ]
     174                 :            :     {
     175                 :          0 :       g_printerr ("Incorrect usage, try --help.\n");
     176                 :          0 :       goto out;
     177                 :            :     }
     178                 :            : 
     179                 :          0 :   flags = G_DBUS_PROXY_FLAGS_NONE;
     180         [ #  # ]:          0 :   if (opt_no_properties)
     181                 :          0 :     flags |= G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES;
     182         [ #  # ]:          0 :   if (opt_no_auto_start)
     183                 :          0 :     flags |= G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START;
     184                 :            : 
     185                 :          0 :   loop = g_main_loop_new (NULL, FALSE);
     186                 :            : 
     187                 :          0 :   error = NULL;
     188         [ #  # ]:          0 :   proxy = g_dbus_proxy_new_for_bus_sync (opt_system_bus ? G_BUS_TYPE_SYSTEM : G_BUS_TYPE_SESSION,
     189                 :            :                                          flags,
     190                 :            :                                          NULL, /* GDBusInterfaceInfo */
     191                 :            :                                          opt_name,
     192                 :            :                                          opt_object_path,
     193                 :            :                                          opt_interface,
     194                 :            :                                          NULL, /* GCancellable */
     195                 :            :                                          &error);
     196         [ #  # ]:          0 :   if (proxy == NULL)
     197                 :            :     {
     198                 :          0 :       g_printerr ("Error creating proxy: %s\n", error->message);
     199                 :          0 :       g_error_free (error);
     200                 :          0 :       goto out;
     201                 :            :     }
     202                 :            : 
     203                 :          0 :   g_signal_connect (proxy,
     204                 :            :                     "g-properties-changed",
     205                 :            :                     G_CALLBACK (on_properties_changed),
     206                 :            :                     NULL);
     207                 :          0 :   g_signal_connect (proxy,
     208                 :            :                     "g-signal",
     209                 :            :                     G_CALLBACK (on_signal),
     210                 :            :                     NULL);
     211                 :          0 :   g_signal_connect (proxy,
     212                 :            :                     "notify::g-name-owner",
     213                 :            :                     G_CALLBACK (on_name_owner_notify),
     214                 :            :                     NULL);
     215                 :          0 :   print_proxy (proxy);
     216                 :            : 
     217                 :          0 :   g_main_loop_run (loop);
     218                 :            : 
     219                 :          0 :  out:
     220         [ #  # ]:          0 :   if (proxy != NULL)
     221                 :          0 :     g_object_unref (proxy);
     222         [ #  # ]:          0 :   if (loop != NULL)
     223                 :          0 :     g_main_loop_unref (loop);
     224                 :          0 :   g_option_context_free (opt_context);
     225                 :          0 :   g_free (opt_name);
     226                 :          0 :   g_free (opt_object_path);
     227                 :          0 :   g_free (opt_interface);
     228                 :            : 
     229                 :          0 :   return 0;
     230                 :            : }

Generated by: LCOV version 1.14