add new config types ConfigSearchText, which uses the new sql NumericalTextInput...
[enigma2.git] / lib / python / Components / config.py
1 import time
2 from enigma import getPrevAsciiCode
3 from Tools.NumericalTextInput import NumericalTextInput
4 from Tools.Directories import resolveFilename, SCOPE_CONFIG
5 from Components.Harddisk import harddiskmanager
6 import copy
7 import os
8
9 # ConfigElement, the base class of all ConfigElements.
10
11 # it stores:
12 #   value    the current value, usefully encoded.
13 #            usually a property which retrieves _value,
14 #            and maybe does some reformatting
15 #   _value   the value as it's going to be saved in the configfile,
16 #            though still in non-string form.
17 #            this is the object which is actually worked on.
18 #   default  the initial value. If _value is equal to default,
19 #            it will not be stored in the config file
20 #   saved_value is a text representation of _value, stored in the config file
21 #
22 # and has (at least) the following methods:
23 #   save()   stores _value into saved_value, 
24 #            (or stores 'None' if it should not be stored)
25 #   load()   loads _value from saved_value, or loads
26 #            the default if saved_value is 'None' (default)
27 #            or invalid.
28 #
29 class ConfigElement(object):
30         def __init__(self):
31
32                 object.__init__(self)
33                 self.saved_value = None
34                 self.save_disabled = False
35                 self.notifiers = []
36                 self.enabled = True
37                 self.callNotifiersOnSaveAndCancel = False
38
39         # you need to override this to do input validation
40         def setValue(self, value):
41                 self._value = value
42                 self.changed()
43
44         def getValue(self):
45                 return self._value
46         
47         value = property(getValue, setValue)
48
49         # you need to override this if self.value is not a string
50         def fromstring(self, value):
51                 return value
52
53         # you can overide this for fancy default handling
54         def load(self):
55                 sv = self.saved_value
56                 if sv is None:
57                         self.value = self.default
58                 else:
59                         self.value = self.fromstring(sv)
60
61         def tostring(self, value):
62                 return str(value)
63
64         # you need to override this if str(self.value) doesn't work
65         def save(self):
66                 if self.save_disabled or self.value == self.default:
67                         self.saved_value = None
68                 else:
69                         self.saved_value = self.tostring(self.value)
70                 if self.callNotifiersOnSaveAndCancel:
71                         self.changed()
72
73         def cancel(self):
74                 self.load()
75                 if self.callNotifiersOnSaveAndCancel:
76                         self.changed()
77
78         def isChanged(self):
79                 sv = self.saved_value
80                 if sv is None and self.value == self.default:
81                         return False
82                 return self.tostring(self.value) != sv
83
84         def changed(self):
85                 for x in self.notifiers:
86                         x(self)
87                         
88         def addNotifier(self, notifier, initial_call = True):
89                 assert callable(notifier), "notifiers must be callable"
90                 self.notifiers.append(notifier)
91
92                 # CHECKME:
93                 # do we want to call the notifier
94                 #  - at all when adding it? (yes, though optional)
95                 #  - when the default is active? (yes)
96                 #  - when no value *yet* has been set,
97                 #    because no config has ever been read (currently yes)
98                 #    (though that's not so easy to detect.
99                 #     the entry could just be new.)
100                 if initial_call:
101                         notifier(self)
102
103         def disableSave(self):
104                 self.save_disabled = True
105
106         def __call__(self, selected):
107                 return self.getMulti(selected)
108
109         def onSelect(self, session):
110                 pass
111
112         def onDeselect(self, session):
113                 pass
114
115 KEY_LEFT = 0
116 KEY_RIGHT = 1
117 KEY_OK = 2
118 KEY_DELETE = 3
119 KEY_BACKSPACE = 4
120 KEY_HOME = 5
121 KEY_END = 6
122 KEY_TOGGLEOW = 7
123 KEY_ASCII = 8
124 KEY_TIMEOUT = 9
125 KEY_NUMBERS = range(12, 12+10)
126 KEY_0 = 12
127 KEY_9 = 12+9
128
129 def getKeyNumber(key):
130         assert key in KEY_NUMBERS
131         return key - KEY_0
132
133 #
134 # ConfigSelection is a "one of.."-type.
135 # it has the "choices", usually a list, which contains
136 # (id, desc)-tuples (or just only the ids, in case the id
137 # will be used as description)
138 #
139 # all ids MUST be plain strings.
140 #
141 class ConfigSelection(ConfigElement):
142         def __init__(self, choices, default = None):
143                 ConfigElement.__init__(self)
144                 self._value = None
145                 self.setChoices(choices, default)
146
147         def setChoices(self, choices, default = None):
148                 self.choices = []
149                 self.description = {}
150                 
151                 if isinstance(choices, list):
152                         for x in choices:
153                                 if isinstance(x, tuple):
154                                         self.choices.append(x[0])
155                                         self.description[x[0]] = x[1]
156                                 else:
157                                         self.choices.append(x)
158                                         self.description[x] = x
159                 elif isinstance(choices, dict):
160                         for (key, val) in choices.items():
161                                 self.choices.append(key)
162                                 self.description[key] = val
163                 else:
164                         assert False, "ConfigSelection choices must be dict or list!"
165                 
166                 #assert len(self.choices), "you can't have an empty configselection"
167                 if len(self.choices) == 0:
168                         self.choices = [""]
169                         self.description[""] = ""
170
171                 if default is None:
172                         default = self.choices[0]
173
174                 assert default in self.choices, "default must be in choice list, but " + repr(default) + " is not!"
175                 for x in self.choices:
176                         assert isinstance(x, str), "ConfigSelection choices must be strings"
177                 
178                 self.default = default
179
180                 if self.value == None or not self.value in self.choices:
181                         self.value = default
182
183         def setValue(self, value):
184                 if value in self.choices:
185                         self._value = value
186                 else:
187                         self._value = self.default
188                 
189                 self.changed()
190
191         def tostring(self, val):
192                 return val
193
194         def getValue(self):
195                 return self._value
196
197         def setCurrentText(self, text):
198                 i = self.choices.index(self.value)
199                 del self.description[self.choices[i]]
200                 self.choices[i] = text
201                 self.description[text] = text
202                 self._value = text
203
204         value = property(getValue, setValue)
205         
206         def getIndex(self):
207                 return self.choices.index(self.value)
208         
209         index = property(getIndex)
210
211         # GUI
212         def handleKey(self, key):
213                 nchoices = len(self.choices)
214                 i = self.choices.index(self.value)
215                 if key == KEY_LEFT:
216                         self.value = self.choices[(i + nchoices - 1) % nchoices]
217                 elif key == KEY_RIGHT:
218                         self.value = self.choices[(i + 1) % nchoices]
219                 elif key == KEY_HOME:
220                         self.value = self.choices[0]
221                 elif key == KEY_END:
222                         self.value = self.choices[nchoices - 1]
223                         
224         def selectNext(self):
225                 nchoices = len(self.choices)
226                 i = self.choices.index(self.value)
227                 self.value = self.choices[(i + 1) % nchoices]
228
229         def getText(self):
230                 descr = self.description[self.value]
231                 if len(descr):
232                         return _(descr)
233                 return descr
234
235         def getMulti(self, selected):
236                 descr = self.description[self.value]
237                 if len(descr):
238                         return ("text", _(descr))
239                 return ("text", descr)
240
241         # HTML
242         def getHTML(self, id):
243                 res = ""
244                 for v in self.choices:
245                         if self.value == v:
246                                 checked = 'checked="checked" '
247                         else:
248                                 checked = ''
249                         res += '<input type="radio" name="' + id + '" ' + checked + 'value="' + v + '">' + self.description[v] + "</input></br>\n"
250                 return res;
251
252         def unsafeAssign(self, value):
253                 # setValue does check if value is in choices. This is safe enough.
254                 self.value = value
255
256 # a binary decision.
257 #
258 # several customized versions exist for different
259 # descriptions.
260 #
261 class ConfigBoolean(ConfigElement):
262         def __init__(self, default = False, descriptions = {False: "false", True: "true"}):
263                 ConfigElement.__init__(self)
264                 self.descriptions = descriptions
265                 self.value = self.default = default
266         def handleKey(self, key):
267                 if key in [KEY_LEFT, KEY_RIGHT]:
268                         self.value = not self.value
269                 elif key == KEY_HOME:
270                         self.value = False
271                 elif key == KEY_END:
272                         self.value = True
273
274         def getText(self):
275                 descr = self.descriptions[self.value]
276                 if len(descr):
277                         return _(descr)
278                 return descr
279
280         def getMulti(self, selected):
281                 descr = self.descriptions[self.value]
282                 if len(descr):
283                         return ("text", _(descr))
284                 return ("text", descr)
285
286         def tostring(self, value):
287                 if not value:
288                         return "false"
289                 else:
290                         return "true"
291
292         def fromstring(self, val):
293                 if val == "true":
294                         return True
295                 else:
296                         return False
297
298         def getHTML(self, id):
299                 if self.value:
300                         checked = ' checked="checked"'
301                 else:
302                         checked = ''
303                 return '<input type="checkbox" name="' + id + '" value="1" ' + checked + " />"
304
305         # this is FLAWED. and must be fixed.
306         def unsafeAssign(self, value):
307                 if value == "1":
308                         self.value = True
309                 else:
310                         self.value = False
311
312 class ConfigYesNo(ConfigBoolean):
313         def __init__(self, default = False):
314                 ConfigBoolean.__init__(self, default = default, descriptions = {False: _("no"), True: _("yes")})
315
316 class ConfigOnOff(ConfigBoolean):
317         def __init__(self, default = False):
318                 ConfigBoolean.__init__(self, default = default, descriptions = {False: _("off"), True: _("on")})
319
320 class ConfigEnableDisable(ConfigBoolean):
321         def __init__(self, default = False):
322                 ConfigBoolean.__init__(self, default = default, descriptions = {False: _("disable"), True: _("enable")})
323
324 class ConfigDateTime(ConfigElement):
325         def __init__(self, default, formatstring, increment = 86400):
326                 ConfigElement.__init__(self)
327                 self.increment = increment
328                 self.formatstring = formatstring
329                 self.value = self.default = int(default)
330
331         def handleKey(self, key):
332                 if key == KEY_LEFT:
333                         self.value = self.value - self.increment
334                 elif key == KEY_RIGHT:
335                         self.value = self.value + self.increment
336                 elif key == KEY_HOME or key == KEY_END:
337                         self.value = self.default
338
339         def getText(self):
340                 return time.strftime(self.formatstring, time.localtime(self.value))
341
342         def getMulti(self, selected):
343                 return ("text", time.strftime(self.formatstring, time.localtime(self.value)))
344
345         def fromstring(self, val):
346                 return int(val)
347
348 # *THE* mighty config element class
349 #
350 # allows you to store/edit a sequence of values.
351 # can be used for IP-addresses, dates, plain integers, ...
352 # several helper exist to ease this up a bit.
353 #
354 class ConfigSequence(ConfigElement):
355         def __init__(self, seperator, limits, censor_char = "", default = None):
356                 ConfigElement.__init__(self)
357                 assert isinstance(limits, list) and len(limits[0]) == 2, "limits must be [(min, max),...]-tuple-list"
358                 assert censor_char == "" or len(censor_char) == 1, "censor char must be a single char (or \"\")"
359                 #assert isinstance(default, list), "default must be a list"
360                 #assert isinstance(default[0], int), "list must contain numbers"
361                 #assert len(default) == len(limits), "length must match"
362
363                 self.marked_pos = 0
364                 self.seperator = seperator
365                 self.limits = limits
366                 self.censor_char = censor_char
367                 
368                 self.default = default
369                 self.value = copy.copy(default)
370                 
371                 self.endNotifier = []
372
373         def validate(self):
374                 max_pos = 0
375                 num = 0
376                 for i in self._value:
377                         max_pos += len(str(self.limits[num][1]))
378
379                         if self._value[num] < self.limits[num][0]:
380                                 self._value[num] = self.limits[num][0]
381
382                         if self._value[num] > self.limits[num][1]:
383                                 self._value[num] = self.limits[num][1]
384
385                         num += 1
386
387                 if self.marked_pos >= max_pos:
388                         for x in self.endNotifier:
389                                 x(self)
390                         self.marked_pos = max_pos - 1
391
392                 if self.marked_pos < 0:
393                         self.marked_pos = 0
394
395         def validatePos(self):
396                 if self.marked_pos < 0:
397                         self.marked_pos = 0
398                         
399                 total_len = sum([len(str(x[1])) for x in self.limits])
400
401                 if self.marked_pos >= total_len:
402                         self.marked_pos = total_len - 1
403                         
404         def addEndNotifier(self, notifier):
405                 self.endNotifier.append(notifier)
406
407         def handleKey(self, key):
408                 if key == KEY_LEFT:
409                         self.marked_pos -= 1
410                         self.validatePos()
411
412                 if key == KEY_RIGHT:
413                         self.marked_pos += 1
414                         self.validatePos()
415                 
416                 if key == KEY_HOME:
417                         self.marked_pos = 0
418                         self.validatePos()
419
420                 if key == KEY_END:
421                         max_pos = 0
422                         num = 0
423                         for i in self._value:
424                                 max_pos += len(str(self.limits[num][1]))
425                                 num += 1
426                         self.marked_pos = max_pos - 1
427                         self.validatePos()
428                 
429                 if key in KEY_NUMBERS or key == KEY_ASCII:
430                         if key == KEY_ASCII:
431                                 code = getPrevAsciiCode()
432                                 if code < 48 or code > 57:
433                                         return
434                                 number = code - 48
435                         else:
436                                 number = getKeyNumber(key)
437                         
438                         block_len = []
439                         for x in self.limits:
440                                 block_len.append(len(str(x[1])))
441                         
442                         total_len = sum(block_len)
443
444                         pos = 0
445                         blocknumber = 0
446                         block_len_total = [0, ]
447                         for x in block_len:
448                                 pos += block_len[blocknumber]
449                                 block_len_total.append(pos)
450                                 if pos - 1 >= self.marked_pos:
451                                         pass
452                                 else:
453                                         blocknumber += 1
454
455                         # length of numberblock
456                         number_len = len(str(self.limits[blocknumber][1]))
457
458                         # position in the block
459                         posinblock = self.marked_pos - block_len_total[blocknumber]
460                         
461                         oldvalue = self._value[blocknumber]
462                         olddec = oldvalue % 10 ** (number_len - posinblock) - (oldvalue % 10 ** (number_len - posinblock - 1))
463                         newvalue = oldvalue - olddec + (10 ** (number_len - posinblock - 1) * number)
464                         
465                         self._value[blocknumber] = newvalue
466                         self.marked_pos += 1
467                 
468                         self.validate()
469                         self.changed()
470         
471         def genText(self):
472                 value = ""
473                 mPos = self.marked_pos
474                 num = 0;
475                 for i in self._value:
476                         if len(value):  #fixme no heading separator possible
477                                 value += self.seperator
478                                 if mPos >= len(value) - 1:
479                                         mPos += 1
480
481                         if self.censor_char == "":
482                                 value += ("%0" + str(len(str(self.limits[num][1]))) + "d") % i
483                         else:
484                                 value += (self.censor_char * len(str(self.limits[num][1])))
485                         num += 1
486                 return (value, mPos)
487                 
488         def getText(self):
489                 (value, mPos) = self.genText()
490                 return value
491         
492         def getMulti(self, selected):
493                 (value, mPos) = self.genText()
494                         # only mark cursor when we are selected
495                         # (this code is heavily ink optimized!)
496                 if self.enabled:
497                         return ("mtext"[1-selected:], value, [mPos])
498                 else:
499                         return ("text", value)
500
501         def tostring(self, val):
502                 return self.seperator.join([self.saveSingle(x) for x in val])
503         
504         def saveSingle(self, v):
505                 return str(v)
506
507         def fromstring(self, value):
508                 return [int(x) for x in value.split(self.seperator)]
509
510 class ConfigIP(ConfigSequence):
511         def __init__(self, default, auto_jump = False):
512                 ConfigSequence.__init__(self, seperator = ".", limits = [(0,255),(0,255),(0,255),(0,255)], default = default)
513                 self.block_len = []
514                 for x in self.limits:
515                         self.block_len.append(len(str(x[1])))
516                 self.marked_block = 0
517                 self.overwrite = True
518                 self.auto_jump = auto_jump
519
520         def handleKey(self, key):
521                 
522                 if key == KEY_LEFT:
523                         if self.marked_block > 0:
524                                 self.marked_block -= 1
525                         self.overwrite = True
526
527                 if key == KEY_RIGHT:
528                         if self.marked_block < len(self.limits)-1:
529                                 self.marked_block += 1
530                         self.overwrite = True
531
532                 if key == KEY_HOME:
533                         self.marked_block = 0
534                         self.overwrite = True
535
536                 if key == KEY_END:
537                         self.marked_block = len(self.limits)-1
538                         self.overwrite = True
539
540                 if key in KEY_NUMBERS or key == KEY_ASCII:
541                         if key == KEY_ASCII:
542                                 code = getPrevAsciiCode()
543                                 if code < 48 or code > 57:
544                                         return
545                                 number = code - 48
546                         else:   
547                                 number = getKeyNumber(key)
548                         oldvalue = self._value[self.marked_block]
549                         
550                         if self.overwrite:
551                                 self._value[self.marked_block] = number
552                                 self.overwrite = False          
553                         else:
554                                 oldvalue *= 10
555                                 newvalue = oldvalue + number
556                                 if self.auto_jump and newvalue > self.limits[self.marked_block][1] and self.marked_block < len(self.limits)-1:
557                                         self.handleKey(KEY_RIGHT)
558                                         self.handleKey(key)
559                                         return
560                                 else:
561                                         self._value[self.marked_block] = newvalue
562
563                         if len(str(self._value[self.marked_block])) >= self.block_len[self.marked_block]:
564                                 self.handleKey(KEY_RIGHT)
565
566                         self.validate()
567                         self.changed()
568
569         def genText(self):
570                 value = ""
571                 block_strlen = []
572                 for i in self._value:
573                         block_strlen.append(len(str(i)))        
574                         if len(value):
575                                 value += self.seperator
576                         value += str(i)
577                 leftPos = sum(block_strlen[:(self.marked_block)])+self.marked_block
578                 rightPos = sum(block_strlen[:(self.marked_block+1)])+self.marked_block
579                 mBlock = range(leftPos, rightPos)
580                 return (value, mBlock)
581         
582         def getMulti(self, selected):
583                 (value, mBlock) = self.genText()
584                 if self.enabled:
585                         return ("mtext"[1-selected:], value, mBlock)
586                 else:
587                         return ("text", value)
588
589         def getHTML(self, id):
590                 # we definitely don't want leading zeros
591                 return '.'.join(["%d" % d for d in self.value])
592
593 class ConfigMAC(ConfigSequence):
594         def __init__(self, default):
595                 ConfigSequence.__init__(self, seperator = ":", limits = [(1,255),(1,255),(1,255),(1,255),(1,255),(1,255)], default = default)
596
597 class ConfigPosition(ConfigSequence):
598         def __init__(self, default, args):
599                 ConfigSequence.__init__(self, seperator = ",", limits = [(0,args[0]),(0,args[1]),(0,args[2]),(0,args[3])], default = default)
600
601 class ConfigClock(ConfigSequence):
602         def __init__(self, default):
603                 import time
604                 t = time.localtime(default)
605                 ConfigSequence.__init__(self, seperator = ":", limits = [(0,23),(0,59)], default = [t.tm_hour, t.tm_min])
606
607         def increment(self):
608                 # Check if Minutes maxed out
609                 if self._value[1] == 59:
610                         # Check if Hours not maxed out
611                         if self._value[0] < 23:
612                                 # Increment Hour, reset Minutes to 0
613                                 self._value[0] += 1
614                                 self._value[1] = 0
615                 else:
616                         # Increment Minutes
617                         self._value[1] += 1
618                 # Trigger change
619                 self.changed()
620
621         def decrement(self):
622                 # Check if Minutes is minimum
623                 if self._value[1] == 0:
624                         # Check if Hour is greater than 0
625                         if self._value[0] > 0:
626                                 # Decrement Hour, set Minutes to 59
627                                 self._value[0] -= 1
628                                 self._value[1] = 59
629                 else:
630                         # Decrement Minutes
631                         self._value[1] -= 1
632                 # Trigger change
633                 self.changed()
634
635 class ConfigInteger(ConfigSequence):
636         def __init__(self, default, limits = (0, 9999999999)):
637                 ConfigSequence.__init__(self, seperator = ":", limits = [limits], default = default)
638         
639         # you need to override this to do input validation
640         def setValue(self, value):
641                 self._value = [value]
642                 self.changed()
643
644         def getValue(self):
645                 return self._value[0]
646         
647         value = property(getValue, setValue)
648
649         def fromstring(self, value):
650                 return int(value)
651
652         def tostring(self, value):
653                 return str(value)
654
655 class ConfigPIN(ConfigInteger):
656         def __init__(self, default, len = 4, censor = ""):
657                 assert isinstance(default, int), "ConfigPIN default must be an integer"
658                 if default == -1:
659                         default = "aaaa"
660                 ConfigSequence.__init__(self, seperator = ":", limits = [(0, (10**len)-1)], censor_char = censor, default = default)
661                 self.len = len
662
663         def getLength(self):
664                 return self.len
665
666 class ConfigFloat(ConfigSequence):
667         def __init__(self, default, limits):
668                 ConfigSequence.__init__(self, seperator = ".", limits = limits, default = default)
669
670         def getFloat(self):
671                 return float(self.value[1] / float(self.limits[1][1] + 1) + self.value[0])
672
673         float = property(getFloat)
674
675 # an editable text...
676 class ConfigText(ConfigElement, NumericalTextInput):
677         def __init__(self, default = "", fixed_size = True, visible_width = False):
678                 ConfigElement.__init__(self)
679                 NumericalTextInput.__init__(self, nextFunc = self.nextFunc, handleTimeout = False)
680                 
681                 self.marked_pos = 0
682                 self.allmarked = (default != "")
683                 self.fixed_size = fixed_size
684                 self.visible_width = visible_width
685                 self.offset = 0
686                 self.overwrite = fixed_size
687                 self.help_window = None
688                 self.value = self.default = default
689
690         def validateMarker(self):
691                 if self.fixed_size:
692                         if self.marked_pos > len(self.text)-1:
693                                 self.marked_pos = len(self.text)-1
694                 else:
695                         if self.marked_pos > len(self.text):
696                                 self.marked_pos = len(self.text)
697                 if self.marked_pos < 0:
698                         self.marked_pos = 0
699                 if self.visible_width:
700                         if self.marked_pos < self.offset:
701                                 self.offset = self.marked_pos
702                         if self.marked_pos >= self.offset + self.visible_width:
703                                 if self.marked_pos == len(self.text):
704                                         self.offset = self.marked_pos - self.visible_width
705                                 else:
706                                         self.offset = self.marked_pos - self.visible_width + 1
707                         if self.offset > 0 and self.offset + self.visible_width > len(self.text):
708                                 self.offset = max(0, len(self.text) - self.visible_width)
709
710         def insertChar(self, ch, pos, owr):
711                 if owr or self.overwrite:
712                         self.text = self.text[0:pos] + ch + self.text[pos + 1:]
713                 elif self.fixed_size:
714                         self.text = self.text[0:pos] + ch + self.text[pos:-1]
715                 else:
716                         self.text = self.text[0:pos] + ch + self.text[pos:]
717
718         def deleteChar(self, pos):
719                 if not self.fixed_size:
720                         self.text = self.text[0:pos] + self.text[pos + 1:]
721                 elif self.overwrite:
722                         self.text = self.text[0:pos] + " " + self.text[pos + 1:]
723                 else:
724                         self.text = self.text[0:pos] + self.text[pos + 1:] + " "
725
726         def deleteAllChars(self):
727                 if self.fixed_size:
728                         self.text = " " * len(self.text)
729                 else:
730                         self.text = ""
731                 self.marked_pos = 0
732
733         def handleKey(self, key):
734                 # this will no change anything on the value itself
735                 # so we can handle it here in gui element
736                 if key == KEY_DELETE:
737                         self.timeout()
738                         if self.allmarked:
739                                 self.deleteAllChars()
740                                 self.allmarked = False
741                         else:
742                                 self.deleteChar(self.marked_pos)
743                                 if self.fixed_size and self.overwrite:
744                                         self.marked_pos += 1
745                 elif key == KEY_BACKSPACE:
746                         self.timeout()
747                         if self.allmarked:
748                                 self.deleteAllChars()
749                                 self.allmarked = False
750                         elif self.marked_pos > 0:
751                                 self.deleteChar(self.marked_pos-1)
752                                 if not self.fixed_size and self.offset > 0:
753                                         self.offset -= 1
754                                 self.marked_pos -= 1
755                 elif key == KEY_LEFT:
756                         self.timeout()
757                         if self.allmarked:
758                                 self.marked_pos = len(self.text)
759                                 self.allmarked = False
760                         else:
761                                 self.marked_pos -= 1
762                 elif key == KEY_RIGHT:
763                         self.timeout()
764                         if self.allmarked:
765                                 self.marked_pos = 0
766                                 self.allmarked = False
767                         else:
768                                 self.marked_pos += 1
769                 elif key == KEY_HOME:
770                         self.timeout()
771                         self.allmarked = False
772                         self.marked_pos = 0
773                 elif key == KEY_END:
774                         self.timeout()
775                         self.allmarked = False
776                         self.marked_pos = len(self.text)
777                 elif key == KEY_TOGGLEOW:
778                         self.timeout()
779                         self.overwrite = not self.overwrite
780                 elif key == KEY_ASCII:
781                         self.timeout()
782                         newChar = unichr(getPrevAsciiCode())
783                         if self.allmarked:
784                                 self.deleteAllChars()
785                                 self.allmarked = False
786                         self.insertChar(newChar, self.marked_pos, False)
787                         self.marked_pos += 1
788                 elif key in KEY_NUMBERS:
789                         owr = self.lastKey == getKeyNumber(key)
790                         newChar = self.getKey(getKeyNumber(key))
791                         if self.allmarked:
792                                 self.deleteAllChars()
793                                 self.allmarked = False
794                         self.insertChar(newChar, self.marked_pos, owr)
795                 elif key == KEY_TIMEOUT:
796                         self.timeout()
797                         if self.help_window:
798                                 self.help_window.update(self)
799                         return
800
801                 if self.help_window:
802                         self.help_window.update(self)
803                 self.validateMarker()
804                 self.changed()
805
806         def nextFunc(self):
807                 self.marked_pos += 1
808                 self.validateMarker()
809                 self.changed()
810
811         def getValue(self):
812                 return self.text.encode("utf-8")
813
814         def setValue(self, val):
815                 try:
816                         self.text = val.decode("utf-8")
817                 except UnicodeDecodeError:
818                         self.text = val.decode("utf-8", "ignore")
819                         print "Broken UTF8!"
820
821         value = property(getValue, setValue)
822         _value = property(getValue, setValue)
823
824         def getText(self):
825                 return self.text.encode("utf-8")
826
827         def getMulti(self, selected):
828                 if self.visible_width:
829                         if self.allmarked:
830                                 mark = range(0, min(self.visible_width, len(self.text)))
831                         else:
832                                 mark = [self.marked_pos-self.offset]
833                         return ("mtext"[1-selected:], self.text[self.offset:self.offset+self.visible_width].encode("utf-8")+" ", mark)
834                 else:
835                         if self.allmarked:
836                                 mark = range(0, len(self.text))
837                         else:
838                                 mark = [self.marked_pos]
839                         return ("mtext"[1-selected:], self.text.encode("utf-8")+" ", mark)
840
841         def onSelect(self, session):
842                 self.allmarked = (self.value != "")
843                 if session is not None:
844                         from Screens.NumericalTextInputHelpDialog import NumericalTextInputHelpDialog
845                         self.help_window = session.instantiateDialog(NumericalTextInputHelpDialog, self)
846                         self.help_window.show()
847
848         def onDeselect(self, session):
849                 self.marked_pos = 0
850                 self.offset = 0
851                 if self.help_window:
852                         session.deleteDialog(self.help_window)
853                         self.help_window = None
854
855         def getHTML(self, id):
856                 return '<input type="text" name="' + id + '" value="' + self.value + '" /><br>\n'
857
858         def unsafeAssign(self, value):
859                 self.value = str(value)
860
861 class ConfigPassword(ConfigText):
862         def __init__(self, default = "", fixed_size = False, visible_width = False, censor = "*"):
863                 ConfigText.__init__(self, default = default, fixed_size = fixed_size, visible_width = visible_width)
864                 self.censor_char = censor
865                 self.hidden = True
866
867         def getMulti(self, selected):
868                 mtext, text, mark = ConfigText.getMulti(self, selected)
869                 if self.hidden:
870                         text = len(text) * self.censor_char
871                 return (mtext, text, mark)
872                         
873         def onSelect(self, session):
874                 ConfigText.onSelect(self, session)
875                 self.hidden = False
876
877         def onDeselect(self, session):
878                 ConfigText.onDeselect(self, session)
879                 self.hidden = True
880
881 class ConfigNumber(ConfigText):
882         def __init__(self, default = 0):
883                 ConfigText.__init__(self, str(default), fixed_size = False)
884
885         def getValue(self):
886                 return int(self.text)
887                 
888         def setValue(self, val):
889                 self.text = str(val)
890
891         value = property(getValue, setValue)
892         _value = property(getValue, setValue)
893
894         def conform(self):
895                 pos = len(self.text) - self.marked_pos
896                 self.text = self.text.lstrip("0")
897                 if self.text == "":
898                         self.text = "0"
899                 if pos > len(self.text):
900                         self.marked_pos = 0
901                 else:
902                         self.marked_pos = len(self.text) - pos
903
904         def handleKey(self, key):
905                 if key in KEY_NUMBERS or key == KEY_ASCII:
906                         if key == KEY_ASCII:
907                                 ascii = getPrevAsciiCode()
908                                 if not (48 <= ascii <= 57):
909                                         return
910                         else:
911                                 ascii = getKeyNumber(key) + 48
912                         newChar = unichr(ascii)
913                         if self.allmarked:
914                                 self.deleteAllChars()
915                                 self.allmarked = False
916                         self.insertChar(newChar, self.marked_pos, False)
917                         self.marked_pos += 1
918                 else:
919                         ConfigText.handleKey(self, key)
920                 self.conform()
921
922         def onSelect(self, session):
923                 self.allmarked = (self.value != "")
924
925         def onDeselect(self, session):
926                 self.marked_pos = 0
927                 self.offset = 0
928
929 class ConfigSearchText(ConfigText):
930         def __init__(self, default = "", fixed_size = False, visible_width = False):
931                 ConfigText.__init__(self, default = default, fixed_size = fixed_size, visible_width = visible_width)
932                 NumericalTextInput.__init__(self, nextFunc = self.nextFunc, handleTimeout = False, search = True)
933
934 class ConfigDirectory(ConfigText):
935         def __init__(self, default="", visible_width=60):
936                 ConfigText.__init__(self, default, fixed_size = True, visible_width = visible_width)
937         def handleKey(self, key):
938                 pass
939         def getValue(self):
940                 if self.text == "":
941                         return None
942                 else:
943                         return ConfigText.getValue(self)
944         def setValue(self, val):
945                 if val == None:
946                         val = ""
947                 ConfigText.setValue(self, val)
948         def getMulti(self, selected):
949                 if self.text == "":
950                         return ("mtext"[1-selected:], _("List of Storage Devices"), range(0))
951                 else:
952                         return ConfigText.getMulti(self, selected)
953
954 # a slider.
955 class ConfigSlider(ConfigElement):
956         def __init__(self, default = 0, increment = 1, limits = (0, 100)):
957                 ConfigElement.__init__(self)
958                 self.value = self.default = default
959                 self.min = limits[0]
960                 self.max = limits[1]
961                 self.increment = increment
962
963         def checkValues(self):
964                 if self.value < self.min:
965                         self.value = self.min
966
967                 if self.value > self.max:
968                         self.value = self.max
969
970         def handleKey(self, key):
971                 if key == KEY_LEFT:
972                         self.value -= self.increment
973                 elif key == KEY_RIGHT:
974                         self.value += self.increment
975                 elif key == KEY_HOME:
976                         self.value = self.min
977                 elif key == KEY_END:
978                         self.value = self.max
979                 else:
980                         return
981                 self.checkValues()
982
983         def getText(self):
984                 return "%d / %d" % (self.value, self.max)
985
986         def getMulti(self, selected):
987                 self.checkValues()
988                 return ("slider", self.value, self.max)
989
990         def fromstring(self, value):
991                 return int(value)
992
993 # a satlist. in fact, it's a ConfigSelection.
994 class ConfigSatlist(ConfigSelection):
995         def __init__(self, list, default = None):
996                 if default is not None:
997                         default = str(default)
998                 ConfigSelection.__init__(self, choices = [(str(orbpos), desc) for (orbpos, desc, flags) in list], default = default)
999
1000         def getOrbitalPosition(self):
1001                 if self.value == "":
1002                         return None
1003                 return int(self.value)
1004         
1005         orbital_position = property(getOrbitalPosition)
1006
1007 class ConfigSet(ConfigElement):
1008         def __init__(self, choices, default = []):
1009                 ConfigElement.__init__(self)
1010                 self.choices = []
1011                 self.description = {}
1012                 if isinstance(choices, list):
1013                         choices.sort()
1014                         for x in choices:
1015                                 if isinstance(x, tuple):
1016                                         self.choices.append(x[0])
1017                                         self.description[x[0]] = str(x[1])
1018                                 else:
1019                                         self.choices.append(x)
1020                                         self.description[x] = str(x)
1021                 else:
1022                         assert False, "ConfigSet choices must be a list!"
1023                 if len(self.choices) == 0:
1024                         self.choices = [""]
1025                         self.description[""] = ""
1026                 if default is None:
1027                         default = []
1028                 self.pos = -1
1029                 default.sort()
1030                 self.default = default
1031                 self.value = default+[]
1032
1033         def toggleChoice(self, choice):
1034                 if choice in self.value:
1035                         self.value.remove(choice)
1036                 else:
1037                         self.value.append(choice)
1038                         self.value.sort()
1039
1040         def handleKey(self, key):
1041                 if key in KEY_NUMBERS + [KEY_DELETE, KEY_BACKSPACE]:
1042                         if self.pos != -1:
1043                                 self.toggleChoice(self.choices[self.pos])
1044                 elif key == KEY_LEFT:
1045                         self.pos -= 1
1046                         if self.pos < -1:
1047                             self.pos = len(self.choices)-1
1048                 elif key == KEY_RIGHT:
1049                         self.pos += 1
1050                         if self.pos >= len(self.choices):
1051                             self.pos = -1
1052                 elif key in [KEY_HOME, KEY_END]:
1053                         self.pos = -1
1054
1055         def genString(self, lst):
1056                 res = ""
1057                 for x in lst:
1058                         res += self.description[x]+" "
1059                 return res
1060
1061         def getText(self):
1062                 return self.genString(self.value)
1063
1064         def getMulti(self, selected):
1065                 if not selected or self.pos == -1:
1066                         return ("text", self.genString(self.value))
1067                 else:
1068                         tmp = self.value+[]
1069                         ch = self.choices[self.pos]
1070                         mem = ch in self.value
1071                         if not mem:
1072                                 tmp.append(ch)
1073                                 tmp.sort()
1074                         ind = tmp.index(ch)
1075                         val1 = self.genString(tmp[:ind])
1076                         val2 = " "+self.genString(tmp[ind+1:])
1077                         if mem:
1078                                 chstr = " "+self.description[ch]+" "
1079                         else:
1080                                 chstr = "("+self.description[ch]+")"
1081                         return ("mtext", val1+chstr+val2, range(len(val1),len(val1)+len(chstr)))
1082
1083         def onDeselect(self, session):
1084                 self.pos = -1
1085                 self.changed()
1086                 
1087         def tostring(self, value):
1088                 return str(value)
1089
1090         def fromstring(self, val):
1091                 return eval(val)
1092
1093 class ConfigLocations(ConfigElement):
1094         def __init__(self, default = [], visible_width = False):
1095                 ConfigElement.__init__(self)
1096                 self.visible_width = visible_width
1097                 self.pos = -1
1098                 self.default = default
1099                 self.locations = []
1100                 self.mountpoints = []
1101                 harddiskmanager.on_partition_list_change.append(self.mountpointsChanged)
1102
1103         def setValue(self, value):
1104                 loc = [x[0] for x in self.locations if x[3]]
1105                 add = [x for x in value if not x in loc]
1106                 diff = add + [x for x in loc if not x in value]
1107                 self.locations = [x for x in self.locations if not x[0] in diff] + [[x, self.getMountpoint(x), True, True] for x in add]
1108                 self.locations.sort(key = lambda x: x[0])
1109                 self.changed()
1110
1111         def getValue(self):
1112                 self.checkChangedMountpoints()
1113                 for x in self.locations:
1114                         x[3] = x[2]
1115                 return [x[0] for x in self.locations if x[3]]
1116         
1117         value = property(getValue, setValue)
1118
1119         def tostring(self, value):
1120                 return str(value)
1121
1122         def fromstring(self, val):
1123                 return eval(val)
1124
1125         def load(self):
1126                 sv = self.saved_value
1127                 if sv is None:
1128                         tmp = self.default
1129                 else:
1130                         tmp = self.fromstring(sv)
1131                 self.locations = [[x, None, False, False] for x in tmp]
1132                 self.refreshMountpoints()
1133                 for x in self.locations:
1134                         if os.path.exists(x[0]):
1135                                 x[1] = self.getMountpoint(x[0])
1136                                 x[2] = True
1137
1138         def save(self):
1139                 if self.save_disabled or self.locations == []:
1140                         self.saved_value = None
1141                 else:
1142                         self.saved_value = self.tostring([x[0] for x in self.locations])
1143
1144         def isChanged(self):
1145                 sv = self.saved_value
1146                 if val is None and self.locations == []:
1147                         return False
1148                 return self.tostring([x[0] for x in self.locations]) != sv
1149
1150         def mountpointsChanged(self, action, dev):
1151                 print "Mounts changed: ", action, dev
1152                 mp = dev.mountpoint+"/"
1153                 if action == "add":
1154                         self.addedMount(mp)
1155                 elif action == "remove":
1156                         self.removedMount(mp)
1157                 self.refreshMountpoints()
1158
1159         def addedMount(self, mp):
1160                 for x in self.locations:
1161                         if x[1] == mp:
1162                                 x[2] = True
1163                         elif x[1] == None and os.path.exists(x[0]):
1164                                 x[1] = self.getMountpoint(x[0])
1165                                 x[2] = True
1166
1167         def removedMount(self, mp):
1168                 for x in self.locations:
1169                         if x[1] == mp:
1170                                 x[2] = False
1171                 
1172         def refreshMountpoints(self):
1173                 self.mountpoints = [p.mountpoint + "/" for p in harddiskmanager.getMountedPartitions() if p.mountpoint != "/"]
1174                 self.mountpoints.sort(key = lambda x: -len(x))
1175
1176         def checkChangedMountpoints(self):
1177                 oldmounts = self.mountpoints
1178                 self.refreshMountpoints()
1179                 if oldmounts == self.mountpoints:
1180                         return
1181                 for x in oldmounts:
1182                         if not x in self.mountpoints:
1183                                 self.removedMount(x)
1184                 for x in self.mountpoints:
1185                         if not x in oldmounts:
1186                                 self.addedMount(x)
1187
1188         def getMountpoint(self, file):
1189                 file = os.path.realpath(file)+"/"
1190                 for m in self.mountpoints:
1191                         if file.startswith(m):
1192                                 return m
1193                 return None
1194
1195         def handleKey(self, key):
1196                 if key == KEY_LEFT:
1197                         self.pos -= 1
1198                         if self.pos < -1:
1199                             self.pos = len(self.value)-1
1200                 elif key == KEY_RIGHT:
1201                         self.pos += 1
1202                         if self.pos >= len(self.value):
1203                             self.pos = -1
1204                 elif key in [KEY_HOME, KEY_END]:
1205                         self.pos = -1
1206
1207         def getText(self):
1208                 return " ".join(self.value)
1209
1210         def getMulti(self, selected):
1211                 if not selected:
1212                         valstr = " ".join(self.value)
1213                         if self.visible_width and len(valstr) > self.visible_width:
1214                                 return ("text", valstr[0:self.visible_width])
1215                         else:
1216                                 return ("text", valstr)
1217                 else:
1218                         i = 0
1219                         valstr = ""
1220                         ind1 = 0
1221                         ind2 = 0
1222                         for val in self.value:
1223                                 if i == self.pos:
1224                                         ind1 = len(valstr)
1225                                 valstr += str(val)+" "
1226                                 if i == self.pos:
1227                                         ind2 = len(valstr)
1228                                 i += 1
1229                         if self.visible_width and len(valstr) > self.visible_width:
1230                                 if ind1+1 < self.visible_width/2:
1231                                         off = 0
1232                                 else:
1233                                         off = min(ind1+1-self.visible_width/2, len(valstr)-self.visible_width)
1234                                 return ("mtext", valstr[off:off+self.visible_width], range(ind1-off,ind2-off))
1235                         else:
1236                                 return ("mtext", valstr, range(ind1,ind2))
1237
1238         def onDeselect(self, session):
1239                 self.pos = -1
1240                 
1241 # nothing.
1242 class ConfigNothing(ConfigSelection):
1243         def __init__(self):
1244                 ConfigSelection.__init__(self, choices = [""])
1245
1246 # until here, 'saved_value' always had to be a *string*.
1247 # now, in ConfigSubsection, and only there, saved_value
1248 # is a dict, essentially forming a tree.
1249 #
1250 # config.foo.bar=True
1251 # config.foobar=False
1252 #
1253 # turns into:
1254 # config.saved_value == {"foo": {"bar": "True"}, "foobar": "False"}
1255 #
1256
1257
1258 class ConfigSubsectionContent(object):
1259         pass
1260
1261 # we store a backup of the loaded configuration
1262 # data in self.stored_values, to be able to deploy
1263 # them when a new config element will be added,
1264 # so non-default values are instantly available
1265
1266 # A list, for example:
1267 # config.dipswitches = ConfigSubList()
1268 # config.dipswitches.append(ConfigYesNo())
1269 # config.dipswitches.append(ConfigYesNo())
1270 # config.dipswitches.append(ConfigYesNo())
1271 class ConfigSubList(list, object):
1272         def __init__(self):
1273                 object.__init__(self)
1274                 list.__init__(self)
1275                 self.stored_values = {}
1276
1277         def save(self):
1278                 for x in self:
1279                         x.save()
1280         
1281         def load(self):
1282                 for x in self:
1283                         x.load()
1284
1285         def getSavedValue(self):
1286                 res = {}
1287                 for i in range(len(self)):
1288                         sv = self[i].saved_value
1289                         if sv is not None:
1290                                 res[str(i)] = sv
1291                 return res
1292
1293         def setSavedValue(self, values):
1294                 self.stored_values = dict(values)
1295                 for (key, val) in self.stored_values.items():
1296                         if int(key) < len(self):
1297                                 self[int(key)].saved_value = val
1298
1299         saved_value = property(getSavedValue, setSavedValue)
1300
1301         def append(self, item):
1302                 i = str(len(self))
1303                 list.append(self, item)
1304                 if i in self.stored_values:
1305                         item.saved_value = self.stored_values[i]
1306                         item.load()
1307
1308         def dict(self):
1309                 res = dict()
1310                 for index in range(len(self)):
1311                         res[str(index)] = self[index]
1312                 return res
1313
1314 # same as ConfigSubList, just as a dictionary.
1315 # care must be taken that the 'key' has a proper
1316 # str() method, because it will be used in the config
1317 # file.
1318 class ConfigSubDict(dict, object):
1319         def __init__(self):
1320                 object.__init__(self)
1321                 dict.__init__(self)
1322                 self.stored_values = {}
1323
1324         def save(self):
1325                 for x in self.values():
1326                         x.save()
1327         
1328         def load(self):
1329                 for x in self.values():
1330                         x.load()
1331
1332         def getSavedValue(self):
1333                 res = {}
1334                 for (key, val) in self.items():
1335                         sv = val.saved_value
1336                         if sv is not None:
1337                                 res[str(key)] = sv
1338                 return res
1339
1340         def setSavedValue(self, values):
1341                 self.stored_values = dict(values)
1342                 for (key, val) in self.items():
1343                         if str(key) in self.stored_values:
1344                                 val = self.stored_values[str(key)]
1345
1346         saved_value = property(getSavedValue, setSavedValue)
1347
1348         def __setitem__(self, key, item):
1349                 dict.__setitem__(self, key, item)
1350                 if str(key) in self.stored_values:
1351                         item.saved_value = self.stored_values[str(key)]
1352                         item.load()
1353
1354         def dict(self):
1355                 return self
1356
1357 # Like the classes above, just with a more "native"
1358 # syntax.
1359 #
1360 # some evil stuff must be done to allow instant
1361 # loading of added elements. this is why this class
1362 # is so complex.
1363 #
1364 # we need the 'content' because we overwrite 
1365 # __setattr__.
1366 # If you don't understand this, try adding
1367 # __setattr__ to a usual exisiting class and you will.
1368 class ConfigSubsection(object):
1369         def __init__(self):
1370                 object.__init__(self)
1371                 self.__dict__["content"] = ConfigSubsectionContent()
1372                 self.content.items = { }
1373                 self.content.stored_values = { }
1374         
1375         def __setattr__(self, name, value):
1376                 if name == "saved_value":
1377                         return self.setSavedValue(value)
1378                 assert isinstance(value, ConfigSubsection) or isinstance(value, ConfigElement) or isinstance(value, ConfigSubList) or isinstance(value, ConfigSubDict), "ConfigSubsections can only store ConfigSubsections, ConfigSubLists, ConfigSubDicts or ConfigElements"
1379                 self.content.items[name] = value
1380                 if name in self.content.stored_values:
1381                         #print "ok, now we have a new item,", name, "and have the following value for it:", self.content.stored_values[name]
1382                         value.saved_value = self.content.stored_values[name]
1383                         value.load()
1384
1385         def __getattr__(self, name):
1386                 return self.content.items[name]
1387
1388         def getSavedValue(self):
1389                 res = self.content.stored_values
1390                 for (key, val) in self.content.items.items():
1391                         sv = val.saved_value
1392                         if sv is not None:
1393                                 res[key] = sv
1394                         elif key in res:
1395                                 del res[key]
1396                 return res
1397
1398         def setSavedValue(self, values):
1399                 values = dict(values)
1400                 
1401                 self.content.stored_values = values
1402
1403                 for (key, val) in self.content.items.items():
1404                         if key in values:
1405                                 val.saved_value = values[key]
1406
1407         saved_value = property(getSavedValue, setSavedValue)
1408
1409         def save(self):
1410                 for x in self.content.items.values():
1411                         x.save()
1412
1413         def load(self):
1414                 for x in self.content.items.values():
1415                         x.load()
1416
1417         def dict(self):
1418                 return self.content.items
1419
1420 # the root config object, which also can "pickle" (=serialize)
1421 # down the whole config tree.
1422 #
1423 # we try to keep non-existing config entries, to apply them whenever
1424 # a new config entry is added to a subsection
1425 # also, non-existing config entries will be saved, so they won't be
1426 # lost when a config entry disappears.
1427 class Config(ConfigSubsection):
1428         def __init__(self):
1429                 ConfigSubsection.__init__(self)
1430
1431         def pickle_this(self, prefix, topickle, result):
1432                 for (key, val) in topickle.items():
1433                         name = prefix + "." + key
1434                         
1435                         if isinstance(val, dict):
1436                                 self.pickle_this(name, val, result)
1437                         elif isinstance(val, tuple):
1438                                 result.append(name + "=" + val[0]) # + " ; " + val[1])
1439                         else:
1440                                 result.append(name + "=" + val)
1441
1442         def pickle(self):
1443                 result = [ ]
1444                 self.pickle_this("config", self.saved_value, result)
1445                 return '\n'.join(result) + "\n"
1446
1447         def unpickle(self, lines):
1448                 tree = { }
1449                 for l in lines:
1450                         if not len(l) or l[0] == '#':
1451                                 continue
1452                         
1453                         n = l.find('=')
1454                         val = l[n+1:].strip()
1455
1456                         names = l[:n].split('.')
1457 #                       if val.find(' ') != -1:
1458 #                               val = val[:val.find(' ')]
1459
1460                         base = tree
1461                         
1462                         for n in names[:-1]:
1463                                 base = base.setdefault(n, {})
1464                         
1465                         base[names[-1]] = val
1466
1467                 # we inherit from ConfigSubsection, so ...
1468                 #object.__setattr__(self, "saved_value", tree["config"])
1469                 if "config" in tree:
1470                         self.setSavedValue(tree["config"])
1471
1472         def saveToFile(self, filename):
1473                 f = open(filename, "w")
1474                 f.write(self.pickle())
1475                 f.close()
1476
1477         def loadFromFile(self, filename):
1478                 f = open(filename, "r")
1479                 self.unpickle(f.readlines())
1480                 f.close()
1481
1482 config = Config()
1483 config.misc = ConfigSubsection()
1484
1485 class ConfigFile:
1486         CONFIG_FILE = resolveFilename(SCOPE_CONFIG, "settings")
1487
1488         def load(self):
1489                 try:
1490                         config.loadFromFile(self.CONFIG_FILE)
1491                 except IOError, e:
1492                         print "unable to load config (%s), assuming defaults..." % str(e)
1493         
1494         def save(self):
1495 #               config.save()
1496                 config.saveToFile(self.CONFIG_FILE)
1497         
1498         def __resolveValue(self, pickles, cmap):
1499                 if cmap.has_key(pickles[0]):
1500                         if len(pickles) > 1:
1501                                 return self.__resolveValue(pickles[1:], cmap[pickles[0]].dict())
1502                         else:
1503                                 return str(cmap[pickles[0]].value)
1504                 return None
1505         
1506         def getResolvedKey(self, key):
1507                 names = key.split('.')
1508                 if len(names) > 1:
1509                         if names[0] == "config":
1510                                 ret=self.__resolveValue(names[1:], config.content.items)
1511                                 if ret and len(ret):
1512                                         return ret
1513                 print "getResolvedKey", key, "failed !! (Typo??)"
1514                 return ""
1515
1516 def NoSave(element):
1517         element.disableSave()
1518         return element
1519
1520 configfile = ConfigFile()
1521
1522 configfile.load()
1523
1524 def getConfigListEntry(*args):
1525         assert len(args) > 1, "getConfigListEntry needs a minimum of two arguments (descr, configElement)"
1526         return args
1527
1528 def updateConfigElement(element, newelement):
1529         newelement.value = element.value
1530         return newelement
1531
1532 #def _(x):
1533 #       return x
1534 #
1535 #config.bla = ConfigSubsection()
1536 #config.bla.test = ConfigYesNo()
1537 #config.nim = ConfigSubList()
1538 #config.nim.append(ConfigSubsection())
1539 #config.nim[0].bla = ConfigYesNo()
1540 #config.nim.append(ConfigSubsection())
1541 #config.nim[1].bla = ConfigYesNo()
1542 #config.nim[1].blub = ConfigYesNo()
1543 #config.arg = ConfigSubDict()
1544 #config.arg["Hello"] = ConfigYesNo()
1545 #
1546 #config.arg["Hello"].handleKey(KEY_RIGHT)
1547 #config.arg["Hello"].handleKey(KEY_RIGHT)
1548 #
1549 ##config.saved_value
1550 #
1551 ##configfile.save()
1552 #config.save()
1553 #print config.pickle()