fix off-by-one error on the progress and fix vars in ToolExistsPrecondition
[enigma2.git] / lib / python / Components / config.py
index 3c01794ab0fdcf92e15a85313a9caae7ad434262..b0f5fe6d9ed13de26096777b25ad61bae5414b15 100644 (file)
@@ -1,4 +1,5 @@
 import time
+from enigma import getPrevAsciiCode
 from Tools.NumericalTextInput import NumericalTextInput
 from Tools.Directories import resolveFilename, SCOPE_CONFIG
 import copy
@@ -97,14 +98,22 @@ class ConfigElement(object):
        def __call__(self, selected):
                return self.getMulti(selected)
 
-       def helpWindow(self):
-               return None
+       def onSelect(self, session):
+               pass
+
+       def onDeselect(self, session):
+               pass
 
 KEY_LEFT = 0
 KEY_RIGHT = 1
 KEY_OK = 2
 KEY_DELETE = 3
-KEY_TIMEOUT = 4
+KEY_BACKSPACE = 4
+KEY_HOME = 5
+KEY_END = 6
+KEY_TOGGLEOW = 7
+KEY_ASCII = 8
+KEY_TIMEOUT = 9
 KEY_NUMBERS = range(12, 12+10)
 KEY_0 = 12
 KEY_9 = 12+9
@@ -124,6 +133,10 @@ def getKeyNumber(key):
 class ConfigSelection(ConfigElement):
        def __init__(self, choices, default = None):
                ConfigElement.__init__(self)
+               self._value = None
+               self.setChoices(choices, default)
+
+       def setChoices(self, choices, default = None):
                self.choices = []
                self.description = {}
                
@@ -154,7 +167,10 @@ class ConfigSelection(ConfigElement):
                for x in self.choices:
                        assert isinstance(x, str), "ConfigSelection choices must be strings"
                
-               self.value = self.default = default
+               self.default = default
+
+               if self.value == None or not self.value in self.choices:
+                       self.value = default
 
        def setValue(self, value):
                if value in self.choices:
@@ -192,6 +208,10 @@ class ConfigSelection(ConfigElement):
                        self.value = self.choices[(i + nchoices - 1) % nchoices]
                elif key == KEY_RIGHT:
                        self.value = self.choices[(i + 1) % nchoices]
+               elif key == KEY_HOME:
+                       self.value = self.choices[0]
+               elif key == KEY_END:
+                       self.value = self.choices[nchoices - 1]
 
        def getText(self):
                descr = self.description[self.value]
@@ -233,6 +253,10 @@ class ConfigBoolean(ConfigElement):
        def handleKey(self, key):
                if key in [KEY_LEFT, KEY_RIGHT]:
                        self.value = not self.value
+               elif key == KEY_HOME:
+                       self.value = False
+               elif key == KEY_END:
+                       self.value = True
 
        def getText(self):
                descr = self.descriptions[self.value]
@@ -294,8 +318,10 @@ class ConfigDateTime(ConfigElement):
        def handleKey(self, key):
                if key == KEY_LEFT:
                        self.value = self.value - self.increment
-               if key == KEY_RIGHT:
+               elif key == KEY_RIGHT:
                        self.value = self.value + self.increment
+               elif key == KEY_HOME or key == KEY_END:
+                       self.value = self.default
 
        def getText(self):
                return time.strftime(self.formatstring, time.localtime(self.value))
@@ -328,6 +354,8 @@ class ConfigSequence(ConfigElement):
                
                self.default = default
                self.value = copy.copy(default)
+               
+               self.endNotifier = []
 
        def validate(self):
                max_pos = 0
@@ -335,15 +363,17 @@ class ConfigSequence(ConfigElement):
                for i in self._value:
                        max_pos += len(str(self.limits[num][1]))
 
-                       while self._value[num] < self.limits[num][0]:
-                               self.value[num] += 1
+                       if self._value[num] < self.limits[num][0]:
+                               self._value[num] = self.limits[num][0]
 
-                       while self._value[num] > self.limits[num][1]:
-                               self._value[num] -= 1
+                       if self._value[num] > self.limits[num][1]:
+                               self._value[num] = self.limits[num][1]
 
                        num += 1
 
                if self.marked_pos >= max_pos:
+                       for x in self.endNotifier:
+                               x(self)
                        self.marked_pos = max_pos - 1
 
                if self.marked_pos < 0:
