changeset 1417:176de79c8c39

core, plugin XEP-0045, frontends: change frontend method "setStatusOnline" for "setPresenceStatus": - remove parameter "online" (can be guess from "presence" value) - process "statuses" dict in quick_frontend, so this method can get a simple unicode "status" - add C.PRESENCE_STATUSES_DEFAULT to define the key to use for fallback status
author souliane <souliane@mailoo.org>
date Mon, 20 Apr 2015 16:39:38 +0200
parents a419da93afef
children 6adf1b0be609
files frontends/src/primitivus/primitivus frontends/src/primitivus/status.py frontends/src/quick_frontend/quick_app.py frontends/src/quick_frontend/quick_contact_list.py src/core/constants.py src/core/sat_main.py src/core/xmpp.py src/plugins/plugin_xep_0045.py
diffstat 8 files changed, 30 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/frontends/src/primitivus/primitivus	Sat Apr 18 00:27:39 2015 +0200
+++ b/frontends/src/primitivus/primitivus	Mon Apr 20 16:39:38 2015 +0200
@@ -746,14 +746,8 @@
 
     #MISC CALLBACKS#
 
-    def setStatusOnline(self, online=True, show="", statuses={}, profile=C.PROF_KEY_NONE):
-        if not online or not statuses:
-            self.contact_lists[profile].status_bar.setPresenceStatus(show if online else 'unavailable', '')
-            return
-        try:
-            self.contact_lists[profile].status_bar.setPresenceStatus(show, statuses['default'])
-        except (KeyError, TypeError):
-            pass
+    def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
+        self.contact_lists[profile].status_bar.setPresenceStatus(show, status)
 
 sat = PrimitivusApp()
 sat.start()
--- a/frontends/src/primitivus/status.py	Sat Apr 18 00:27:39 2015 +0200
+++ b/frontends/src/primitivus/status.py	Mon Apr 20 16:39:38 2015 +0200
@@ -21,7 +21,7 @@
 import urwid
 from urwid_satext import sat_widgets
 from sat_frontends.quick_frontend.constants import Const as commonConst
-from sat_frontends.primitivus.constants import Const
+from sat_frontends.primitivus.constants import Const as C
 
 
 class StatusBar(urwid.Columns):
@@ -32,7 +32,7 @@
         status_prefix = urwid.Text('[')
         status_suffix = urwid.Text(']')
         self.status = sat_widgets.ClickableText('')
-        self.setPresenceStatus('unavailable', '')
+        self.setPresenceStatus(C.PRESENCE_UNAVAILABLE, '')
         urwid.Columns.__init__(self, [('weight', 1, self.presence), ('weight', 1, status_prefix),
                                       ('weight', 9, self.status), ('weight', 1, status_suffix)])
         urwid.connect_signal(self.presence, 'click', self.onPresenceClick)
@@ -55,19 +55,20 @@
 
     def onChange(self, sender=None, user_data=None):
         new_value = user_data.get_text()
-        previous = ([key for key in Const.PRESENCE if Const.PRESENCE[key][0] == self.presence.get_text()][0], self.status.get_text())
+        previous = ([key for key in C.PRESENCE if C.PRESENCE[key][0] == self.presence.get_text()][0], self.status.get_text())
         if isinstance(user_data, sat_widgets.ClickableText):
             new = ([key for key in commonConst.PRESENCE if commonConst.PRESENCE[key] == new_value][0], previous[1])
         elif isinstance(user_data, sat_widgets.AdvancedEdit):
             new = (previous[0], new_value[0])
         if new != previous:
             for profile in self.host.profiles:  # FIXME: for now all the profiles share the same status
-                self.host.bridge.setPresence(show=new[0], statuses={'default': new[1]}, profile_key=profile)  # FIXME: manage multilingual statuses
+                self.host.bridge.setPresence(show=new[0], status=new[1], profile_key=profile)  # FIXME: manage multilingual statuses
             self.setPresenceStatus(new[0], new[1])
         self.host.removePopUp()
 
     def setPresenceStatus(self, show, status):
-        show_icon, show_attr = Const.PRESENCE.get(show)
+        show_icon, show_attr = C.PRESENCE.get(show)
         self.presence.set_text(('show_normal', show_icon))
-        self.status.set_text((show_attr, status))
+        if status is not None:
+            self.status.set_text((show_attr, status))
         self.host.redraw()
