diff browser_side/panels.py @ 230:266e9678eec0

browser_side: added the flag REUSE_EXISTING_LIBERVIA_WIDGETS - do not create a new chat panel for contacts/groups if a similar one is found in the same tab - this goes with a modification of the methods to create new panels, especially arguments handling - improvement of the accepted groups list for MicroblogPanel (remove duplicates and keep sorted) Details for the new flag: # Set to true to not create a new LiberviaWidget when a similar one # already exist (i.e. a chat panel with the same target). Instead # the existing widget will be eventually removed from its parent # and added to new WidgetsPanel, or replaced to the expected # position if the previous and the new parent are the same.
author souliane <souliane@mailoo.org>
date Tue, 08 Oct 2013 13:57:35 +0200
parents 744426c2b699
children fab7aa366576
line wrap: on
line diff
--- a/browser_side/panels.py	Tue Oct 08 13:48:00 2013 +0200
+++ b/browser_side/panels.py	Tue Oct 08 13:57:35 2013 +0200
@@ -301,10 +301,8 @@
         """Panel used to show microblog
         @param accepted_groups: groups displayed in this panel, if empty, show all microblogs from all contacts
         """
-        base_widget.LiberviaWidget.__init__(self, host, ", ".join(accepted_groups), selectable = True)
-        #base_widget.ScrollPanelWrapper.__init__(self)
-        #DropCell.__init__(self)
-        self.accepted_groups = accepted_groups
+        base_widget.LiberviaWidget.__init__(self, host, ", ".join(accepted_groups), selectable=True)
+        self.setAcceptedGroup(accepted_groups)
         self.entries = {}
         self.comments = {}
         self.selected_entry = None
@@ -314,23 +312,44 @@
 
     @classmethod
     def registerClass(cls):
-        base_widget.LiberviaWidget.addDropKey("GROUP", cls.createGroup)
-        base_widget.LiberviaWidget.addDropKey("CONTACT_TITLE", cls.createMeta)
+        base_widget.LiberviaWidget.addDropKey("GROUP", cls.createPanel)
+        base_widget.LiberviaWidget.addDropKey("CONTACT_TITLE", cls.createMetaPanel)
 
     @classmethod
-    def createGroup(cls, host, item):
-        _new_panel = MicroblogPanel(host, [item]) #XXX: pyjamas doesn't support use of cls directly
-        _new_panel.setAcceptedGroup(item)
+    def createPanel(cls, host, item):
+        """Generic panel creation for one, several or all groups (meta).
+        @parem host: the SatWebFrontend instance
+        @param item: single group as a string, list of groups
+         (as an array) or None (for the meta group = "all groups")
+        @return: the created MicroblogPanel
+        """
+        _items = item if isinstance(item, list) else ([] if item is None else [item])
+        _type = 'ALL' if _items == [] else 'GROUP'
+        # XXX: pyjamas doesn't support use of cls directly
+        _new_panel = MicroblogPanel(host, _items)
         host.FillMicroblogPanel(_new_panel)
-        host.bridge.call('getMassiveLastMblogs', _new_panel.massiveInsert, 'GROUP', [item], 10)
+        host.bridge.call('getMassiveLastMblogs', _new_panel.massiveInsert, _type, _items, 10)
+        host.setSelected(_new_panel)
         return _new_panel
 
     @classmethod
-    def createMeta(cls, host, item):
-        _new_panel = MicroblogPanel(host, []) #XXX: pyjamas doesn't support use of cls directly
-        host.FillMicroblogPanel(_new_panel)
-        host.bridge.call('getMassiveLastMblogs', _new_panel.massiveInsert, 'ALL', [], 10)
-        return _new_panel
+    def createMetaPanel(cls, host, item):
+        """Needed for the drop keys to not be mixed between meta panel and panel for "Contacts" group"""
+        return MicroblogPanel.createPanel(host, None)
+
+    @property
+    def accepted_groups(self):
+        return self._accepted_groups
+
+    def matchEntity(self, entity):
+        """
+        @param entity: single group as a string, list of groups
+        (as an array) or None (for the meta group = "all groups")
+        @return: True if self matches the given entity
+        """
+        entity = entity if isinstance(entity, list) else ([] if entity is None else [entity])
+        entity.sort()  # sort() do not return the sorted list: do it here, not on the "return" line
+        return self.accepted_groups == entity
 
     def getWarningData(self):
         if self.selected_entry:
