remove unnecessary debug and fix typo bug
[enigma2.git] / lib / python / Components / config.py
index 3b64763ee380cb1319c13641afdf1ac6653d5085..c4d46320e123d554091d6c6734772262a1f417f2 100644 (file)
@@ -1,8 +1,10 @@
 import time
 import time
+from enigma import getPrevAsciiCode
 from Tools.NumericalTextInput import NumericalTextInput
 from Tools.Directories import resolveFilename, SCOPE_CONFIG
 from Tools.NumericalTextInput import NumericalTextInput
 from Tools.Directories import resolveFilename, SCOPE_CONFIG
+from Components.Harddisk import harddiskmanager
 import copy
 import copy
-
+import os
 
 # ConfigElement, the base class of all ConfigElements.
 
 
 # ConfigElement, the base class of all ConfigElements.
 
@@ -26,6 +28,7 @@ import copy
 #
 class ConfigElement(object):
        def __init__(self):
 #
 class ConfigElement(object):
        def __init__(self):
+
                object.__init__(self)
                self.saved_value = None
                self.save_disabled = False
                object.__init__(self)
                self.saved_value = None
                self.save_disabled = False
@@ -48,10 +51,11 @@ class ConfigElement(object):
 
        # you can overide this for fancy default handling
        def load(self):
 
        # 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.default
                else:
-                       self.value = self.fromstring(self.saved_value)
+                       self.value = self.fromstring(sv)
 
        def tostring(self, value):
                return str(value)
 
        def tostring(self, value):
                return str(value)
@@ -66,23 +70,30 @@ class ConfigElement(object):
        def cancel(self):
                self.load()
 
        def cancel(self):
                self.load()
 
+       def isChanged(self):
+               sv = self.saved_value
+               if sv is None and self.value == self.default:
+                       return False
+               return self.tostring(self.value) != sv
+
        def changed(self):
                for x in self.notifiers:
                        x(self)
                        
        def changed(self):
                for x in self.notifiers:
                        x(self)
                        
-       def addNotifier(self, notifier):
+       def addNotifier(self, notifier, initial_call = True):
                assert callable(notifier), "notifiers must be callable"
                self.notifiers.append(notifier)
                assert callable(notifier), "notifiers must be callable"
                self.notifiers.append(notifier)
-               
+
                # CHECKME:
                # do we want to call the notifier
                # CHECKME:
                # do we want to call the notifier
-               #  - at all when adding it? (yes)
+               #  - at all when adding it? (yes, though optional)
                #  - when the default is active? (yes)
                #  - when no value *yet* has been set,
                #    because no config has ever been read (currently yes)
                #    (though that's not so easy to detect.
                #     the entry could just be new.)
                #  - when the default is active? (yes)
                #  - when no value *yet* has been set,
                #    because no config has ever been read (currently yes)
                #    (though that's not so easy to detect.
                #     the entry could just be new.)
-               notifier(self)
+               if initial_call:
+                       notifier(self)
 
        def disableSave(self):
                self.save_disabled = True
 
        def disableSave(self):
                self.save_disabled = True
@@ -90,14 +101,22 @@ class ConfigElement(object):
        def __call__(self, selected):
                return self.getMulti(selected)
 
        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_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
 KEY_NUMBERS = range(12, 12+10)
 KEY_0 = 12
 KEY_9 = 12+9
@@ -117,6 +136,10 @@ def getKeyNumber(key):
 class ConfigSelection(ConfigElement):
        def __init__(self, choices, default = None):
                ConfigElement.__init__(self)
 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 = {}
                
                self.choices = []
                self.description = {}
                
@@ -147,7 +170,10 @@ class ConfigSelection(ConfigElement):
                for x in self.choices:
                        assert isinstance(x, str), "ConfigSelection choices must be strings"
                
                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:
 
        def setValue(self, value):
                if value in self.choices:
@@ -158,11 +184,18 @@ class ConfigSelection(ConfigElement):
                self.changed()
 
        def tostring(self, val):
                self.changed()
 
        def tostring(self, val):
-               return (val, ','.join(self.choices))
+               return val
 
        def getValue(self):
                return self._value
 
 
        def getValue(self):
                return self._value
 
