changeset 616:1c0d5a87c554 frontends_multi_profiles

browser_side: add and use method displayWidget to harmonize widget's management in Libervia (not completely done, there are some issues)
author souliane <souliane@mailoo.org>
date Wed, 11 Feb 2015 11:18:13 +0100
parents 70872a83ef15
children 5baca9d46c34
files src/browser/libervia_main.py src/browser/sat_browser/blog.py src/browser/sat_browser/chat.py src/browser/sat_browser/contact_list.py src/browser/sat_browser/menu.py src/browser/sat_browser/plugin_sec_otr.py src/browser/sat_browser/richtext.py
diffstat 7 files changed, 81 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/libervia_main.py	Tue Feb 10 20:52:02 2015 +0100
+++ b/src/browser/libervia_main.py	Wed Feb 11 11:18:13 2015 +0100
@@ -26,6 +26,7 @@
 ###
 
 from sat_frontends.quick_frontend.quick_app import QuickApp
+from sat_frontends.quick_frontend.quick_widgets import WidgetAlreadyExistsError
 
 from sat_frontends.tools.misc import InputHistory
 from sat_frontends.tools import strings
@@ -43,7 +44,6 @@
 from sat_browser.contact_list import ContactList
 from sat_browser import base_widget
 from sat_browser import panels
-from sat_browser import chat
 from sat_browser import blog
 from sat_browser import dialog
 from sat_browser import xmlui
@@ -169,6 +169,7 @@
         if selected:
             selected.removeStyleName('selected_widget')
 
+        # FIXME: check that widget is in the current WidgetsPanel
         widgets_panel.selected = widget
         self.selected_widget = widget
 
@@ -233,17 +234,23 @@
             lib_wid.refresh()
         self.resize()
 
