[polari/wip/fmuellner/combined-gsoc: 75/137] replaced old notification logic with GActions



commit f3fb88af271d1ff7a0f8127e34d5a9780fa3d2ac
Author: raresv <rares visalom gmail com>
Date:   Sat Jul 16 04:01:21 2016 +0300

    replaced old notification logic with GActions

 data/resources/user-details.ui |    4 +-
 src/userList.js                |   76 +++++++++----------------------------
 src/userTracker.js             |   82 +++++++++++++++------------------------
 3 files changed, 52 insertions(+), 110 deletions(-)
---
diff --git a/data/resources/user-details.ui b/data/resources/user-details.ui
index cf996ef..c6e1f08 100644
--- a/data/resources/user-details.ui
+++ b/data/resources/user-details.ui
@@ -123,7 +123,7 @@
             <child>
               <object class="GtkImage" id="notificationIcon">
                 <property name="icon-name">alarm-symbolic</property>
-                <property name="visible">True</property>
+                <property name="visible">False</property>
                 <property name="halign">start</property>
                 <property name="margin-left">0</property>
                 <style>
@@ -140,7 +140,7 @@
               <object class="GtkLabel" id="notificationLabel">
                 <property name="ellipsize">end</property>
                 <property name="halign">start</property>
-                <property name="visible">True</property>
+                <property name="visible">False</property>
                 <property name="margin-start">20</property>
                 <property name="hexpand">True</property>
                 <property name="use-markup">True</property>
