Imported Upstream version 2.66.6
[platform/upstream/glib.git] / gio / gportalnotificationbackend.c
1 /*
2 * Copyright © 2016 Red Hat, Inc.
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 * Author: Matthias Clasen
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 #include "gportalsupport.h"
28
29 #define G_TYPE_PORTAL_NOTIFICATION_BACKEND  (g_portal_notification_backend_get_type ())
30 #define G_PORTAL_NOTIFICATION_BACKEND(o)    (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_PORTAL_NOTIFICATION_BACKEND, GPortalNotificationBackend))
31
32 typedef struct _GPortalNotificationBackend GPortalNotificationBackend;
33 typedef GNotificationBackendClass       GPortalNotificationBackendClass;
34
35 struct _GPortalNotificationBackend
36 {
37   GNotificationBackend parent;
38 };
39
40 GType g_portal_notification_backend_get_type (void);
41
42 G_DEFINE_TYPE_WITH_CODE (GPortalNotificationBackend, g_portal_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, "portal", 110))
46
47 static gboolean
48 g_portal_notification_backend_is_supported (void)
49 {
50   return glib_should_use_portal ();
51 }
52
53 static void
54 g_portal_notification_backend_send_notification (GNotificationBackend *backend,
55                                                  const gchar          *id,
56                                                  GNotification        *notification)
57 {
58   g_dbus_connection_call (backend->dbus_connection,
59                           "org.freedesktop.portal.Desktop",
60                           "/org/freedesktop/portal/desktop",
61                           "org.freedesktop.portal.Notification",
62                           "AddNotification",
63                           g_variant_new ("(s@a{sv})",
64                                          id,
65                                          g_notification_serialize (notification)),
66                           G_VARIANT_TYPE_UNIT,
67                           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
68 }
69
70 static void
71 g_portal_notification_backend_withdraw_notification (GNotificationBackend *backend,
72                                                      const gchar          *id)
73 {
74   g_dbus_connection_call (backend->dbus_connection,
75                           "org.freedesktop.portal.Desktop",
76                           "/org/freedesktop/portal/desktop",
77                           "org.freedesktop.portal.Notification",
78                           "RemoveNotification",
79                           g_variant_new ("(s)", id),
80                           G_VARIANT_TYPE_UNIT,
81                           G_DBUS_CALL_FLAGS_NONE, -1, NULL, NULL, NULL);
82 }
83
84 static void
85 g_portal_notification_backend_init (GPortalNotificationBackend *backend)
86 {
87 }
88
89 static void
90 g_portal_notification_backend_class_init (GPortalNotificationBackendClass *class)
91 {
92   GNotificationBackendClass *backend_class = G_NOTIFICATION_BACKEND_CLASS (class);
93
94   backend_class->is_supported = g_portal_notification_backend_is_supported;
95   backend_class->send_notification = g_portal_notification_backend_send_notification;
96   backend_class->withdraw_notification = g_portal_notification_backend_withdraw_notification;
97 }