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