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