@@ -357,6 +387,9 @@ class ConfigSequence(ConfigElement):
 
                if self.marked_pos >= total_len:
                        self.marked_pos = total_len - 1
+                       
+       def addEndNotifier(self, notifier):
+               self.endNotifier.append(notifier)
 
        def handleKey(self, key):
                if key == KEY_LEFT:
@@ -367,7 +400,28 @@ class ConfigSequence(ConfigElement):
                        self.marked_pos += 1
                        self.validatePos()
                
-               if key in KEY_NUMBERS:
+               if key == KEY_HOME:
+                       self.marked_pos = 0
+                       self.validatePos()
+
+               if key == KEY_END:
+                       max_pos = 0
+                       num = 0
+                       for i in self._value:
+                               max_pos += len(str(self.limits[num][1]))
+                               num += 1
+                       self.marked_pos = max_pos - 1
+                       self.validatePos()
+               
+               if key in KEY_NUMBERS or key == KEY_ASCII:
+                       if key == KEY_ASCII:
+                               code = getPrevAsciiCode()
+                               if code < 48 or code > 57:
+                                       return
+                               number = code - 48
+                       else:
+                               number = getKeyNumber(key)
+                       
                        block_len = []
                        for x in self.limits:
                                block_len.append(len(str(x[1])))
@@ -385,8 +439,6 @@ class ConfigSequence(ConfigElement):
                                else:
                                        blocknumber += 1
 
-                       number = getKeyNumber(key)
-                       
                        # length of numberblock
                        number_len = len(str(self.limits[blocknumber][1]))
 
@@ -440,11 +492,15 @@ class ConfigSequence(ConfigElement):
                return str(v)
 
        def fromstring(self, value):
-               return [int(x) for x in self.saved_value.split(self.seperator)]
+               return [int(x) for x in value.split(self.seperator)]
 
 class ConfigIP(ConfigSequence):
        def __init__(self, default):
                ConfigSequence.__init__(self, seperator = ".", limits = [(0,255),(0,255),(0,255),(0,255)], default = default)
+       
+       def getHTML(self, id):
+               # we definitely don't want leading zeros
+               return '.'.join(["%d" % d for d in self.value])
 
 class ConfigMAC(ConfigSequence):
        def __init__(self, default):
@@ -461,7 +517,7 @@ class ConfigClock(ConfigSequence):
                ConfigSequence.__init__(self, seperator = ":", limits = [(0,23),(0,59)], default = [t.tm_hour, t.tm_min])
 
 class ConfigInteger(ConfigSequence):
-       def __init__(self, default, limits = (0, 10000000000)):
+       def __init__(self, default, limits = (0, 9999999999)):
                ConfigSequence.__init__(self, seperator = ":", limits = [limits], default = default)
        
        # you need to override this to do input validation
@@ -502,43 +558,132 @@ class ConfigFloat(ConfigSequence):
 
 # an editable text...
 class ConfigText(ConfigElement, NumericalTextInput):
-       def __init__(self, default = "", fixed_size = True):
+       def __init__(self, default = "", fixed_size = True, visible_width = False):
                ConfigElement.__init__(self)
                NumericalTextInput.__init__(self, nextFunc = self.nextFunc, handleTimeout = False)
                
                self.marked_pos = 0
+               self.allmarked = (default != "")
                self.fixed_size = fixed_size
-
+               self.visible_width = visible_width
+               self.offset = 0
+               self.overwrite = fixed_size
+               self.help_window = None
                self.value = self.default = default
 
        def validateMarker(self):
+               if self.fixed_size:
+                       if self.marked_pos > len(self.text)-1:
+                               self.marked_pos = len(self.text)-1
+               else:
+                       if self.marked_pos > len(self.text):
+                               self.marked_pos = len(self.text)
                if self.marked_pos < 0:
                        self.marked_pos = 0
