Merge branch 'bug_236_recordpath' into experimental
[enigma2.git] / lib / python / Components / config.py
index c999f2aaeb9c13718de7aa35dbb14d19fd0abf32..876e3a34a7b56683960a98981088ef409eb1bf35 100755 (executable)
@@ -1,6 +1,6 @@
 from enigma import getPrevAsciiCode
 from Tools.NumericalTextInput import NumericalTextInput
-from Tools.Directories import resolveFilename, SCOPE_CONFIG
+from Tools.Directories import resolveFilename, SCOPE_CONFIG, fileExists
 from Components.Harddisk import harddiskmanager
 from copy import copy as copy_copy
 from os import path as os_path
@@ -279,14 +279,15 @@ class ConfigSelection(ConfigElement):
                if default is None:
                        default = self.choices.default()
 
+               self._descr = None
                self.default = self._value = self.last_value = default
-               self.changed()
 
        def setChoices(self, choices, default = None):
                self.choices = choicesList(choices)
 
                if default is None:
                        default = self.choices.default()
+               self.default = default
 
                if self.value not in self.choices:
                        self.value = default
@@ -296,6 +297,7 @@ class ConfigSelection(ConfigElement):
                        self._value = value
                else:
                        self._value = self.default
+               self._descr = None
                self.changed()
 
        def tostring(self, val):
@@ -307,7 +309,7 @@ class ConfigSelection(ConfigElement):
        def setCurrentText(self, text):
                i = self.choices.index(self.value)
                self.choices[i] = text
-               self.description[text] = text
+               self._descr = self.description[text] = text
                self._value = text
 
        value = property(getValue, setValue)
@@ -336,13 +338,18 @@ class ConfigSelection(ConfigElement):
                self.value = self.choices[(i + 1) % nchoices]
 
        def getText(self):
-               descr = self.description[self.value]
+               if self._descr is not None:
+                       return self._descr
+               descr = self._descr = self.description[self.value]
                if descr:
                        return _(descr)
                return descr
 
        def getMulti(self, selected):
-               descr = self.description[self.value]
+               if self._descr is not None:
+                       descr = self._descr
+               else:
+                       descr = self._descr = self.description[self.value]
                if descr:
                        return ("text", _(descr))
                return ("text", descr)
@@ -378,7 +385,7 @@ class ConfigBoolean(ConfigElement):
                self.value = self.last_value = self.default = default
 
        def handleKey(self, key):
-               if key in [KEY_LEFT, KEY_RIGHT]:
+               if key in (KEY_LEFT, KEY_RIGHT):
                        self.value = not self.value
                elif key == KEY_HOME:
                        self.value = False
@@ -906,13 +913,14 @@ class ConfigText(ConfigElement, NumericalTextInput):
                        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
+                       self.timeout()
+                       newChar = unichr(getPrevAsciiCode())
+                       if not self.useableChars or newChar in self.useableChars:
+                               if self.allmarked:
+                                       self.deleteAllChars()
+                                       self.allmarked = False
+                               self.insertChar(newChar, self.marked_pos, False)
+                               self.marked_pos += 1
                elif key in KEY_NUMBERS:
                        owr = self.lastKey == getKeyNumber(key)
                        newChar = self.getKey(getKeyNumber(key))
@@ -1009,6 +1017,42 @@ class ConfigPassword(ConfigText):
                ConfigText.onDeselect(self, session)
                self.hidden = True
 
+# lets the user select between [min, min+stepwidth, min+(stepwidth*2)..., maxval] with maxval <= max depending
+# on the stepwidth
+# min, max, stepwidth, default are int values
+# wraparound: pressing RIGHT key at max value brings you to min value and vice versa if set to True
+class ConfigSelectionNumber(ConfigSelection):
+       def __init__(self, min, max, stepwidth, default = None, wraparound = False):
+               self.wraparound = wraparound
+               if default is None:
+                       default = min
+               default = str(default)
+               choices = []
+               step = min
+               while step <= max:
+                       choices.append(str(step))
+                       step += stepwidth
+               
+               ConfigSelection.__init__(self, choices, default)
+               
+       def getValue(self):
+               return int(self.text)
+
+       def setValue(self, val):
+               self.text = str(val)
+               
+       def handleKey(self, key):
+               if not self.wraparound:
+                       if key == KEY_RIGHT:
+                               if len(self.choices) == (self.choices.index(self.value) + 1):
+                                       return
+                       if key == KEY_LEFT:
+                               if self.choices.index(self.value) == 0:
+                                       return
+               ConfigSelection.handleKey(self, key)
+                               
+                               
+
 class ConfigNumber(ConfigText):
        def __init__(self, default = 0):
                ConfigText.__init__(self, str(default), fixed_size = False)
@@ -1022,6 +1066,13 @@ class ConfigNumber(ConfigText):
        value = property(getValue, setValue)
        _value = property(getValue, setValue)
 
