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