Imported Upstream version 2.73.3
[platform/upstream/glib.git] / gio / gportalnotificationbackend.c
1 /*
2 * Copyright © 2016 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Matthias Clasen
20 */
21
22 #include "config.h"
23 #include "gnotificationbackend.h"
24
25 #include "giomodule-priv.h"
26 #include "gdbusconnection.h"
27 #include "gapplication.h"
28 #include "gnotification-private.h"
29 #include "gportalsupport.h"
30
31 #define G_TYPE_PORTAL_NOTIFICATION_BACKEND  (g_portal_notification_backend_get_type ())
32 #define G_PORTAL_NOTIFICATION_BACKEND(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_PORTAL_NOTIFICATION_BACKEND, GPortalNotificationBackend))
33
34 typedef struct _GPortalNotificationBackend GPortalNotificationBackend;
35 typedef GNotificationBackendClass       GPortalNotificationBackendClass;
36
37 struct _GPortalNotificationBackend
38 {
39   GNotificationBackend parent;
40 };
41
42 GType g_portal_notification_backend_get_type (void);
43
44 G_DEFINE_TYPE_WITH_CODE (GPortalNotificationBackend, g_portal_notification_backend, G_TYPE_NOTIFICATION_BACKEND,
45   _g_io_modules_ensure_extension_points_registered ();
46   g_io_extension_point_implement (G_NOTIFICATION_BACKEND_EXTENSION_POINT_NAME,
47                                  g_define_type_id, "portal", 110))
48
49 static gboolean
50 g_portal_notification_backend_is_supported (void)
51 {
52   return glib_should_use_portal ();
53 }
54
55 static void
56 g_portal_notification_backend_send_notification (GNotificationBackend *backend,
57                                                  const gchar          *id,
58                                                  GNotification        *notification)
59 {
60   g_dbus_connection_call (backend->dbus_connection,
61                           "org.freedesktop.portal.Desktop",
62                           "/org/freedesktop/portal/desktop",
63                           "org.freedesktop.portal.Notification",
64                           "AddNotification",
65                           g_variant_new ("(s@a{sv})",
66                                          id,
67                                          g_notification_serialize (notification)),
68                           G_VARIANT_TYPE_UNIT,
69                           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
70 }
71
72 static void
73 g_portal_notification_backend_withdraw_notification (GNotificationBackend *backend,
74                                                      const gchar          *id)
75 {
76   g_dbus_connection_call (backend->dbus_connection,
77                           "org.freedesktop.portal.Desktop",
78                           "/org/freedesktop/portal/desktop",
79                           "org.freedesktop.portal.Notification",
80                           "RemoveNotification",
81                           g_variant_new ("(s)", id),
82                           G_VARIANT_TYPE_UNIT,
83                           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
84 }
85
86 static void
87 g_portal_notification_backend_init (GPortalNotificationBackend *backend)
88 {
89 }
90
91 static void
92 g_portal_notification_backend_class_init (GPortalNotificationBackendClass *class)
93 {
94   GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
95
96   backend_class->is_supported = g_portal_notification_backend_is_supported;
97   backend_class->send_notification = g_portal_notification_backend_send_notification;
98   backend_class->withdraw_notification = g_portal_notification_backend_withdraw_notification;
99 }