diff libervia.py @ 326:36927be51481

browser_side: fixed the behavior regarding "Enable unibox" parameter: - unibox is disabled by default - dynamic refresh of the concerned widgets - use the generic method refresh instead of setUniBox
author souliane <souliane@mailoo.org>
date Tue, 07 Jan 2014 15:36:18 +0100
parents d07b54fdc60a
children 835a8ae799e7
line wrap: on
line diff
--- a/libervia.py	Tue Jan 07 15:39:16 2014 +0100
+++ b/libervia.py	Tue Jan 07 15:36:18 2014 +0100
@@ -186,6 +186,13 @@
         self._register.call('isRegistered', self._isRegisteredCB)
         self.initialised = False
         self.init_cache = []  # used to cache events until initialisation is done
+        # define here the parameters that have an incidende to UI refresh
+        self.params_ui = {"unibox": {"name": Const.ENABLE_UNIBOX_PARAM,
+                                     "category": Const.ENABLE_UNIBOX_KEY,
+                                     "cast": lambda value: value == 'true',
+                                     "value": None
+                                     }
+                          }
 
     def addSelectedListener(self, callback):
         self._selected_listeners.add(callback)
@@ -264,11 +271,16 @@
         except KeyError:
             print ('WARNING: trying to remove a non registered Widget:', wid.getDebugName())
 
-    def _setUniBox(self, enable):
-        """Enable or disable the unibox widget.
-        @param enable: boolean
-        """
-        self.uni_box = self.panel.setUniBoxPanel(enable == 'true')
+    def refresh(self):
+        """Refresh the general display."""
+        self.panel.refresh()
+        if self.params_ui['unibox']['value']:
+            self.uni_box = self.panel.unibox_panel.unibox
+        else:
+            self.uni_box = None
+        for lib_wid in self.libervia_widgets:
+            lib_wid.refresh()
+        self.resize()
 
     def addTab(self, label, wid, select=True):
         """Create a new tab and eventually add a widget in
@@ -328,13 +340,18 @@
             self._defaultDomain = "libervia.org"
 
         self.bridge.call("getNewAccountDomain", (domain_cb, domain_eb))
+        self.discuss_panel.addWidget(panels.MicroblogPanel(self, []))
 
-        def unibox_cb(enable):
-            self._setUniBox(enable)
-            self.discuss_panel.addWidget(panels.MicroblogPanel(self, []))
-            self.resize()  # resize after all the UI elements have been attached
+        # get ui params and refresh the display
+        count = 0  # used to do something similar to DeferredList
 
-        self.bridge.call('asyncGetParamA', unibox_cb, Const.ENABLE_UNIBOX_PARAM, Const.ENABLE_UNIBOX_KEY)
+        def params_ui_cb(param, value=None):
+            count += 1
+            refresh = count == len(self.params_ui)
+            self._paramUpdate(param['name'], value, param['category'], refresh)
+        for param in self.params_ui:
+            self.bridge.call('asyncGetParamA', lambda value: params_ui_cb(self.params_ui[param], value),
+                             self.params_ui[param]['name'], self.params_ui[param]['category'])
 
     def _tryAutoConnect(self):
         """This method retrieve the eventual URL parameters to auto-connect the user."""
@@ -395,6 +412,8 @@
             self._newMessageCb(*args)
         elif name == 'presenceUpdate':
             self._presenceUpdateCb(*args)
+        elif name == 'paramUpdate':
+            self._paramUpdate(*args)
         elif name == 'roomJoined':
             self._roomJoinedCb(*args)
         elif name == 'roomLeft':
@@ -785,6 +804,18 @@
     def _newAlert(self, message, title, alert_type):
         dialog.InfoDialog(title, message).show()
 
+    def _paramUpdate(self, name, value, category, refresh=True):
+        """This is called when the paramUpdate signal is received, but also
+        during initialization when the UI parameters values are retrieved.
+        @param refresh: set to True to refresh the general UI
+        """
+        for param in self.params_ui:
+            if name == self.params_ui[param]['name']:
+                self.params_ui[param]['value'] = self.params_ui[param]['cast'](value)
+                if refresh:
+                    self.refresh()
+                break
+
     def sendError(self, errorData):
         dialog.InfoDialog("Error while sending message",
                           "Your message can't be sent", Width="400px").center()