diff src/browser/libervia_main.py @ 462:07433bd892ee

server and browser sides: fixes UI parameters initialisation with user values + small refactoring
author souliane <souliane@mailoo.org>
date Wed, 04 Jun 2014 00:23:14 +0200
parents d0cd8ae579f7
children 97c72fe4a5f2
line wrap: on
line diff
--- a/src/browser/libervia_main.py	Wed Jun 04 00:22:00 2014 +0200
+++ b/src/browser/libervia_main.py	Wed Jun 04 00:23:14 2014 +0200
@@ -197,13 +197,7 @@
         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": C.ENABLE_UNIBOX_PARAM,
-                                     "category": C.ENABLE_UNIBOX_KEY,
-                                     "cast": lambda value: value == 'true',
-                                     "value": None
-                                     }
-                          }
+        self.ui_params = {key: C.UI_PARAMS[key]['initial_value'] for key in C.UI_PARAMS}
 
     def addSelectedListener(self, callback):
         self._selected_listeners.add(callback)
@@ -285,7 +279,7 @@
     def refresh(self):
         """Refresh the general display."""
         self.panel.refresh()
-        if self.params_ui['unibox']['value']:
+        if self.getUIParam('unibox'):
             self.uni_box = self.panel.unibox_panel.unibox
         else:
             self.uni_box = None
@@ -367,13 +361,15 @@
         # get ui params and refresh the display
         count = 0  # used to do something similar to DeferredList
 
-        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 ui_params_cb(param):
+            def cb(value):
+                count += 1
+                refresh = count == len(C.UI_PARAMS)
+                self._paramUpdate(C.UI_PARAMS[param]['name'], value, C.UI_PARAMS[param]['category'], refresh)
+            return cb
+
+        for param in C.UI_PARAMS:
+            self.bridge.call('asyncGetParamA', ui_params_cb(param), C.UI_PARAMS[param]['name'], C.UI_PARAMS[param]['category'])
 
     def _tryAutoConnect(self, skip_validation=False):
         """This method retrieve the eventual URL parameters to auto-connect the user.
@@ -857,13 +853,20 @@
         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)
+        for param in C.UI_PARAMS:
+            if name == C.UI_PARAMS[param]['name'] and category == C.UI_PARAMS[param]['category']:
+                self.ui_params[param] = C.UI_PARAMS[param]['cast_from_str'](value)
                 if refresh:
                     self.refresh()
                 break
 
+    def getUIParam(self, param):
+        """Return UI param cached value
+
+        @param param: the parameter key
+        """
+        return self.ui_params[param] if param in self.ui_params else None
+
     def sendError(self, errorData):
         dialog.InfoDialog("Error while sending message",
                           "Your message can't be sent", Width="400px").center()