use setup.xml for the menu instead of an inline string
[enigma2.git] / lib / python / Screens / Setup.py
1 from Screen import Screen
2 from Components.ActionMap import ActionMap
3 from Components.config import config                            #global config instance
4 from Components.config import configEntry
5 from Components.config import configBoolean
6 from Components.ConfigList import ConfigList
7 from Components.Label import Label
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 # read the setupmenu
16 try:
17         # first we search in the current path
18         setupfile = file('data/menu.xml', 'r')
19 except:
20         # if not found in the current path, we use the global datadir-path
21         setupfile = file('/usr/share/enigma2/setup.xml', 'r')
22 setupdom = xml.dom.minidom.parseString(setupfile.read())
23 setupfile.close()
24
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 Setup(Screen):
37
38         def addItems(self, list, childNode):
39                 for x in childNode:
40                         if x.nodeType != xml.dom.minidom.Element.nodeType:
41                                 continue
42                         elif x.tagName == 'item':
43                                 ItemText = getValbyAttr(x, "text")
44                                 b = eval(XMLTools.mergeText(x.childNodes));
45                                 print "item " + ItemText + " " + b.configPath
46                                 if b == "":
47                                         continue
48                                 #add to configlist
49                                 item = b.controlType(b)
50                                 
51                                 # the first b is the item itself, ignored by the configList.
52                                 # the second one is converted to string.
53                                 list.append( (ItemText, item) )
54
55         def keyOk(self):
56                 self["config"].handleKey(0)
57         def keyLeft(self):
58                 self["config"].handleKey(1)
59         def keyRight(self):
60                 self["config"].handleKey(2)
61
62         def keySave(self):
63                 print "save requested"
64                 for x in self["config"]:
65                         selection =     self["config"].getCurrent()
66                         selection.save()
67
68         def __init__(self, session, setup):
69                 Screen.__init__(self, session)
70
71                 print "request setup for " + setup
72                 
73                 xmldata = setupdom.childNodes[0]
74                 
75                 entries = xmldata.childNodes
76
77                 list = []
78                                 
79                 for x in entries:             #walk through the actual nodelist
80                         if x.nodeType != xml.dom.minidom.Element.nodeType:
81                                 continue
82                         elif x.tagName == 'setup':
83                                 ItemText = getValbyAttr(x, "key")
84                                 if ItemText != setup:
85                                         continue
86                                 self.addItems(list, x.childNodes);
87                 
88                 #check for list.entries > 0 else self.close
89                 
90                 self["config"] = ConfigList(list)
91
92                 self["ok"] = Label("OK")
93                 self["cancel"] = Label("Cancel")
94
95                 self["actions"] = ActionMap(["SetupActions"], 
96                         {
97                                 "cancel": self.close,
98                                 "ok": self.keyOk,
99                                 "left": self.keyLeft,
100                                 "right": self.keyRight,
101                                 "save": self.keySave
102                         })