show hdd capacity in aboutbox
[enigma2.git] / lib / python / Components / config.py
index 015ac0678ffddcffa9880748e4c4b01f4cfa9872..14c03655e8d702aac685ed0960c6a2420524d350 100644 (file)
@@ -1,27 +1,51 @@
 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 ""
+
+       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")
+               
+               for x in self.configElements:
+                       wstr = x + "=" + self.configElements[x]
+                       
+                       if wstr[len(wstr) - 1] != '\n':
+                               wstr = wstr + "\n"
+
+                       fileHandle.write(wstr)
+
+               fileHandle.close()              
 
 class configBoolean:
        def __init__(self, parent):
@@ -38,12 +62,12 @@ class configBoolean:
                self.parent.reload()
 
        def save(self):
-               print "save bool"
+               self.parent.save()
 
        def handleKey(self, key):
-               if key == 1:
+               if key == config.prevElement:
                        self.parent.value = self.parent.value - 1
-               if key == 2:
+               if key == config.nextElement:
                        self.parent.value = self.parent.value + 1
                
                self.checkValues()                      
@@ -53,6 +77,42 @@ class configBoolean:
        def __call__(self):                     #needed by configlist
                self.checkValues()                      
                return ("text", self.parent.vals[self.parent.value])
+               
+class configSequence:
+       def __init__(self, parent):
+               self.parent = parent
+               
+       def checkValues(self):
+               pass
+#              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.prevElement:
+                       self.parent.value = self.parent.value - 1
+               if key == config.nextElement:
+                       self.parent.value = self.parent.value + 1
+               
+               self.checkValues()                      
+
+               self.parent.change()    
+
+       def __call__(self):                     #needed by configlist
+               value = ""
+               for i in self.parent.value:
+                       if (value != ""):
+                               value += self.parent.vals[0]
+                       value += str(i)
+               return ("text", value)
 
 class configValue:
        def __init__(self, obj):
@@ -63,25 +123,10 @@ 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.choseElement = 0
+               self.prevElement = 1
+               self.nextElement = 2
+                                               
 config = Config();
 configfile = configFile()
 
@@ -93,7 +138,7 @@ class ConfigSlider:
                self.parent.reload()
 
        def save(self):
-               print "slider - save"
+               self.parent.save()
 
        def checkValues(self):
                if self.parent.value < 0:
@@ -103,9 +148,9 @@ class ConfigSlider:
                        self.parent.value = 10  
 
        def handleKey(self, key):
-               if key == 1:
+               if key == config.prevElement:
                        self.parent.value = self.parent.value - 1
-               if key == 2:
+               if key == config.nextElement:
                        self.parent.value = self.parent.value + 1
                                        
                self.checkValues()      
@@ -120,26 +165,34 @@ class ConfigSubsection:
                pass
 
 class configElement:
-       def dataType(self, control):
+       def datafromFile(self, control, data):
                if control == ConfigSlider:
-                       return int;
+                       return int(data);
                elif control == configBoolean:
-                       return int;
+                       return int(data);
+               else: 
+                       return ""       
+
+       def datatoFile(self, control, data):
+               if control == ConfigSlider:
+                       return str(data);
+               elif control == configBoolean:
+                       return str(data);
                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
+                       self.save()             #add missing value to dict
                else:
                        self.value = value
-                       print "value ok"
 
        def __init__(self, configPath, control, defaultValue, vals):
                self.configPath = configPath
@@ -156,3 +209,5 @@ class configElement:
                        notifier(self)
        def reload(self):
                self.loadData()
+       def save(self):
+               configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))