show hdd capacity in aboutbox
[enigma2.git] / lib / python / Components / config.py
index 15119617bdbd826d6d1a271867148fd90a496ad3..14c03655e8d702aac685ed0960c6a2420524d350 100644 (file)
-#  temp stuff :)
+class configFile:
+       def __init__(self):
+               self.changed = 0
+               self.configElements = { }
+               try:
+                       self.file = open("config")
+               except IOError:
+                       print "cannot open config file"
+                       return 
+               
+               while 1:
+                       line = self.file.readline()
+                       if line == "":
+                               break
+                       
+                       if line.startswith("#"):                #skip comments
+                               continue        
+                               
+                       self.addElement(line)
+               self.file.close()
+
+       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):
                self.parent = parent
-               self.val = parent.value
-               self.vals = parent.vals
-                       
+               
+       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 == 1:
-                       self.val = self.val - 1
-               if key == 2:
-                       self.val = self.val + 1
-                       
-               if self.val < 0:
-                       self.val = 0    
+               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()    
 
-#              if self.val > 1:
-#                      self.val = 1    
-       
        def __call__(self):                     #needed by configlist
-       
-               print len(self.vals)
-               print self.val
-                       
-               if(self.val > (len(self.vals) - 1)):
-                       self.val = len(self.vals) - 1
-       
-               return ("text",self.vals[self.val])
+               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):
@@ -34,51 +121,93 @@ class configValue:
        def __str__(self):
                return self.obj
 
-def configEntry(obj):
-       # das hier ist ein zugriff auf die registry...
-       if obj == "HKEY_LOCAL_ENIGMA/IMPORTANT/USER_ANNOYING_STUFF/SDTV/FLASHES/GREEN":
-               return ("SDTV green flashes", configBoolean(obj))
-       elif obj == "HKEY_LOCAL_ENIGMA/IMPORTANT/USER_ANNOYING_STUFF/HDTV/FLASHES/GREEN":
-               return ("HDTV reen flashes", configBoolean(obj))
-       else:
-               return ("invalid", "")
-
 class Config:
        def __init__(self):
-               pass
-               
+               self.choseElement = 0
+               self.prevElement = 1
+               self.nextElement = 2
+                                               
 config = Config();
+configfile = configFile()
 
 class ConfigSlider:
        def __init__(self, parent):
                self.parent = parent
-               self.val = parent.value
+
+       def cancel(self):
+               self.parent.reload()
+
+       def save(self):
+               self.parent.save()
+
+       def checkValues(self):
+               if self.parent.value < 0:
+                       self.parent.value = 0   
+
+               if self.parent.value > 10:
+                       self.parent.value = 10  
+
        def handleKey(self, key):
-               if key == 1:
-                       self.val = self.val - 1
-               if key == 2:
-                       self.val = self.val + 1
-                       
-               if self.val < 0:
-                       self.val = 0    
+               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()    
 
-               if self.val > 10:
-                       self.val = 10   
        def __call__(self):                     #needed by configlist
-               return ("slider", self.val * 10)
+               self.checkValues()      
+               return ("slider", self.parent.value * 10)
 
 class ConfigSubsection:
        def __init__(self):
                pass
 
 class configElement:
+       def datafromFile(self, control, data):
+               if control == ConfigSlider:
+                       return int(data);
+               elif control == configBoolean:
+                       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 = 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
+
        def __init__(self, configPath, control, defaultValue, vals):
                self.configPath = configPath
-#              self.value = 0  #read from registry else use default
-               self.value = defaultValue       #read from registry else use default
+               self.defaultValue = defaultValue
                self.controlType = control
                self.vals = vals
                self.notifierList = [ ]
+               self.loadData()         
        def addNotifier(self, notifier):
                self.notifierList.append(notifier);
                notifier(self);
+       def change(self):
+               for notifier in self.notifierList:
+                       notifier(self)
+       def reload(self):
+               self.loadData()
+       def save(self):
+               configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))