+       def setCurrentText(self, text):
+               i = self.choices.index(self.value)
+               del self.description[self.choices[i]]
+               self.choices[i] = text
+               self.description[text] = text
+               self._value = text
+
        value = property(getValue, setValue)
        
        def getIndex(self):
        value = property(getValue, setValue)
        
        def getIndex(self):
@@ -178,9 +211,27 @@ class ConfigSelection(ConfigElement):
                        self.value = self.choices[(i + nchoices - 1) % nchoices]
                elif key == KEY_RIGHT:
                        self.value = self.choices[(i + 1) % nchoices]
                        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 selectNext(self):
+               nchoices = len(self.choices)
+               i = self.choices.index(self.value)
+               self.value = self.choices[(i + 1) % nchoices]
+
+       def getText(self):
+               descr = self.description[self.value]
+               if len(descr):
+                       return _(descr)
+               return descr
 
        def getMulti(self, selected):
 
        def getMulti(self, selected):
-               return ("text", self.description[self.value])
+               descr = self.description[self.value]
+               if len(descr):
+                       return ("text", _(descr))
+               return ("text", descr)
 
        # HTML
        def getHTML(self, id):
 
        # HTML
        def getHTML(self, id):
@@ -210,11 +261,24 @@ class ConfigBoolean(ConfigElement):
        def handleKey(self, key):
                if key in [KEY_LEFT, KEY_RIGHT]:
                        self.value = not self.value
        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]
+               if len(descr):
+                       return _(descr)
+               return descr
 
        def getMulti(self, selected):
 
        def getMulti(self, selected):
-               return ("text", _(self.descriptions[self.value]))
+               descr = self.descriptions[self.value]
+               if len(descr):
+                       return ("text", _(descr))
+               return ("text", descr)
 
 
-       def tostring(self, value):
+       def tostring(self, value):
                if not value:
                        return "false"
                else:
                if not value:
                        return "false"
                else:
@@ -262,8 +326,13 @@ class ConfigDateTime(ConfigElement):
        def handleKey(self, key):
                if key == KEY_LEFT:
                        self.value = self.value - self.increment
        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
                        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))
 
        def getMulti(self, selected):
                return ("text", time.strftime(self.formatstring, time.localtime(self.value)))
 
        def getMulti(self, selected):
                return ("text", time.strftime(self.formatstring, time.localtime(self.value)))
@@ -293,6 +362,8 @@ class ConfigSequence(ConfigElement):
                
                self.default = default
                self.value = copy.copy(default)
                
                self.default = default
                self.value = copy.copy(default)
+               
+               self.endNotifier = []
 
        def validate(self):
                max_pos = 0
 
        def validate(self):
                max_pos = 0
@@ -300,15 +371,17 @@ class ConfigSequence(ConfigElement):
                for i in self._value:
                        max_pos += len(str(self.limits[num][1]))
 
                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:
 
                        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:
                        self.marked_pos = max_pos - 1
 
                if self.marked_pos < 0:
@@ -322,6 +395,9 @@ class ConfigSequence(ConfigElement):
 
                if self.marked_pos >= total_len:
                        self.marked_pos = total_len - 1
 
                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:
 
        def handleKey(self, key):
                if key == KEY_LEFT:
@@ -332,7 +408,28 @@ class ConfigSequence(ConfigElement):
                        self.marked_pos += 1
                        self.validatePos()
                
                        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])))
                        block_len = []
                        for x in self.limits:
                                block_len.append(len(str(x[1])))
@@ -350,8 +447,6 @@ class ConfigSequence(ConfigElement):
                                else:
                                        blocknumber += 1
 
                                else:
                                        blocknumber += 1
 
-                       number = getKeyNumber(key)
-                       
                        # length of numberblock
                        number_len = len(str(self.limits[blocknumber][1]))
 
                        # length of numberblock
                        number_len = len(str(self.limits[blocknumber][1]))
 
@@ -367,8 +462,8 @@ class ConfigSequence(ConfigElement):
                
                        self.validate()
                        self.changed()
                
                        self.validate()
                        self.changed()
-                       
-       def getMulti(self, selected):
+       
+       def genText(self):
                value = ""
                mPos = self.marked_pos
                num = 0;
                value = ""
                mPos = self.marked_pos
                num = 0;
