fix boundaries of configSequence input
[enigma2.git] / lib / python / Components / config.py
index 015ac0678ffddcffa9880748e4c4b01f4cfa9872..5a92f076834f8439ab97d241f224ace4f0ae7ed8 100644 (file)
@@ -1,29 +1,55 @@
 class configFile:
        def __init__(self):
-               pass
-
-       def openFile(self):
+               self.changed = 0
+               self.configElements = { }
                try:
                        self.file = open("config")
                except IOError:
-                       self.file = ""
-
-       def getKey(self, key, dataType):
-               self.openFile()                 #good idea? (open every time we need it?) else we have to seek
+                       print "cannot open config file"
+                       return 
+               
                while 1:
                        line = self.file.readline()
                        if line == "":
                                break
-                       if line.startswith(key):
-                               x = line.find("=")
-                               if x > -1:
-                                       self.file.close()
-                                       return dataType(line[x + 1:])
-
+                       
+                       if line.startswith("#"):                #skip comments
+                               continue        
+                               
+                       self.addElement(line)
                self.file.close()
-               return ""
 
-class configBoolean:
+       def addElement(self, line):
+               x = line.find("=")
+               if x > -1:
+                       self.configElements[line[:x]] = line[x + 1:]
+       
+       def getKey(self, key):
+               return self.configElements[key]
+
+       def setKey(self, key, value):
+               self.changed = 1
+               self.configElements[key] = value
+
+       def save(self):
+               if self.changed == 0:           #no changes, so no write to disk needed
+                       return
+                       
+               fileHandle = open("config", "w")
+               
+               keys = self.configElements.keys()
+               keys.sort()
+               for x in keys:
+                       wstr = x + "=" + self.configElements[x]
+                       
+                       if wstr[len(wstr) - 1] != '\n':
+                               wstr = wstr + "\n"
+
+                       fileHandle.write(wstr)
+
+               fileHandle.close()              
+
+class configSelection:
        def __init__(self, parent):
                self.parent = parent
                
@@ -38,22 +64,128 @@ class configBoolean:
                self.parent.reload()
 
        def save(self):
-               print "save bool"
+               self.parent.save()
 
        def handleKey(self, key):
-               if key == 1:
+               if key == config.key["prevElement"]:
                        self.parent.value = self.parent.value - 1
-               if key == 2:
+               if key == config.key["nextElement"]:
                        self.parent.value = self.parent.value + 1
                
                self.checkValues()                      
 
                self.parent.change()    
 
-       def __call__(self):                     #needed by configlist
-               self.checkValues()                      
+       def __call__(self, selected):                   #needed by configlist
+               self.checkValues()
                return ("text", self.parent.vals[self.parent.value])
 
+class configSatlist:
+       def __init__(self, parent):
+               self.parent = parent
+
+       def checkValues(self):
+               if self.parent.value < 0:
+                       self.parent.value = 0   
+
+               if(self.parent.value >= (len(self.parent.vals) - 1)):
+                       self.parent.value = len(self.parent.vals) - 1
+                       
+       def cancel(self):
+               self.parent.reload()
+
+       def save(self):
+               self.parent.save()
+
+       def handleKey(self, key):
+               if key == config.key["prevElement"]:
+                       self.parent.value = self.parent.value - 1
+               if key == config.key["nextElement"]:
+                       self.parent.value = self.parent.value + 1
+               
+               self.checkValues()                      
+
+               self.parent.change()    
+
+       def __call__(self, selected):                   #needed by configlist
+               self.checkValues()
+               #fixme
+               return ("text", str(self.parent.vals[self.parent.value][0]))
+               
+class configSequence:
+       def __init__(self, parent):
+               self.parent = parent
+               self.markedPos = 0
+               
+       def checkValues(self):
+               maxPos = len(self.parent.value) * len(self.parent.vals[1]) + len(self.parent.value)
+                       
+               if self.markedPos >= maxPos:
+                       self.markedPos = maxPos - 1
+               if self.markedPos < 0:
+                       self.markedPos = 0
+                       
+       def cancel(self):
+               self.parent.reload()
+
+       def save(self):
+               self.parent.save()
+
+       def handleKey(self, key):
+               #this will no change anything on the value itself
+               #so we can handle it here in gui element
+               if key == config.key["prevElement"]:
+                       self.markedPos -= 1
+               if key == config.key["nextElement"]:
+                       self.markedPos += 1
+               
+               if key >= config.key["0"] and key <= config.key["9"]:
+                       number = 9 - config.key["9"] + key
+                       # length of numberblock
+                       numberLen = len(str(self.parent.vals[1][1]))
+                       # position in the block
+                       posinblock = self.markedPos % numberLen
+                       # blocknumber
+                       blocknumber = self.markedPos / numberLen
+                       
+                       oldvalue = self.parent.value[blocknumber]
+                       olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
+                       newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
+                       
+                       print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
+                       print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
+                       self.parent.value[blocknumber] = newvalue
+                       self.markedPos += 1
+               
+               self.checkValues()                      
+               
+               print "markPos:",
+               print self.markedPos
+
+               #FIXME: dont call when press left/right
+               self.parent.change()    
+
+       def __call__(self, selected):                   #needed by configlist
+               value = ""
+               mPos = self.markedPos
+               print "Positon: " + str(mPos)
+               for i in self.parent.value:
+                       if len(value):  #fixme no heading separator possible
+                               value += self.parent.vals[0]
+                               if mPos >= len(value) - 1:
+                                       mPos += 1
+                               
+                       #diff =         self.parent.vals[1] - len(str(i))
+                       #if diff > 0:
+                               ## if this helps?!
+                               #value += " " * diff
+                       print (("%0" + str(len(str(self.parent.vals[1][1]))) + "d") % i)
+                       value += ("%0" + str(len(str(self.parent.vals[1][1]))) + "d") % i
+
+                       # only mark cursor when we are selected
+                       # (this code is heavily ink optimized!)
+               return ("mtext"[1-selected:], value, [mPos])
+
 class configValue:
        def __init__(self, obj):
                self.obj = obj
