[polari/wip/bastianilso/error-handling] Show notifications on a per room/server basis



commit 695a689c4c1b49fbc55b790a458c002c2a8e2f45
Author: Bastian Ilsø <bastianilso src gnome org>
Date:   Sat Jul 18 09:18:53 2015 +0200

    Show notifications on a per room/server basis
    
    Error messages adds an extra amount of notifications to Polari.
    This patch shows notifications based on what room on what
    server is active. Pending notifications from inactive rooms
    or servers are indicated with a symbol in the room list.

 src/appNotifications.js |   80 ++++++++++++++++++++++++++++++++++++++--------
 src/mainWindow.js       |   53 +++++++++++++++++--------------
 2 files changed, 95 insertions(+), 38 deletions(-)
---
diff --git a/src/appNotifications.js b/src/appNotifications.js
index 73435f4..d0f1e0a 100644
--- a/src/appNotifications.js
+++ b/src/appNotifications.js
@@ -99,6 +99,7 @@ const ConnectingNotification = new Lang.Class({
     Extends: AppNotification,
 
     _init: function(account) {
+        //log('created a connecting notification..');
         this.parent();
         this._account = account;
         this._grid = new Gtk.Grid({ orientation: Gtk.Orientation.HORIZONTAL,
@@ -146,7 +147,6 @@ const ErrorNotification = new Lang.Class({
                                     column_spacing: 12 });
 
         this._grid.add(new Gtk.Image({icon_name: 'dialog-error-symbolic' }));
-
         // Not sure if it is necessary to support all types of error messages.
         // Telepathy-idle only emit a small subset of all of them..
         this._connectionErrorMessages = {};
@@ -174,7 +174,7 @@ const ErrorNotification = new Lang.Class({
                         // we already try again when account info is updated in SSL patch
                 }));
             }));
-
+        log('creating Connecting Notification..');
         let text = _("Unable to connect to %s").format(account.display_name);
         let label = new Gtk.Label({ label: text, max_width_chars: 30, wrap: true });
         this._grid.add(label);
@@ -205,12 +205,6 @@ const ErrorNotification = new Lang.Class({
             return;
         this.close();
     },
-
-    close: function() {
-        this.parent();
-        this._account.notification = null;
-        this._account.error = null;
-    },
 });
 
 const NotificationQueue = new Lang.Class({
@@ -225,19 +219,77 @@ const NotificationQueue = new Lang.Class({
         this._grid = new Gtk.Grid({ orientation: Gtk.Orientation.VERTICAL,
                                     row_spacing: 6, visible: true });
         this.widget.add(this._grid);
+        this._notifications = {};
+        this._activeNotifications = {};
+        let initParams = {};
+        this._activeNotifications['room'] = initParams;
+        this._activeNotifications['account'] = initParams;
+        this._updateVisibility();
+    },
+
+    setNotification: function(params) {
+        this._notifications[params.identifier] = params;
+        //log('checking if ' + params.identifier + ' is active..');
+        //log('this._activeNotifications[' + params.type + '].identifier: ' + 
this._activeNotifications[params.type].identifier)
+        if (this._activeNotifications[params.type].identifier == params.identifier) {
+                //log('..true. calling displayNotification.');
+                this._displayNotification(params);
+        }
     },
 
-    addNotification: function(notification) {
-        this._grid.add(notification.widget);
+    loadNotifications: function(identifier, type) {
+        log('currently notifications are loaded for: ' + this._activeNotifications[type].identifier);
+        log('the notification is: ' + this._activeNotifications[type].notification);
+        // We only perform this check if activeNotification has already been initialized.
+        // Otherwise we'll never be able to load notifications from this._activeNotifications.
+        if (this._activeNotifications[type]) {
+            //log('loadNotifications: checking archived notification == displayed notification..');
+            //log('activeNotification for ' + type + ': ' + this._activeNotifications[type]);
+            //log('archived notifications for ' + identifier + ': ' + this._notifications[identifier]);
+            if (this._activeNotifications[type] == this._notifications[identifier]) {
+                //log('match between active notification and current notification!');
+                return;
+            }
+            //log('loadNotifications: notifications did not match');
+        }
+
+        let notification = this._notifications[identifier] ? this._notifications[identifier].notification : 
null;
+        let params = { notification: notification,
+                       type: type, identifier: identifier };
+
+        this._displayNotification(params);
+    },
+
+    _displayNotification: function(params) {
+        log('now displaying ' + params.notification + ' for: ' + params.identifier);
 
-        notification.widget.connect('destroy',
-                                    Lang.bind(this, this._onChildDestroy));
+        //log('_displayNotification: checking if any notifications of that type is visible..');
+        if (this._activeNotifications[params.type].notification) {
+            //log('_displayNotification: true. removing visible notification');
+            //this._activeNotifications[params.type].notification.close();
+            this._grid.remove(this._activeNotifications[params.type].notification.widget);
+        }
+        //log('storing parameters inside this._activeNotifications[' + params.type + ']..');
+        this._activeNotifications[params.type] = params;
+        //log('_displayNotification: checking if our notification is empty...');
+        if (!params.notification) {
+            //log('_displayNotification: notification is empty, returning..');
+            this._updateVisibility();
+            return;
+        }
+
+        this._grid.add(params.notification.widget);
+        params.notification.widget.connect('destroy',
+                                    Lang.bind(this, this._updateVisibility));
         this.widget.show();
     },
 
-    _onChildDestroy: function() {
-        if (this._grid.get_children().length == 0)
+    _updateVisibility: function() {
+        //log('amount of children in tree: ' + this._grid.get_children().length);
+        if (this._grid.get_children().length == 0) {
+            //log('hiding widget..');
            this.widget.hide();
+        }
     }
 });
 
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 263247d..2e6f86b 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -120,47 +120,45 @@ const MainWindow = new Lang.Class({
     },
 
     _onAccountChanged: function(am, account) {
-        log(account.display_name + ': ***********');
-        log(account.display_name + ': new change!');
-        let notification, statusReason = null;
+        log('***********');
+        if (!account.connection_status_reason)
+            return;
+        let notification = null;
+        let app = Gio.Application.get_default();
+        let statusReason = account.connection_status_reason;
         if (account.connection_status == Tp.ConnectionStatus.CONNECTING) {
-            log(account.display_name + ': we are connecting to the server..');
+            //log(account.display_name + ': we are connecting to the server..');
             notification = new AppNotifications.ConnectingNotification(account);
         } else if (account.connection_status == Tp.ConnectionStatus.DISCONNECTED) {
-            if (!account.connection_status_reason)
-                return;
-            statusReason = account.connection_status_reason;
             if (statusReason == Tp.ConnectionStatusReason.REQUESTED)
                 return;
-
             notification = new AppNotifications.ErrorNotification(account,
                                                                   this.window,
                                                                   statusReason);
-            log(account.display_name + ': we are disconnected due to error: ' + 
account.connection_status_reason);
+            //log(account.display_name + ': we are disconnected due to error: ' + 
account.connection_status_reason);
         }
-        if (!notification)
-            return;
 
         let oldReason = account.notification ? account.notification.statusReason
                                              : null;
-        if (oldReason == notification.statusReason) {
-            log(account.display_name + ': same notification is already visible');
-            log(account.display_name + ': ignoring ' + notification)
+        //log(account.display_name + ': comparing ' + oldReason + ' with ' + statusReason);
+        if (notification && oldReason == statusReason) {
+            //log(account.display_name + ': same notification is already in queue');
+            //log(account.display_name + ': ignoring ' + notification)
             return;
         }
-        log(account.notification + ' is not equal to ' + notification);
-        log(account.display_name + ': displaying ' + notification)
-        let app = Gio.Application.get_default();
+        // add a notification for the account to the queue.
 
-        app.notificationQueue.addNotification(notification);
-        app.mark_busy();
+        let identifier = account.get_path_suffix();
+        let type = 'account';
+        let params = ({ identifier: identifier,
+                        notification: notification,
+                        type: type });
+        app.notificationQueue.setNotification(params);
+        log(account.display_name + ': notification set to ' + notification + ' for ' + 
account.get_path_suffix());
 
         account.notification = notification;
-        notification.widget.connect('destroy',
-            function() {
-                app.unmark_busy();
-                delete account._notification;
-            });
+        if (!notification)
+            return;
     },
 
     _updateDecorations: function() {
@@ -206,6 +204,13 @@ const MainWindow = new Lang.Class({
         this._membersChangedId =
             this._room.connect('members-changed',
                                Lang.bind(this, this._updateUserListLabel));
+        let app = Gio.Application.get_default();
+        log('**********')
+        log('loading archived notifications for: ' + room.account.get_path_suffix());
+        app.notificationQueue.loadNotifications(room.account.get_path_suffix(), 'account');
+        log('**********')
+        //log('loading archived notifications for: ' + room.id);
+        app.notificationQueue.loadNotifications(room.id, 'room');
     },
 
     _createWidget: function(app) {


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