@@ -381,9 +476,16 @@ class ConfigSequence(ConfigElement):
                        if self.censor_char == "":
                                value += ("%0" + str(len(str(self.limits[num][1]))) + "d") % i
                        else:
                        if self.censor_char == "":
                                value += ("%0" + str(len(str(self.limits[num][1]))) + "d") % i
                        else:
-                               value += (self.censorChar * len(str(self.limits[num][1])))
+                               value += (self.censor_char * len(str(self.limits[num][1])))
                        num += 1
                        num += 1
-
+               return (value, mPos)
+               
+       def getText(self):
+               (value, mPos) = self.genText()
+               return value
+       
+       def getMulti(self, selected):
+               (value, mPos) = self.genText()
                        # only mark cursor when we are selected
                        # (this code is heavily ink optimized!)
                if self.enabled:
                        # only mark cursor when we are selected
                        # (this code is heavily ink optimized!)
                if self.enabled:
@@ -398,11 +500,29 @@ class ConfigSequence(ConfigElement):
                return str(v)
 
        def fromstring(self, value):
                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)
 
 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])
+       
+       def genText(self):
+               value = ""
+               mPos = self.marked_pos
+               num = 0;
+               for i in self._value:
+                       if len(value):
+                               value += self.seperator
+                               if mPos >= len(value) - 1:
+                                       mPos += 1
+                       value += (" " * (len(str(self.limits[num][1]))-len(str(i))))
+                       value += str(i)
+                       num += 1
+               return (value, mPos)
 
 class ConfigMAC(ConfigSequence):
        def __init__(self, default):
 
 class ConfigMAC(ConfigSequence):
        def __init__(self, default):
@@ -414,10 +534,12 @@ class ConfigPosition(ConfigSequence):
 
 class ConfigClock(ConfigSequence):
        def __init__(self, default):
 
 class ConfigClock(ConfigSequence):
        def __init__(self, default):
-               ConfigSequence.__init__(self, seperator = ":", limits = [(0,23),(0,59)], default = default)
+               import time
+               t = time.localtime(default)
+               ConfigSequence.__init__(self, seperator = ":", limits = [(0,23),(0,59)], default = [t.tm_hour, t.tm_min])
 
 class ConfigInteger(ConfigSequence):
 
 class ConfigInteger(ConfigSequence):
-       def __init__(self, default, limits):
+       def __init__(self, default, limits = (0, 9999999999)):
                ConfigSequence.__init__(self, seperator = ":", limits = [limits], default = default)
        
        # you need to override this to do input validation
                ConfigSequence.__init__(self, seperator = ":", limits = [limits], default = default)
        
        # you need to override this to do input validation
@@ -436,9 +558,16 @@ class ConfigInteger(ConfigSequence):
        def tostring(self, value):
                return str(value)
 
        def tostring(self, value):
                return str(value)
 
-class ConfigPIN(ConfigSequence):
+class ConfigPIN(ConfigInteger):
        def __init__(self, default, len = 4, censor = ""):
        def __init__(self, default, len = 4, censor = ""):
-               ConfigSequence.__init__(self, seperator = ":", limits = [(0, (10**len)-1)], censor_char = censor, default = [default])
+               assert isinstance(default, int), "ConfigPIN default must be an integer"
+               if default == -1:
+                       default = "aaaa"
+               ConfigSequence.__init__(self, seperator = ":", limits = [(0, (10**len)-1)], censor_char = censor, default = default)
+               self.len = len
+
+       def getLength(self):
+               return self.len
 
 class ConfigFloat(ConfigSequence):
        def __init__(self, default, limits):
 
 class ConfigFloat(ConfigSequence):
        def __init__(self, default, limits):
@@ -451,43 +580,132 @@ class ConfigFloat(ConfigSequence):
 
 # an editable text...
 class ConfigText(ConfigElement, NumericalTextInput):
 
 # 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
                ConfigElement.__init__(self)
                NumericalTextInput.__init__(self, nextFunc = self.nextFunc, handleTimeout = False)
                
                self.marked_pos = 0
+               self.allmarked = (default != "")
                self.fixed_size = fixed_size
                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):
                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 < 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 nextEntry(self):
-       #       self.vals[1](self.getConfigPath())
+       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 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:
 
        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:
                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:
                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
                        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:
                elif key in KEY_NUMBERS:
