user predefined vars instead of integers for keyhandling in Config
[enigma2.git] / lib / python / Components / config.py
1 class configFile:
2         def __init__(self):
3                 self.changed = 0
4                 self.configElements = { }
5                 try:
6                         self.file = open("config")
7                 except IOError:
8                         print "cannot open config file"
9                         return 
10                 
11                 while 1:
12                         line = self.file.readline()
13                         if line == "":
14                                 break
15                         
16                         if line.startswith("#"):                #skip comments
17                                 continue        
18                                 
19                         self.addElement(line)
20                 self.file.close()
21
22         def addElement(self, line):
23                 x = line.find("=")
24                 if x > -1:
25                         self.configElements[line[:x]] = line[x + 1:]
26         
27         def getKey(self, key):
28                 return self.configElements[key]
29
30         def setKey(self, key, value):
31                 self.changed = 1
32                 self.configElements[key] = value
33
34         def save(self):
35                 if self.changed == 0:           #no changes, so no write to disk needed
36                         return
37                         
38                 fileHandle = open("config", "w")
39                 
40                 for x in self.configElements:
41                         wstr = x + "=" + self.configElements[x]
42                         
43                         if wstr[len(wstr) - 1] != '\n':
44                                 wstr = wstr + "\n"
45
46                         fileHandle.write(wstr)
47
48                 fileHandle.close()              
49
50 class configBoolean:
51         def __init__(self, parent):
52                 self.parent = parent
53                 
54         def checkValues(self):
55                 if self.parent.value < 0:
56                         self.parent.value = 0   
57
58                 if(self.parent.value >= (len(self.parent.vals) - 1)):
59                         self.parent.value = len(self.parent.vals) - 1
60
61         def cancel(self):
62                 self.parent.reload()
63
64         def save(self):
65                 self.parent.save()
66
67         def handleKey(self, key):
68                 if key == config.prevElement:
69                         self.parent.value = self.parent.value - 1
70                 if key == config.nextElement:
71                         self.parent.value = self.parent.value + 1
72                 
73                 self.checkValues()                      
74
75                 self.parent.change()    
76
77         def __call__(self):                     #needed by configlist
78                 self.checkValues()                      
79                 return ("text", self.parent.vals[self.parent.value])
80
81 class configValue:
82         def __init__(self, obj):
83                 self.obj = obj
84                 
85         def __str__(self):
86                 return self.obj
87
88 class Config:
89         def __init__(self):
90                 self.choseElement = 0
91                 self.prevElement = 1
92                 self.nextElement = 2
93                                                 
94 config = Config();
95 configfile = configFile()
96
97 class ConfigSlider:
98         def __init__(self, parent):
99                 self.parent = parent
100
101         def cancel(self):
102                 self.parent.reload()
103
104         def save(self):
105                 self.parent.save()
106
107         def checkValues(self):
108                 if self.parent.value < 0:
109                         self.parent.value = 0   
110
111                 if self.parent.value > 10:
112                         self.parent.value = 10  
113
114         def handleKey(self, key):
115                 if key == config.prevElement:
116                         self.parent.value = self.parent.value - 1
117                 if key == config.nextElement:
118                         self.parent.value = self.parent.value + 1
119                                         
120                 self.checkValues()      
121                 self.parent.change()    
122
123         def __call__(self):                     #needed by configlist
124                 self.checkValues()      
125                 return ("slider", self.parent.value * 10)
126
127 class ConfigSubsection:
128         def __init__(self):
129                 pass
130
131 class configElement:
132         def datafromFile(self, control, data):
133                 if control == ConfigSlider:
134                         return int(data);
135                 elif control == configBoolean:
136                         return int(data);
137                 else: 
138                         return ""       
139
140         def datatoFile(self, control, data):
141                 if control == ConfigSlider:
142                         return str(data);
143                 elif control == configBoolean:
144                         return str(data);
145                 else: 
146                         return ""       
147
148         def loadData(self):
149                 try:
150                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
151                 except:         
152                         value = ""
153
154                 if value == "":
155                         print "value not found - using default"
156                         self.value = self.defaultValue
157                         self.save()             #add missing value to dict
158                 else:
159                         self.value = value
160
161         def __init__(self, configPath, control, defaultValue, vals):
162                 self.configPath = configPath
163                 self.defaultValue = defaultValue
164                 self.controlType = control
165                 self.vals = vals
166                 self.notifierList = [ ]
167                 self.loadData()         
168         def addNotifier(self, notifier):
169                 self.notifierList.append(notifier);
170                 notifier(self);
171         def change(self):
172                 for notifier in self.notifierList:
173                         notifier(self)
174         def reload(self):
175                 self.loadData()
176         def save(self):
177                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))