-               if self.marked_pos >= len(self.text):
-                       self.marked_pos = len(self.text) - 1
+               if self.visible_width:
+                       if self.marked_pos < self.offset:
+                               self.offset = self.marked_pos
+                       if self.marked_pos >= self.offset + self.visible_width:
+                               if self.marked_pos == len(self.text):
+                                       self.offset = self.marked_pos - self.visible_width
+                               else:
+                                       self.offset = self.marked_pos - self.visible_width + 1
+                       if self.offset > 0 and self.offset + self.visible_width > len(self.text):
+                               self.offset = max(0, len(self.text) - self.visible_width)
+
+       def insertChar(self, ch, pos, owr):
+               if owr or self.overwrite:
+                       self.text = self.text[0:pos] + ch + self.text[pos + 1:]
+               elif self.fixed_size:
+                       self.text = self.text[0:pos] + ch + self.text[pos:-1]
+               else:
+                       self.text = self.text[0:pos] + ch + self.text[pos:]
+
+       def deleteChar(self, pos):
+               if not self.fixed_size:
+                       self.text = self.text[0:pos] + self.text[pos + 1:]
+               elif self.overwrite:
+                       self.text = self.text[0:pos] + " " + self.text[pos + 1:]
+               else:
+                       self.text = self.text[0:pos] + self.text[pos + 1:] + " "
 
-       #def nextEntry(self):
-       #       self.vals[1](self.getConfigPath())
+       def deleteAllChars(self):
+               if self.fixed_size:
+                       self.text = " " * len(self.text)
+               else:
+                       self.text = ""
+               self.marked_pos = 0
 
        def handleKey(self, key):
                # this will no change anything on the value itself
                # so we can handle it here in gui element
                if key == KEY_DELETE:
-                       self.text = self.text[0:self.marked_pos] + self.text[self.marked_pos + 1:]
+                       self.timeout()
+                       if self.allmarked:
+                               self.deleteAllChars()
+                               self.allmarked = False
+                       else:
+                               self.deleteChar(self.marked_pos)
+                               if self.fixed_size and self.overwrite:
+                                       self.marked_pos += 1
+               elif key == KEY_BACKSPACE:
+                       self.timeout()
+                       if self.allmarked:
+                               self.deleteAllChars()
+                               self.allmarked = False
+                       elif self.marked_pos > 0:
+                               self.deleteChar(self.marked_pos-1)
+                               if not self.fixed_size and self.offset > 0:
+                                       self.offset -= 1
+                               self.marked_pos -= 1
                elif key == KEY_LEFT:
-                       self.marked_pos -= 1
+                       self.timeout()
+                       if self.allmarked:
+                               self.marked_pos = len(self.text)
+                               self.allmarked = False
+                       else:
+                               self.marked_pos -= 1
                elif key == KEY_RIGHT:
+                       self.timeout()
+                       if self.allmarked:
+                               self.marked_pos = 0
+                               self.allmarked = False
+                       else:
+                               self.marked_pos += 1
+               elif key == KEY_HOME:
+                       self.timeout()
+                       self.allmarked = False
+                       self.marked_pos = 0
+               elif key == KEY_END:
+                       self.timeout()
+                       self.allmarked = False
+                       self.marked_pos = len(self.text)
+               elif key == KEY_TOGGLEOW:
+                       self.timeout()
+                       self.overwrite = not self.overwrite
+               elif key == KEY_ASCII:
+                       self.timeout()
+                       newChar = unichr(getPrevAsciiCode())
+                       if self.allmarked:
+                               self.deleteAllChars()
+                               self.allmarked = False
+                       self.insertChar(newChar, self.marked_pos, False)
                        self.marked_pos += 1
-                       if not self.fixed_size:
-                               if self.marked_pos >= len(self.text):
-                                       self.text = self.text.ljust(len(self.text) + 1)
                elif key in KEY_NUMBERS:
-                       number = self.getKey(getKeyNumber(key))
-                       self.text = self.text[0:self.marked_pos] + unicode(number) + self.text[self.marked_pos + 1:]
+                       owr = self.lastKey == getKeyNumber(key)
+                       newChar = self.getKey(getKeyNumber(key))
+                       if self.allmarked:
+                               self.deleteAllChars()
+                               self.allmarked = False
+                       self.insertChar(newChar, self.marked_pos, owr)
                elif key == KEY_TIMEOUT:
                        self.timeout()
+                       if self.help_window:
+                               self.help_window.update(self)
                        return
 
+               if self.help_window:
+                       self.help_window.update(self)
                self.validateMarker()
                self.changed()
 
@@ -561,14 +706,35 @@ class ConfigText(ConfigElement, NumericalTextInput):
        _value = property(getValue, setValue)
 
        def getText(self):
