changeset 574:b07f0fe2763a

browser_side: safer attributes handling in getOrCreateLiberviaWidget + use it to create MUC panels
author souliane <souliane@mailoo.org>
date Sat, 18 Oct 2014 13:16:56 +0200
parents 12823bcbd05b
children f8a355bede6a
files src/browser/libervia_main.py src/browser/sat_browser/base_widget.py src/browser/sat_browser/contact.py src/browser/sat_browser/menu.py src/browser/sat_browser/panels.py src/browser/sat_browser/plugin_sec_otr.py src/browser/sat_browser/richtext.py
diffstat 7 files changed, 39 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/libervia_main.py	Fri Oct 17 13:26:33 2014 +0200
+++ b/src/browser/libervia_main.py	Sat Oct 18 13:16:56 2014 +0200
@@ -665,9 +665,9 @@
 
     def getLiberviaWidget(self, class_, entity, ignoreOtherTabs=True):
         """Get the corresponding panel if it exists.
-        @param class_: class of the panel (ChatPanel, MicroblogPanel...)
-        @param entity: polymorphic parameter, see class_.matchEntity.
-        @param ignoreOtherTabs: if True, the widgets that are not
+        @param class_ (class): class of the panel (ChatPanel, MicroblogPanel...)
+        @param entity (dict): dictionnary to define the entity.
+        @param ignoreOtherTabs (bool): if True, the widgets that are not
         contained by the currently selected tab will be ignored
         @return: the existing widget that has been found or None."""
         selected_tab = self.tab_panel.getCurrentPanel()
@@ -678,7 +678,7 @@
                 continue
             if isinstance(lib_wid, class_):
                 try:
-                    if lib_wid.matchEntity(entity):
+                    if lib_wid.matchEntity(*(entity.values())):  # XXX: passing **entity bugs!
                         log.debug("existing widget found: %s" % lib_wid.getDebugName())
                         return lib_wid
                 except AttributeError as e:
