Skip to content

Commit

Permalink
Introduce NotificationsManager (#2237)
Browse files Browse the repository at this point in the history
  • Loading branch information
lenemter authored Jan 25, 2025
1 parent 4ed139b commit cc33dd6
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 114 deletions.
4 changes: 2 additions & 2 deletions src/DBus.vala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class Gala.DBus {
private static WindowManagerGala wm;

[DBus (visible = false)]
public static void init (WindowManagerGala _wm, ScreenshotManager screenshot_manager) {
public static void init (WindowManagerGala _wm, NotificationsManager notifications_manager, ScreenshotManager screenshot_manager) {
wm = _wm;

Bus.own_name (BusType.SESSION, "org.pantheon.gala", BusNameOwnerFlags.NONE,
Expand All @@ -33,7 +33,7 @@ public class Gala.DBus {
Bus.own_name (BusType.SESSION, "org.gnome.Shell", BusNameOwnerFlags.NONE,
(connection) => {
try {
connection.register_object ("/org/gnome/Shell", new DBusAccelerator (wm.get_display ()));
connection.register_object ("/org/gnome/Shell", new DBusAccelerator (wm.get_display (), notifications_manager));
connection.register_object ("/org/gnome/Shell/Screenshot", screenshot_manager);
} catch (Error e) { warning (e.message); }
},
Expand Down
18 changes: 16 additions & 2 deletions src/DBusAccelerator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ namespace Gala {

[DBus (name="org.gnome.Shell")]
public class DBusAccelerator {
private const string NOTIFICATION_COMPONENT_NAME = "DBusAccelerator";

public signal void accelerator_activated (uint action, GLib.HashTable<string, Variant> parameters);

private Meta.Display display;
private NotificationsManager notifications_manager;
private GLib.HashTable<unowned string, GrabbedAccelerator> grabbed_accelerators;

public DBusAccelerator (Meta.Display _display) {
public DBusAccelerator (Meta.Display _display, NotificationsManager _notifications_manager) {
display = _display;
notifications_manager = _notifications_manager;
grabbed_accelerators = new HashTable<unowned string, GrabbedAccelerator> (str_hash, str_equal);
display.accelerator_activated.connect (on_accelerator_activated);
}
Expand Down Expand Up @@ -166,7 +170,17 @@ namespace Gala {
level = (int)(double_level * 100);
}

MediaFeedback.send (icon, level);
var hints = new GLib.HashTable<string, Variant> (null, null);
hints.set ("x-canonical-private-synchronous", new Variant.string ("gala-feedback"));
hints.set ("value", new Variant.int32 (level));

notifications_manager.send.begin (
NOTIFICATION_COMPONENT_NAME,
icon,
label,
"",
hints
);
}
}
}
107 changes: 0 additions & 107 deletions src/MediaFeedback.vala

This file was deleted.

79 changes: 79 additions & 0 deletions src/NotificationsManager.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* SPDX-License-Identifier: GPL-3.0-or-later
* SPDX-FileCopyrightText: 2016 Rico Tzschichholz
* 2025 elementary, Inc. (https://elementary.io)
*/

/**
* Gala.NotificationsManager can be used to send notifications to org.freedesktop.Notifications
*/
public class Gala.NotificationsManager : GLib.Object {
[DBus (name = "org.freedesktop.Notifications")]
private interface DBusNotifications : GLib.Object {
public abstract async uint32 notify (string app_name, uint32 replaces_id, string app_icon, string summary,
string body, string[] actions, HashTable<string, Variant> hints, int32 expire_timeout) throws DBusError, IOError;
}

private const int EXPIRE_TIMEOUT = 2000;

private DBusNotifications? notifications = null;
private GLib.HashTable<string, uint32> replaces_id_table = new GLib.HashTable<string, uint32> (str_hash, str_equal);

construct {
Bus.watch_name (BusType.SESSION, "org.freedesktop.Notifications", BusNameWatcherFlags.NONE, on_watch, on_unwatch);
}

private void on_watch (DBusConnection connection) {
connection.get_proxy.begin<DBusNotifications> (
"org.freedesktop.Notifications", "/org/freedesktop/Notifications", DBusProxyFlags.NONE, null,
(obj, res) => {
try {
notifications = ((DBusConnection) obj).get_proxy.end<DBusNotifications> (res);
} catch (Error e) {
warning ("NotificationsManager: Couldn't connect to notifications server: %s", e.message);
notifications = null;
}
}
);
}

private void on_unwatch (DBusConnection conn) {
warning ("NotificationsManager: Lost connection to notifications server");
notifications = null;
}

public async void send (
string component_name,
string icon,
string summary,
string body,
GLib.HashTable<string, Variant> hints
) {
if (notifications == null) {
warning ("NotificationsManager: Unable to send notification. No connection to notification server");
return;
}

uint32? replaces_id = replaces_id_table.get (component_name);
if (replaces_id == null) {
replaces_id = 0;
}

try {
var notification_id = yield notifications.notify (
"gala-feedback",
replaces_id,
icon,
summary,
body,
{},
hints,
EXPIRE_TIMEOUT
);

replaces_id_table.insert (component_name, notification_id);
} catch (Error e) {
critical ("NotificationsManager: There was an error sending a notification: %s", e.message);
}
}
}
6 changes: 4 additions & 2 deletions src/WindowManager.vala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ namespace Gala {

public WindowTracker? window_tracker { get; private set; }

private NotificationsManager notifications_manager;

private ScreenshotManager screenshot_manager;

/**
Expand Down Expand Up @@ -190,9 +192,9 @@ namespace Gala {
private void show_stage () {
unowned Meta.Display display = get_display ();

notifications_manager = new NotificationsManager ();
screenshot_manager = new ScreenshotManager (this);
DBus.init (this, screenshot_manager);
MediaFeedback.init ();
DBus.init (this, notifications_manager, screenshot_manager);

WindowListener.init (display);
KeyboardManager.init (display);
Expand Down
2 changes: 1 addition & 1 deletion src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ gala_bin_sources = files(
'InternalUtils.vala',
'KeyboardManager.vala',
'Main.vala',
'MediaFeedback.vala',
'NotificationsManager.vala',
'NotificationStack.vala',
'PantheonShell.vala',
'PluginManager.vala',
Expand Down

0 comments on commit cc33dd6

Please sign in to comment.