-               return self.value
+               return self.text.encode("utf-8")
 
        def getMulti(self, selected):
-               return ("mtext"[1-selected:], self.value, [self.marked_pos])
+               if self.visible_width:
+                       if self.allmarked:
+                               mark = range(0, min(self.visible_width, len(self.text)))
+                       else:
+                               mark = [self.marked_pos-self.offset]
+                       return ("mtext"[1-selected:], self.text[self.offset:self.offset+self.visible_width].encode("utf-8")+" ", mark)
+               else:
+                       if self.allmarked:
+                               mark = range(0, len(self.text))
+                       else:
+                               mark = [self.marked_pos]
+                       return ("mtext"[1-selected:], self.text.encode("utf-8")+" ", mark)
 
-       def helpWindow(self):
-               from Screens.NumericalTextInputHelpDialog import NumericalTextInputHelpDialog
-               return (NumericalTextInputHelpDialog,self)
+       def onSelect(self, session):
+               self.allmarked = (self.value != "")
+               if session is not None:
+                       from Screens.NumericalTextInputHelpDialog import NumericalTextInputHelpDialog
+                       self.help_window = session.instantiateDialog(NumericalTextInputHelpDialog, self)
+                       self.help_window.show()
+
+       def onDeselect(self, session):
+               self.marked_pos = 0
+               self.offset = 0
+               if self.help_window:
+                       session.deleteDialog(self.help_window)
+                       self.help_window = None
 
        def getHTML(self, id):
                return '<input type="text" name="' + id + '" value="' + self.value + '" /><br>\n'
@@ -576,6 +742,54 @@ class ConfigText(ConfigElement, NumericalTextInput):
        def unsafeAssign(self, value):
                self.value = str(value)
 
+class ConfigNumber(ConfigText):
+       def __init__(self, default = 0):
+               ConfigText.__init__(self, str(default), fixed_size = False)
+
+       def getValue(self):
+               return int(self.text)
+               
+       def setValue(self, val):
+               self.text = str(val)
+
+       value = property(getValue, setValue)
+       _value = property(getValue, setValue)
+
+       def conform(self):
+               pos = len(self.text) - self.marked_pos
+               self.text = self.text.lstrip("0")
+               if self.text == "":
+                       self.text = "0"
+               if pos > len(self.text):
+                       self.marked_pos = 0
+               else:
+                       self.marked_pos = len(self.text) - pos
+
+       def handleKey(self, key):
+               if key in KEY_NUMBERS or key == KEY_ASCII:
+                       if key == KEY_ASCII:
+                               ascii = getPrevAsciiCode()
+                               if not (48 <= ascii <= 57):
+                                       return
+                       else:
+                               ascii = getKeyNumber(key) + 48
+                       newChar = unichr(ascii)
+                       if self.allmarked:
+                               self.deleteAllChars()
+                               self.allmarked = False
+                       self.insertChar(newChar, self.marked_pos, False)
+                       self.marked_pos += 1
+               else:
+                       ConfigText.handleKey(self, key)
+               self.conform()
+
+       def onSelect(self, session):
+               self.allmarked = (self.value != "")
+
+       def onDeselect(self, session):
+               self.marked_pos = 0
+               self.offset = 0
+
 # a slider.
 class ConfigSlider(ConfigElement):
        def __init__(self, default = 0, increment = 1, limits = (0, 100)):
@@ -597,6 +811,10 @@ class ConfigSlider(ConfigElement):
                        self.value -= self.increment
                elif key == KEY_RIGHT:
                        self.value += self.increment
+               elif key == KEY_HOME:
+                       self.value = self.min
+               elif key == KEY_END:
+                       self.value = self.max
                else:
                        return
 
@@ -618,7 +836,7 @@ class ConfigSatlist(ConfigSelection):
        def __init__(self, list, default = None):
                if default is not None:
                        default = str(default)
-               ConfigSelection.__init__(self, choices = [(str(orbpos), desc) for (orbpos, desc) in list], default = default)
+               ConfigSelection.__init__(self, choices = [(str(orbpos), desc) for (orbpos, desc, flags) in list], default = default)
 
        def getOrbitalPosition(self):
                if self.value == "":
@@ -694,6 +912,12 @@ class ConfigSubList(list, object):
                        item.saved_value = self.stored_values[i]
                        item.load()
 
