Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gio/ggtknotificationbackend.c
Line
Count
Source (jump to first uncovered line)
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 "gdbusprivate.h"
26
#include "gapplication.h"
27
#include "gnotification-private.h"
28
29
#define G_TYPE_GTK_NOTIFICATION_BACKEND  (g_gtk_notification_backend_get_type ())
30
#define G_GTK_NOTIFICATION_BACKEND(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_GTK_NOTIFICATION_BACKEND, GGtkNotificationBackend))
31
32
typedef struct _GGtkNotificationBackend GGtkNotificationBackend;
33
typedef GNotificationBackendClass       GGtkNotificationBackendClass;
34
35
struct _GGtkNotificationBackend
36
{
37
  GNotificationBackend parent;
38
};
39
40
GType g_gtk_notification_backend_get_type (void);
41
42
G_DEFINE_TYPE_WITH_CODE (GGtkNotificationBackend, g_gtk_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
43
  _g_io_modules_ensure_extension_points_registered ();
44
  g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
45
                                 g_define_type_id, "gtk", 100))
46
47
static gboolean
48
g_gtk_notification_backend_is_supported (void)
49
0
{
50
0
  GDBusConnection *session_bus;
51
0
  GVariant *reply;
52
53
  /* Find out if the notification server is running. This is a
54
   * synchronous call because gio extension points don't support async
55
   * backend verification. This is only run once and only contacts the
56
   * dbus daemon. */
57
58
0
  session_bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
59
0
  if (session_bus == NULL)
60
0
    return FALSE;
61
62
0
  reply = g_dbus_connection_call_sync (session_bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS,
63
0
                                       DBUS_INTERFACE_DBUS,
64
0
                                       "GetNameOwner", g_variant_new ("(s)", "org.gtk.Notifications"),
65
0
                                       G_VARIANT_TYPE ("(s)"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL);
66
67
0
  g_object_unref (session_bus);
68
69
0
  if (reply)
70
0
    {
71
0
      g_variant_unref (reply);
72
0
      return TRUE;
73
0
    }
74
0
  else
75
0
    return FALSE;
76
0
}
77
78
static void
79
g_gtk_notification_backend_send_notification (GNotificationBackend *backend,
80
                                              const gchar          *id,
81
                                              GNotification        *notification)
82
0
{
83
0
  GVariant *params;
84
85
0
  params = g_variant_new ("(ss@a{sv})", g_application_get_application_id (backend->application),
86
0
                                        id,
87
0
                                        g_notification_serialize (notification));
88
89
0
  g_dbus_connection_call (backend->dbus_connection,
90
0
                          "org.gtk.Notifications", "/org/gtk/Notifications",
91
0
                          "org.gtk.Notifications", "AddNotification", params,
92
0
                          G_VARIANT_TYPE_UNIT,
93
0
                          G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
94
0
}
95
96
static void
97
g_gtk_notification_backend_withdraw_notification (GNotificationBackend *backend,
98
                                                  const gchar          *id)
99
0
{
100
0
  GVariant *params;
101
102
0
  params = g_variant_new ("(ss)", g_application_get_application_id (backend->application), id);
103
104
0
  g_dbus_connection_call (backend->dbus_connection, "org.gtk.Notifications",
105
0
                          "/org/gtk/Notifications", "org.gtk.Notifications",
106
0
                          "RemoveNotification", params, G_VARIANT_TYPE_UNIT,
107
0
                          G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
108
0
}
109
110
static void
111
g_gtk_notification_backend_init (GGtkNotificationBackend *backend)
112
0
{
113
0
}
114
115
static void
116
g_gtk_notification_backend_class_init (GGtkNotificationBackendClass *class)
117
0
{
118
0
  GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
119
120
0
  backend_class->is_supported = g_gtk_notification_backend_is_supported;
121
0
  backend_class->send_notification = g_gtk_notification_backend_send_notification;
122
0
  backend_class->withdraw_notification = g_gtk_notification_backend_withdraw_notification;
123
0
}