[geary/wip/766133-gnotification: 2/2] Clean up desktop notification code



commit c42971062975ad88a9a3ef31bc8ab33f15112a4c
Author: Michael Gratton <mike vee net>
Date:   Sat Apr 13 00:15:36 2019 +1000

    Clean up desktop notification code
    
    Rename class to reflect what it does, rather than how it is implemented.
    Modernise the code and make it fit with coding conventions better.

 po/POTFILES.in                                     |   2 +-
 src/client/application/geary-controller.vala       |  25 ++--
 src/client/meson.build                             |   2 +-
 ...notification.vala => notification-desktop.vala} | 147 ++++++++++++---------
 4 files changed, 106 insertions(+), 70 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 6025091a..5a9ebaf8 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -80,9 +80,9 @@ src/client/folder-list/folder-list-special-grouping.vala
 src/client/folder-list/folder-list-tree.vala
 src/client/notification/in-app-notification.vala
 src/client/notification/libmessagingmenu.vala
-src/client/notification/glib-notification.vala
 src/client/notification/new-messages-indicator.vala
 src/client/notification/new-messages-monitor.vala
+src/client/notification/notification-desktop.vala
 src/client/notification/null-indicator.vala
 src/client/notification/unity-launcher.vala
 src/client/sidebar/sidebar-branch.vala
diff --git a/src/client/application/geary-controller.vala b/src/client/application/geary-controller.vala
index 21c5fad7..7ff8f29b 100644
--- a/src/client/application/geary-controller.vala
+++ b/src/client/application/geary-controller.vala
@@ -137,6 +137,8 @@ public class GearyController : Geary.BaseObject {
         get; private set; default = new ContactListStoreCache();
     }
 
+    public Notification.Desktop? notifications { get; private set; default = null; }
+
     private Geary.Account? current_account = null;
     private Gee.Map<Geary.AccountInformation,AccountContext> accounts =
         new Gee.HashMap<Geary.AccountInformation,AccountContext>();
