show hdd capacity in aboutbox
[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 configSequence:
82         def __init__(self, parent):
83                 self.parent = parent
84                 
85         def checkValues(self):
86                 pass
87 #               if self.parent.value < 0:
88 #                       self.parent.value = 0   
89 #
90 #               if(self.parent.value >= (len(self.parent.vals) - 1)):
91 #                       self.parent.value = len(self.parent.vals) - 1
92 #
93         def cancel(self):
94                 self.parent.reload()
95
96         def save(self):
97                 self.parent.save()
98
99         def handleKey(self, key):
100                 if key == config.prevElement:
101                         self.parent.value = self.parent.value - 1
102                 if key == config.nextElement:
103                         self.parent.value = self.parent.value + 1
104                 
105                 self.checkValues()                      
106
107                 self.parent.change()    
108
109         def __call__(self):                     #needed by configlist
110                 value = ""
111                 for i in self.parent.value:
112                         if (value != ""):
113                                 value += self.parent.vals[0]
114                         value += str(i)
115                 return ("text", value)
116
117 class configValue:
118         def __init__(self, obj):
119                 self.obj = obj
120                 
121         def __str__(self):
122                 return self.obj
123
124 class Config:
125         def __init__(self):
126                 self.choseElement = 0
127                 self.prevElement = 1
128                 self.nextElement = 2
129                                                 
130 config = Config();
131 configfile = configFile()
132
133 class ConfigSlider:
134         def __init__(self, parent):
135                 self.parent = parent
136
137         def cancel(self):
138                 self.parent.reload()
139
140         def save(self):
141                 self.parent.save()
142
143         def checkValues(self):
144                 if self.parent.value < 0:
145                         self.parent.value = 0   
146
147                 if self.parent.value > 10:
148                         self.parent.value = 10  
149
150         def handleKey(self, key):
151                 if key == config.prevElement:
152                         self.parent.value = self.parent.value - 1
153                 if key == config.nextElement:
154                         self.parent.value = self.parent.value + 1
155                                         
156                 self.checkValues()      
157                 self.parent.change()    
158
159         def __call__(self):                     #needed by configlist
160                 self.checkValues()      
161                 return ("slider", self.parent.value * 10)
162
163 class ConfigSubsection:
164         def __init__(self):
165                 pass
166
167 class configElement:
168         def datafromFile(self, control, data):
169                 if control == ConfigSlider:
170                         return int(data);
171                 elif control == configBoolean:
172                         return int(data);
173                 else: 
174                         return ""       
175
176         def datatoFile(self, control, data):
177                 if control == ConfigSlider:
178                         return str(data);
179                 elif control == configBoolean:
180                         return str(data);
181                 else: 
182                         return ""       
183
184         def loadData(self):
185                 try:
186                         value = self.datafromFile(self.controlType, configfile.getKey(self.configPath))
187                 except:         
188                         value = ""
189
190                 if value == "":
191                         print "value not found - using default"
192                         self.value = self.defaultValue
193                         self.save()             #add missing value to dict
194                 else:
195                         self.value = value
196
197         def __init__(self, configPath, control, defaultValue, vals):
198                 self.configPath = configPath
199                 self.defaultValue = defaultValue
200                 self.controlType = control
201                 self.vals = vals
202                 self.notifierList = [ ]
203                 self.loadData()         
204         def addNotifier(self, notifier):
205                 self.notifierList.append(notifier);
206                 notifier(self);
207         def change(self):
208                 for notifier in self.notifierList:
209                         notifier(self)
210         def reload(self):
211                 self.loadData()
212         def save(self):
213                 configfile.setKey(self.configPath, self.datatoFile(self.controlType,self.value))