+       def isChanged(self):
+               sv = self.saved_value
+               strv = self.tostring(self.value)
+               if sv is None and strv == self.default:
+                       return False
+               return strv != sv
+
        def conform(self):
                pos = len(self.text) - self.marked_pos
                self.text = self.text.lstrip("0")
@@ -1180,7 +1231,7 @@ class ConfigSet(ConfigElement):
                                self.pos = -1
                        else:
                                self.pos += 1
-               elif key in [KEY_HOME, KEY_END]:
+               elif key in (KEY_HOME, KEY_END):
                        self.pos = -1
 
        def genString(self, lst):
@@ -1234,7 +1285,6 @@ class ConfigLocations(ConfigElement):
                self.default = default
                self.locations = []
                self.mountpoints = []
-               harddiskmanager.on_partition_list_change.append(self.mountpointsChanged)
                self.value = default[:]
 
        def setValue(self, value):
@@ -1271,7 +1321,7 @@ class ConfigLocations(ConfigElement):
                locations = [[x, None, False, False] for x in tmp]
                self.refreshMountpoints()
                for x in locations:
-                       if os_path.exists(x[0]):
+                       if fileExists(x[0]):
                                x[1] = self.getMountpoint(x[0])
                                x[2] = True
                self.locations = locations
@@ -1290,20 +1340,11 @@ class ConfigLocations(ConfigElement):
                        return False
                return self.tostring([x[0] for x in locations]) != sv
 
-       def mountpointsChanged(self, action, dev):
-               print "Mounts changed: ", action, dev
-               mp = dev.mountpoint+"/"
-               if action == "add":
-                       self.addedMount(mp)
-               elif action == "remove":
-                       self.removedMount(mp)
-               self.refreshMountpoints()
-
        def addedMount(self, mp):
                for x in self.locations:
                        if x[1] == mp:
                                x[2] = True
-                       elif x[1] == None and os_path.exists(x[0]):
+                       elif x[1] == None and fileExists(x[0]):
                                x[1] = self.getMountpoint(x[0])
                                x[2] = True
 
@@ -1313,7 +1354,7 @@ class ConfigLocations(ConfigElement):
                                x[2] = False
 
        def refreshMountpoints(self):
-               self.mountpoints = [p.mountpoint + "/" for p in harddiskmanager.getMountedPartitions() if p.mountpoint != "/"]
+               self.mountpoints = [p.mountpoint for p in harddiskmanager.getMountedPartitions() if p.mountpoint != "/"]
                self.mountpoints.sort(key = lambda x: -len(x))
 
        def checkChangedMountpoints(self):
@@ -1345,7 +1386,7 @@ class ConfigLocations(ConfigElement):
                        self.pos += 1
                        if self.pos >= len(self.value):
                                self.pos = -1
-               elif key in [KEY_HOME, KEY_END]:
+               elif key in (KEY_HOME, KEY_END):
                        self.pos = -1
 
        def getText(self):
@@ -1385,7 +1426,7 @@ class ConfigLocations(ConfigElement):
 # nothing.
 class ConfigNothing(ConfigSelection):
        def __init__(self):
-               ConfigSelection.__init__(self, choices = [""])
+               ConfigSelection.__init__(self, choices = [("","")])
 
 # until here, 'saved_value' always had to be a *string*.
 # now, in ConfigSubsection, and only there, saved_value
@@ -1426,8 +1467,8 @@ class ConfigSubList(list, object):
 
        def getSavedValue(self):
                res = { }
-               for i in range(len(self)):
-                       sv = self[i].saved_value
+               for i, val in enumerate(self):
+                       sv = val.saved_value
                        if sv is not None:
                                res[str(i)] = sv
                return res
@@ -1448,7 +1489,7 @@ class ConfigSubList(list, object):
                        item.load()
 
        def dict(self):
-               return dict([(str(index), value) for index, value in self.enumerate()])
+               return dict([(str(index), value) for index, value in enumerate(self)])
 
 # same as ConfigSubList, just as a dictionary.
 # care must be taken that the 'key' has a proper
@@ -1479,7 +1520,7 @@ class ConfigSubDict(dict, object):
                self.stored_values = dict(values)
                for (key, val) in self.items():
                        if str(key) in self.stored_values:
-                               val = self.stored_values[str(key)]
+                               val.saved_value = self.stored_values[str(key)]
 
        saved_value = property(getSavedValue, setSavedValue)
 
@@ -1607,8 +1648,9 @@ class Config(ConfigSubsection):
                        self.setSavedValue(tree["config"])
 
        def saveToFile(self, filename):
+               text = self.pickle()
                f = open(filename, "w")
-               f.write(self.pickle())
+               f.write(text)
                f.close()
 
        def loadFromFile(self, filename):