-                       number = self.getKey(getKeyNumber(key))
-                       self.text = self.text[0:self.marked_pos] + str(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()
                elif key == KEY_TIMEOUT:
                        self.timeout()
+                       if self.help_window:
+                               self.help_window.update(self)
                        return
 
                        return
 
+               if self.help_window:
+                       self.help_window.update(self)
                self.validateMarker()
                self.changed()
 
                self.validateMarker()
                self.changed()
 
@@ -509,12 +727,36 @@ class ConfigText(ConfigElement, NumericalTextInput):
        value = property(getValue, setValue)
        _value = property(getValue, setValue)
 
        value = property(getValue, setValue)
        _value = property(getValue, setValue)
 
+       def getText(self):
+               return self.text.encode("utf-8")
+
        def getMulti(self, selected):
        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 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 helpWindow(self):
-               from Screens.NumericalTextInputHelpDialog import NumericalTextInputHelpDialog
-               return (NumericalTextInputHelpDialog,self)
+       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'
 
        def getHTML(self, id):
                return '<input type="text" name="' + id + '" value="' + self.value + '" /><br>\n'
@@ -522,6 +764,74 @@ class ConfigText(ConfigElement, NumericalTextInput):
        def unsafeAssign(self, value):
                self.value = str(value)
 
        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)
+
+       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)):
 # a slider.
 class ConfigSlider(ConfigElement):
        def __init__(self, default = 0, increment = 1, limits = (0, 100)):
@@ -543,11 +853,16 @@ class ConfigSlider(ConfigElement):
                        self.value -= self.increment
                elif key == KEY_RIGHT:
                        self.value += self.increment
                        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
                else:
                        return
-
                self.checkValues()
                self.checkValues()
-               self.changed()
+
+       def getText(self):
+               return "%d / %d" % (self.value, self.max)
 
        def getMulti(self, selected):
                self.checkValues()
 
        def getMulti(self, selected):
                self.checkValues()
@@ -561,7 +876,7 @@ class ConfigSatlist(ConfigSelection):
        def __init__(self, list, default = None):
                if default is not None:
                        default = str(default)
        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 == "":
 
        def getOrbitalPosition(self):
                if self.value == "":
@@ -570,8 +885,242 @@ class ConfigSatlist(ConfigSelection):
        
        orbital_position = property(getOrbitalPosition)
 
        
        orbital_position = property(getOrbitalPosition)
 
