add modifyEntry
[enigma2.git] / lib / python / Components / config.py
index 1f2975eff65e1d5a2aa036089fac0fa85971c875..0faf26dab27e4392d25b147255eef54b69b6ec00 100755 (executable)
@@ -144,6 +144,7 @@ class choicesList(object): # XXX: we might want a better name for this
        LIST_TYPE_DICT = 2
 
        def __init__(self, choices, type = None):
+               object.__init__(self)
                self.choices = choices
                if type is None:
                        if isinstance(choices, list):
@@ -156,14 +157,14 @@ class choicesList(object): # XXX: we might want a better name for this
                        self.type = type
 
        def __list__(self):
-               if self.type is choicesList.LIST_TYPE_LIST:
+               if self.type == choicesList.LIST_TYPE_LIST:
                        ret = [isinstance(x, tuple) and x[0] or x for x in self.choices]
                else:
                        ret = self.choices.keys()
                return ret or [""]
 
        def __iter__(self):
-               if self.type is choicesList.LIST_TYPE_LIST:
+               if self.type == choicesList.LIST_TYPE_LIST:
                        ret = [isinstance(x, tuple) and x[0] or x for x in self.choices]
                else:
                        ret = self.choices
@@ -173,7 +174,7 @@ class choicesList(object): # XXX: we might want a better name for this
                return len(self.choices) or 1
 
        def __getitem__(self, index):
-               if self.type is choicesList.LIST_TYPE_LIST:
+               if self.type == choicesList.LIST_TYPE_LIST:
                        ret = self.choices[index]
                        if isinstance(ret, tuple):
                                ret = ret[0]
@@ -184,9 +185,10 @@ class choicesList(object): # XXX: we might want a better name for this
                return self.__list__().index(value)
 
        def __setitem__(self, index, value):
-               if self.type is choicesList.LIST_TYPE_LIST:
-                       if isinstance(self.choices[index], tuple):
-                               self.choices[index] = (value, self.choices[index][1])
+               if self.type == choicesList.LIST_TYPE_LIST:
+                       orig = self.choices[index]
+                       if isinstance(orig, tuple):
+                               self.choices[index] = (value, orig[1])
                        else:
                                self.choices[index] = value
                else:
@@ -206,7 +208,7 @@ class choicesList(object): # XXX: we might want a better name for this
 
 class descriptionList(choicesList): # XXX: we might want a better name for this
        def __list__(self):
-               if self.type is choicesList.LIST_TYPE_LIST:
+               if self.type == choicesList.LIST_TYPE_LIST:
                        ret = [isinstance(x, tuple) and x[1] or x for x in self.choices]
                else:
                        ret = self.choices.values()
@@ -216,22 +218,23 @@ class descriptionList(choicesList): # XXX: we might want a better name for this
                return iter(self.__list__())
 
        def __getitem__(self, index):
-               if self.type is choicesList.LIST_TYPE_LIST:
+               if self.type == choicesList.LIST_TYPE_LIST:
                        for x in self.choices:
                                if isinstance(x, tuple):
-                                       if x[0] is index:
+                                       if x[0] == index:
                                                return str(x[1])
-                               elif x is index:
+                               elif x == index:
                                        return str(x)
                        return str(index) # Fallback!
                else:
                        return str(self.choices.get(index, ""))
 
        def __setitem__(self, index, value):
-               if self.type is choicesList.LIST_TYPE_LIST:
+               if self.type == choicesList.LIST_TYPE_LIST:
                        i = self.index(index)
-                       if isinstance(self.choices[i], tuple):
-                               self.choices[i] = (self.choices[i][0], value)
+                       orig = self.choices[i]
+                       if isinstance(orig, tuple):
+                               self.choices[i] = (orig[0], value)
                        else:
                                self.choices[i] = value
                else:
@@ -248,8 +251,6 @@ class descriptionList(choicesList): # XXX: we might want a better name for this
 class ConfigSelection(ConfigElement):
        def __init__(self, choices, default = None):
                ConfigElement.__init__(self)