-    def addTab(self, label, wid, select=True):
-        """Create a new tab and eventually add a widget in
-        @param label: label of the tab
-        @param wid: LiberviaWidget to add
-        @param select: True to select the added tab
+    def addTab(self, label, wid=None, select=False):
+        """Create a new tab and eventually add a widget to it.
+
+        @param label (unicode): label of the tab
+        @param wid (LiberviaWidget): optional widget to add
+        @param select (bool): True to select the added tab
+        @return: WidgetsPanel
         """
         widgets_panel = base_widget.WidgetsPanel(self)
         self.tab_panel.add(widgets_panel, label)
-        widgets_panel.addWidget(wid)
+        tab_index = self.tab_panel.getWidgetCount() - 1
+        if wid is not None:
+            self.addWidget(wid, tab_index)
         if select:
-            self.tab_panel.selectTab(self.tab_panel.getWidgetCount() - 1)
+            self.tab_panel.selectTab(tab_index)
+            if wid is not None:
+                self.setSelected(wid)
         return widgets_panel
 
     def addWidget(self, wid, tab_index=None):
@@ -255,7 +262,7 @@
         if tab_index is None or tab_index < 0 or tab_index >= self.tab_panel.getWidgetCount():
             panel = self.tab_panel.getCurrentPanel()
         else:
-            panel = self.tab_panel.tabBar.getTabWidget(tab_index)
+            panel = self.tab_panel.deck.getWidget(tab_index)
         panel.addWidget(wid)
 
     def displayNotification(self, title, body):
@@ -332,7 +339,7 @@
 
         self.bridge.getNewAccountDomain(callback=domain_cb, errback=domain_eb)
         self.plug_profiles([C.PROF_KEY_NONE]) # XXX: None was used intitially, but pyjamas bug when using variable arguments and None is the only arg.
-        microblog_widget = self.widgets.getOrCreateWidget(blog.MicroblogPanel, (), profile=C.PROF_KEY_NONE)
+        microblog_widget = self.displayWidget(blog.MicroblogPanel, ())
         self.setSelected(microblog_widget)
         # self.discuss_panel.addWidget(panels.MicroblogPanel(self, []))
 
@@ -404,8 +411,8 @@
         elif "public_blog" in data:
             # TODO: use the bare instead of node when all blogs can be retrieved
             node = jid.JID(data['public_blog']).node
-            web_widget = self.widgets.getOrCreateWidget(panels.WebPanel, "/blog/{}".format(node), show_url=False, profile=C.PROF_KEY_NONE, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE) # FIXME: won't work with unicode nodes
-            self.addTab("{}'s blog".format(unicode(node)), web_widget)
+            # FIXME: "/blog/{}" won't work with unicode nodes
+            self.displayWidget(panels.WebPanel, "/blog/{}".format(node), show_url=False, new_tab="{}'s blog".format(unicode(node)))
         else:
             dialog.InfoDialog("Error",
                               "Unmanaged action result", Width="400px").center()
@@ -545,7 +552,7 @@
     def getEntityMBlog(self, entity):
         log.info("geting mblog for entity [%s]" % (entity,))
         for lib_wid in self.libervia_widgets:
-            if isinstance(lib_wid, panels.MicroblogPanel):
+            if isinstance(lib_wid, blog.MicroblogPanel):
                 if lib_wid.isJidAccepted(entity):
                     self.bridge.call('getMassiveLastMblogs', lib_wid.massiveInsert, 'JID', [entity], 10)
 
@@ -572,6 +579,50 @@
     #                 return None
     #     return None
 
+    def displayWidget(self, class_, target, dropped=False, new_tab=None, *args, **kwargs):
+        """Get or create a LiberviaWidget and select it. When the user dropped
+        something, a new widget is always created, otherwise we look for an
+        existing widget and re-use it if it's in the current tab.
+
+        @arg class_(class): see sat_frontends.quick_frontend.quick_widgets.getOrCreateWidget
+        @arg target: see sat_frontends.quick_frontend.quick_widgets.getOrCreateWidget
+        @arg dropped(bool): if True, assume the widget has been dropped
+        @arg new_tab(unicode): if not None, it holds the name of a new tab to
+            open for the widget. If None, use the default behavior.
+        @param args(list): optional args to create a new instance of class_
+        @param kwargs(list): optional kwargs to create a new instance of class_
+        @return: the widget
+        """
+        kwargs['profile'] = C.PROF_KEY_NONE
+
+        if dropped:
+            kwargs['on_new_widget'] = None
+            kwargs['on_existing_widget'] = C.WIDGET_RECREATE
+            wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
+            self.setSelected(wid)
+            return wid
+
+        if new_tab:
+            kwargs['on_new_widget'] = None
+            kwargs['on_existing_widget'] = C.WIDGET_RECREATE
+            wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
+            self.addTab(new_tab, wid)
+            return wid
+
+        kwargs['on_existing_widget'] = C.WIDGET_RAISE
+        try:
+            wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
+        except WidgetAlreadyExistsError:
+            kwargs['on_existing_widget'] = C.WIDGET_KEEP
+            wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
+            if wid.getWidgetsPanel() != self.tab_panel.getCurrentPanel():
+                kwargs['on_existing_widget'] = C.WIDGET_RECREATE
+                wid = self.widgets.getOrCreateWidget(class_, target, *args, **kwargs)
+                self.addWidget(wid)
+        self.setSelected(wid)
+        return wid
+
+
     # 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): class of the panel (ChatPanel, MicroblogPanel...)
@@ -806,7 +857,7 @@
             self.contact_panel.updateAvatar(entity_jid_s, avatar)
 
             for lib_wid in self.libervia_widgets:
-                if isinstance(lib_wid, panels.MicroblogPanel):
+                if isinstance(lib_wid, blog.MicroblogPanel):
                     if lib_wid.isJidAccepted(entity_jid_s) or (self.whoami and entity_jid_s == self.whoami.bare):
                         lib_wid.updateValue('avatar', entity_jid_s, avatar)
 
--- a/src/browser/sat_browser/blog.py	Tue Feb 10 20:52:02 2015 +0100
+++ b/src/browser/sat_browser/blog.py	Wed Feb 11 11:18:13 2015 +0100
@@ -445,7 +445,7 @@
         items_ = tuple(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
-        widget = host.widgets.getOrCreateWidget(MicroblogPanel, items_, profile=C.PROF_KEY_NONE, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE)
+        widget = host.displayWidget(MicroblogPanel, items_, dropped=True)
         host.FillMicroblogPanel(widget)
         host.bridge.getMassiveLastMblogs(type_, items_, 10, profile=C.PROF_KEY_NONE, callback=widget.massiveInsert)
         widget.refresh() # FIXME: needed ?
--- a/src/browser/sat_browser/chat.py	Tue Feb 10 20:52:02 2015 +0100
+++ b/src/browser/sat_browser/chat.py	Wed Feb 11 11:18:13 2015 +0100
@@ -360,4 +360,4 @@
 
 
 quick_widgets.register(QuickChat, Chat)
-base_widget.LiberviaWidget.addDropKey("CONTACT", lambda host, item: host.widgets.getOrCreateWidget(Chat, jid.JID(item), profile=C.PROF_KEY_NONE, on_new_widget=None, on_existing_widget=C.WIDGET_RECREATE))
+base_widget.LiberviaWidget.addDropKey("CONTACT", lambda host, item: host.displayWidget(Chat, jid.JID(item), dropped=True))
--- a/src/browser/sat_browser/contact_list.py	Tue Feb 10 20:52:02 2015 +0100
+++ b/src/browser/sat_browser/contact_list.py	Wed Feb 11 11:18:13 2015 +0100
@@ -35,9 +35,9 @@
 from sat_frontends.tools import jid
 from constants import Const as C
 import base_widget
-import panels
 import html_tools
 import chat
+import blog
 
 
 unicode = str # XXX: pyjama doesn't manage unicode
@@ -83,7 +83,7 @@
         self.addClickListener(self)
 
     def onClick(self, sender):
-        self.host.getOrCreateLiberviaWidget(panels.MicroblogPanel, {'item': self.group})
+        self.host.displayWidget(blog.MicroblogPanel, self.group)
 
 
 class ContactLabel(HTML):
@@ -219,7 +219,7 @@
 
         if handle_click:
             def cb(contact_jid):
-                host.widgets.getOrCreateWidget(chat.Chat, contact_jid, type_=C.CHAT_ONE2ONE, profile=C.PROF_KEY_NONE)
+                host.displayWidget(chat.Chat, contact_jid, type_=C.CHAT_ONE2ONE)
             self.click_listener = cb
 
     def add(self, jid_, name=None):
@@ -314,7 +314,7 @@
         self.addClickListener(self)
 
     def onClick(self, sender):
-        self.host.getOrCreateLiberviaWidget(panels.MicroblogPanel, {'item': None})
+        self.host.displayWidget(blog.MicroblogPanel, None)
 
 
 class ContactList(SimplePanel, QuickContactList):
--- a/src/browser/sat_browser/menu.py	Tue Feb 10 20:52:02 2015 +0100
+++ b/src/browser/sat_browser/menu.py	Wed Feb 11 11:18:13 2015 +0100
@@ -33,6 +33,7 @@
 from constants import Const as C
 import file_tools
 import xmlui
+import chat
 import panels
 import dialog
 import contact_group
@@ -98,7 +99,7 @@
 
     # General menu
     def onWebWidget(self):
-        web_widget = self.host.widgets.getOrCreateWidget(panels.WebPanel, C.WEB_PANEL_DEFAULT_URL, profile=C.PROF_KEY_NONE, on_existing_widget=C.WIDGET_RECREATE)
+        web_widget = self.host.displayWidget(panels.WebPanel, C.WEB_PANEL_DEFAULT_URL)
         self.host.setSelected(web_widget)
 
     def onDisconnect(self):
@@ -156,7 +157,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, {'item': room_jid, 'type_': "group"}, True, jid.JID(room_jid).bare)
+                    self.host.displayWidget(chat.Chat, room_jid, type_="group", new_tab=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/plugin_sec_otr.py	Tue Feb 10 20:52:02 2015 +0100
+++ b/src/browser/sat_browser/plugin_sec_otr.py	Wed Feb 11 11:18:13 2015 +0100
@@ -32,7 +32,7 @@
 from sat_frontends.tools import jid
 import otrjs_wrapper as otr
 import dialog
-import panels
+import chat
 
 
 NS_OTR = "otr_plugin"
@@ -449,7 +449,7 @@
             if otrctx is None:
                 def confirm(confirm):
                     if confirm:
-                        self.host.getOrCreateLiberviaWidget(panels.ChatPanel, {'item': jid_})
+                        self.host.displayWidget(chat.Chat, 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	Tue Feb 10 20:52:02 2015 +0100
+++ b/src/browser/sat_browser/richtext.py	Wed Feb 11 11:18:13 2015 +0100
@@ -37,7 +37,8 @@
 import base_panels
 import list_manager
 import html_tools
-import panels
+import blog
+import chat
 
 
 class RichTextEditor(base_panels.BaseTextEditor, FlexTable):
@@ -458,11 +459,11 @@
         if target == "":
             return True
         if target.startswith("@"):
-            _class = panels.MicroblogPanel
+            _class = blog.MicroblogPanel
             target = None if target == "@@" else target[1:]
         else:
-            _class = panels.ChatPanel
-        self.host.getOrCreateLiberviaWidget(_class, {'item': target})
+            _class = chat.Chat
+        self.host.displayWidget(_class, target)
         return True
 
     def syncFromEditor(self, content):