@@ -339,13 +358,13 @@
                 return ("NONE", None)
             return ("PUBLIC", "This is a <span class='warningTarget'>comment</span> and keep the initial post visibility, so it is potentialy public")
 
-        elif not self.accepted_groups:
+        elif not self._accepted_groups:
             # we have a meta MicroblogPanel, we publish publicly
             return ("PUBLIC", self.warning_msg_public)
         else:
             # we only accept one group at the moment
             # FIXME: manage several groups
-            return ("GROUP", self.warning_msg_group % self.accepted_groups[0])
+            return ("GROUP", self.warning_msg_group % self._accepted_groups[0])
 
     def onTextEntered(self, text):
         if self.selected_entry:
@@ -354,16 +373,16 @@
             if not comments_node:
                 raise Exception("ERROR: comments node is empty")
             self.host.bridge.call("sendMblogComment", None, comments_node, text)
-        elif not self.accepted_groups:
+        elif not self._accepted_groups:
             # we are entering a public microblog
             self.host.bridge.call("sendMblog", None, "PUBLIC", None, text)
         else:
             # we are entering a microblog restricted to a group
             # FIXME: manage several groups
-            self.host.bridge.call("sendMblog", None, "GROUP", self.accepted_groups[0], text)
+            self.host.bridge.call("sendMblog", None, "GROUP", self._accepted_groups[0], text)
 
     def accept_all(self):
-        return not self.accepted_groups #we accept every microblog only if we are not filtering by groups
+        return not self._accepted_groups  # we accept every microblog only if we are not filtering by groups
 
     def getEntries(self):
         """Ask all the entries for the currenly accepted groups,
@@ -481,13 +500,17 @@
             updateVPanel(self.vpanel)
 
     def setAcceptedGroup(self, group):
-        """Set the group which can be displayed in this panel
+        """Add one or more group(s) which can be displayed in this panel.
+        Prevent from duplicate values and keep the list sorted.
         @param group: string of the group, or list of string
         """
-        if isinstance(group, list):
-            self.accepted_groups.extend(group)
-        else:
-            self.accepted_groups.append(group)
+        if not hasattr(self, "_accepted_groups"):
+            self._accepted_groups = []
+        groups = group if isinstance(group, list) else [group]
+        for _group in groups:
+            if _group not in self._accepted_groups:
+                self._accepted_groups.append(_group)
+        self._accepted_groups.sort()
 
     def isJidAccepted(self, jid):
         """Tell if a jid is actepted and shown in this panel
@@ -495,7 +518,7 @@
         @return: True if the jid is accepted"""
         if self.accept_all():
             return True
-        for group in self.accepted_groups:
+        for group in self._accepted_groups:
             if self.host.contact_panel.isContactInGroup(group, jid):
                 return True
         return False
@@ -619,16 +642,34 @@
 
     @classmethod
     def registerClass(cls):
-        base_widget.LiberviaWidget.addDropKey("CONTACT", cls.createChat)
+        base_widget.LiberviaWidget.addDropKey("CONTACT", cls.createPanel)
 
     @classmethod
-    def createChat(cls, host, item):
-        _contact = JID(item)
+    def createPanel(cls, host, item):
+        _contact = item if isinstance(item, JID) else JID(item)
         host.contact_panel.setContactMessageWaiting(_contact.bare, False)
         _new_panel = ChatPanel(host, _contact)  # XXX: pyjamas doesn't seems to support creating with cls directly
         _new_panel.historyPrint()
+        host.setSelected(_new_panel)
         return _new_panel
 
+    def refresh(self):
+        """Refresh the display of this widget."""
+        self.host.contact_panel.setContactMessageWaiting(self.target.bare, False)
+        self.content_scroll.scrollToBottom()
+
+    def matchEntity(self, entity):
+        """
+        @param entity: target jid as a string or JID instance
+        @return: True if self matches the given entity
+        """
+        entity = entity if isinstance(entity, JID) else JID(entity)
+        try:
+            return self.target.bare == entity.bare
+        except AttributeError as e:
+            e.include_traceback()
+            return False
+
     def getWarningData(self):
         if self.type not in ["one2one", "group"]:
             raise Exception("Unmanaged type !")