From b3260921526ff3d6934d95beba076948fa3fa04d Mon Sep 17 00:00:00 2001 From: Gustavo Marques Date: Sun, 23 Jul 2023 13:49:39 -0300 Subject: [PATCH] remove Session.vala session storage while using the org.freedesktop.Notifications interface is flawed and broken. notifications can't be closed by apps and actions cannot be activated after the application exits or there's a system reset. so remove the between session capabilities from the indicator. Signed-off-by: Gustavo Marques --- meson.build | 1 - po/POTFILES | 1 - src/Indicator.vala | 22 +--- src/Services/Notification.vala | 24 ---- src/Services/Session.vala | 173 ----------------------------- src/Widgets/AppEntry.vala | 2 - src/Widgets/NotificationsList.vala | 13 +-- 7 files changed, 5 insertions(+), 231 deletions(-) delete mode 100644 src/Services/Session.vala diff --git a/meson.build b/meson.build index 475d8e70..d8395d22 100644 --- a/meson.build +++ b/meson.build @@ -45,7 +45,6 @@ shared_module( 'src/Widgets/AppEntry.vala', 'src/Services/NotificationsMonitor.vala', 'src/Services/Notification.vala', - 'src/Services/Session.vala', 'src/Services/Interfaces.vala', gresource, config_vala, diff --git a/po/POTFILES b/po/POTFILES index dcb2a911..13c9c13a 100644 --- a/po/POTFILES +++ b/po/POTFILES @@ -4,5 +4,4 @@ src/Widgets/NotificationEntry.vala src/Widgets/AppEntry.vala src/Services/NotificationsMonitor.vala src/Services/Notification.vala -src/Services/Session.vala src/Services/Interfaces.vala diff --git a/src/Indicator.vala b/src/Indicator.vala index 8c493058..799371de 100644 --- a/src/Indicator.vala +++ b/src/Indicator.vala @@ -27,7 +27,6 @@ public class Notifications.Indicator : Wingpanel.Indicator { private Gtk.ModelButton clear_all_btn; private Gtk.Spinner? dynamic_icon = null; private NotificationsList nlist; - private List previous_session = null; public Indicator () { Object ( @@ -77,27 +76,14 @@ public class Notifications.Indicator : Wingpanel.Indicator { return Gdk.EVENT_PROPAGATE; }); - previous_session = Session.get_instance ().get_session_notifications (); - Timeout.add (2000, () => { // Do not block animated drawing of wingpanel - load_session_notifications.begin (() => { // load asynchromously so spinner continues to rotate - set_display_icon_name (); - nlist.add.connect (set_display_icon_name); - nlist.remove.connect (set_display_icon_name); - }); - - return Source.REMOVE; - }); + set_display_icon_name (); + nlist.add.connect (set_display_icon_name); + nlist.remove.connect (set_display_icon_name); } return dynamic_icon; } - private async void load_session_notifications () { - foreach (var notification in previous_session) { - yield nlist.add_entry (notification, false); // This is slow as NotificationEntry is complex - } - } - public override Gtk.Widget? get_widget () { if (main_box == null) { var not_disturb_switch = new Granite.SwitchModelButton (_("Do Not Disturb")); @@ -177,7 +163,7 @@ public class Notifications.Indicator : Wingpanel.Indicator { } if (app_settings == null || app_settings.get_boolean (REMEMBER_KEY)) { - nlist.add_entry.begin (notification, true); + nlist.add_entry (notification); } set_display_icon_name (); diff --git a/src/Services/Notification.vala b/src/Services/Notification.vala index 4a6b20e8..559af627 100644 --- a/src/Services/Notification.vala +++ b/src/Services/Notification.vala @@ -35,7 +35,6 @@ public class Notifications.Notification : Object { public string[] actions; public uint32 replaces_id; public uint32 id; - public bool has_temp_file; public GLib.DateTime timestamp; public GLib.Icon badge_icon { get; construct set; } @@ -59,28 +58,6 @@ public class Notifications.Notification : Object { private const string DESKTOP_ENTRY_KEY = "desktop-entry"; private const string FALLBACK_DESKTOP_ID = "gala-other" + DESKTOP_ID_EXT; - public Notification ( - uint32 _id, string _app_name, string _app_icon, string _summary, string _message_body, string _image_path, - string[] _actions, string _desktop_id, int64 _unix_time, uint64 _replaces_id, string _sender, bool _has_temp_file - ) { - app_name = _app_name; - app_icon = _app_icon; - summary = _summary; - message_body = _message_body; - image_path = _image_path; - replaces_id = (uint32) _replaces_id; - id = _id; - sender = _sender; - - actions = _actions; - timestamp = new GLib.DateTime.from_unix_local (_unix_time); - - desktop_id = _desktop_id; - app_info = new DesktopAppInfo (desktop_id); - - has_temp_file = _has_temp_file; - } - public Notification.from_message (DBusMessage message, uint32 _id) { var body = message.get_body (); @@ -131,7 +108,6 @@ public class Notifications.Notification : Object { var tmpfile = store_pixbuf (buf); if (tmpfile != null) { image_path = tmpfile; - has_temp_file = true; } } diff --git a/src/Services/Session.vala b/src/Services/Session.vala deleted file mode 100644 index d741b7fd..00000000 --- a/src/Services/Session.vala +++ /dev/null @@ -1,173 +0,0 @@ -/*- - * Copyright 2015-2021 elementary, Inc. (https://elementary.io) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Library General Public License as published by - * the Free Software Foundation, either version 2.1 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library General Public License for more details. - * - * You should have received a copy of the GNU Library General Public License - * along with this program. If not, see . - */ - - -/* This class is meant to remember the - * notifications from the current session - * and restore them in the next session. - * - */ -public class Notifications.Session : GLib.Object { - private const string SESSION_FILE_NAME = ".notifications.session"; - private static Session? instance = null; - - private File session_file = null; - - private const string ACTIONS_KEY = "Actions"; - private const string APP_ICON_KEY = "AppIcon"; - private const string APP_NAME_KEY = "AppName"; - private const string BODY_KEY = "Body"; - private const string IMAGE_KEY = "Image"; - private const string DESKTOP_ID_KEY = "DesktopID"; - private const string REPLACES_ID_KEY = "ReplacesID"; - private const string SENDER_KEY = "Sender"; - private const string SUMMARY_KEY = "Summary"; - private const string UNIX_TIME_KEY = "UnixTime"; - private const string HAS_TEMP_FILE_KEY = "HasTempFile"; - - private KeyFile key; - - public static Session get_instance () { - if (instance == null) { - instance = new Session (); - } - - return instance; - } - - private Session () { - session_file = File.new_for_path (Path.build_filename (Environment.get_user_cache_dir (), SESSION_FILE_NAME)); - if (!session_file.query_exists ()) { - create_session_file (); - } - - key = new KeyFile (); - key.set_list_separator (';'); - } - - public List get_session_notifications () { - var list = new List (); - try { - key.load_from_file (session_file.get_path (), KeyFileFlags.NONE); - foreach (unowned string group in key.get_groups ()) { - var notification = new Notification ( - (uint32)int.parse (group), - key.get_string (group, APP_NAME_KEY), - key.get_string (group, APP_ICON_KEY), - key.get_string (group, SUMMARY_KEY), - key.get_string (group, BODY_KEY), - key.get_string (group, IMAGE_KEY), - key.get_string_list (group, ACTIONS_KEY), - key.get_string (group, DESKTOP_ID_KEY), - key.get_int64 (group, UNIX_TIME_KEY), - key.get_uint64 (group, REPLACES_ID_KEY), - key.get_string (group, SENDER_KEY), - key.get_boolean (group, HAS_TEMP_FILE_KEY) - ); - list.prepend (notification); - } - } catch (KeyFileError e) { - warning (e.message); - } catch (FileError e) { - warning (e.message); - } - - list.reverse (); - return list; - } - - public void add_notification (Notification notification, bool write_file = true) { - string id = notification.id.to_string (); - key.set_int64 (id, UNIX_TIME_KEY, notification.timestamp.to_unix ()); - key.set_string (id, APP_ICON_KEY, notification.app_icon); - key.set_string (id, APP_NAME_KEY, notification.app_name); - key.set_string (id, BODY_KEY, notification.message_body); - key.set_string (id, IMAGE_KEY, notification.image_path); - key.set_string (id, DESKTOP_ID_KEY, notification.desktop_id); - key.set_string (id, SENDER_KEY, notification.sender); - key.set_string (id, SUMMARY_KEY, notification.summary); - key.set_string_list (id, ACTIONS_KEY, notification.actions); - key.set_uint64 (id, REPLACES_ID_KEY, notification.replaces_id); - key.set_boolean (id, HAS_TEMP_FILE_KEY, notification.has_temp_file); - - if (write_file) { - write_contents (); - } - } - - public void remove_notification (Notification notification, bool write_file = true) { - try { - if (notification.has_temp_file) { - var file = File.new_for_path (notification.image_path); - try { - file.delete (); - } catch (Error e) { - critical ("Unable to delete tmpfile: %s", notification.image_path); - } - } - key.remove_group (notification.id.to_string ()); - } catch (KeyFileError e) { - warning (e.message); - } - - if (write_file) { - write_contents (); - } - } - - public void remove_notifications (Notification[] notifications) { - foreach (unowned var notification in notifications) { - remove_notification (notification, false); // Only update file once - } - - write_contents (); - } - - public void clear () { - try { - FileUtils.set_contents (session_file.get_path (), ""); - } catch (FileError e) { - warning (e.message); - } - } - - private void create_session_file () { - try { - session_file.create (FileCreateFlags.REPLACE_DESTINATION); - } catch (Error e) { - warning (e.message); - } - } - - private uint write_timeout_id = 0; - private void write_contents () { - if (write_timeout_id > 0) { - Source.remove (write_timeout_id); - } - - write_timeout_id = Timeout.add (1000, () => { - write_timeout_id = 0; - try { - FileUtils.set_contents (session_file.get_path (), key.to_data ()); - } catch (FileError e) { - warning (e.message); - } - - return Source.REMOVE; - }); - } -} diff --git a/src/Widgets/AppEntry.vala b/src/Widgets/AppEntry.vala index ca3b06a3..0f84ccfd 100644 --- a/src/Widgets/AppEntry.vala +++ b/src/Widgets/AppEntry.vala @@ -76,7 +76,6 @@ public class Notifications.AppEntry : Gtk.ListBoxRow { app_notifications.remove (entry); entry.dismiss (); - Session.get_instance ().remove_notification (entry.notification); if (app_notifications.length () == 0) { clear (); } @@ -90,6 +89,5 @@ public class Notifications.AppEntry : Gtk.ListBoxRow { }); app_notifications = new List (); - Session.get_instance ().remove_notifications (to_remove); } } diff --git a/src/Widgets/NotificationsList.vala b/src/Widgets/NotificationsList.vala index d0082273..2160ab55 100644 --- a/src/Widgets/NotificationsList.vala +++ b/src/Widgets/NotificationsList.vala @@ -46,7 +46,7 @@ public class Notifications.NotificationsList : Gtk.ListBox { row_activated.connect (on_row_activated); } - public async void add_entry (Notification notification, bool add_to_session = true, bool write_file = true) { + public void add_entry (Notification notification) { var entry = new NotificationEntry (notification); if (app_entries[notification.desktop_id] != null) { @@ -70,13 +70,6 @@ public class Notifications.NotificationsList : Gtk.ListBox { } show_all (); - - Idle.add (add_entry.callback); - yield; - - if (add_to_session) { // If notification was obtained from session do not write it back - Session.get_instance ().add_notification (notification); - } } public uint count_notifications (out uint number_of_apps) { @@ -121,10 +114,6 @@ public class Notifications.NotificationsList : Gtk.ListBox { app_entries.unset (app_entry.app_id); app_entry.clear_all_notification_entries (); app_entry.destroy (); - - if (app_entries.size == 0) { - Session.get_instance ().clear (); - } } private void on_row_activated (Gtk.ListBoxRow row) {