--- a/frontends/src/quick_frontend/quick_app.py	Sat Apr 18 00:27:39 2015 +0200
+++ b/frontends/src/quick_frontend/quick_app.py	Mon Apr 20 16:39:38 2015 +0200
@@ -96,11 +96,11 @@
                 contact_list.setCache(jid.JID(entity), key, value)
 
         if not self.bridge.isConnected(self.profile):
-            self.host.setStatusOnline(False, profile=self.profile)
+            self.host.setPresenceStatus(C.PRESENCE_UNAVAILABLE, '', profile=self.profile)
         else:
-            self.host.setStatusOnline(True, profile=self.profile)
 
             contact_list.fill()
+            self.host.setPresenceStatus(profile=self.profile)
 
             #The waiting subscription requests
             self.bridge.getWaitingSub(self.profile, callback=self._plug_profile_gotWaitingSub)
@@ -125,7 +125,6 @@
         #Presence must be requested after rooms are filled
         self.host.bridge.getPresenceStatuses(self.profile, callback=self._plug_profile_gotPresences)
 
-
     def _plug_profile_gotPresences(self, presences):
         def gotEntityData(data, contact):
             for key in ('avatar', 'nick'):
@@ -443,13 +442,13 @@
     def connectedHandler(self, profile):
         """called when the connection is made"""
         log.debug(_("Connected"))
-        self.setStatusOnline(True, profile=profile)
+        self.setPresenceStatus(profile=profile)
 
     def disconnectedHandler(self, profile):
         """called when the connection is closed"""
         log.debug(_("Disconnected"))
         self.contact_lists[profile].clearContacts()
-        self.setStatusOnline(False, profile=profile)
+        self.setPresenceStatus(C.PRESENCE_UNAVAILABLE, '', profile=profile)
 
     def newContactHandler(self, JabberId, attributes, groups, profile):
         entity = jid.JID(JabberId)
@@ -505,20 +504,22 @@
         assert alert_type in ['INFO', 'ERROR']
         self.showDialog(unicode(msg), unicode(title), alert_type.lower())
 
-    def setStatusOnline(self, online=True, show="", statuses={}, profile=C.PROF_KEY_NONE):
+    def setPresenceStatus(self, show='', status=None, profile=C.PROF_KEY_NONE):
         raise NotImplementedError
 
     def presenceUpdateHandler(self, entity_s, show, priority, statuses, profile):
 
         log.debug(_(u"presence update for %(entity)s (show=%(show)s, priority=%(priority)s, statuses=%(statuses)s) [profile:%(profile)s]")
-              % {'entity': entity_s, C.PRESENCE_SHOW: show, C.PRESENCE_PRIORITY: priority, C.PRESENCE_STATUSES: statuses, 'profile': profile})
+                  % {'entity': entity_s, C.PRESENCE_SHOW: show, C.PRESENCE_PRIORITY: priority, C.PRESENCE_STATUSES: statuses, 'profile': profile})
         entity = jid.JID(entity_s)
 
         if entity == self.profiles[profile].whoami:
-            if show == "unavailable":
-                self.setStatusOnline(False, profile=profile)
+            if show == C.PRESENCE_UNAVAILABLE:
+                self.setPresenceStatus(C.PRESENCE_UNAVAILABLE, '', profile=profile)
             else:
-                self.setStatusOnline(True, show, statuses, profile=profile)
+                # FIXME: try to retrieve user language status before fallback to default
+                status = statuses.get(C.PRESENCE_STATUSES_DEFAULT, None)
+                self.setPresenceStatus(show, status, profile=profile)
             return
 
         # #FIXME: must be moved in a plugin
--- a/frontends/src/quick_frontend/quick_contact_list.py	Sat Apr 18 00:27:39 2015 +0200
+++ b/frontends/src/quick_frontend/quick_contact_list.py	Mon Apr 20 16:39:38 2015 +0200
@@ -175,7 +175,7 @@
 
                 if name == 'status':  # XXX: we get the first status for 'status' key
                     # TODO: manage main language for statuses
-                    return cache[C.PRESENCE_STATUSES].get('default', '')
+                    return cache[C.PRESENCE_STATUSES].get(C.PRESENCE_STATUSES_DEFAULT, '')
 
             return cache[name]
         except KeyError:
