LCOV - code coverage report
Current view: top level - glib/gio/tests - gdbus-example-objectmanager-server.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 0 53 0.0 %
Date: 2024-04-23 05:16:05 Functions: 0 5 0.0 %
Branches: 0 20 0.0 %

           Branch data     Line data    Source code
       1                 :            : 
       2                 :            : #include "gdbus-object-manager-example/objectmanager-gen.h"
       3                 :            : 
       4                 :            : /* ---------------------------------------------------------------------------------------------------- */
       5                 :            : 
       6                 :            : static GDBusObjectManagerServer *manager = NULL;
       7                 :            : 
       8                 :            : static gboolean
       9                 :          0 : on_animal_poke (ExampleAnimal          *animal,
      10                 :            :                 GDBusMethodInvocation  *invocation,
      11                 :            :                 gboolean                make_sad,
      12                 :            :                 gboolean                make_happy,
      13                 :            :                 gpointer                user_data)
      14                 :            : {
      15   [ #  #  #  #  :          0 :   if ((make_sad && make_happy) || (!make_sad && !make_happy))
             #  #  #  # ]
      16                 :            :     {
      17                 :          0 :       g_dbus_method_invocation_return_dbus_error (invocation,
      18                 :            :                                                   "org.gtk.GDBus.Examples.ObjectManager.Error.Failed",
      19                 :            :                                                   "Exactly one of make_sad or make_happy must be TRUE");
      20                 :          0 :       goto out;
      21                 :            :     }
      22                 :            : 
      23         [ #  # ]:          0 :   if (make_sad)
      24                 :            :     {
      25         [ #  # ]:          0 :       if (g_strcmp0 (example_animal_get_mood (animal), "Sad") == 0)
      26                 :            :         {
      27                 :          0 :           g_dbus_method_invocation_return_dbus_error (invocation,
      28                 :            :                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.SadAnimalIsSad",
      29                 :            :                                                       "Sad animal is already sad");
      30                 :          0 :           goto out;
      31                 :            :         }
      32                 :            : 
      33                 :          0 :       example_animal_set_mood (animal, "Sad");
      34                 :          0 :       example_animal_complete_poke (animal, invocation);
      35                 :          0 :       goto out;
      36                 :            :     }
      37                 :            : 
      38         [ #  # ]:          0 :   if (make_happy)
      39                 :            :     {
      40         [ #  # ]:          0 :       if (g_strcmp0 (example_animal_get_mood (animal), "Happy") == 0)
      41                 :            :         {
      42                 :          0 :           g_dbus_method_invocation_return_dbus_error (invocation,
      43                 :            :                                                       "org.gtk.GDBus.Examples.ObjectManager.Error.HappyAnimalIsHappy",
      44                 :            :                                                       "Happy animal is already happy");
      45                 :          0 :           goto out;
      46                 :            :         }
      47                 :            : 
      48                 :          0 :       example_animal_set_mood (animal, "Happy");
      49                 :          0 :       example_animal_complete_poke (animal, invocation);
      50                 :          0 :       goto out;
      51                 :            :     }
      52                 :            : 
      53                 :            :   g_assert_not_reached ();
      54                 :            : 
      55                 :          0 :  out:
      56                 :          0 :   return G_DBUS_METHOD_INVOCATION_HANDLED;
      57                 :            : }
      58                 :            : 
      59                 :            : 
      60                 :            : static void
      61                 :          0 : on_bus_acquired (GDBusConnection *connection,
      62                 :            :                  const gchar     *name,
      63                 :            :                  gpointer         user_data)
      64                 :            : {
      65                 :            :   ExampleObjectSkeleton *object;
      66                 :            :   guint n;
      67                 :            : 
      68                 :          0 :   g_print ("Acquired a message bus connection\n");
      69                 :            : 
      70                 :            :   /* Create a new org.freedesktop.DBus.ObjectManager rooted at /example/Animals */
      71                 :          0 :   manager = g_dbus_object_manager_server_new ("/example/Animals");
      72                 :            : 
      73         [ #  # ]:          0 :   for (n = 0; n < 10; n++)
      74                 :            :     {
      75                 :            :       gchar *s;
      76                 :            :       ExampleAnimal *animal;
      77                 :            : 
      78                 :            :       /* Create a new D-Bus object at the path /example/Animals/N where N is 000..009 */
      79                 :          0 :       s = g_strdup_printf ("/example/Animals/%03d", n);
      80                 :          0 :       object = example_object_skeleton_new (s);
      81                 :          0 :       g_free (s);
      82                 :            : 
      83                 :            :       /* Make the newly created object export the interface
      84                 :            :        * org.gtk.GDBus.Example.ObjectManager.Animal (note
      85                 :            :        * that @object takes its own reference to @animal).
      86                 :            :        */
      87                 :          0 :       animal = example_animal_skeleton_new ();
      88                 :          0 :       example_animal_set_mood (animal, "Happy");
      89                 :          0 :       example_object_skeleton_set_animal (object, animal);
      90                 :          0 :       g_object_unref (animal);
      91                 :            : 
      92                 :            :       /* Cats are odd animals - so some of our objects implement the
      93                 :            :        * org.gtk.GDBus.Example.ObjectManager.Cat interface in addition
      94                 :            :        * to the .Animal interface
      95                 :            :        */
      96         [ #  # ]:          0 :       if (n % 2 == 1)
      97                 :            :         {
      98                 :            :           ExampleCat *cat;
      99                 :          0 :           cat = example_cat_skeleton_new ();
     100                 :          0 :           example_object_skeleton_set_cat (object, cat);
     101                 :          0 :           g_object_unref (cat);
     102                 :            :         }
     103                 :            : 
     104                 :            :       /* Handle Poke() D-Bus method invocations on the .Animal interface */
     105                 :          0 :       g_signal_connect (animal,
     106                 :            :                         "handle-poke",
     107                 :            :                         G_CALLBACK (on_animal_poke),
     108                 :            :                         NULL); /* user_data */
     109                 :            : 
     110                 :            :       /* Export the object (@manager takes its own reference to @object) */
     111                 :          0 :       g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
     112                 :          0 :       g_object_unref (object);
     113                 :            :     }
     114                 :            : 
     115                 :            :   /* Export all objects */
     116                 :          0 :   g_dbus_object_manager_server_set_connection (manager, connection);
     117                 :          0 : }
     118                 :            : 
     119                 :            : static void
     120                 :          0 : on_name_acquired (GDBusConnection *connection,
     121                 :            :                   const gchar     *name,
     122                 :            :                   gpointer         user_data)
     123                 :            : {
     124                 :          0 :   g_print ("Acquired the name %s\n", name);
     125                 :          0 : }
     126                 :            : 
     127                 :            : static void
     128                 :          0 : on_name_lost (GDBusConnection *connection,
     129                 :            :               const gchar     *name,
     130                 :            :               gpointer         user_data)
     131                 :            : {
     132                 :          0 :   g_print ("Lost the name %s\n", name);
     133                 :          0 : }
     134                 :            : 
     135                 :            : 
     136                 :            : gint
     137                 :          0 : main (gint argc, gchar *argv[])
     138                 :            : {
     139                 :            :   GMainLoop *loop;
     140                 :            :   guint id;
     141                 :            : 
     142                 :          0 :   loop = g_main_loop_new (NULL, FALSE);
     143                 :            : 
     144                 :          0 :   id = g_bus_own_name (G_BUS_TYPE_SESSION,
     145                 :            :                        "org.gtk.GDBus.Examples.ObjectManager",
     146                 :            :                        G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
     147                 :            :                        G_BUS_NAME_OWNER_FLAGS_REPLACE,
     148                 :            :                        on_bus_acquired,
     149                 :            :                        on_name_acquired,
     150                 :            :                        on_name_lost,
     151                 :            :                        loop,
     152                 :            :                        NULL);
     153                 :            : 
     154                 :          0 :   g_main_loop_run (loop);
     155                 :            : 
     156                 :          0 :   g_bus_unown_name (id);
     157                 :          0 :   g_main_loop_unref (loop);
     158                 :            : 
     159                 :          0 :   return 0;
     160                 :            : }

Generated by: LCOV version 1.14