install Console.py
[enigma2.git] / lib / python / Components / config.py
index b91c449e3b1348771be543178f8c722a7b046e0f..b448ca6db4fc7f340a38e69c83194821f771d2ff 100644 (file)
@@ -51,10 +51,11 @@ class ConfigElement(object):
 
        # you can overide this for fancy default handling
        def load(self):
-               if self.saved_value is None:
+               sv = self.saved_value
+               if sv is None:
                        self.value = self.default
                else:
-                       self.value = self.fromstring(self.saved_value)
+                       self.value = self.fromstring(sv)
 
        def tostring(self, value):
                return str(value)
@@ -70,9 +71,10 @@ class ConfigElement(object):
                self.load()
 
        def isChanged(self):
-               if self.saved_value is None and self.value == self.default:
+               sv = self.saved_value
+               if sv is None and self.value == self.default:
                        return False
-               return self.tostring(self.value) != self.saved_value
+               return self.tostring(self.value) != sv
 
        def changed(self):
                for x in self.notifiers:
@@ -501,9 +503,77 @@ class ConfigSequence(ConfigElement):
                return [int(x) for x in value.split(self.seperator)]
 
 class ConfigIP(ConfigSequence):
-       def __init__(self, default):
+       def __init__(self, default, auto_jump = False):
                ConfigSequence.__init__(self, seperator = ".", limits = [(0,255),(0,255),(0,255),(0,255)], default = default)
+               self.block_len = []
+               for x in self.limits:
+                       self.block_len.append(len(str(x[1])))
+               self.marked_block = 0
+               self.overwrite = True
+               self.auto_jump = auto_jump
+
+       def handleKey(self, key):
+               if key == KEY_LEFT:
+                       if self.marked_block > 0:
+                               self.marked_block -= 1
+                       self.overwrite = True
+
+               if key == KEY_RIGHT:
+                       if self.marked_block < len(self.limits)-1:
+                               self.marked_block += 1
+                       self.overwrite = True
+
+               if key == KEY_HOME:
+                       self.marked_block = 0
+                       self.overwrite = True
+
+               if key == KEY_END:
+                       self.marked_block = len(self.limits)-1
+                       self.overwrite = True
+
+               if key in KEY_NUMBERS:
+                       number = getKeyNumber(key)
+                       oldvalue = self._value[self.marked_block]
+                       
+                       if self.overwrite:
+                               self._value[self.marked_block] = number
+                               self.overwrite = False          
+                       else:
+                               oldvalue *= 10
+                               newvalue = oldvalue + number
+                               if self.auto_jump and newvalue > self.limits[self.marked_block][1] and self.marked_block < len(self.limits)-1:
+                                       self.handleKey(KEY_RIGHT)
+                                       self.handleKey(key)
+                                       return
+                               else:
+                                       self._value[self.marked_block] = newvalue
+
+                       if len(str(self._value[self.marked_block])) >= self.block_len[self.marked_block]:
+                               self.handleKey(KEY_RIGHT)
+
+                       self.validate()
+                       self.changed()
+
+       def genText(self):
+               value = ""
+               block_strlen = []
+               for i in self._value:
+                       block_strlen.append(len(str(i)))        
+                       if len(value):
+                               value += self.seperator
+                       value += str(i)
+               leftPos = sum(block_strlen[:(self.marked_block)])+self.marked_block
+               rightPos = sum(block_strlen[:(self.marked_block+1)])+self.marked_block
+               mBlock = range(leftPos, rightPos)
+               return (value, mBlock)
        
+       def getMulti(self, selected):
+               (value, mBlock) = self.genText()
+               if self.enabled:
+                       return ("mtext"[1-selected:], value, mBlock)
+               else:
+                       return ("text", value)
+
        def getHTML(self, id):
                # we definitely don't want leading zeros
                return '.'.join(["%d" % d for d in self.value])
@@ -748,6 +818,26 @@ class ConfigText(ConfigElement, NumericalTextInput):
        def unsafeAssign(self, value):
                self.value = str(value)
 
+class ConfigPassword(ConfigText):
+       def __init__(self, default = "", fixed_size = False, visible_width = False, censor = "*"):
+               ConfigText.__init__(self, default = default, fixed_size = fixed_size, visible_width = visible_width)
+               self.censor_char = censor
+               self.hidden = True
+
+       def getMulti(self, selected):
+               mtext, text, mark = ConfigText.getMulti(self, selected)
+               if self.hidden:
+                       text = len(text) * self.censor_char
+               return (mtext, text, mark)
+                       
+       def onSelect(self, session):
+               ConfigText.onSelect(self, session)
+               self.hidden = False
+
+       def onDeselect(self, session):
+               ConfigText.onDeselect(self, session)
+               self.hidden = True
+
 class ConfigNumber(ConfigText):
        def __init__(self, default = 0):
                ConfigText.__init__(self, str(default), fixed_size = False)
@@ -968,10 +1058,11 @@ class ConfigLocations(ConfigElement):
                return eval(val)
 
        def load(self):
-               if self.saved_value is None:
+               sv = self.saved_value
+               if sv is None:
                        tmp = self.default
                else:
-                       tmp = self.fromstring(self.saved_value)
+                       tmp = self.fromstring(sv)
                self.locations = [[x, None, False, False] for x in tmp]
                self.refreshMountpoints()
                for x in self.locations:
@@ -986,9 +1077,10 @@ class ConfigLocations(ConfigElement):
                        self.saved_value = self.tostring([x[0] for x in self.locations])
 
        def isChanged(self):
-               if self.saved_value is None and self.locations == []:
+               sv = self.saved_value
+               if val is None and self.locations == []:
                        return False
-               return self.tostring([x[0] for x in self.locations]) != self.saved_value
+               return self.tostring([x[0] for x in self.locations]) != sv
 
        def mountpointsChanged(self, action, dev):
                print "Mounts changed: ", action, dev
@@ -1140,7 +1232,7 @@ class ConfigSubList(list, object):
                                self[int(key)].saved_value = val
 
        saved_value = property(getSavedValue, setSavedValue)
-       
+
        def append(self, item):
                i = str(len(self))
                list.append(self, item)
@@ -1175,8 +1267,9 @@ class ConfigSubDict(dict, object):
        def getSavedValue(self):
                res = {}
                for (key, val) in self.items():
-                       if val.saved_value is not None:
-                               res[str(key)] = val.saved_value
+                       sv = val.saved_value
+                       if sv is not None:
+                               res[str(key)] = sv
                return res
 
        def setSavedValue(self, values):
@@ -1230,11 +1323,11 @@ class ConfigSubsection(object):
        def getSavedValue(self):
                res = self.content.stored_values
                for (key, val) in self.content.items.items():
-                       if val.saved_value is not None:
-                               res[key] = val.saved_value
+                       sv = val.saved_value
+                       if sv is not None:
+                               res[key] = sv
                        elif key in res:
                                del res[key]
-                               
                return res
 
        def setSavedValue(self, values):