@@ -63,24 +195,19 @@ class configValue:
 
 class Config:
        def __init__(self):
-               pass
-
-       def saveLine(self, file, element):
-               #FIXME can handle INTs only
-               line = element.configPath + "=" + str(element.value) + "\n"
-               file.write(line)
-
-       def save(self):
-               fileHandle = open("config", "w")
-               
-               for groupElement in self.__dict__.items():
-                       for element in groupElement[1].__dict__.items():
-                               self.saveLine(fileHandle, element[1])
-               
-               fileHandle.close()              
-               
-               while 1:
-                       pass    
+               self.key = { "choseElement": 0,
+                                        "prevElement": 1,
+                                        "nextElement": 2,
+                                        "0": 10,
+                                        "1": 11,
+                                        "2": 12,
+                                        "3": 13,
+                                        "4": 14,
+                                        "5": 15,
+                                        "6": 16,
+                                        "7": 17,
+                                        "8": 18,
+                                        "9": 19 }
                
 config = Config();
 configfile = configFile()
@@ -93,7 +220,7 @@ class ConfigSlider:
                self.parent.reload()
 
        def save(self):
-               print "slider - save"
+               self.parent.save()
 
        def checkValues(self):
                if self.parent.value < 0:
@@ -103,16 +230,16 @@ class ConfigSlider:
                        self.parent.value = 10  
 
        def handleKey(self, key):
-               if key == 1:
+               if key == config.key["prevElement"]:
                        self.parent.value = self.parent.value - 1
-               if key == 2:
+               if key == config.key["nextElement"]:
                        self.parent.value = self.parent.value + 1
                                        
                self.checkValues()      
                self.parent.change()    
 
-       def __call__(self):                     #needed by configlist
-               self.checkValues()      
+       def __call__(self, selected):                   #needed by configlist
+               self.checkValues()
                return ("slider", self.parent.value * 10)
 
 class ConfigSubsection:
@@ -120,26 +247,75 @@ class ConfigSubsection:
                pass
 
 class configElement:
-       def dataType(self, control):
+
+       def getIndexbyEntry(self, data):
+               cnt = 0;
+               tcnt = -1; #for defaultval
+               for x in self.vals:
+                       if int(x[1]) == int(data):
+                                       return cnt
+                       if int(x[1]) == int(self.defaultValue):
+                                       tcnt = cnt
+                       cnt += 1
+               if tcnt != -1:
+                       return tcnt                     
+               return 0        #prevent bigger then array
+
+       def datafromFile(self, control, data):
+               if control == ConfigSlider:
+                       return int(data);
+               elif control == configSelection:
+                       return int(data);
+               elif control == configSequence:
+                       list = [ ]
+                       part = data.split(self.vals[0])
+                       for x in part:
+                               list.append(int(x))
+                       return list
+               elif control == configSatlist:
+                       return self.getIndexbyEntry(data)
+               else: 
+                       return ""       
+
+       def datatoFile(self, control, data):
                if control == ConfigSlider:
-                       return int;
-               elif control == configBoolean:
-                       return int;
+                       return str(data);
+               elif control == configSelection:
+                       return str(data);
+               elif control == configSequence:
+                       value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
+#                      just in case you don't understand the above, here an equivalent:
+#                      value = ""
+#                      for i in data:
+#                              if value !="":
+#                                      value += self.vals[0]
+#                              value += str(i)
+                       return value
+               elif control == configSatlist:
+                       return str(self.vals[self.value][1]);
                else: 
                        return ""       
 
        def loadData(self):
                try:
-                       value = configfile.getKey(self.configPath, self.dataType(self.controlType))
+                       value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
                except:         
                        value = ""
 
                if value == "":
                        print "value not found - using default"
-                       self.value = self.defaultValue
+
+                       if self.controlType == configSatlist:
+                               self.value = self.getIndexbyEntry(self.defaultValue)
+                       else:   
+                               self.value = self.defaultValue
+
+                       self.save()             #add missing value to dict
                else:
                        self.value = value
-                       print "value ok"
+                       
+               #is this right? activate settings after load/cancel and use default     
+               self.change()
 
        def __init__(self, configPath, control, defaultValue, vals):
                self.configPath = configPath
@@ -147,6 +323,7 @@ class configElement:
                self.controlType = control
                self.vals = vals
                self.notifierList = [ ]
+               self.enabled = True
                self.loadData()         
        def addNotifier(self, notifier):
                self.notifierList.append(notifier);
@@ -156,3 +333,17 @@ class configElement:
                        notifier(self)
        def reload(self):
                self.loadData()
+       def save(self):
+               configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
+
+class configElement_nonSave(configElement):
+       def __init__(self, configPath, control, defaultValue, vals):
+               configElement.__init__(self, configPath, control, defaultValue, vals)
+
+       def save(self):
+               pass
+               
+def getConfigListEntry(description, element):
+       b = element
+       item = b.controlType(b)
+       return ((description, item))
\ No newline at end of file