diff src/browser/libervia_main.py @ 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 daa9be94e88f
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()