@@ -157,7 +159,6 @@ public class GearyController : Geary.BaseObject {
     private NewMessagesMonitor? new_messages_monitor = null;
     private NewMessagesIndicator? new_messages_indicator = null;
     private UnityLauncher? unity_launcher = null;
-    private GLibNotification? gLibNotification = null;
     private uint select_folder_timeout_id = 0;
     private int64 next_folder_select_allowed_usec = 0;
     private Geary.Nonblocking.Mutex select_folder_mutex = new Geary.Nonblocking.Mutex();
@@ -328,7 +329,10 @@ public class GearyController : Geary.BaseObject {
 
         unity_launcher = new UnityLauncher(new_messages_monitor);
 
-        gLibNotification = new GLibNotification(new_messages_monitor);
+        this.notifications = new Notification.Desktop(
+            this.new_messages_monitor,
+            this.application
+        );
 
         this.main_window.conversation_list_view.grab_focus();
 
@@ -442,7 +446,7 @@ public class GearyController : Geary.BaseObject {
         }
 
         // Release monitoring early so held resources can be freed up
-        this.gLibNotification = null;
+        this.notifications = null;
         this.new_messages_indicator = null;
         this.unity_launcher = null;
         this.new_messages_monitor.clear_folders();
@@ -706,9 +710,13 @@ public class GearyController : Geary.BaseObject {
         Geary.ServiceProblemReport? service_report =
             report as Geary.ServiceProblemReport;
         if (service_report != null && service_report.service.protocol == SMTP) {
-            gLibNotification.set_error_notification(
-                _("Error sending email"),
-                _("Geary encountered an error sending an email.  If the problem persists, please manually 
delete the email from your Outbox folder.")
+            this.notifications.set_error_notification(
+                /// Notification title.
+                _("A problem occurred sending email for %s").printf(
+                    service_report.account.display_name
+                ),
+                /// Notification body
+                _("Email will not be sent until re-connected")
             );
         }
     }
@@ -905,7 +913,6 @@ public class GearyController : Geary.BaseObject {
         if (folder.special_folder_type == Geary.SpecialFolderType.OUTBOX) {
             main_window.status_bar.deactivate_message(StatusBar.Message.OUTBOX_SEND_FAILURE);
             main_window.status_bar.deactivate_message(StatusBar.Message.OUTBOX_SAVE_SENT_MAIL_FAILED);
-            gLibNotification.clear_error_notification();
         }
     }
 
@@ -2613,7 +2620,7 @@ public class GearyController : Geary.BaseObject {
         ).printf(Util.Email.to_short_recipient_display(rfc822.to));
         InAppNotification notification = new InAppNotification(message);
         this.main_window.add_notification(notification);
-        GLibNotification.play_sound("message-sent-email");
+        this.notifications.play_sound("message-sent-email");
     }
 
     private void on_conversation_view_added(ConversationListBox list) {
@@ -2945,7 +2952,7 @@ public class GearyController : Geary.BaseObject {
 
                 case Geary.Protocol.SMTP:
                     context.account.outgoing.restart.begin(context.cancellable);
-                    gLibNotification.clear_error_notification();
+                    this.notifications.clear_error_notification();
                     break;
                 }
             }
diff --git a/src/client/meson.build b/src/client/meson.build
index 52740edb..9f8d9eaf 100644
--- a/src/client/meson.build
+++ b/src/client/meson.build
@@ -81,7 +81,7 @@ geary_client_vala_sources = files(
   'folder-list/folder-list-search-branch.vala',
   'folder-list/folder-list-special-grouping.vala',
 
-  'notification/glib-notification.vala',
+  'notification/notification-desktop.vala',
   'notification/in-app-notification.vala',
   'notification/libmessagingmenu.vala',
   'notification/new-messages-indicator.vala',
diff --git a/src/client/notification/glib-notification.vala 
b/src/client/notification/notification-desktop.vala
similarity index 58%
rename from src/client/notification/glib-notification.vala
rename to src/client/notification/notification-desktop.vala
index ca1d8b32..e00cfe68 100644
--- a/src/client/notification/glib-notification.vala
+++ b/src/client/notification/notification-desktop.vala
@@ -4,38 +4,81 @@
  * (version 2.1 or later).  See the COPYING file in this distribution.
  */
 
-// Displays a notification via GLibNotification
-public class GLibNotification : Geary.BaseObject {
+/**
+ * Manages standard desktop application notifications.
+ */
+public class Notification.Desktop : Geary.BaseObject {
+
+
     public const Geary.Email.Field REQUIRED_FIELDS =
         Geary.Email.Field.ORIGINATORS | Geary.Email.Field.SUBJECT;
 
+    private const string ARRIVED_ID = "email-arrived";
+    private const string ERROR_ID = "error";
+
+
+    private static void init_sound() {
+        if (Desktop.sound_context == null) {
+            Canberra.Context.create(out sound_context);
+        }
+    }
+
     private static Canberra.Context? sound_context = null;
 
     private weak NewMessagesMonitor monitor;
+    private weak GearyApplication application;
     private GLib.Notification? current_notification = null;
     private GLib.Notification? error_notification = null;
     private Geary.Folder? folder = null;
     private Geary.Email? email = null;
 
 
-    public GLibNotification(NewMessagesMonitor monitor) {
+    public Desktop(NewMessagesMonitor monitor,
+                   GearyApplication application) {
         this.monitor = monitor;
+        this.application = application;
         init_sound();
 
         this.monitor.add_required_fields(REQUIRED_FIELDS);
         this.monitor.new_messages_arrived.connect(on_new_messages_arrived);
     }
 
-    ~GLibNotification() {
+    ~Desktop() {
         this.monitor.new_messages_arrived.disconnect(on_new_messages_arrived);
     }
 
-    private static void init_sound() {
-        if (sound_context == null)
-            Canberra.Context.create(out sound_context);
+    public void play_sound(string sound) {
+        if (this.application.config.play_sounds) {
+            Desktop.sound_context.play(
+                0, Canberra.PROP_EVENT_ID, sound
+            );
+        }
+    }
+
+    public void clear_arrived_notification() {
+        this.application.withdraw_notification(ARRIVED_ID);
+        this.current_notification = null;
+    }
+
+    public void set_error_notification(string summary, string body) {
+        // Only one error at a time, guys.  (This means subsequent errors will
+        // be dropped.  Since this is only used for one thing now, that's ok,
+        // but it means in the future, a more robust system will be needed.)
+        if (this.error_notification == null) {
+            this.error_notification = issue_notification(
+                ERROR_ID, summary, body, null, null
+            );
+        }
+    }
+
+    public void clear_error_notification() {
+        this.error_notification = null;
+        this.application.withdraw_notification(ERROR_ID);
     }
 
-    private void on_new_messages_arrived(Geary.Folder folder, int total, int added) {
+    private void on_new_messages_arrived(Geary.Folder folder,
+                                         int total,
+                                         int added) {
         if (added == 1 && monitor.last_new_message_folder != null &&
             monitor.last_new_message != null) {
             notify_one_message_async.begin(
@@ -51,20 +94,30 @@ public class GLibNotification : Geary.BaseObject {
     private void notify_new_mail(Geary.Folder folder, int added) {
         // don't pass email if invoked
         this.folder = null;
-        email = null;
-
-        if (!GearyApplication.instance.config.show_notifications ||
-            !monitor.should_notify_new_messages(folder))
-            return;
+        this.email = null;
+
+        if (this.application.config.show_notifications &&
+            this.monitor.should_notify_new_messages(folder)) {
+            string body = ngettext(
+                /// Notification body text for new email when no other
+                /// new messages are already awaiting.
+                "%d new message", "%d new messages", added
+            ).printf(added);
+            int total = monitor.get_new_message_count(folder);
+            if (total > added) {
+                body = ngettext(
+                    /// Notification body text for new email when
+                    /// other new messages have already been notified
+                    /// about
+                    "%s, %d new message total", "%s, %d new messages total",
+                    total
+                ).printf(body, total);
+            }
 
-        string body = ngettext("%d new message", "%d new messages", added).printf(added);
-        int total = monitor.get_new_message_count(folder);
-        if (total > added) {
-            body = ngettext("%s, %d new message total", "%s, %d new messages total", total).printf(
-                body, total);
+            issue_current_notification(
+                this.folder.account.information.display_name, body, null
+            );
         }
-
-        issue_current_notification(folder.account.information.display_name, body, null);
     }
 
     private async void notify_one_message_async(Geary.Folder folder,
@@ -74,13 +127,11 @@ public class GLibNotification : Geary.BaseObject {
         this.folder = folder;
         this.email = email;
 
-        if (!GearyApplication.instance.config.show_notifications ||
-            !monitor.should_notify_new_messages(folder))
-            return;
-
         Geary.RFC822.MailboxAddress? originator =
             Util.Email.get_primary_originator(email);
-        if (originator != null) {
+        if (this.application.config.show_notifications &&
+            this.monitor.should_notify_new_messages(folder) &&
+            originator != null) {
             Application.ContactStore contacts =
                 this.monitor.get_contact_store(folder.account);
             Application.Contact contact = yield contacts.load(
@@ -121,53 +172,31 @@ public class GLibNotification : Geary.BaseObject {
 
     private void issue_current_notification(string summary, string body, Gdk.Pixbuf? icon) {
         // only one outstanding notification at a time
-        if (current_notification != null) {
-            GearyApplication.instance.withdraw_notification("email.arrived");
-            current_notification = null;
-        }
-
-        current_notification = issue_notification("email.arrived", summary, body, icon, "message-new_email");
-
+        clear_arrived_notification();
+        this.current_notification = issue_notification(
+            ARRIVED_ID, summary, body, icon, "message-new_email"
+        );
     }
 
-    private GLib.Notification issue_notification(string id, string summary, string body,
-        Gdk.Pixbuf? icon, string? sound) {
+    private GLib.Notification issue_notification(string id,
+                                                 string summary,
+                                                 string body,
+                                                 Gdk.Pixbuf? icon,
+                                                 string? sound) {
         GLib.Notification notification = new GLib.Notification(summary);
         notification.set_body(body);
         //notification.set_default_action("app.activate");
 
-        if (icon != null)
+        if (icon != null) {
             notification.set_icon(icon);
+        }
 
         if (sound != null) {
             play_sound(sound);
         }
 
-        GearyApplication.instance.send_notification(id, notification);
-
+        this.application.send_notification(id, notification);
         return notification;
     }
 
-    public static void play_sound(string sound) {
-        if (!GearyApplication.instance.config.play_sounds)
-            return;
-
-        init_sound();
-        sound_context.play(0, Canberra.PROP_EVENT_ID, sound);
-    }
-
-    public void set_error_notification(string summary, string body) {
-        // Only one error at a time, guys.  (This means subsequent errors will
-        // be dropped.  Since this is only used for one thing now, that's ok,
-        // but it means in the future, a more robust system will be needed.)
-        if (error_notification != null)
-            return;
-
-        error_notification = issue_notification("error", summary, body, null, null);
-    }
-
-    public void clear_error_notification() {
-        GearyApplication.instance.withdraw_notification("error");
-        error_notification = null;
-    }
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]