service: add sort of servicelist including all required layers
[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         def cancel(self):
93                 self.parent.reload()
94
95         def save(self):
96                 self.parent.save()
97
98         def handleKey(self, key):
99                 if key == config.key["prevElement"]:
100                         self.parent.value = self.parent.value - 1
101                 if key == config.key["nextElement"]:
102                         self.parent.value = self.parent.value + 1
103                 
104                 self.checkValues()                      
105
106                 self.parent.change()    
107
108         def __call__(self, selected):                   #needed by configlist
109                 self.checkValues()
110                 #fixme
111                 return ("text", str(self.parent.vals[self.parent.value][0]))
112                 
113 class configSequence:
114         def __init__(self, parent):
115                 self.parent = parent
116                 self.markedPos = 0
117                 
118         def checkValues(self):
119                 maxPos = len(self.parent.value) * self.parent.vals[1] 
120                 print maxPos
121                         
122                 if self.markedPos >= maxPos:
123                         self.markedPos = maxPos - 1
124                 if self.markedPos < 0:
125                         self.markedPos = 0
126                         
127         def cancel(self):
128                 self.parent.reload()
129
130         def save(self):
131                 self.parent.save()
132
133         def handleKey(self, key):
134                 #this will no change anything on the value itself
135                 #so we can handle it here in gui element
136                 if key == config.key["prevElement"]:
137                         self.markedPos -= 1
138                 if key == config.key["nextElement"]:
139                         self.markedPos += 1
140                 
141                 if key >= config.key["0"] and key <= config.key["9"]:
142                         number = 9 - config.key["9"] + key
143                         # length of numberblock
144                         numberLen = len(str(self.parent.vals[1][1]))
145                         # position in the block
146                         posinblock = self.markedPos % numberLen
147                         # blocknumber
148                         blocknumber = self.markedPos / numberLen
149                         
150                         oldvalue = self.parent.value[blocknumber]
151                         olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
152                         newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
153                         
154                         print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
155                         print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
156                         self.parent.value[blocknumber] = newvalue
157                         self.markedPos += 1
158                 
159                 self.checkValues()                      
160                 
161                 print "markPos:",
162                 print self.markedPos
163
164                 #FIXME: dont call when press left/right
165                 self.parent.change()    
166
167         def __call__(self, selected):                   #needed by configlist
168                 value = ""
169                 mPos = self.markedPos
170                 print "Positon: " + str(mPos)
171                 for i in self.parent.value:
172                         if len(value):  #fixme no heading separator possible
173                                 value += self.parent.vals[0]
174                                 if mPos >= len(value) - 1:
175                                         mPos += 1
176                                 
177                         #diff =         self.parent.vals[1] - len(str(i))
178                         #if diff > 0:
179                                 ## if this helps?!
180                                 #value += " " * diff
181                         print (("%0" + str(len(str(self.parent.vals[1][1]))) + "d") % i)
182                         value += ("%0" + str(len(str(self.parent.vals[1][1]))) + "d") % i
183
184                         # only mark cursor when we are selected
185                         # (this code is heavily ink optimized!)
186                 return ("mtext"[1-selected:], value, [mPos])
187
188 class configValue:
189         def __init__(self, obj):
190                 self.obj = obj
191                 
192         def __str__(self):
193                 return self.obj
194
195 class Config:
196         def __init__(self):
197                 self.key = { "choseElement": 0,
198                                          "prevElement": 1,
199                                          "nextElement": 2,
200                                          "0": 10,
201                                          "1": 11,
202                                          "2": 12,
203                                          "3": 13,
204                                          "4": 14,
205                                          "5": 15,
206                                          "6": 16,
207                                          "7": 17,
208                                          "8": 18,
209                                          "9": 19 }
210                 
211 config = Config();
212 configfile = configFile()
213
214 class ConfigSlider:
215         def __init__(self, parent):
216                 self.parent = parent
217
218         def cancel(self):
219                 self.parent.reload()
220
221         def save(self):
222                 self.parent.save()
223
224         def checkValues(self):
225                 if self.parent.value < 0:
226                         self.parent.value = 0   
227
228                 if self.parent.value > 10:
229                         self.parent.value = 10  
230
231         def handleKey(self, key):
232                 if key == config.key["prevElement"]:
233                         self.parent.value = self.parent.value - 1
234                 if key == config.key["nextElement"]:
235                         self.parent.value = self.parent.value + 1
236                                         
237                 self.checkValues()      
238                 self.parent.change()    
239
240         def __call__(self, selected):                   #needed by configlist
241                 self.checkValues()
242                 return ("slider", self.parent.value * 10)
243
244 class ConfigSubsection:
245         def __init__(self):
246                 pass
247
248 class configElement:
249
250         def getIndexbyEntry(self, data):
251                 cnt = 0;
252                 tcnt = -1; #for defaultval
253                 for x in self.vals:
254                         if int(x[1]) == int(data):
255                                         return cnt
256                         if int(x[1]) == int(self.defaultValue):
257                                         tcnt = cnt
258                         cnt += 1
259                 if tcnt != -1:
260                         return tcnt                     
261                 return 0        #prevent bigger then array
262
263         def datafromFile(self, control, data):
264                 if control == ConfigSlider:
265                         return int(data);
266                 elif control == configSelection:
267                         return int(data);
268                 elif control == configSequence:
269                         list = [ ]
270                         part = data.split(self.vals[0])
271                         for x in part:
272                                 list.append(int(x))
273                         return list
274                 elif control == configSatlist:
275                         return self.getIndexbyEntry(data)
276                 else: 
277                         return ""       
278
279         def datatoFile(self, control, data):
280                 if control == ConfigSlider:
281                         return str(data);
282                 elif control == configSelection:
283                         return str(data);
284                 elif control == configSequence:
285                         value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
286 #                       just in case you don't understand the above, here an equivalent:
287 #                       value = ""
288 #                       for i in data:
289 #                               if value !="":
290 #                                       value += self.vals[0]
291 #                               value += str(i)
292                         return value
293                 elif control == configSatlist:
294                         return str(self.vals[self.value][1]);
295                 else: 
296                         return ""       
297
298         def loadData(self):
299                 try:
300                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
301                 except:         
302                         value = ""
303
304                 if value == "":
305                         print "value not found - using default"
306
307                         if self.controlType == configSatlist:
308                                 self.value = self.getIndexbyEntry(self.defaultValue)
309                         else:   
310                                 self.value = self.defaultValue
311
312                         self.save()             #add missing value to dict
313                 else:
314                         self.value = value
315                         
316                 #is this right? activate settings after load/cancel and use default     
317                 self.change()
318
319         def __init__(self, configPath, control, defaultValue, vals):
320                 self.configPath = configPath
321                 self.defaultValue = defaultValue
322                 self.controlType = control
323                 self.vals = vals
324                 self.notifierList = [ ]
325                 self.enabled = True
326                 self.loadData()         
327         def addNotifier(self, notifier):
328                 self.notifierList.append(notifier);
329                 notifier(self);
330         def change(self):
331                 for notifier in self.notifierList:
332                         notifier(self)
333         def reload(self):
334                 self.loadData()
335         def save(self):
336                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))