+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)
+
+class ConfigLocations(ConfigElement):
+       def __init__(self, default = [], visible_width = False):
+               ConfigElement.__init__(self)
+               self.visible_width = visible_width
+               self.pos = -1
+               self.default = default
+               self.locations = []
+               self.mountpoints = []
+               harddiskmanager.on_partition_list_change.append(self.mountpointsChanged)
+
+       def setValue(self, value):
+               loc = [x[0] for x in self.locations if x[3]]
+               add = [x for x in value if not x in loc]
+               diff = add + [x for x in loc if not x in value]
+               self.locations = [x for x in self.locations if not x[0] in diff] + [[x, self.getMountpoint(x), True, True] for x in add]
+               self.locations.sort(key = lambda x: x[0])
+               self.changed()
+
+       def getValue(self):
+               self.checkChangedMountpoints()
+               for x in self.locations:
+                       x[3] = x[2]
+               return [x[0] for x in self.locations if x[3]]
+       
+       value = property(getValue, setValue)
+
+       def tostring(self, value):
+               return str(value)
+
+       def fromstring(self, val):
+               return eval(val)
+
+       def load(self):
+               sv = self.saved_value
+               if sv is None:
+                       tmp = self.default
+               else:
+                       tmp = self.fromstring(sv)
+               self.locations = [[x, None, False, False] for x in tmp]
+               self.refreshMountpoints()
+               for x in self.locations:
+                       if os.path.exists(x[0]):
+                               x[1] = self.getMountpoint(x[0])
+                               x[2] = True
+
+       def save(self):
+               if self.save_disabled or self.locations == []:
+                       self.saved_value = None
+               else:
+                       self.saved_value = self.tostring([x[0] for x in self.locations])
+
+       def isChanged(self):
+               sv = self.saved_value
+               if val is None and self.locations == []:
+                       return False
+               return self.tostring([x[0] for x in self.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]):
+                               x[1] = self.getMountpoint(x[0])
+                               x[2] = True
+
+       def removedMount(self, mp):
+               for x in self.locations:
+                       if x[1] == mp:
+                               x[2] = False
+               
+       def refreshMountpoints(self):
+               self.mountpoints = [p.mountpoint + "/" for p in harddiskmanager.getMountedPartitions() if p.mountpoint != "/"]
+               self.mountpoints.sort(key = lambda x: -len(x))
+
+       def checkChangedMountpoints(self):
+               oldmounts = self.mountpoints
+               self.refreshMountpoints()
+               if oldmounts == self.mountpoints:
+                       return
+               for x in oldmounts:
+                       if not x in self.mountpoints:
+                               self.removedMount(x)
+               for x in self.mountpoints:
+                       if not x in oldmounts:
+                               self.addedMount(x)
+
+       def getMountpoint(self, file):
+               file = os.path.realpath(file)+"/"
+               for m in self.mountpoints:
+                       if file.startswith(m):
+                               return m
+               return None
+
+       def handleKey(self, key):
+               if key == KEY_LEFT:
+                       self.pos -= 1
+                       if self.pos < -1:
+                           self.pos = len(self.value)-1
+               elif key == KEY_RIGHT:
+                       self.pos += 1
+                       if self.pos >= len(self.value):
+                           self.pos = -1
+               elif key in [KEY_HOME, KEY_END]:
+                       self.pos = -1
+
+       def getText(self):
+               return " ".join(self.value)
+
+       def getMulti(self, selected):
+               if not selected:
+                       valstr = " ".join(self.value)
+                       if self.visible_width and len(valstr) > self.visible_width:
+                               return ("text", valstr[0:self.visible_width])
+                       else:
+                               return ("text", valstr)
+               else:
+                       i = 0
+                       valstr = ""
+                       ind1 = 0
+                       ind2 = 0
+                       for val in self.value:
+                               if i == self.pos:
+                                       ind1 = len(valstr)
+                               valstr += str(val)+" "
+                               if i == self.pos:
+                                       ind2 = len(valstr)
+                               i += 1
+                       if self.visible_width and len(valstr) > self.visible_width:
+                               if ind1+1 < self.visible_width/2:
+                                       off = 0
+                               else:
+                                       off = min(ind1+1-self.visible_width/2, len(valstr)-self.visible_width)
+                               return ("mtext", valstr[off:off+self.visible_width], range(ind1-off,ind2-off))
+                       else:
+                               return ("mtext", valstr, range(ind1,ind2))
+
+       def onDeselect(self, session):
+               self.pos = -1
+               
 # nothing.
 # nothing.
-class ConfigDummy(ConfigSelection):
+class ConfigNothing(ConfigSelection):
        def __init__(self):
                ConfigSelection.__init__(self, choices = [""])
 
        def __init__(self):
                ConfigSelection.__init__(self, choices = [""])
 
@@ -629,14 +1178,20 @@ class ConfigSubList(list, object):
                                self[int(key)].saved_value = val
 
        saved_value = property(getSavedValue, setSavedValue)
                                self[int(key)].saved_value = val
 
        saved_value = property(getSavedValue, setSavedValue)
-       
+
        def append(self, item):
        def append(self, item):
-               list.append(self, item)
                i = str(len(self))
                i = str(len(self))
+               list.append(self, item)
                if i in self.stored_values:
                        item.saved_value = self.stored_values[i]
                        item.load()
 
                if i in self.stored_values:
                        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
 # 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
@@ -658,8 +1213,9 @@ class ConfigSubDict(dict, object):
        def getSavedValue(self):
                res = {}
                for (key, val) in self.items():
        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):
                return res
 
        def setSavedValue(self, values):
@@ -676,6 +1232,9 @@ class ConfigSubDict(dict, object):
                        item.saved_value = self.stored_values[str(key)]
                        item.load()
 
                        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.
 #
 # Like the classes above, just with a more "native"
 # syntax.
 #
@@ -697,6 +1256,7 @@ class ConfigSubsection(object):
        def __setattr__(self, name, value):
                if name == "saved_value":
                        return self.setSavedValue(value)
        def __setattr__(self, name, value):
                if name == "saved_value":
                        return self.setSavedValue(value)
