add summary support for setup
[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 addItems(self, list, childNode):
68                 for x in childNode:
69                         if x.nodeType != xml.dom.minidom.Element.nodeType:
70                                 continue
71                         elif x.tagName == 'item':
72                                 ItemText = _(getValbyAttr(x, "text"))
73                                 b = eval(XMLTools.mergeText(x.childNodes));
74                                 print "item " + ItemText + " " + b.configPath
75                                 if b == "":
76                                         continue
77                                 #add to configlist
78                                 item = b.controlType(b)
79                                 
80                                 # the first b is the item itself, ignored by the configList.
81                                 # the second one is converted to string.
82                                 list.append( (ItemText, item) )
83
84         def handleKey(self, key):
85                 # ignore keys when not enabled
86                 if self["config"].getCurrent()[1].parent.enabled:
87                         self["config"].handleKey(config.key[key])
88                         print self["config"].getCurrent()
89                         self.changedEntry()
90
91         def keyOk(self):
92                 self.handleKey("choseElement")
93
94         def keyLeft(self):
95                 self.handleKey("prevElement")
96
97         def keyRight(self):
98                 self.handleKey("nextElement")
99
100         def keySave(self):
101                 print "save requested"
102                 for x in self["config"].list:
103                         x[1].save()
104                 self.close()
105
106         def keyCancel(self):
107                 print "cancel requested"
108                 for x in self["config"].list:
109                         x[1].cancel()
110                 self.close()
111                 
112         def keyNumberGlobal(self, number):
113                 self.handleKey(str(number))
114
115         def __init__(self, session, setup):
116                 Screen.__init__(self, session)
117
118                 xmldata = setupdom.childNodes[0]
119                 
120                 entries = xmldata.childNodes
121
122                 self.onChangedEntry = [ ]
123                 list = []
124                                 
125                 for x in entries:             #walk through the actual nodelist
126                         if x.nodeType != xml.dom.minidom.Element.nodeType:
127                                 continue
128                         elif x.tagName == 'setup':
129                                 ItemText = getValbyAttr(x, "key")
130                                 if ItemText != setup:
131                                         continue
132                                 self.addItems(list, x.childNodes);
133                                 myTitle = getValbyAttr(x, "title")
134                 
135                 #check for list.entries > 0 else self.close
136                 
137                 self["config"] = ConfigList(list)
138
139                 self.setup_title = myTitle
140                 self["title"] = Label(_(self.setup_title))
141
142                 self["oktext"] = Label(_("OK"))
143                 self["canceltext"] = Label(_("Cancel"))
144                 self["ok"] = Pixmap()
145                 self["cancel"] = Pixmap()
146                 
147                 self["actions"] = NumberActionMap(["SetupActions"], 
148                         {
149                                 "cancel": self.keyCancel,
150                                 "ok": self.keyOk,
151                                 "left": self.keyLeft,
152                                 "right": self.keyRight,
153                                 "save": self.keySave,
154                                 "1": self.keyNumberGlobal,
155                                 "2": self.keyNumberGlobal,
156                                 "3": self.keyNumberGlobal,
157                                 "4": self.keyNumberGlobal,
158                                 "5": self.keyNumberGlobal,
159                                 "6": self.keyNumberGlobal,
160                                 "7": self.keyNumberGlobal,
161                                 "8": self.keyNumberGlobal,
162                                 "9": self.keyNumberGlobal,
163                                 "0": self.keyNumberGlobal
164                         }, -1)
165
166                 self.changedEntry()
167
168         # for summary:
169         def changedEntry(self):
170                 for x in self.onChangedEntry:
171                         x()
172
173         def getCurrentEntry(self):
174                 return self["config"].getCurrent()[0]
175
176         def getCurrentValue(self):
177                 return str(self["config"].getCurrent()[1].parent.value)
178
179         def createSummary(self):
180                 return SetupSummary