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