add configsatlist
[enigma2.git] / lib / python / Components / config.py
1 class configFile:
2         def __init__(self):
3                 self.changed = 0
4                 self.configElements = { }
5                 try:
6                         self.file = open("config")
7                 except IOError:
8                         print "cannot open config file"
9                         return 
10                 
11                 while 1:
12                         line = self.file.readline()
13                         if line == "":
14                                 break
15                         
16                         if line.startswith("#"):                #skip comments
17                                 continue        
18                                 
19                         self.addElement(line)
20                 self.file.close()
21
22         def addElement(self, line):
23                 x = line.find("=")
24                 if x > -1:
25                         self.configElements[line[:x]] = line[x + 1:]
26         
27         def getKey(self, key):
28                 return self.configElements[key]
29
30         def setKey(self, key, value):
31                 self.changed = 1
32                 self.configElements[key] = value
33
34         def save(self):
35                 if self.changed == 0:           #no changes, so no write to disk needed
36                         return
37                         
38                 fileHandle = open("config", "w")
39                 
40                 for x in self.configElements:
41                         wstr = x + "=" + self.configElements[x]
42                         
43                         if wstr[len(wstr) - 1] != '\n':
44                                 wstr = wstr + "\n"
45
46                         fileHandle.write(wstr)
47
48                 fileHandle.close()              
49
50 class configSelection:
51         def __init__(self, parent):
52                 self.parent = parent
53                 
54         def checkValues(self):
55                 if self.parent.value < 0:
56                         self.parent.value = 0   
57
58                 if(self.parent.value >= (len(self.parent.vals) - 1)):
59                         self.parent.value = len(self.parent.vals) - 1
60
61         def cancel(self):
62                 self.parent.reload()
63
64         def save(self):
65                 self.parent.save()
66
67         def handleKey(self, key):
68                 if key == config.key["prevElement"]:
69                         self.parent.value = self.parent.value - 1
70                 if key == config.key["nextElement"]:
71                         self.parent.value = self.parent.value + 1
72                 
73                 self.checkValues()                      
74
75                 self.parent.change()    
76
77         def __call__(self, selected):                   #needed by configlist
78                 self.checkValues()
79                 return ("text", self.parent.vals[self.parent.value])
80
81 class configSatlist:
82         def __init__(self, parent):
83                 self.parent = parent
84
85         def checkValues(self):
86                 if self.parent.value < 0:
87                         self.parent.value = 0   
88
89                 if(self.parent.value >= (len(self.parent.vals) - 1)):
90                         self.parent.value = len(self.parent.vals) - 1
91                         
92                 print "value" + str(self.parent.value)
93                 print "name " + self.parent.vals[self.parent.value][0]
94
95         def cancel(self):
96                 self.parent.reload()
97
98         def save(self):
99                 self.parent.save()
100
101         def handleKey(self, key):
102                 if key == config.key["prevElement"]:
103                         self.parent.value = self.parent.value - 1
104                 if key == config.key["nextElement"]:
105                         self.parent.value = self.parent.value + 1
106                 
107                 self.checkValues()                      
108
109                 self.parent.change()    
110
111         def __call__(self, selected):                   #needed by configlist
112                 self.checkValues()
113                 #fixme
114                 return ("text", str(self.parent.vals[self.parent.value][0]))
115                 
116 class configSequence:
117         def __init__(self, parent):
118                 self.parent = parent
119                 self.markedPos = 0
120                 
121         def checkValues(self):
122                 maxPos = len(self.parent.value) * self.parent.vals[1] 
123                 print maxPos
124                         
125                 if self.markedPos >= maxPos:
126                         self.markedPos = maxPos - 1
127                 if self.markedPos < 0:
128                         self.markedPos = 0
129                         
130         def cancel(self):
131                 self.parent.reload()
132
133         def save(self):
134                 self.parent.save()
135
136         def handleKey(self, key):
137                 #this will no change anything on the value itself
138                 #so we can handle it here in gui element
139                 if key == config.key["prevElement"]:
140                         self.markedPos -= 1
141                 if key == config.key["nextElement"]:
142                         self.markedPos += 1
143                 
144                 if key >= config.key["0"] and key <= config.key["9"]:
145                         number = 9 - config.key["9"] + key
146                         # length of numberblock
147                         numberLen = len(str(self.parent.vals[1][1]))
148                         # position in the block
149                         posinblock = self.markedPos % numberLen
150                         # blocknumber
151                         blocknumber = self.markedPos / numberLen
152                         
153                         oldvalue = self.parent.value[blocknumber]
154                         olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
155                         newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
156                         
157                         print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
158                         print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
159                         self.parent.value[blocknumber] = newvalue
160                         self.markedPos += 1
161                 
162                 self.checkValues()                      
163                 
164                 print "markPos:",
165                 print self.markedPos
166
167                 #FIXME: dont call when press left/right
168                 self.parent.change()    
169
170         def __call__(self, selected):                   #needed by configlist
171                 value = ""
172                 mPos = self.markedPos
173                 print "Positon: " + str(mPos)
174                 for i in self.parent.value:
175                         if len(value):  #fixme no heading separator possible
176                                 value += self.parent.vals[0]
177                                 if mPos >= len(value) - 1:
178                                         mPos += 1
179                                 
180                         #diff =         self.parent.vals[1] - len(str(i))
181                         #if diff > 0:
182                                 ## if this helps?!
183                                 #value += " " * diff
184                         print (("%0" + str(len(str(self.parent.vals[1][1]))) + "d") % i)
185                         value += ("%0" + str(len(str(self.parent.vals[1][1]))) + "d") % i
186
187                         # only mark cursor when we are selected
188                         # (this code is heavily ink optimized!)
189                 return ("mtext"[1-selected:], value, [mPos])
190
191 class configValue:
192         def __init__(self, obj):
193                 self.obj = obj
194                 
195         def __str__(self):
196                 return self.obj
197
198 class Config:
199         def __init__(self):
200                 self.key = { "choseElement": 0,
201                                          "prevElement": 1,
202                                          "nextElement": 2,
203                                          "0": 10,
204                                          "1": 11,
205                                          "2": 12,
206                                          "3": 13,
207                                          "4": 14,
208                                          "5": 15,
209                                          "6": 16,
210                                          "7": 17,
211                                          "8": 18,
212                                          "9": 19 }
213                 
214 config = Config();
215 configfile = configFile()
216
217 class ConfigSlider:
218         def __init__(self, parent):
219                 self.parent = parent
220
221         def cancel(self):
222                 self.parent.reload()
223
224         def save(self):
225                 self.parent.save()
226
227         def checkValues(self):
228                 if self.parent.value < 0:
229                         self.parent.value = 0   
230
231                 if self.parent.value > 10:
232                         self.parent.value = 10  
233
234         def handleKey(self, key):
235                 if key == config.key["prevElement"]:
236                         self.parent.value = self.parent.value - 1
237                 if key == config.key["nextElement"]:
238                         self.parent.value = self.parent.value + 1
239                                         
240                 self.checkValues()      
241                 self.parent.change()    
242
243         def __call__(self, selected):                   #needed by configlist
244                 self.checkValues()
245                 return ("slider", self.parent.value * 10)
246
247 class ConfigSubsection:
248         def __init__(self):
249                 pass
250
251 class configElement:
252         def datafromFile(self, control, data):
253                 if control == ConfigSlider:
254                         return int(data);
255                 elif control == configSelection:
256                         return int(data);
257                 elif control == configSequence:
258                         list = [ ]
259                         part = data.split(self.vals[0])
260                         for x in part:
261                                 list.append(int(x))
262                         return list
263                 else: 
264                         return ""       
265
266         def datatoFile(self, control, data):
267                 if control == ConfigSlider:
268                         return str(data);
269                 elif control == configSelection:
270                         return str(data);
271                 elif control == configSequence:
272                         value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
273 #                       just in case you don't understand the above, here an equivalent:
274 #                       value = ""
275 #                       for i in data:
276 #                               if value !="":
277 #                                       value += self.vals[0]
278 #                               value += str(i)
279                         return value
280                 else: 
281                         return ""       
282
283         def loadData(self):
284                 try:
285                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
286                 except:         
287                         value = ""
288
289                 if value == "":
290                         print "value not found - using default"
291                         self.value = self.defaultValue
292                         self.save()             #add missing value to dict
293                 else:
294                         self.value = value
295
296         def __init__(self, configPath, control, defaultValue, vals):
297                 self.configPath = configPath
298                 self.defaultValue = defaultValue
299                 self.controlType = control
300                 self.vals = vals
301                 self.notifierList = [ ]
302                 self.enabled = True
303                 self.loadData()         
304         def addNotifier(self, notifier):
305                 self.notifierList.append(notifier);
306                 notifier(self);
307         def change(self):
308                 for notifier in self.notifierList:
309                         notifier(self)
310         def reload(self):
311                 self.loadData()
312         def save(self):
313                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))