c6df96a1998c91b71210d1869d0bdc31a7b29ad4
[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
8 import xml.dom.minidom
9 from xml.dom import EMPTY_NAMESPACE
10 from skin import elementsWithTag
11
12 from Tools import XMLTools
13
14 # read the setupmenu
15 try:
16         # first we search in the current path
17         setupfile = file('data/setup.xml', 'r')
18 except:
19         # if not found in the current path, we use the global datadir-path
20         setupfile = file('/usr/share/enigma2/setup.xml', 'r')
21 setupdom = xml.dom.minidom.parseString(setupfile.read())
22 setupfile.close()
23
24 def getValbyAttr(x, attr):
25         for p in range(x.attributes.length):
26                 a = x.attributes.item(p)
27                 attrib = str(a.name)
28                 value = str(a.value)
29                 if attrib == attr:
30                         return value
31         
32         return ""
33
34 class Setup(Screen):
35
36         def addItems(self, list, childNode):
37                 for x in childNode:
38                         if x.nodeType != xml.dom.minidom.Element.nodeType:
39                                 continue
40                         elif x.tagName == 'item':
41                                 ItemText = getValbyAttr(x, "text")
42                                 b = eval(XMLTools.mergeText(x.childNodes));
43                                 print "item " + ItemText + " " + b.configPath
44                                 if b == "":
45                                         continue
46                                 #add to configlist
47                                 item = b.controlType(b)
48                                 
49                                 # the first b is the item itself, ignored by the configList.
50                                 # the second one is converted to string.
51                                 list.append( (ItemText, item) )
52
53         def keyOk(self):
54                 if (self["config"].getCurrent()[1].parent.enabled == True):
55                         self["config"].handleKey(config.key["choseElement"])
56         def keyLeft(self):
57                 if (self["config"].getCurrent()[1].parent.enabled == True):
58                         self["config"].handleKey(config.key["prevElement"])
59         def keyRight(self):
60                 if (self["config"].getCurrent()[1].parent.enabled == True):
61                         self["config"].handleKey(config.key["nextElement"])
62
63         def keySave(self):
64                 print "save requested"
65                 for x in self["config"].list:
66                         x[1].save()
67                 self.close()
68
69         def keyCancel(self):
70                 print "cancel requested"
71                 for x in self["config"].list:
72                         x[1].cancel()
73                 self.close()
74                 
75         def keyNumberGlobal(self, number):
76                 print "You pressed number " + str(number)
77                 if (self["config"].getCurrent()[1].parent.enabled == True):
78                         self["config"].handleKey(config.key[str(number)])
79
80
81         def __init__(self, session, setup):
82                 Screen.__init__(self, session)
83
84                 print "request setup for " + setup
85                 
86                 xmldata = setupdom.childNodes[0]
87                 
88                 entries = xmldata.childNodes
89
90                 list = []
91                                 
92                 for x in entries:             #walk through the actual nodelist
93                         if x.nodeType != xml.dom.minidom.Element.nodeType:
94                                 continue
95                         elif x.tagName == 'setup':
96                                 ItemText = getValbyAttr(x, "key")
97                                 if ItemText != setup:
98                                         continue
99                                 self.addItems(list, x.childNodes);
100                                 myTitle = getValbyAttr(x, "title")
101                 
102                 #check for list.entries > 0 else self.close
103                 
104                 self["config"] = ConfigList(list)
105
106                 self["title"] = Label(myTitle);
107
108                 self["ok"] = Label("OK")
109                 self["cancel"] = Label("Cancel")
110
111                 self["actions"] = NumberActionMap(["SetupActions"], 
112                         {
113                                 "cancel": self.keyCancel,
114                                 "ok": self.keyOk,
115                                 "left": self.keyLeft,
116                                 "right": self.keyRight,
117                                 "save": self.keySave,
118                                 "1": self.keyNumberGlobal,
119                                 "2": self.keyNumberGlobal,
120                                 "3": self.keyNumberGlobal,
121                                 "4": self.keyNumberGlobal,
122                                 "5": self.keyNumberGlobal,
123                                 "6": self.keyNumberGlobal,
124                                 "7": self.keyNumberGlobal,
125                                 "8": self.keyNumberGlobal,
126                                 "9": self.keyNumberGlobal,
127                                 "0": self.keyNumberGlobal
128                         }, -1)