diff --git a/src/userList.js b/src/userList.js
index 759acd3..bc90d8a 100644
--- a/src/userList.js
+++ b/src/userList.js
@@ -121,9 +121,9 @@ const UserDetails = new Lang.Class({
                                                         'expanded',
                                                         READWRITE,
                                                         false),
-                  'isUserWatched': GObject.ParamSpec.boolean('isUserWatched',
-                                                             'isUserWatched',
-                                                             'isUserWatched',
+                  'notifications-enabled': GObject.ParamSpec.boolean('notifications-enabled',
+                                                             'notifications-enabled',
+                                                             'notifications-enabled',
                                                              READWRITE,
                                                              false)},
 
@@ -140,8 +140,11 @@ const UserDetails = new Lang.Class({
 
         this._notificationLabel.set_text("Will notify if user appears online.");
 
-        this.bind_property('isUserWatched', this._notificationIcon, 'visible', 
GObject.BindingFlags.SYNC_CREATE);
-        this.bind_property('isUserWatched', this._notificationLabel, 'visible', 
GObject.BindingFlags.SYNC_CREATE);
+        this._notificationIcon.no_show_all = true;
+        this._notificationLabel.no_show_all = true;
+
+        this.bind_property('notifications-enabled', this._notificationIcon, 'visible', 
GObject.BindingFlags.SYNC_CREATE);
+        this.bind_property('notifications-enabled', this._notificationLabel, 'visible', 
GObject.BindingFlags.SYNC_CREATE);
 
         this._fullnameLabel.ellipsize = Pango.EllipsizeMode.END;
         this._fullnameLabel.max_width_chars = MAX_USERS_WIDTH_CHARS;
@@ -280,9 +283,6 @@ const UserDetails = new Lang.Class({
             this._lastLabel.hide();
         }
 
-        //this._notificationIcon.hide();
-        //this._notificationLabel.hide();
-
         this._revealDetails();
     },
 
@@ -344,6 +344,8 @@ const UserPopover = new Lang.Class({
 
         this.parent(params);
 
+        this._app = Gio.Application.get_default();
+
         this._nickLabel = new Gtk.Label({ halign: Gtk.Align.START, margin_top: 0, ellipsize: 
Pango.EllipsizeMode.END, max_width_chars: MAX_USERS_WIDTH_CHARS });
         this._statusLabel = new Gtk.Label({ halign: Gtk.Align.START, margin_bottom: 0, use_markup: true });
 
@@ -355,13 +357,14 @@ const UserPopover = new Lang.Class({
         this._hbox.add(this._headervbox);
 
         this._notifyButton = new Gtk.ToggleButton({ image: new Gtk.Image({ icon_name: 'alarm-symbolic' }), 
halign: Gtk.Align.END, hexpand: true });
-        this._notifyButton.connect('clicked',
-                                    Lang.bind(this, this._onNotifyButtonClicked));
+        this._notifyButton.bind_property('sensitive', this._notifyButton, 'visible', 0);
+
         this._hbox.add(this._notifyButton);
 
 
         this._userDetails = new UserDetails();
         this.bind_property('visible', this._userDetails, 'expanded', 0);
+        this._notifyButton.bind_property('active', this._userDetails, 'notifications-enabled', 
GObject.BindingFlags.SYNC_CREATE);
 
         this._vbox = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL });
         this._vbox.add(this._hbox);
@@ -370,8 +373,6 @@ const UserPopover = new Lang.Class({
         this.add(this._vbox);
 
         this._vbox.show_all();
-
-        this._notifyButton.bind_property('active', this._userDetails, 'isUserWatched', 
GObject.BindingFlags.SYNC_CREATE);
     },
 
     set nickname(nickname) {
@@ -379,6 +380,11 @@ const UserPopover = new Lang.Class({
 
         let baseNick = Polari.util_get_basenick(nickname);
 
+        let notifyActionName  = this._userTracker.getNotifyActionName(this._nickname);
+        let notifyAction = this._app.lookup_action(notifyActionName);
+
+        this._notifyButton.action_name = 'app.' + notifyActionName;
+
         /*TODO: these need to be disconnected when not used anymore*/
         this._userTracker.watchUser(this._room, this._nickname, Lang.bind(this, this._onNickStatusChanged));
         this._userTracker.connect("status-changed::"+this._nickname, Lang.bind(this, this._updateContents));
@@ -387,7 +393,6 @@ const UserPopover = new Lang.Class({
         this._updateContents();
 
         /*TODO: disconnect when not needed anymore*/
-
         if (this._contactsChangedSignal)
             this._userTracker.disconnect(this._contactsChangedSignal);
 
@@ -431,54 +436,9 @@ const UserPopover = new Lang.Class({
             this._statusLabel.sensitive = false;
         }
 
-        this._updateNotifyButton();
-
         this._userDetails.nickname = this._nickname;
     },
 
-    _onNotifyButtonClicked: function() {
-        if (!this._userTracker.isUserWatched(this._nickname, this._room.account.get_display_name())) {
-            this._userTracker.addToWatchlist(this._nickname, this._room.account.get_display_name());
-            this._updateNotifyButton();
-        }
-        else {
-            this._userTracker.popUserFromWatchlist(this._nickname, this._room.account.get_display_name());
-            this._updateNotifyButton();
-        }
-    },
-
-    _updateNotifyButton: function() {
-        let isUserGloballyOnline = this._userTracker.getNickStatus(this._nickname) == 
Tp.ConnectionPresenceType.AVAILABLE ? true : false;
-
-        /*TODO: too many conditionals*/
-        if (!this._userTracker.isUserWatched(this._nickname, this._room.account.get_display_name()))
-            if (this._userTracker.getNickRoomStatus(this._nickname, this._room) == 
Tp.ConnectionPresenceType.AVAILABLE) {
-                this._notifyButton.visible = false;
-                //this._notifyButton.set_active(false);
-            }
-            else {
-                if (isUserGloballyOnline)
-                    this._notifyButton.visible = false;
-                else
-                    this._notifyButton.visible = true;
-
-                //this._notifyButton.set_active(false);
-            }
-        else
-            if (this._userTracker.getNickRoomStatus(this._nickname, this._room) == 
Tp.ConnectionPresenceType.AVAILABLE) {
-                this._notifyButton.visible = false;
-                //this._notifyButton.set_active(true);
-            }
-            else {
-                if (isUserGloballyOnline)
-                    this._notifyButton.visible = false;
-                else
-                    this._notifyButton.visibile = true;
-
-                //this._notifyButton.set_active(true);
-            }
-    },
-
     _onNickStatusChanged: function(nickName, status) {
         this._updateContents();
     },
diff --git a/src/userTracker.js b/src/userTracker.js
index ddfdb63..14cf7c7 100644
--- a/src/userTracker.js
+++ b/src/userTracker.js
@@ -221,10 +221,6 @@ const UserTracker = new Lang.Class({
         this._untrackMember(this._globalContactMapping, oldMember, room);
         this._trackMember(this._roomMapping.get(room)._contactMapping, newMember, room);
         this._trackMember(this._globalContactMapping, newMember, room);
-
-        /*TODO: is this needed here?*/
-        if (this.isUserWatched(newMember.alias, newMember.get_account().get_display_name()))
-            this.emitWatchedUserNotification(room, newMember);
     },
 
     _onMemberDisconnected: function(room, member, message) {
@@ -253,9 +249,6 @@ const UserTracker = new Lang.Class({
 
         this._trackMember(this._roomMapping.get(room)._contactMapping, member, room);
         this._trackMember(this._globalContactMapping, member, room);
-
-        if (this.isUserWatched(member.alias, member.get_account().get_display_name()))
-            this.emitWatchedUserNotification(room, member);
     },
 
     _onMemberLeft: function(room, member, message) {
@@ -277,6 +270,13 @@ const UserTracker = new Lang.Class({
             if (map == this._globalContactMapping) {
                 this.emit("status-changed::" + baseNick, member.alias, Tp.ConnectionPresenceType.AVAILABLE);
                 //log("[global status] user " + member.alias + " is globally online");
+
+                let notifyActionName = this.getNotifyActionName(member.alias);
+                let notifyAction = this._app.lookup_action(notifyActionName);
+                if (notifyAction.get_state().get_boolean()) {
+                    this.emitWatchedUserNotification(room, member);
+                    notifyAction.enabled = false;
+                }
             }
             else
                 //log("[Local UserTracker] User " + member.alias + " is now available in room " + 
member._room.channelName + " on " + this._account.get_display_name());
@@ -285,10 +285,10 @@ const UserTracker = new Lang.Class({
                         handlerInfo.handler(handlerInfo.nickName, Tp.ConnectionPresenceType.AVAILABLE);
                     else if (!handlerInfo.nickName)
                         handlerInfo.handler(member.alias, Tp.ConnectionPresenceType.AVAILABLE);
-
-            if (this._globalContactMapping == map)
-                this.emit("contacts-changed::" + baseNick);
         }
+
+        if (this._globalContactMapping == map)
+            this.emit("contacts-changed::" + baseNick);
     },
 
     _untrackMember: function(map, member, room) {
@@ -301,7 +301,7 @@ const UserTracker = new Lang.Class({
         if (indexToDelete > -1) {
             let removedMember = contacts.splice(indexToDelete, 1)[0];
 
-            if (contacts.length == 0)
+            if (contacts.length == 0) {
                 if (map == this._globalContactMapping)
                     this.emit("status-changed::" + baseNick, member.alias, 
Tp.ConnectionPresenceType.OFFLINE);
                 else
@@ -311,6 +311,13 @@ const UserTracker = new Lang.Class({
                         else if (!handlerInfo.nickName)
                             handlerInfo.handler(member.alias, Tp.ConnectionPresenceType.OFFLINE);
 
+                let notifyActionName = this.getNotifyActionName(member.alias);
+                let notifyAction = this._app.lookup_action(notifyActionName);
+                if (!notifyAction.get_state().get_boolean()) {
+                    notifyAction.enabled = true;
+                }
+            }
+
             if (this._globalContactMapping == map)
                 this.emit("contacts-changed::" + baseNick);
         }
@@ -379,42 +386,6 @@ const UserTracker = new Lang.Class({
         this._roomMapping.get(room)._handlerMapping.delete(handlerID);
     },
 
-    addToWatchlist: function(user, network) {
-        let baseNick = Polari.util_get_basenick(user);
-
-        let isAlreadyWatched = this._watchlist.indexOf([baseNick, network]) != -1;
-
-        if (!isAlreadyWatched)
-            this._watchlist.push([baseNick, network]);
-        //this._watchlist.push([user, network]);
-    },
-
-    isUserWatched: function (user, network) {
-        let baseNick = Polari.util_get_basenick(user);
-
-        for (var i = 0; i < this._watchlist.length; i++) {
-            if (this._watchlist[i][0] == baseNick && this._watchlist[i][1] == network) {
-                return true;
-            }
-        }
-
-        return false;
-    },
-
-    popUserFromWatchlist: function (user, network) {
-        let baseNick = Polari.util_get_basenick(user);
-
-        let indexToDelete = -1;
-        for (var i = 0; i < this._watchlist.length; i++) {
-            if (this._watchlist[i][0] == baseNick && this._watchlist[i][1] == network) {
-                indexToDelete = i;
-            }
-        }
-
-        if (indexToDelete != -1)
-            this._watchlist.splice(indexToDelete, 1);
-    },
-
     emitWatchedUserNotification: function (room, member) {
         let notification = new Gio.Notification();
         notification.set_title("User is online");
@@ -428,13 +399,24 @@ const UserTracker = new Lang.Class({
 
         this._app.send_notification('watched-user-notification', notification);
 
-        this.popUserFromWatchlist(member.alias, member.get_account().get_display_name());
-
         let baseNick = Polari.util_get_basenick(member.alias);
         this.emit("notification-emitted::" + baseNick);
     },
 
     getNotifyActionName: function(nickName) {
-        return 'notify-user-' + this.account.get_path_suffix() + '-' + Polari.util_get_basenick(nickName);
+        let notifyActionName = 'notify-user-' + this._account.get_path_suffix() + '-' + 
Polari.util_get_basenick(nickName);
+
+        if (!this._app.lookup_action(notifyActionName)) {
+            let newNotifyActionProps = {
+                name: notifyActionName,
+                state: GLib.Variant.new('b', false)
+            };
+
+            let newNotifyAction = new Gio.SimpleAction(newNotifyActionProps);
+
+            this._app.add_action(newNotifyAction);
+        }
+
+        return notifyActionName;
     }
 });


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