X-Git-Url: https://git.cweiske.de/enigma2.git/blobdiff_plain/cd1ef2a9024596a6f5938b701e8a8ac623e40ee6..c2bbed774d6e5c51b4af23a60a95aca74aa5d4e9:/lib/python/Components/config.py?ds=sidebyside diff --git a/lib/python/Components/config.py b/lib/python/Components/config.py old mode 100644 new mode 100755 index b91c449e..d79337ba --- a/lib/python/Components/config.py +++ b/lib/python/Components/config.py @@ -34,6 +34,7 @@ class ConfigElement(object): self.save_disabled = False self.notifiers = [] self.enabled = True + self.callNotifiersOnSaveAndCancel = False # you need to override this to do input validation def setValue(self, value): @@ -51,10 +52,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) @@ -65,14 +67,19 @@ class ConfigElement(object): self.saved_value = None else: self.saved_value = self.tostring(self.value) + if self.callNotifiersOnSaveAndCancel: + self.changed() def cancel(self): self.load() + if self.callNotifiersOnSaveAndCancel: + self.changed() 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 +508,84 @@ 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 or key == KEY_ASCII: + if key == KEY_ASCII: + code = getPrevAsciiCode() + if code < 48 or code > 57: + return + number = code - 48 + else: + 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]) @@ -522,6 +604,34 @@ class ConfigClock(ConfigSequence): t = time.localtime(default) ConfigSequence.__init__(self, seperator = ":", limits = [(0,23),(0,59)], default = [t.tm_hour, t.tm_min]) + def increment(self): + # Check if Minutes maxed out + if self._value[1] == 59: + # Check if Hours not maxed out + if self._value[0] < 23: + # Increment Hour, reset Minutes to 0 + self._value[0] += 1 + self._value[1] = 0 + else: + # Increment Minutes + self._value[1] += 1 + # Trigger change + self.changed() + + def decrement(self): + # Check if Minutes is minimum + if self._value[1] == 0: + # Check if Hour is greater than 0 + if self._value[0] > 0: + # Decrement Hour, set Minutes to 59 + self._value[0] -= 1 + self._value[1] = 59 + else: + # Decrement Minutes + self._value[1] -= 1 + # Trigger change + self.changed() + class ConfigInteger(ConfigSequence): def __init__(self, default, limits = (0, 9999999999)): ConfigSequence.__init__(self, seperator = ":", limits = [limits], default = default) @@ -700,12 +810,12 @@ class ConfigText(ConfigElement, NumericalTextInput): def getValue(self): return self.text.encode("utf-8") - + def setValue(self, val): try: self.text = val.decode("utf-8") except UnicodeDecodeError: - self.text = val + self.text = val.decode("utf-8", "ignore") print "Broken UTF8!" value = property(getValue, setValue) @@ -748,6 +858,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 +1098,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 +1117,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 +1272,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 +1307,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 +1363,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):