|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: GPL-3.0-or-later |
| 3 | + * SPDX-FileCopyrightText: 2016 Rico Tzschichholz |
| 4 | + * 2025 elementary, Inc. (https://elementary.io) |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Gala.NotificationsManager can be used to send notifications to org.freedesktop.Notifications |
| 9 | + */ |
| 10 | +public class Gala.NotificationsManager : GLib.Object { |
| 11 | + [DBus (name = "org.freedesktop.Notifications")] |
| 12 | + private interface DBusNotifications : GLib.Object { |
| 13 | + public abstract async uint32 notify (string app_name, uint32 replaces_id, string app_icon, string summary, |
| 14 | + string body, string[] actions, HashTable<string, Variant> hints, int32 expire_timeout) throws DBusError, IOError; |
| 15 | + } |
| 16 | + |
| 17 | + private const int EXPIRE_TIMEOUT = 2000; |
| 18 | + |
| 19 | + private DBusNotifications? notifications = null; |
| 20 | + private GLib.HashTable<string, uint32> replaces_id_table = new GLib.HashTable<string, uint32> (str_hash, str_equal); |
| 21 | + |
| 22 | + construct { |
| 23 | + Bus.watch_name (BusType.SESSION, "org.freedesktop.Notifications", BusNameWatcherFlags.NONE, on_watch, on_unwatch); |
| 24 | + } |
| 25 | + |
| 26 | + private void on_watch (DBusConnection connection) { |
| 27 | + connection.get_proxy.begin<DBusNotifications> ( |
| 28 | + "org.freedesktop.Notifications", "/org/freedesktop/Notifications", DBusProxyFlags.NONE, null, |
| 29 | + (obj, res) => { |
| 30 | + try { |
| 31 | + notifications = ((DBusConnection) obj).get_proxy.end<DBusNotifications> (res); |
| 32 | + } catch (Error e) { |
| 33 | + warning ("NotificationsManager: Couldn't connect to notifications server: %s", e.message); |
| 34 | + notifications = null; |
| 35 | + } |
| 36 | + } |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + private void on_unwatch (DBusConnection conn) { |
| 41 | + warning ("NotificationsManager: Lost connection to notifications server"); |
| 42 | + notifications = null; |
| 43 | + } |
| 44 | + |
| 45 | + public async void send ( |
| 46 | + string component_name, |
| 47 | + string icon, |
| 48 | + string summary, |
| 49 | + string body, |
| 50 | + GLib.HashTable<string, Variant> hints |
| 51 | + ) { |
| 52 | + if (notifications == null) { |
| 53 | + warning ("NotificationsManager: Unable to send notification. No connection to notification server"); |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + uint32? replaces_id = replaces_id_table.get (component_name); |
| 58 | + if (replaces_id == null) { |
| 59 | + replaces_id = 0; |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + var notification_id = yield notifications.notify ( |
| 64 | + "gala-feedback", |
| 65 | + replaces_id, |
| 66 | + icon, |
| 67 | + summary, |
| 68 | + body, |
| 69 | + {}, |
| 70 | + hints, |
| 71 | + EXPIRE_TIMEOUT |
| 72 | + ); |
| 73 | + |
| 74 | + replaces_id_table.insert (component_name, notification_id); |
| 75 | + } catch (Error e) { |
| 76 | + critical ("NotificationsManager: There was an error sending a notification: %s", e.message); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments