494d1d882a3e86cd3fb3e1f456a7dbe7e4cb63bc
[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 configSelection:
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, selected):                   #needed by configlist
78                 self.checkValues()
79                 return ("text", self.parent.vals[self.parent.value])
80                 
81 class configSequence:
82         def __init__(self, parent):
83                 self.parent = parent
84                 self.markedPos = 0
85                 
86         def checkValues(self):
87                 maxPos = len(self.parent.value) * self.parent.vals[1] 
88                 print maxPos
89                         
90                 if self.markedPos >= maxPos:
91                         self.markedPos = maxPos - 1
92                 if self.markedPos < 0:
93                         self.markedPos = 0
94                         
95         def cancel(self):
96                 self.parent.reload()
97
98         def save(self):
99                 self.parent.save()
100
101         def handleKey(self, key):
102                 #this will no change anything on the value itself
103                 #so we can handle it here in gui element
104                 if key == config.prevElement:
105                         self.markedPos -= 1
106                 if key == config.nextElement:
107                         self.markedPos += 1
108                 
109                 self.checkValues()                      
110                 
111                 print "markPos:",
112                 print self.markedPos
113
114                 #FIXME: dont call when press left/right
115                 self.parent.change()    
116
117         def __call__(self, selected):                   #needed by configlist
118                 value = ""
119                 mPos = self.markedPos
120                 print mPos
121                 for i in self.parent.value:
122                         if len(value):  #fixme no heading separator possible
123                                 value += self.parent.vals[0]
124                                 if mPos >= len(value) - 1:
125                                         mPos += 1
126                                 
127                         diff =  self.parent.vals[1] - len(str(i))
128                         if diff > 0:
129                                 # if this helps?!
130                                 value += " " * diff
131                         value +=        str(i)
132
133                         # only mark cursor when we are selected
134                         # (this code is heavily ink optimized!)
135                 return ("mtext"[1-selected:], value, [mPos])
136
137 class configValue:
138         def __init__(self, obj):
139                 self.obj = obj
140                 
141         def __str__(self):
142                 return self.obj
143
144 class Config:
145         def __init__(self):
146                 self.choseElement = 0
147                 self.prevElement = 1
148                 self.nextElement = 2
149                                                 
150 config = Config();
151 configfile = configFile()
152
153 class ConfigSlider:
154         def __init__(self, parent):
155                 self.parent = parent
156
157         def cancel(self):
158                 self.parent.reload()
159
160         def save(self):
161                 self.parent.save()
162
163         def checkValues(self):
164                 if self.parent.value < 0:
165                         self.parent.value = 0   
166
167                 if self.parent.value > 10:
168                         self.parent.value = 10  
169
170         def handleKey(self, key):
171                 if key == config.prevElement:
172                         self.parent.value = self.parent.value - 1
173                 if key == config.nextElement:
174                         self.parent.value = self.parent.value + 1
175                                         
176                 self.checkValues()      
177                 self.parent.change()    
178
179         def __call__(self, selected):                   #needed by configlist
180                 self.checkValues()
181                 return ("slider", self.parent.value * 10)
182
183 class ConfigSubsection:
184         def __init__(self):
185                 pass
186
187 class configElement:
188         def datafromFile(self, control, data):
189                 if control == ConfigSlider:
190                         return int(data);
191                 elif control == configSelection:
192                         return int(data);
193                 elif control == configSequence:
194                         list = [ ]
195                         part = data.split(self.vals[0])
196                         for x in part:
197                                 list.append(int(x))
198                         return list
199                 else: 
200                         return ""       
201
202         def datatoFile(self, control, data):
203                 if control == ConfigSlider:
204                         return str(data);
205                 elif control == configSelection:
206                         return str(data);
207                 elif control == configSequence:
208                         value = ((len(data) * ("%d" + self.vals[0]))[0:-1]) % tuple(data)
209 #                       just in case you don't understand the above, here an equivalent:
210 #                       value = ""
211 #                       for i in data:
212 #                               if value !="":
213 #                                       value += self.vals[0]
214 #                               value += str(i)
215                         return value
216                 else: 
217                         return ""       
218
219         def loadData(self):
220                 try:
221                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
222                 except:         
223                         value = ""
224
225                 if value == "":
226                         print "value not found - using default"
227                         self.value = self.defaultValue
228                         self.save()             #add missing value to dict
229                 else:
230                         self.value = value
231
232         def __init__(self, configPath, control, defaultValue, vals):
233                 self.configPath = configPath
234                 self.defaultValue = defaultValue
235                 self.controlType = control
236                 self.vals = vals
237                 self.notifierList = [ ]
238                 self.enabled = True
239                 self.loadData()         
240         def addNotifier(self, notifier):
241                 self.notifierList.append(notifier);
242                 notifier(self);
243         def change(self):
244                 for notifier in self.notifierList:
245                         notifier(self)
246         def reload(self):
247                 self.loadData()
248         def save(self):
249                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))