-
-               # this is an exakt copy of def setChoices.. but we save the call overhead
                self.choices = choicesList(choices)
 
                if default is None:
@@ -264,8 +265,8 @@ class ConfigSelection(ConfigElement):
                if default is None:
                        default = self.choices.default()
 
-               self.default = self._value = self.last_value = default
-               self.changed()
+               if self.value not in self.choices:
+                       self.value = default
 
        def setValue(self, value):
                if value in self.choices:
@@ -283,7 +284,7 @@ class ConfigSelection(ConfigElement):
        def setCurrentText(self, text):
                i = self.choices.index(self.value)
                self.choices[i] = text
-               descriptionList(self.choices.choices, self.choices.type)[text] = text
+               self.description[text] = text
                self._value = text
 
        value = property(getValue, setValue)
@@ -305,20 +306,20 @@ class ConfigSelection(ConfigElement):
                        self.value = self.choices[0]
                elif key == KEY_END:
                        self.value = self.choices[nchoices - 1]
-                       
+
        def selectNext(self):
                nchoices = len(self.choices)
                i = self.choices.index(self.value)
                self.value = self.choices[(i + 1) % nchoices]
 
        def getText(self):
-               descr = descriptionList(self.choices.choices, self.choices.type)[self.value]
+               descr = self.description[self.value]
                if len(descr):
                        return _(descr)
                return descr
 
        def getMulti(self, selected):
-               descr = descriptionList(self.choices.choices, self.choices.type)[self.value]
+               descr = self.description[self.value]
                if len(descr):
                        return ("text", _(descr))
                return ("text", descr)
@@ -327,7 +328,7 @@ class ConfigSelection(ConfigElement):
        def getHTML(self, id):
                res = ""
                for v in self.choices:
-                       descr = descriptionList(self.choices.choices, self.choices.type)[v]
+                       descr = self.description[v]
                        if self.value == v:
                                checked = 'checked="checked" '
                        else:
@@ -339,6 +340,8 @@ class ConfigSelection(ConfigElement):
                # setValue does check if value is in choices. This is safe enough.
                self.value = value
 
+       description = property(lambda self: descriptionList(self.choices.choices, self.choices.type))
+
 # a binary decision.
 #
 # several customized versions exist for different
@@ -1150,9 +1153,8 @@ class ConfigSet(ConfigElement):
 
        def genString(self, lst):
                res = ""
-               description = descriptionList(self.choices.choices, choicesList.LIST_TYPE_LIST)
                for x in lst:
-                       res += description[x]+" "
+                       res += self.description[x]+" "
                return res
 
        def getText(self):
@@ -1162,7 +1164,6 @@ class ConfigSet(ConfigElement):
                if not selected or self.pos == -1:
                        return ("text", self.genString(self.value))
                else:
-                       description = descriptionList(self.choices.choices, choicesList.LIST_TYPE_LIST)
                        tmp = self.value[:]
                        ch = self.choices[self.pos]
                        mem = ch in self.value
@@ -1173,9 +1174,9 @@ class ConfigSet(ConfigElement):
                        val1 = self.genString(tmp[:ind])
                        val2 = " "+self.genString(tmp[ind+1:])
                        if mem:
-                               chstr = " "+description[ch]+" "
+                               chstr = " "+self.description[ch]+" "
                        else:
-                               chstr = "("+description[ch]+")"
+                               chstr = "("+self.description[ch]+")"
                        return ("mtext", val1+chstr+val2, range(len(val1),len(val1)+len(chstr)))
 
        def onDeselect(self, session):
@@ -1190,6 +1191,8 @@ class ConfigSet(ConfigElement):
        def fromstring(self, val):
                return eval(val)
 
+       description = property(lambda self: descriptionList(self.choices.choices, choicesList.LIST_TYPE_LIST))
+
 class ConfigLocations(ConfigElement):
        def __init__(self, default = [], visible_width = False):
                ConfigElement.__init__(self)