@@ -688,10 +688,10 @@
 
     def getOrCreateLiberviaWidget(self, class_, entity, select=True, new_tab=None):
         """Get the matching LiberviaWidget if it exists, or create a new one.
-        @param class_: class of the panel (ChatPanel, MicroblogPanel...)
-        @param entity: polymorphic parameter, see class_.matchEntity.
-        @param select: if True, select the widget that has been found or created
-        @param new_tab: if not None, a widget which is created is created in
+        @param class_ (class): class of the panel (ChatPanel, MicroblogPanel...)
+        @param entity (dict): dictionnary to define the entity.
+        @param select (bool): if True, select the widget that has been found or created
+        @param new_tab (str): if not None, a widget which is created is created in
         a new tab. In that case new_tab is a unicode to label that new tab.
         If new_tab is not None and a widget is found, no tab is created.
         @return: the newly created wigdet if REUSE_EXISTING_LIBERVIA_WIDGETS
@@ -702,7 +702,7 @@
         if REUSE_EXISTING_LIBERVIA_WIDGETS:
             lib_wid = self.getLiberviaWidget(class_, entity, new_tab is None)
         if lib_wid is None:  # create a new widget
-            lib_wid = class_.createPanel(self, entity[0] if isinstance(entity, tuple) else entity)
+            lib_wid = class_.createPanel(self, *(entity.values()))  # XXX: passing **entity bugs!
             if new_tab is None:
                 self.addWidget(lib_wid)
             else:
@@ -733,7 +733,7 @@
 
     def newMessageCb(self, from_jid, msg, msg_type, to_jid, extra):
         other = to_jid if from_jid.bare == self.whoami.bare else from_jid
-        lib_wid = self.getLiberviaWidget(panels.ChatPanel, other, ignoreOtherTabs=False)
+        lib_wid = self.getLiberviaWidget(panels.ChatPanel, {'item': other}, ignoreOtherTabs=False)
         self.displayNotification(from_jid, msg)
         if msg_type == 'headline' and from_jid.full() == self._defaultDomain:
             try:
@@ -767,7 +767,7 @@
                 self.status_panel.setStatus(statuses.values()[0])  # pylint: disable=E1103
         else:
             if entity_jid.bare in self.room_list:
-                wid = self.getLiberviaWidget(panels.ChatPanel, entity_jid.bare, ignoreOtherTabs=False)
+                wid = self.getLiberviaWidget(panels.ChatPanel, {'item': entity_jid}, ignoreOtherTabs=False)
             else:
                 wid = self.contact_panel
                 if show == 'unavailable':  # XXX: save some resources as for now we only need 'unavailable'
@@ -786,14 +786,17 @@
 
         if _target not in self.room_list:
             self.room_list.append(_target)
-        chat_panel = panels.ChatPanel(self, _target, type_='group')
+
+        # XXX: it's not really beautiful, but it works :)
+        if node.startswith('sat_tarot_'):
+            tab_name = "Tarot"
+        elif node.startswith('sat_radiocol_'):
+            tab_name = "Radio collective"
+        else:
+            tab_name = _target.node
+
+        chat_panel = self.getOrCreateLiberviaWidget(panels.ChatPanel, {'item': _target, 'type_': 'group'}, new_tab=tab_name)
         chat_panel.setUserNick(user_nick)
-        if node.startswith('sat_tarot_'):  # XXX: it's not really beautiful, but it works :)
-            self.addTab("Tarot", chat_panel)
-        elif node.startswith('sat_radiocol_'):
-            self.addTab("Radio collective", chat_panel)
-        else:
-            self.addTab(_target.node, chat_panel)
         chat_panel.setPresents(room_nicks)
         chat_panel.historyPrint()
         chat_panel.refresh()
--- a/src/browser/sat_browser/base_widget.py	Fri Oct 17 13:26:33 2014 +0200
+++ b/src/browser/sat_browser/base_widget.py	Sat Oct 18 13:16:56 2014 +0200
@@ -455,7 +455,7 @@
         # the event will not propagate to children
         VerticalPanel.doAttachChildren(self)
 
-    def matchEntity(self, entity):
+    def matchEntity(self, item):
         """Check if this widget corresponds to the given entity.
 
         This method should be overwritten by child classes.
--- a/src/browser/sat_browser/contact.py	Fri Oct 17 13:26:33 2014 +0200
+++ b/src/browser/sat_browser/contact.py	Sat Oct 18 13:16:56 2014 +0200
@@ -80,7 +80,7 @@
         self.addClickListener(self)
 
     def onClick(self, sender):
-        self.host.getOrCreateLiberviaWidget(panels.MicroblogPanel, self.group)
+        self.host.getOrCreateLiberviaWidget(panels.MicroblogPanel, {'item': self.group})
 
 
 class ContactLabel(HTML):
@@ -205,7 +205,7 @@
 
         if handle_click:
             def cb(contact_jid):
-                self.host.getOrCreateLiberviaWidget(panels.ChatPanel, contact_jid)
+                self.host.getOrCreateLiberviaWidget(panels.ChatPanel, {'item': contact_jid})
             self.click_listener = cb
 
     def add(self, jid_s, name=None):
@@ -295,7 +295,7 @@
         self.addClickListener(self)
 
     def onClick(self, sender):
-        self.host.getOrCreateLiberviaWidget(panels.MicroblogPanel, None)
+        self.host.getOrCreateLiberviaWidget(panels.MicroblogPanel, {'item': None})
 
 
 class ContactPanel(SimplePanel):
--- a/src/browser/sat_browser/menu.py	Fri Oct 17 13:26:33 2014 +0200
+++ b/src/browser/sat_browser/menu.py	Sat Oct 18 13:16:56 2014 +0200
@@ -156,7 +156,7 @@
                 if room_jid not in [room.bare for room in self.host.room_list]:
                     self.host.bridge.call('joinMUC', lambda room_jid: invite(room_jid, contacts), room_jid, nick)
                 else:
-                    self.host.getOrCreateLiberviaWidget(panels.ChatPanel, (room_jid, "group"), True, jid.JID(room_jid).bare)
+                    self.host.getOrCreateLiberviaWidget(panels.ChatPanel, {'item': room_jid, 'type_': "group"}, True, jid.JID(room_jid).bare)
                     invite(room_jid, contacts)
 
         dialog.RoomAndContactsChooser(self.host, join, ok_button="Join", visible=(True, False))
--- a/src/browser/sat_browser/panels.py	Fri Oct 17 13:26:33 2014 +0200
+++ b/src/browser/sat_browser/panels.py	Sat Oct 18 13:16:56 2014 +0200
@@ -747,15 +747,15 @@
     def accepted_groups(self):
         return self._accepted_groups
 
-    def matchEntity(self, entity):
+    def matchEntity(self, item):
         """
-        @param entity: single group as a string, list of groups
+        @param item: 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
+        groups = item if isinstance(item, list) else ([] if item is None else [item])
+        groups.sort()  # sort() do not return the sorted list: do it here, not on the "return" line
+        return self.accepted_groups == groups
 
     def getWarningData(self, comment=None):
         """
@@ -1151,10 +1151,11 @@
         base_widget.LiberviaWidget.addDropKey("CONTACT", cls.createPanel)
 
     @classmethod
-    def createPanel(cls, host, item):
+    def createPanel(cls, host, item, type_='one2one'):
+        assert(item)
         _contact = item if isinstance(item, jid.JID) else jid.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 = ChatPanel(host, _contact, type_)  # XXX: pyjamas doesn't seems to support creating with cls directly
         _new_panel.historyPrint()
         host.setSelected(_new_panel)
         _new_panel.refresh()
@@ -1182,17 +1183,15 @@
         else:
             self.host.showWarning(*self.getWarningData())
 
-    def matchEntity(self, entity):
+    def matchEntity(self, item, type_=None):
         """
         @param entity: target jid as a string or jid.JID instance.
         Could also be a couple with a type in the second element.
         @return: True if self matches the given entity
         """
-        if isinstance(entity, tuple):
-            entity, type_ = entity if len(entity) > 1 else (entity[0], self.type)
-        else:
+        if type_ is None:
             type_ = self.type
-        entity = entity if isinstance(entity, jid.JID) else jid.JID(entity)
+        entity = item if isinstance(item, jid.JID) else jid.JID(item)
         try:
             return self.target.bare == entity.bare and self.type == type_
         except AttributeError as e:
--- a/src/browser/sat_browser/plugin_sec_otr.py	Fri Oct 17 13:26:33 2014 +0200
+++ b/src/browser/sat_browser/plugin_sec_otr.py	Sat Oct 18 13:16:56 2014 +0200
@@ -446,7 +446,7 @@
             if otrctx is None:
                 def confirm(confirm):
                     if confirm:
-                        self.host.getOrCreateLiberviaWidget(panels.ChatPanel, jid)
+                        self.host.getOrCreateLiberviaWidget(panels.ChatPanel, {'item': jid})
                         decrypt(self.context_manager.startContext(jid))
                     else:
                         # FIXME: plain text messages with whitespaces would be lost here when WHITESPACE_START_AKE is True
--- a/src/browser/sat_browser/richtext.py	Fri Oct 17 13:26:33 2014 +0200
+++ b/src/browser/sat_browser/richtext.py	Sat Oct 18 13:16:56 2014 +0200
@@ -461,7 +461,7 @@
             target = None if target == "@@" else target[1:]
         else:
             _class = panels.ChatPanel
-        self.host.getOrCreateLiberviaWidget(_class, target)
+        self.host.getOrCreateLiberviaWidget(_class, {'item': target})
         return True
 
     def syncFromEditor(self, content):