LCOV - code coverage report
Current view: top level - glib/gio - ggtknotificationbackend.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 25 27 92.6 %
Date: 2024-04-23 05:16:05 Functions: 8 8 100.0 %
Branches: 6 10 60.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : * Copyright © 2013 Lars Uebernickel
       3                 :            : *
       4                 :            : * This library is free software; you can redistribute it and/or
       5                 :            : * modify it under the terms of the GNU Lesser General Public
       6                 :            : * License as published by the Free Software Foundation; either
       7                 :            : * version 2.1 of the License, or (at your option) any later version.
       8                 :            : *
       9                 :            : * This library is distributed in the hope that it will be useful,
      10                 :            : * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11                 :            : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12                 :            : * Lesser General Public License for more details.
      13                 :            : *
      14                 :            : * You should have received a copy of the GNU Lesser General
      15                 :            : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      16                 :            : *
      17                 :            : * Authors: Lars Uebernickel <lars@uebernic.de>
      18                 :            : */
      19                 :            : 
      20                 :            : #include "config.h"
      21                 :            : #include "gnotificationbackend.h"
      22                 :            : 
      23                 :            : #include "giomodule-priv.h"
      24                 :            : #include "gdbusconnection.h"
      25                 :            : #include "gapplication.h"
      26                 :            : #include "gnotification-private.h"
      27                 :            : 
      28                 :            : #define G_TYPE_GTK_NOTIFICATION_BACKEND  (g_gtk_notification_backend_get_type ())
      29                 :            : #define G_GTK_NOTIFICATION_BACKEND(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_GTK_NOTIFICATION_BACKEND, GGtkNotificationBackend))
      30                 :            : 
      31                 :            : typedef struct _GGtkNotificationBackend GGtkNotificationBackend;
      32                 :            : typedef GNotificationBackendClass       GGtkNotificationBackendClass;
      33                 :            : 
      34                 :            : struct _GGtkNotificationBackend
      35                 :            : {
      36                 :            :   GNotificationBackend parent;
      37                 :            : };
      38                 :            : 
      39                 :            : GType g_gtk_notification_backend_get_type (void);
      40                 :            : 
      41   [ +  +  +  -  :        229 : G_DEFINE_TYPE_WITH_CODE (GGtkNotificationBackend, g_gtk_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
                   +  - ]
      42                 :            :   _g_io_modules_ensure_extension_points_registered ();
      43                 :            :   g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
      44                 :            :                                  g_define_type_id, "gtk", 100))
      45                 :            : 
      46                 :            : static gboolean
      47                 :          1 : g_gtk_notification_backend_is_supported (void)
      48                 :            : {
      49                 :            :   GDBusConnection *session_bus;
      50                 :            :   GVariant *reply;
      51                 :            : 
      52                 :            :   /* Find out if the notification server is running. This is a
      53                 :            :    * synchronous call because gio extension points don't support async
      54                 :            :    * backend verification. This is only run once and only contacts the
      55                 :            :    * dbus daemon. */
      56                 :            : 
      57                 :          1 :   session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
      58         [ -  + ]:          1 :   if (session_bus == NULL)
      59                 :          0 :     return FALSE;
      60                 :            : 
      61                 :          1 :   reply = g_dbus_connection_call_sync (session_bus, "org.freedesktop.DBus", "/org/freedesktop/DBus",
      62                 :            :                                        "org.freedesktop.DBus",
      63                 :            :                                        "GetNameOwner", g_variant_new ("(s)", "org.gtk.Notifications"),
      64                 :            :                                        G_VARIANT_TYPE ("(s)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
      65                 :            : 
      66                 :          1 :   g_object_unref (session_bus);
      67                 :            : 
      68         [ +  - ]:          1 :   if (reply)
      69                 :            :     {
      70                 :          1 :       g_variant_unref (reply);
      71                 :          1 :       return TRUE;
      72                 :            :     }
      73                 :            :   else
      74                 :          0 :     return FALSE;
      75                 :            : }
      76                 :            : 
      77                 :            : static void
      78                 :          5 : g_gtk_notification_backend_send_notification (GNotificationBackend *backend,
      79                 :            :                                               const gchar          *id,
      80                 :            :                                               GNotification        *notification)
      81                 :            : {
      82                 :            :   GVariant *params;
      83                 :            : 
      84                 :          5 :   params = g_variant_new ("(ss@a{sv})", g_application_get_application_id (backend->application),
      85                 :            :                                         id,
      86                 :            :                                         g_notification_serialize (notification));
      87                 :            : 
      88                 :          5 :   g_dbus_connection_call (backend->dbus_connection,
      89                 :            :                           "org.gtk.Notifications", "/org/gtk/Notifications",
      90                 :            :                           "org.gtk.Notifications", "AddNotification", params,
      91                 :            :                           G_VARIANT_TYPE_UNIT,
      92                 :            :                           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
      93                 :          5 : }
      94                 :            : 
      95                 :            : static void
      96                 :          1 : g_gtk_notification_backend_withdraw_notification (GNotificationBackend *backend,
      97                 :            :                                                   const gchar          *id)
      98                 :            : {
      99                 :            :   GVariant *params;
     100                 :            : 
     101                 :          1 :   params = g_variant_new ("(ss)", g_application_get_application_id (backend->application), id);
     102                 :            : 
     103                 :          1 :   g_dbus_connection_call (backend->dbus_connection, "org.gtk.Notifications",
     104                 :            :                           "/org/gtk/Notifications", "org.gtk.Notifications",
     105                 :            :                           "RemoveNotification", params, G_VARIANT_TYPE_UNIT,
     106                 :            :                           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
     107                 :          1 : }
     108                 :            : 
     109                 :            : static void
     110                 :          1 : g_gtk_notification_backend_init (GGtkNotificationBackend *backend)
     111                 :            : {
     112                 :          1 : }
     113                 :            : 
     114                 :            : static void
     115                 :          1 : g_gtk_notification_backend_class_init (GGtkNotificationBackendClass *class)
     116                 :            : {
     117                 :          1 :   GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
     118                 :            : 
     119                 :          1 :   backend_class->is_supported = g_gtk_notification_backend_is_supported;
     120                 :          1 :   backend_class->send_notification = g_gtk_notification_backend_send_notification;
     121                 :          1 :   backend_class->withdraw_notification = g_gtk_notification_backend_withdraw_notification;
     122                 :          1 : }

Generated by: LCOV version 1.14