Debian Codegraph @

modules/module-notifications-api.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2021 Collabora Ltd.
4 * @author Julian Bouzas <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include <wp/wp.h>
10 #include "dbus-connection-state.h"
11
12 WP_DEFINE_LOCAL_LOG_TOPIC ("m-notification")
13
14 #define DBUS_INTERFACE_NAME "org.freedesktop.Notifications"
15 #define DBUS_OBJECT_PATH "/org/freedesktop/Notifications"
16
17 enum
18 {
19 ACTION_GET_DBUS,
20 ACTION_SEND,
21 LAST_SIGNAL
22 };
23
24 static guint signals[LAST_SIGNAL] = { 0 };
25
26 struct _WpNotificationsPlugin
27 {
28 WpPlugin parent;
29
30 WpPlugin *dbus;
31 };
32
33 G_DECLARE_FINAL_TYPE (WpNotificationsPlugin,
34 wp_notifications_plugin, WP, NOTIFICATIONS_PLUGIN,
35 WpPlugin)
36 G_DEFINE_TYPE (WpNotificationsPlugin, wp_notifications_plugin,
37 WP_TYPE_PLUGIN)
38
39 static gpointer
40 wp_notifications_plugin_get_dbus (WpNotificationsPlugin *self)
41 {
42 return self->dbus ? g_object_ref (self->dbus) : NULL;
43 }
44
45 static void
46 wp_notifications_plugin_send (WpNotificationsPlugin *self,
47 const gchar *summary, const gchar *body_message)
48 {
49 g_autoptr (GDBusConnection) conn = NULL;
50 g_autoptr (GError) error = NULL;
51 g_autoptr (GVariant) res = NULL;
52 GVariantBuilder hints;
53
54 g_object_get (self->dbus, "connection", &conn, NULL);
55 g_return_if_fail (conn);
56
57 /* Set urgency */
58 g_variant_builder_init (&hints, G_VARIANT_TYPE("a{sv}"));
59 g_variant_builder_add (&hints, "{sv}", "urgency", g_variant_new_byte (0));
60
61 /* Notify */
62 res = g_dbus_connection_call_sync (conn, DBUS_INTERFACE_NAME,
63 DBUS_OBJECT_PATH, DBUS_INTERFACE_NAME, "Notify",
64 g_variant_new("(susssasa{sv}i)", "wireplumber", 0, "", summary,
65 body_message, NULL, &hints, -1), NULL, G_DBUS_CALL_FLAGS_NONE, -1,
66 NULL, &error);
67 if (error) {
68 g_autofree gchar *remote_error = g_dbus_error_get_remote_error (error);
69 g_dbus_error_strip_remote_error (error);
70
71 wp_warning_object (self, "Notify: %s (%s)", error->message, remote_error);
72 return;
73 }
74 }
75
76 static void
77 wp_notifications_plugin_init (WpNotificationsPlugin * self)
78 {
79 }
80
81 static void
82 wp_notifications_plugin_enable (WpPlugin * plugin,
83 WpTransition * transition)
84 {
85 WpNotificationsPlugin *self = WP_NOTIFICATIONS_PLUGIN (plugin);
86 g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self));
87
88 self->dbus = wp_plugin_find (core, "dbus-connection");
89 if (!self->dbus) {
90 wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY,
91 WP_LIBRARY_ERROR_INVARIANT,
92 "dbus-connection module must be loaded before notifications"));
93 return;
94 }
95
96 wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0);
97 }
98
99 static void
100 wp_notifications_plugin_disable (WpPlugin * plugin)
101 {
102 WpNotificationsPlugin *self = WP_NOTIFICATIONS_PLUGIN (plugin);
103
104 g_clear_object (&self->dbus);
105
106 wp_object_update_features (WP_OBJECT (self), 0, WP_PLUGIN_FEATURE_ENABLED);
107 }
108
109 static void
110 wp_notifications_plugin_class_init (WpNotificationsPluginClass * klass)
111 {
112 WpPluginClass *plugin_class = (WpPluginClass *) klass;
113
114 plugin_class->enable = wp_notifications_plugin_enable;
115 plugin_class->disable = wp_notifications_plugin_disable;
116
117 /**
118 * WpNotificationsPlugin::get-dbus:
119 *
120 * Returns: (transfer full): the dbus object
121 */
122 signals[ACTION_GET_DBUS] = g_signal_new_class_handler (
123 "get-dbus", G_TYPE_FROM_CLASS (klass),
124 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
125 (GCallback) wp_notifications_plugin_get_dbus,
126 NULL, NULL, NULL,
127 G_TYPE_OBJECT, 0);
128
129 /**
130 * WpNotificationsPlugin::send:
131 *
132 * @brief
133 * @em summary: the summary
134 * @em body_message: The body message
135 */
136 signals[ACTION_SEND] = g_signal_new_class_handler (
137 "send", G_TYPE_FROM_CLASS (klass),
138 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
139 (GCallback) wp_notifications_plugin_send,
140 NULL, NULL, NULL, G_TYPE_VARIANT,
141 2, G_TYPE_STRING, G_TYPE_STRING);
142 }
143
144 WP_PLUGIN_EXPORT GObject *
145 wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error)
146 {
147 return G_OBJECT (g_object_new (
148 wp_notifications_plugin_get_type(),
149 "name", "notifications-api",
150 "core", core,
151 NULL));
152 }