changeset 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 4f25aa5039b3
children b62c1cf0dbf7
files src/browser/constants.py src/browser/libervia_main.py src/browser/panels.py src/common/constants.py src/server/server.py
diffstat 5 files changed, 45 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/src/browser/constants.py	Wed Jun 04 00:22:00 2014 +0200
+++ b/src/browser/constants.py	Wed Jun 04 00:23:14 2014 +0200
@@ -17,8 +17,23 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-from libervia.common import constants
+from libervia.common.constants import Const as C
+
+
+# Auxiliary functions
+param_to_bool = lambda value: value == 'true'
 
 
-class Const(constants.Const):
+class Const(C):
     """Add here the constants that are only used by the browser side."""
+
+    # Parameters that have an incidence on UI display/refresh:
+    #     - they can be any parameter (not necessarily specific to Libervia)
+    #     - 'cast_from_str' is a method used to eventually convert to a non string type
+    #     - 'initial_value' is used to initialize the display before any profile connection
+    UI_PARAMS = {"unibox": {"name": C.ENABLE_UNIBOX_PARAM,
+                            "category": C.COMPOSITION_KEY,
+                            "cast_from_str": param_to_bool,
+                            "initial_value": False
+                             },
+                 }
--- 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()
--- a/src/browser/panels.py	Wed Jun 04 00:22:00 2014 +0200
+++ b/src/browser/panels.py	Wed Jun 04 00:23:14 2014 +0200
@@ -79,7 +79,7 @@
 
     def refresh(self):
         """Enable or disable this panel. Contained widgets are created when necessary."""
-        enable = self.host.params_ui['unibox']['value']
+        enable = self.host.getUIParam('unibox')
         self.setVisible(enable)
         if enable and not self.unibox:
             self.button = Button('<img src="media/icons/tango/actions/32/format-text-italic.png" class="richTextIcon"/>')
--- a/src/common/constants.py	Wed Jun 04 00:22:00 2014 +0200
+++ b/src/common/constants.py	Wed Jun 04 00:23:14 2014 +0200
@@ -24,7 +24,7 @@
 class Const(constants.Const):
 
     # Frontend parameters
-    ENABLE_UNIBOX_KEY = D_("Composition")
+    COMPOSITION_KEY = D_("Composition")
     ENABLE_UNIBOX_PARAM = D_("Enable unibox")
 
     # MISC
--- a/src/server/server.py	Wed Jun 04 00:22:00 2014 +0200
+++ b/src/server/server.py	Wed Jun 04 00:23:14 2014 +0200
@@ -770,15 +770,15 @@
         <params>
         <individual>
         <category name="%(category_name)s" label="%(category_label)s">
-            <param name="%(param_name)s" label="%(param_label)s" value="false" type="bool" security="0"/>
+            <param name="%(enable_unibox)s" label="%(enable_unibox_label)s" value="false" type="bool" security="0"/>
          </category>
         </individual>
         </params>
         """ % {
-            'category_name': C.ENABLE_UNIBOX_KEY,
-            'category_label': _(C.ENABLE_UNIBOX_KEY),
-            'param_name': C.ENABLE_UNIBOX_PARAM,
-            'param_label': _(C.ENABLE_UNIBOX_PARAM)
+            'category_name': C.COMPOSITION_KEY,
+            'category_label': _(C.COMPOSITION_KEY),
+            'enable_unibox': C.ENABLE_UNIBOX_PARAM,
+            'enable_unibox_label': _(C.ENABLE_UNIBOX_PARAM),
         }
 
         self.sat_host.bridge.paramsRegisterApp(params, C.SECURITY_LIMIT, C.APP_NAME)