015ac0678ffddcffa9880748e4c4b01f4cfa9872
[enigma2.git] / lib / python / Components / config.py
1 class configFile:
2         def __init__(self):
3                 pass
4
5         def openFile(self):
6                 try:
7                         self.file = open("config")
8                 except IOError:
9                         self.file = ""
10
11         def getKey(self, key, dataType):
12                 self.openFile()                 #good idea? (open every time we need it?) else we have to seek
13                 while 1:
14                         line = self.file.readline()
15                         if line == "":
16                                 break
17                         if line.startswith(key):
18                                 x = line.find("=")
19                                 if x > -1:
20                                         self.file.close()
21                                         return dataType(line[x + 1:])
22
23                 self.file.close()
24                 return ""
25
26 class configBoolean:
27         def __init__(self, parent):
28                 self.parent = parent
29                 
30         def checkValues(self):
31                 if self.parent.value < 0:
32                         self.parent.value = 0   
33
34                 if(self.parent.value >= (len(self.parent.vals) - 1)):
35                         self.parent.value = len(self.parent.vals) - 1
36
37         def cancel(self):
38                 self.parent.reload()
39
40         def save(self):
41                 print "save bool"
42
43         def handleKey(self, key):
44                 if key == 1:
45                         self.parent.value = self.parent.value - 1
46                 if key == 2:
47                         self.parent.value = self.parent.value + 1
48                 
49                 self.checkValues()                      
50
51                 self.parent.change()    
52
53         def __call__(self):                     #needed by configlist
54                 self.checkValues()                      
55                 return ("text", self.parent.vals[self.parent.value])
56
57 class configValue:
58         def __init__(self, obj):
59                 self.obj = obj
60                 
61         def __str__(self):
62                 return self.obj
63
64 class Config:
65         def __init__(self):
66                 pass
67
68         def saveLine(self, file, element):
69                 #FIXME can handle INTs only
70                 line = element.configPath + "=" + str(element.value) + "\n"
71                 file.write(line)
72
73         def save(self):
74                 fileHandle = open("config", "w")
75                 
76                 for groupElement in self.__dict__.items():
77                         for element in groupElement[1].__dict__.items():
78                                 self.saveLine(fileHandle, element[1])
79                 
80                 fileHandle.close()              
81                 
82                 while 1:
83                         pass    
84                 
85 config = Config();
86 configfile = configFile()
87
88 class ConfigSlider:
89         def __init__(self, parent):
90                 self.parent = parent
91
92         def cancel(self):
93                 self.parent.reload()
94
95         def save(self):
96                 print "slider - save"
97
98         def checkValues(self):
99                 if self.parent.value < 0:
100                         self.parent.value = 0   
101
102                 if self.parent.value > 10:
103                         self.parent.value = 10  
104
105         def handleKey(self, key):
106                 if key == 1:
107                         self.parent.value = self.parent.value - 1
108                 if key == 2:
109                         self.parent.value = self.parent.value + 1
110                                         
111                 self.checkValues()      
112                 self.parent.change()    
113
114         def __call__(self):                     #needed by configlist
115                 self.checkValues()      
116                 return ("slider", self.parent.value * 10)
117
118 class ConfigSubsection:
119         def __init__(self):
120                 pass
121
122 class configElement:
123         def dataType(self, control):
124                 if control == ConfigSlider:
125                         return int;
126                 elif control == configBoolean:
127                         return int;
128                 else: 
129                         return ""       
130
131         def loadData(self):
132                 try:
133                         value = configfile.getKey(self.configPath, self.dataType(self.controlType))
134                 except:         
135                         value = ""
136
137                 if value == "":
138                         print "value not found - using default"
139                         self.value = self.defaultValue
140                 else:
141                         self.value = value
142                         print "value ok"
143
144         def __init__(self, configPath, control, defaultValue, vals):
145                 self.configPath = configPath
146                 self.defaultValue = defaultValue
147                 self.controlType = control
148                 self.vals = vals
149                 self.notifierList = [ ]
150                 self.loadData()         
151         def addNotifier(self, notifier):
152                 self.notifierList.append(notifier);
153                 notifier(self);
154         def change(self):
155                 for notifier in self.notifierList:
156                         notifier(self)
157         def reload(self):
158                 self.loadData()