@@ -330,7 +330,7 @@
         else:
             alerts = {alert.bare for alert in self._alerts}
             selected = {selected.bare for selected in self._selected}
-        return ((show is not None and show != "unavailable")
+        return ((show is not None and show != C.PRESENCE_UNAVAILABLE)
                 or self.show_disconnected
                 or entity in alerts
                 or entity in selected)
--- a/src/core/constants.py	Sat Apr 18 00:27:39 2015 +0200
+++ b/src/core/constants.py	Mon Apr 20 16:39:38 2015 +0200
@@ -98,6 +98,7 @@
     PRESENCE_SHOW_XA = 'xa'
     PRESENCE_SHOW = 'show'
     PRESENCE_STATUSES = 'statuses'
+    PRESENCE_STATUSES_DEFAULT = 'default'
     PRESENCE_PRIORITY = 'priority'
 
 
--- a/src/core/sat_main.py	Sat Apr 18 00:27:39 2015 +0200
+++ b/src/core/sat_main.py	Mon Apr 20 16:39:38 2015 +0200
@@ -603,8 +603,7 @@
         self.profiles[profile].presence.available(to_jid, show, statuses, priority)
         #XXX: FIXME: temporary fix to work around openfire 3.7.0 bug (presence is not broadcasted to generating resource)
         if '' in statuses:
-            statuses['default'] = statuses['']
-            del statuses['']
+            statuses[C.PRESENCE_STATUSES_DEFAULT] = statuses.pop('')
         self.bridge.presenceUpdate(self.profiles[profile].jid.full(), show,
                                    int(priority), statuses, profile)
 
--- a/src/core/xmpp.py	Sat Apr 18 00:27:39 2015 +0200
+++ b/src/core/xmpp.py	Mon Apr 20 16:39:38 2015 +0200
@@ -315,8 +315,7 @@
             statuses = {}
 
         if None in statuses:  # we only want string keys
-            statuses["default"] = statuses[None]
-            del statuses[None]
+            statuses[C.PRESENCE_STATUSES_DEFAULT] = statuses.pop(None)
 
         self.host.memory.setPresenceStatus(entity, show or "",
                                            int(priority), statuses,
@@ -334,8 +333,7 @@
             statuses = {}
 
         if None in statuses:  # we only want string keys
-            statuses["default"] = statuses[None]
-            del statuses[None]
+            statuses[C.PRESENCE_STATUSES_DEFAULT] = statuses.pop(None)
         self.host.memory.setPresenceStatus(entity, "unavailable", 0, statuses, self.parent.profile)
 
         if not self.host.trigger.point("presenceReceived", entity, "unavailable", 0, statuses, self.parent.profile):
@@ -356,16 +354,14 @@
 
         # default for us is None for wokkel
         # so we must temporarily switch to wokkel's convention...
-        if 'default' in statuses:
-            statuses[None] = statuses['default']
-            del statuses['default']
+        if C.PRESENCE_STATUSES_DEFAULT in statuses:
+            statuses[None] = statuses.pop(C.PRESENCE_STATUSES_DEFAULT)
 
         presence_elt = xmppim.AvailablePresence(entity, show, statuses, priority)
 
         # ... before switching back
         if None in statuses:
-            statuses['default'] = statuses[None]
-            del statuses[None]
+            statuses['default'] = statuses.pop(None)
 
         if not self.host.trigger.point("presence_available", presence_elt, self.parent):
             return
--- a/src/plugins/plugin_xep_0045.py	Sat Apr 18 00:27:39 2015 +0200
+++ b/src/plugins/plugin_xep_0045.py	Mon Apr 20 16:39:38 2015 +0200
@@ -734,7 +734,7 @@
         self.host.bridge.roomUserChangedNick(room.roomJID.userhost(), user.nick, new_nick, self.parent.profile)
 
     def userUpdatedStatus(self, room, user, show, status):
-        self.host.bridge.presenceUpdate(room.roomJID.userhost() + '/' + user.nick, show or '', 0, {'default': status or ''}, self.parent.profile)
+        self.host.bridge.presenceUpdate(room.roomJID.userhost() + '/' + user.nick, show or '', 0, {C.PRESENCE_STATUSES_DEFAULT: status or ''}, self.parent.profile)
 
     def receivedGroupChat(self, room, user, body):
         log.debug(u'receivedGroupChat: room=%s user=%s body=%s' % (room.roomJID.full(), user, body))