+       def dict(self):
+               res = dict()
+               for index in range(len(self)):
+                       res[str(index)] = self[index]
+               return res
+
 # same as ConfigSubList, just as a dictionary.
 # care must be taken that the 'key' has a proper
 # str() method, because it will be used in the config
@@ -733,6 +957,9 @@ class ConfigSubDict(dict, object):
                        item.saved_value = self.stored_values[str(key)]
                        item.load()
 
+       def dict(self):
+               return self
+
 # Like the classes above, just with a more "native"
 # syntax.
 #
@@ -778,10 +1005,10 @@ class ConfigSubsection(object):
                values = dict(values)
                
                self.content.stored_values = values
-               
+
                for (key, val) in self.content.items.items():
                        if key in values:
-                               val.setSavedValue(values[key])
+                               val.saved_value = values[key]
 
        saved_value = property(getSavedValue, setSavedValue)
 
@@ -793,6 +1020,96 @@ class ConfigSubsection(object):
                for x in self.content.items.values():
                        x.load()
 
+       def dict(self):
+               return self.content.items
+
+class ConfigSet(ConfigElement):
+       def __init__(self, choices, default = []):
+               ConfigElement.__init__(self)
+               self.choices = []
+               self.description = {}
+               if isinstance(choices, list):
+                       choices.sort()
+                       for x in choices:
+                               if isinstance(x, tuple):
+                                       self.choices.append(x[0])
+                                       self.description[x[0]] = str(x[1])
+                               else:
+                                       self.choices.append(x)
+                                       self.description[x] = str(x)
+               else:
+                       assert False, "ConfigSet choices must be a list!"
+               if len(self.choices) == 0:
+                       self.choices = [""]
+                       self.description[""] = ""
+               if default is None:
+                       default = []
+               self.pos = -1
+               default.sort()
+               self.default = default
+               self.value = default+[]
+
+       def toggleChoice(self, choice):
+               if choice in self.value:
+                       self.value.remove(choice)
+               else:
+                       self.value.append(choice)
+                       self.value.sort()
+
+       def handleKey(self, key):
+               if key in KEY_NUMBERS + [KEY_DELETE, KEY_BACKSPACE]:
+                       if self.pos != -1:
+                               self.toggleChoice(self.choices[self.pos])
+               elif key == KEY_LEFT:
+                       self.pos -= 1
+                       if self.pos < -1:
+                           self.pos = len(self.choices)-1
+               elif key == KEY_RIGHT:
+                       self.pos += 1
+                       if self.pos >= len(self.choices):
+                           self.pos = -1
+               elif key in [KEY_HOME, KEY_END]:
+                       self.pos = -1
+
+       def genString(self, lst):
+               res = ""
+               for x in lst:
+                       res += self.description[x]+" "
+               return res
+
+       def getText(self):
+               return self.genString(self.value)
+
+       def getMulti(self, selected):
+               if not selected or self.pos == -1:
+                       return ("text", self.genString(self.value))
+               else:
+                       tmp = self.value+[]
+                       ch = self.choices[self.pos]
+                       mem = ch in self.value
+                       if not mem:
+                               tmp.append(ch)
+                               tmp.sort()
+                       ind = tmp.index(ch)
+                       val1 = self.genString(tmp[:ind])
+                       val2 = " "+self.genString(tmp[ind+1:])
+                       if mem:
+                               chstr = " "+self.description[ch]+" "
+                       else:
+                               chstr = "("+self.description[ch]+")"
+                       return ("mtext", val1+chstr+val2, range(len(val1),len(val1)+len(chstr)))
+
+       def onDeselect(self, session):
+               self.pos = -1
+               self.changed()
+               
+       def tostring(self, value):
+               return str(value)
+
+       def fromstring(self, val):
+               return eval(val)
+
+
 # the root config object, which also can "pickle" (=serialize)
 # down the whole config tree.
 #
@@ -874,7 +1191,7 @@ class ConfigFile:
        def __resolveValue(self, pickles, cmap):
                if cmap.has_key(pickles[0]):
                        if len(pickles) > 1:
-                               return self.__resolveValue(pickles[1:], cmap[pickles[0]].content.items)
+                               return self.__resolveValue(pickles[1:], cmap[pickles[0]].dict())
                        else:
                                return str(cmap[pickles[0]].value)
                return None