+               assert isinstance(value, ConfigSubsection) or isinstance(value, ConfigElement) or isinstance(value, ConfigSubList) or isinstance(value, ConfigSubDict), "ConfigSubsections can only store ConfigSubsections, ConfigSubLists, ConfigSubDicts or ConfigElements"
                self.content.items[name] = value
                if name in self.content.stored_values:
                        #print "ok, now we have a new item,", name, "and have the following value for it:", self.content.stored_values[name]
                self.content.items[name] = value
                if name in self.content.stored_values:
                        #print "ok, now we have a new item,", name, "and have the following value for it:", self.content.stored_values[name]
@@ -709,18 +1269,21 @@ class ConfigSubsection(object):
        def getSavedValue(self):
                res = self.content.stored_values
                for (key, val) in self.content.items.items():
        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):
                values = dict(values)
                
                self.content.stored_values = values
                return res
 
        def setSavedValue(self, values):
                values = dict(values)
                
                self.content.stored_values = values
-               
+
                for (key, val) in self.content.items.items():
                        if key in 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)
 
 
        saved_value = property(getSavedValue, setSavedValue)
 
@@ -732,6 +1295,9 @@ class ConfigSubsection(object):
                for x in self.content.items.values():
                        x.load()
 
                for x in self.content.items.values():
                        x.load()
 
+       def dict(self):
+               return self.content.items
+
 # the root config object, which also can "pickle" (=serialize)
 # down the whole config tree.
 #
 # the root config object, which also can "pickle" (=serialize)
 # down the whole config tree.
 #
@@ -781,7 +1347,8 @@ class Config(ConfigSubsection):
 
                # we inherit from ConfigSubsection, so ...
                #object.__setattr__(self, "saved_value", tree["config"])
 
                # we inherit from ConfigSubsection, so ...
                #object.__setattr__(self, "saved_value", tree["config"])
-               self.setSavedValue(tree["config"])
+               if "config" in tree:
+                       self.setSavedValue(tree["config"])
 
        def saveToFile(self, filename):
                f = open(filename, "w")
 
        def saveToFile(self, filename):
                f = open(filename, "w")
@@ -797,7 +1364,7 @@ config = Config()
 config.misc = ConfigSubsection()
 
 class ConfigFile:
 config.misc = ConfigSubsection()
 
 class ConfigFile:
-       CONFIG_FILE = resolveFilename(SCOPE_CONFIG, "config2")
+       CONFIG_FILE = resolveFilename(SCOPE_CONFIG, "settings")
 
        def load(self):
                try:
 
        def load(self):
                try:
@@ -806,11 +1373,26 @@ class ConfigFile:
                        print "unable to load config (%s), assuming defaults..." % str(e)
        
        def save(self):
                        print "unable to load config (%s), assuming defaults..." % str(e)
        
        def save(self):
-               config.save()
+#              config.save()
                config.saveToFile(self.CONFIG_FILE)
        
                config.saveToFile(self.CONFIG_FILE)
        
+       def __resolveValue(self, pickles, cmap):
+               if cmap.has_key(pickles[0]):
+                       if len(pickles) > 1:
+                               return self.__resolveValue(pickles[1:], cmap[pickles[0]].dict())
+                       else:
+                               return str(cmap[pickles[0]].value)
+               return None
+       
        def getResolvedKey(self, key):
        def getResolvedKey(self, key):
-               return None # FIXME
+               names = key.split('.')
+               if len(names) > 1:
+                       if names[0] == "config":
+                               ret=self.__resolveValue(names[1:], config.content.items)
+                               if ret and len(ret):
+                                       return ret
+               print "getResolvedKey", key, "failed !! (Typo??)"
+               return ""
 
 def NoSave(element):
        element.disableSave()
 
 def NoSave(element):
        element.disableSave()
@@ -820,8 +1402,13 @@ configfile = ConfigFile()
 
 configfile.load()
 
 
 configfile.load()
 
-def getConfigListEntry(desc, config):
-       return (desc, config)
+def getConfigListEntry(*args):
+       assert len(args) > 1, "getConfigListEntry needs a minimum of two arguments (descr, configElement)"
+       return args
+
+def updateConfigElement(element, newelement):
+       newelement.value = element.value
+       return newelement
 
 #def _(x):
 #      return x
 
 #def _(x):
 #      return x