raise exception when unknown setup id is searched for
[enigma2.git] / lib / python / Screens / Setup.py
1 from Screen import Screen
2 from Components.ActionMap import NumberActionMap
3 from Components.config import config
4 from Components.ConfigList import ConfigListScreen
5 from Components.Label import Label
6 from Components.Pixmap import Pixmap
7
8 import xml.dom.minidom
9 from skin import elementsWithTag
10
11 from Tools import XMLTools
12
13 # FIXME: use resolveFile!
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 class SetupSummary(Screen):
25         skin = """
26         <screen position="6,0" size="120,64">
27                 <widget name="SetupTitle" position="6,0" size="120,16" font="Regular;12" />
28                 <widget name="SetupEntry" position="6,16" size="120,32" font="Regular;12" />
29                 <widget name="SetupValue" position="6,48" size="120,16" font="Regular;12" />
30         </screen>"""
31
32         def __init__(self, session, parent):
33                 Screen.__init__(self, session)
34                 self["SetupTitle"] = Label(_(parent.setup_title))
35                 self["SetupEntry"] = Label("")
36                 self["SetupValue"] = Label("")
37                 self.parent = parent
38                 self.onShow.append(self.addWatcher)
39                 self.onHide.append(self.removeWatcher)
40                 
41         def addWatcher(self):
42                 self.parent.onChangedEntry.append(self.selectionChanged)
43                 self.parent["config"].onSelectionChanged.append(self.selectionChanged)
44                 self.selectionChanged()
45         
46         def removeWatcher(self):
47                 self.parent.onChangedEntry.remove(self.selectionChanged)
48                 self.parent["config"].onSelectionChanged.remove(self.selectionChanged)
49
50         def selectionChanged(self):
51                 self["SetupEntry"].text = self.parent.getCurrentEntry()
52                 self["SetupValue"].text = self.parent.getCurrentValue()
53
54 class Setup(ConfigListScreen, Screen):
55
56         ALLOW_SUSPEND = True
57
58         def __init__(self, session, setup):
59                 Screen.__init__(self, session)
60
61                 xmldata = setupdom.childNodes[0]
62                 
63                 entries = xmldata.childNodes
64
65                 self.onChangedEntry = [ ]
66                 list = []
67                                 
68                 for x in entries:             #walk through the actual nodelist
69                         if x.nodeType != xml.dom.minidom.Element.nodeType:
70                                 continue
71                         elif x.tagName == 'setup':
72                                 if x.getAttribute("key") != setup:
73                                         continue
74                                 self.addItems(list, x.childNodes);
75                                 myTitle = x.getAttribute("title").encode("UTF-8")
76
77                 #check for list.entries > 0 else self.close
78                 
79                 self.setup_title = myTitle
80                 self["title"] = Label(_(self.setup_title))
81
82                 self["oktext"] = Label(_("OK"))
83                 self["canceltext"] = Label(_("Cancel"))
84                 self["ok"] = Pixmap()
85                 self["cancel"] = Pixmap()
86                 
87                 self["actions"] = NumberActionMap(["SetupActions"], 
88                         {
89                                 "cancel": self.keyCancel,
90                                 "save": self.keySave,
91                         }, -2)
92
93                 ConfigListScreen.__init__(self, list, session = session, on_change = self.changedEntry)
94
95                 self.changedEntry()
96
97         # for summary:
98         def changedEntry(self):
99                 for x in self.onChangedEntry:
100                         x()
101
102         def getCurrentEntry(self):
103                 return self["config"].getCurrent()[0]
104
105         def getCurrentValue(self):
106                 return str(self["config"].getCurrent()[1].getText())
107
108         def createSummary(self):
109                 return SetupSummary
110
111         def addItems(self, list, childNode):
112                 for x in childNode:
113                         if x.nodeType != xml.dom.minidom.Element.nodeType:
114                                 continue
115                         elif x.tagName == 'item':
116                                 item_level = int(x.getAttribute("level") or "0")
117
118                                 if item_level > config.usage.setup_level.index:
119                                         continue
120
121                                 item_text = _(x.getAttribute("text").encode("UTF-8") or "??")
122                                 b = eval(XMLTools.mergeText(x.childNodes));
123                                 if b == "":
124                                         continue
125                                 #add to configlist
126                                 item = b
127                                 # the first b is the item itself, ignored by the configList.
128                                 # the second one is converted to string.
129                                 list.append( (item_text, item) )
130
131 def getSetupTitle(id):
132         xmldata = setupdom.childNodes[0].childNodes
133         for x in elementsWithTag(xmldata, "setup"):
134                 if x.getAttribute("key") == id:
135                         return x.getAttribute("title").encode("UTF-8")
136         raise "unknown setup id '%s'!" % repr(id)