add ability to remove list and config from a wizard
[enigma2.git] / lib / python / Components / config.py
1 from time import *
2 from Tools.NumericalTextInput import *
3 from Tools.Directories import *
4
5 class configFile:
6         def __init__(self):
7                 self.changed = 0
8                 self.configElements = { }
9                 try:
10                         self.file = open(resolveFilename(SCOPE_CONFIG, "config"))
11                 except IOError:
12                         print "cannot open config file"
13                         return 
14                 
15                 while 1:
16                         line = self.file.readline()
17                         if line == "":
18                                 break
19                         
20                         if line.startswith("#"):                #skip comments
21                                 continue        
22                                 
23                         self.addElement(line)
24                 self.file.close()
25
26         def addElement(self, line):
27                 x = line.find("=")
28                 if x > -1:
29                         self.configElements[line[:x]] = line[x + 1:-1]
30         
31         def getKey(self, key):
32                 return self.configElements[key]
33
34         def setKey(self, key, value):
35                 self.changed = 1
36                 self.configElements[key] = value
37
38         def save(self):
39                 if self.changed == 0:           #no changes, so no write to disk needed
40                         return
41                         
42                 fileHandle = open(resolveFilename(SCOPE_CONFIG, "config"), "w")
43                 
44                 keys = self.configElements.keys()
45                 keys.sort()
46                 for x in keys:
47                         wstr = x + "=" + self.configElements[x] + "\n"
48
49                         fileHandle.write(wstr)
50
51                 fileHandle.close()              
52
53 class configSelection:
54         def __init__(self, parent):
55                 self.parent = parent
56                 
57         def checkValues(self):
58                 if self.parent.value < 0:
59                         self.parent.value = len(self.parent.vals) - 1   
60                 elif(self.parent.value > (len(self.parent.vals) - 1)):
61                         self.parent.value = 0
62
63         def cancel(self):
64                 self.parent.reload()
65
66         def save(self):
67                 self.parent.save()
68
69         def handleKey(self, key):
70                 if key == config.key["prevElement"]:
71                         self.parent.value = self.parent.value - 1
72                 if key == config.key["nextElement"]:
73                         self.parent.value = self.parent.value + 1
74                 
75                 self.checkValues()                      
76
77                 self.parent.change()
78
79         def __call__(self, selected):                   #needed by configlist
80                 self.checkValues()
81                 return ("text", _(self.parent.vals[self.parent.value]))
82
83 class configDateTime:
84         def __init__(self, parent):
85                 self.parent = parent
86                 
87         def checkValues(self):
88                 pass
89 #               if self.parent.value < 0:
90                         #self.parent.value = 0  
91
92                 #if(self.parent.value >= (len(self.parent.vals) - 1)):
93                         #self.parent.value = len(self.parent.vals) - 1
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 - self.parent.vals[1]
104                 if key == config.key["nextElement"]:
105                         self.parent.value = self.parent.value + self.parent.vals[1]
106                 
107                 self.checkValues()
108
109                 self.parent.change()    
110
111         def __call__(self, selected):                   #needed by configlist
112                 self.checkValues()
113                 return ("text", strftime(self.parent.vals[0], localtime(self.parent.value)))
114         
115 class configSatlist:
116         def __init__(self, parent):
117                 self.parent = parent
118
119         def checkValues(self):
120                 if self.parent.value < 0:
121                         self.parent.value = 0   
122
123                 if(self.parent.value >= (len(self.parent.vals) - 1)):
124                         self.parent.value = len(self.parent.vals) - 1
125                         
126         def cancel(self):
127                 self.parent.reload()
128
129         def save(self):
130                 self.parent.save()
131
132         def handleKey(self, key):
133                 if key == config.key["prevElement"]:
134                         self.parent.value = self.parent.value - 1
135                 if key == config.key["nextElement"]:
136                         self.parent.value = self.parent.value + 1
137                 
138                 self.checkValues()                      
139
140                 self.parent.change()    
141
142         def __call__(self, selected):                   #needed by configlist
143                 self.checkValues()
144                 #fixme
145                 return ("text", str(self.parent.vals[self.parent.value][0]))
146
147 class configSequenceArg:
148         def get(self, type, args = ()):
149                 # configsequencearg.get ("IP")
150                 if (type == "IP"):
151                         return (("."), [(0,255),(0,255),(0,255),(0,255)], "")
152                 # configsequencearg.get ("MAC")
153                 if (type == "MAC"):
154                         return ((":"), [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], "")
155                 # configsequencearg.get ("CLOCK")
156                 if (type == "CLOCK"):
157                         return ((":"), [(0,23),(0,59)], "")
158                 # configsequencearg.get("INTEGER", (min, max)) => x with min <= x <= max
159                 if (type == "INTEGER"):
160                         return ((":"), [args], "")
161                 # configsequencearg.get("PINCODE", (number, "*")) => pin with number = length of pincode and "*" as numbers shown as stars
162                 # configsequencearg.get("PINCODE", (number, "")) => pin with number = length of pincode and numbers shown
163                 if (type == "PINCODE"):
164                         return ((":"), [(0, (10**args[0])-1)], args[1])
165                 # configsequencearg.get("FLOAT", [(min,max),(min1,max1)]) => x.y with min <= x <= max and min1 <= y <= max1
166                 if (type == "FLOAT"):
167                         return (("."), args, "")
168
169 configsequencearg = configSequenceArg()
170                 
171 class configSequence:
172         def __init__(self, parent):
173                 self.parent = parent
174                 self.markedPos = 0
175                 self.seperator = self.parent.vals[0]
176                 self.valueBounds = self.parent.vals[1]
177                 self.censorChar = self.parent.vals[2]
178
179         def checkValues(self):
180                 maxPos = 0
181                 num = 0
182                 for i in self.parent.value:
183                         maxPos += len(str(self.valueBounds[num][1]))
184                         while (self.valueBounds[num][0] > self.parent.value[num]):
185                                 self.parent.value[num] += 1
186
187                         while (self.valueBounds[num][1] < self.parent.value[num]):
188                                 self.parent.value[num] -= 1
189                                 
190 #                       if (self.valueBounds[num][0] <= i <= self.valueBounds[num][1]):
191                                 #pass
192                         #else:
193                                 #self.parent.value[num] = self.valueBounds[num][0]
194                         num += 1
195                 
196                 if self.markedPos >= maxPos:
197                         self.markedPos = maxPos - 1
198                 if self.markedPos < 0:
199                         self.markedPos = 0
200                         
201         def cancel(self):
202                 self.parent.reload()
203
204         def save(self):
205                 self.parent.save()
206
207         def handleKey(self, key):
208                 #this will no change anything on the value itself
209                 #so we can handle it here in gui element
210                 if key == config.key["prevElement"]:
211                         self.markedPos -= 1
212                 if key == config.key["nextElement"]:
213                         self.markedPos += 1
214                 
215                 if key >= config.key["0"] and key <= config.key["9"]:
216                         self.blockLen = []
217                         for x in self.valueBounds:
218                                 self.blockLen.append(len(str(x[1])))
219                                 
220                         pos = 0
221                         blocknumber = 0
222                         self.blockLenTotal = [0,]
223                         for x in self.blockLen:
224                                 pos += self.blockLen[blocknumber]
225                                 self.blockLenTotal.append(pos)
226                                 if (pos - 1 >= self.markedPos):
227                                         pass
228                                 else:
229                                         blocknumber += 1
230                                         
231                         number = 9 - config.key["9"] + key
232                         # length of numberblock
233                         numberLen = len(str(self.valueBounds[blocknumber][1]))
234                         # position in the block
235                         posinblock = self.markedPos - self.blockLenTotal[blocknumber]
236                         
237                         oldvalue = self.parent.value[blocknumber]
238                         olddec = oldvalue % 10 ** (numberLen - posinblock) - (oldvalue % 10 ** (numberLen - posinblock - 1))
239                         newvalue = oldvalue - olddec + (10 ** (numberLen - posinblock - 1) * number)
240                         
241                         print "You actually pressed a number (" + str(number) + ") which will be added at block number " + str(blocknumber) + " on position " + str(posinblock)
242                         print "Old value: " + str(oldvalue) + " olddec: " + str(olddec) + " newvalue: " + str(newvalue)
243                         self.parent.value[blocknumber] = newvalue
244                         self.markedPos += 1
245                 
246                 self.checkValues()                      
247                 
248                 print "markPos:",
249                 print self.markedPos
250
251                 #FIXME: dont call when press left/right
252                 self.parent.change()    
253
254         def __call__(self, selected):                   #needed by configlist
255                 value = ""
256                 mPos = self.markedPos
257                 print "Positon: " + str(mPos)
258                 num = 0;
259                 for i in self.parent.value:
260                         if len(value):  #fixme no heading separator possible
261                                 value += self.seperator
262                                 if mPos >= len(value) - 1:
263                                         mPos += 1
264                                 
265                         #diff =         self.valueBounds - len(str(i))
266                         #if diff > 0:
267                                 ## if this helps?!
268                                 #value += " " * diff
269                         print (("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i)
270                         if (self.censorChar == ""):
271                                 value += ("%0" + str(len(str(self.valueBounds[num][1]))) + "d") % i
272                         else:
273                                 value += (self.censorChar * len(str(self.valueBounds[num][1])))
274                         num += 1
275                         # only mark cursor when we are selected
276                         # (this code is heavily ink optimized!)
277                 if (self.parent.enabled == True):
278                         return ("mtext"[1-selected:], value, [mPos])
279                 else:
280                         return ("text", value)
281                 
282 class configText:
283         # used as first parameter
284         # is the text of a fixed size or is the user able to extend the length of the text
285         extendableSize = 1
286         fixedSize = 2
287
288         def __init__(self, parent):
289                 self.parent = parent
290                 self.markedPos = 0
291                 self.mode = self.parent.vals[0]
292                 self.textInput = NumericalTextInput(self.nextEntry)
293
294         def checkValues(self):
295                 if (self.markedPos < 0):
296                         self.markedPos = 0
297                 if (self.markedPos >= len(self.parent.value)):
298                         self.markedPos = len(self.parent.value) - 1
299                         
300         def cancel(self):
301                 self.parent.reload()
302
303         def save(self):
304                 self.parent.save()
305                 
306         def nextEntry(self):
307                 print self.parent
308                 self.parent.vals[1](self.parent.getConfigPath())
309
310         def handleKey(self, key):
311                 #this will no change anything on the value itself
312                 #so we can handle it here in gui element
313                 if key == config.key["delete"]:
314                         self.parent.value = self.parent.value[0:self.markedPos] + self.parent.value[self.markedPos + 1:]
315                 if key == config.key["prevElement"]:
316                         self.textInput.nextKey()
317                         self.markedPos -= 1
318
319                 if key == config.key["nextElement"]:
320                         self.textInput.nextKey()
321                         self.markedPos += 1
322                         if (self.mode == self.extendableSize):
323                                 if (self.markedPos >= len(self.parent.value)):
324                                         self.parent.value = self.parent.value.ljust(len(self.parent.value) + 1)
325                         
326                                 
327                 if key >= config.key["0"] and key <= config.key["9"]:
328                         number = 9 - config.key["9"] + key
329
330                         self.parent.value = self.parent.value[0:self.markedPos] + str(self.textInput.getKey(number)) + self.parent.value[self.markedPos + 1:]
331                 
332                 self.checkValues()                      
333                 
334                 self.parent.change()    
335
336         def __call__(self, selected):                   #needed by configlist
337                 return ("mtext"[1-selected:], str(self.parent.value), [self.markedPos])
338                 
339 class configValue:
340         def __init__(self, obj):
341                 self.obj = obj
342                 
343         def __str__(self):
344                 return self.obj
345
346 class Config:
347         def __init__(self):
348                 self.key = { "choseElement": 0,
349                                          "prevElement": 1,
350                                          "nextElement": 2,
351                                          "delete": 3,
352                                          "0": 10,
353                                          "1": 11,
354                                          "2": 12,
355                                          "3": 13,
356                                          "4": 14,
357                                          "5": 15,
358                                          "6": 16,
359                                          "7": 17,
360                                          "8": 18,
361                                          "9": 19 }
362                 
363 config = Config();
364
365 configfile = configFile()
366
367 class ConfigSlider:
368         def __init__(self, parent):
369                 self.parent = parent
370
371         def cancel(self):
372                 self.parent.reload()
373
374         def save(self):
375                 self.parent.save()
376
377         def checkValues(self):
378                 if self.parent.value < 0:
379                         self.parent.value = 0   
380
381                 if self.parent.value > 10:
382                         self.parent.value = 10  
383
384         def handleKey(self, key):
385                 if key == config.key["prevElement"]:
386                         self.parent.value = self.parent.value - 1
387                 if key == config.key["nextElement"]:
388                         self.parent.value = self.parent.value + 1
389                                         
390                 self.checkValues()      
391                 self.parent.change()    
392
393         def __call__(self, selected):                   #needed by configlist
394                 self.checkValues()
395                 return ("slider", self.parent.value * 10)
396
397 class ConfigSubsection:
398         def __init__(self):
399                 pass
400
401 class configElement:
402
403         def getIndexbyEntry(self, data):
404                 cnt = 0;
405                 tcnt = -1; #for defaultval
406                 for x in self.vals:
407                         if int(x[1]) == int(data):
408                                         return cnt
409                         if int(x[1]) == int(self.defaultValue):
410                                         tcnt = cnt
411                         cnt += 1
412                 if tcnt != -1:
413                         return tcnt
414                 return 0        #prevent bigger then array
415
416         def datafromFile(self, control, data):
417                 if control == ConfigSlider:
418                         return int(data)
419                 elif control == configSelection:
420                         return int(data)
421                 elif control == configDateTime:
422                         return int(data)
423                 elif control == configText:
424                         return str(data)
425                 elif control == configSequence:
426                         list = [ ]
427                         part = data.split(self.vals[0])
428                         for x in part:
429                                 list.append(int(x))
430                         return list
431                 elif control == configSatlist:
432                         return self.getIndexbyEntry(data)
433                 else: 
434                         return ""       
435
436         def datatoFile(self, control, data):
437                 if control == ConfigSlider:
438                         return str(data)
439                 elif control == configSelection:
440                         return str(data)
441                 elif control == configDateTime:
442                         return str(data)
443                 elif control == configText:
444                         return str(data.strip())
445
446                 elif control == configSequence:
447                         print data
448                         try:
449                                 value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
450                         except: 
451                                 value = str(data)       
452 #                       just in case you don't understand the above, here an equivalent:
453 #                       value = ""
454 #                       for i in data:
455 #                               if value !="":
456 #                                       value += self.vals[0]
457 #                               value += str(i)
458                         return value
459                 elif control == configSatlist:
460                         return str(self.vals[self.value][1]);
461                 else: 
462                         return ""       
463
464         def loadData(self):
465                 #print "load:" + self.configPath
466                 try:
467                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
468                 except:         
469                         value = ""
470
471                 if value == "":
472                         #print "value not found - using default"
473
474                         if self.controlType == configSatlist:
475                                 self.value = self.getIndexbyEntry(self.defaultValue)
476                         else:   
477                                 self.value = self.defaultValue
478
479                         self.save()             #add missing value to dict
480                 else:
481                         #print "set val:" + str(value)
482                         self.value = value
483                         
484                 #is this right? activate settings after load/cancel and use default     
485                 self.change()
486
487         def __init__(self, configPath, control, defaultValue, vals):
488                 self.configPath = configPath
489                 self.defaultValue = defaultValue
490                 self.controlType = control
491                 self.vals = vals
492                 self.notifierList = [ ]
493                 self.enabled = True
494                 self.loadData()         
495                 
496         def getConfigPath(self):
497                 return self.configPath
498         
499         def addNotifier(self, notifier):
500                 self.notifierList.append(notifier);
501                 notifier(self);
502         def change(self):
503                 for notifier in self.notifierList:
504                         notifier(self)
505         def reload(self):
506                 self.loadData()
507         def save(self):
508                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))
509
510 class configElement_nonSave(configElement):
511         def __init__(self, configPath, control, defaultValue, vals):
512                 configElement.__init__(self, configPath, control, defaultValue, vals)
513
514         def save(self):
515                 pass
516                 
517 def getConfigListEntry(description, element):
518         b = element
519         item = b.controlType(b)
520         return ((description, item))
521
522
523 def configElementBoolean(name, default, texts=(_("Enable"), _("Disable"))):
524         return configElement(name, configSelection, default, texts)
525
526 config.misc = ConfigSubsection()