move __init__ to top
[enigma2.git] / lib / python / Screens / Setup.py
1 from Screen import Screen
2 from Components.ActionMap import NumberActionMap
3 from Components.config import config                            #global config instance
4 from Components.config import configSelection
5 from Components.ConfigList import ConfigList
6 from Components.Label import Label
7 from Components.Pixmap import Pixmap
8
9 import xml.dom.minidom
10 from xml.dom import EMPTY_NAMESPACE
11 from skin import elementsWithTag
12
13 from Tools import XMLTools
14
15 # FIXME: use resolveFile!
16 # read the setupmenu
17 try:
18         # first we search in the current path
19         setupfile = file('data/setup.xml', 'r')
20 except:
21         # if not found in the current path, we use the global datadir-path
22         setupfile = file('/usr/share/enigma2/setup.xml', 'r')
23 setupdom = xml.dom.minidom.parseString(setupfile.read())
24 setupfile.close()
25
26 def getValbyAttr(x, attr):
27         for p in range(x.attributes.length):
28                 a = x.attributes.item(p)
29                 attrib = str(a.name)
30                 value = str(a.value)
31                 if attrib == attr:
32                         return value
33         
34         return ""
35
36 class SetupSummary(Screen):
37         skin = """
38         <screen position="0,0" size="132,64">
39                 <widget name="SetupTitle" position="0,0" size="132,16" font="Regular;12" />
40                 <widget name="SetupEntry" position="0,16" size="132,32" font="Regular;12" />
41                 <widget name="SetupValue" position="0,48" size="132,16" font="Regular;12" />
42         </screen>"""
43
44         def __init__(self, session, parent):
45                 Screen.__init__(self, session)
46                 self["SetupTitle"] = Label(_(parent.setup_title))
47                 self["SetupEntry"] = Label("")
48                 self["SetupValue"] = Label("")
49                 self.parent = parent
50                 self.onShow.append(self.addWatcher)
51                 self.onHide.append(self.removeWatcher)
52                 
53         def addWatcher(self):
54                 self.parent.onChangedEntry.append(self.selectionChanged)
55                 self.parent["config"].onSelectionChanged.append(self.selectionChanged)
56                 self.selectionChanged()
57         
58         def removeWatcher(self):
59                 self.parent.onChangedEntry.remove(self.selectionChanged)
60                 self.parent["config"].onSelectionChanged.remove(self.selectionChanged)
61
62         def selectionChanged(self):
63                 self["SetupEntry"].text = self.parent.getCurrentEntry()
64                 self["SetupValue"].text = self.parent.getCurrentValue()
65
66 class Setup(Screen):
67         def __init__(self, session, setup):
68                 Screen.__init__(self, session)
69
70                 xmldata = setupdom.childNodes[0]
71                 
72                 entries = xmldata.childNodes
73
74                 self.onChangedEntry = [ ]
75                 list = []
76                                 
77                 for x in entries:             #walk through the actual nodelist
78                         if x.nodeType != xml.dom.minidom.Element.nodeType:
79                                 continue
80                         elif x.tagName == 'setup':
81                                 ItemText = getValbyAttr(x, "key")
82                                 if ItemText != setup:
83                                         continue
84                                 self.addItems(list, x.childNodes);
85                                 myTitle = getValbyAttr(x, "title")
86                 
87                 #check for list.entries > 0 else self.close
88                 
89                 self["config"] = ConfigList(list)
90
91                 self.setup_title = myTitle
92                 self["title"] = Label(_(self.setup_title))
93
94                 self["oktext"] = Label(_("OK"))
95                 self["canceltext"] = Label(_("Cancel"))
96                 self["ok"] = Pixmap()
97                 self["cancel"] = Pixmap()
98                 
99                 self["actions"] = NumberActionMap(["SetupActions"], 
100                         {
101                                 "cancel": self.keyCancel,
102                                 "ok": self.keyOk,
103                                 "left": self.keyLeft,
104                                 "right": self.keyRight,
105                                 "save": self.keySave,
106                                 "1": self.keyNumberGlobal,
107                                 "2": self.keyNumberGlobal,
108                                 "3": self.keyNumberGlobal,
109                                 "4": self.keyNumberGlobal,
110                                 "5": self.keyNumberGlobal,
111                                 "6": self.keyNumberGlobal,
112                                 "7": self.keyNumberGlobal,
113                                 "8": self.keyNumberGlobal,
114                                 "9": self.keyNumberGlobal,
115                                 "0": self.keyNumberGlobal
116                         }, -1)
117
118                 self.changedEntry()
119
120         # for summary:
121         def changedEntry(self):
122                 for x in self.onChangedEntry:
123                         x()
124
125         def getCurrentEntry(self):
126                 return self["config"].getCurrent()[0]
127
128         def getCurrentValue(self):
129                 return str(self["config"].getCurrent()[1].parent.value)
130
131         def createSummary(self):
132                 return SetupSummary
133
134         def addItems(self, list, childNode):
135                 for x in childNode:
136                         if x.nodeType != xml.dom.minidom.Element.nodeType:
137                                 continue
138                         elif x.tagName == 'item':
139                                 ItemText = _(getValbyAttr(x, "text"))
140                                 b = eval(XMLTools.mergeText(x.childNodes));
141                                 print "item " + ItemText + " " + b.configPath
142                                 if b == "":
143                                         continue
144                                 #add to configlist
145                                 item = b.controlType(b)
146                                 
147                                 # the first b is the item itself, ignored by the configList.
148                                 # the second one is converted to string.
149                                 list.append( (ItemText, item) )
150
151         def handleKey(self, key):
152                 # ignore keys when not enabled
153                 if self["config"].getCurrent()[1].parent.enabled:
154                         self["config"].handleKey(config.key[key])
155                         print self["config"].getCurrent()
156                         self.changedEntry()
157
158         def keyOk(self):
159                 self.handleKey("choseElement")
160
161         def keyLeft(self):
162                 self.handleKey("prevElement")
163
164         def keyRight(self):
165                 self.handleKey("nextElement")
166
167         def keySave(self):
168                 print "save requested"
169                 for x in self["config"].list:
170                         x[1].save()
171                 self.close()
172
173         def keyCancel(self):
174                 print "cancel requested"
175                 for x in self["config"].list:
176                         x[1].cancel()
177                 self.close()
178                 
179         def keyNumberGlobal(self, number):
180                 self.handleKey(str(number))