another change of policy (with correct aspect ratio)
[enigma2.git] / lib / python / Screens / Setup.py
1 from Screen import Screen
2 from Components.ActionMap import NumberActionMap
3 from Components.config import config, ConfigNothing
4 from Components.SystemInfo import SystemInfo
5 from Components.ConfigList import ConfigListScreen
6 from Components.Label import Label
7 from Components.Pixmap import Pixmap
8
9 import xml.dom.minidom
10 from skin import elementsWithTag
11
12 from Tools import XMLTools
13
14 # FIXME: use resolveFile!
15 # read the setupmenu
16 try:
17         # first we search in the current path
18         setupfile = file('data/setup.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 class SetupSummary(Screen):
26         skin = """
27         <screen position="6,0" size="120,64">
28                 <widget name="SetupTitle" position="6,0" size="120,16" font="Regular;12" />
29                 <widget name="SetupEntry" position="6,16" size="120,32" font="Regular;12" />
30                 <widget name="SetupValue" position="6,48" size="120,16" font="Regular;12" />
31         </screen>"""
32
33         def __init__(self, session, parent):
34                 Screen.__init__(self, session)
35                 self["SetupTitle"] = Label(_(parent.setup_title))
36                 self["SetupEntry"] = Label("")
37                 self["SetupValue"] = Label("")
38                 self.parent = parent
39                 self.onShow.append(self.addWatcher)
40                 self.onHide.append(self.removeWatcher)
41                 
42         def addWatcher(self):
43                 self.parent.onChangedEntry.append(self.selectionChanged)
44                 self.parent["config"].onSelectionChanged.append(self.selectionChanged)
45                 self.selectionChanged()
46         
47         def removeWatcher(self):
48                 self.parent.onChangedEntry.remove(self.selectionChanged)
49                 self.parent["config"].onSelectionChanged.remove(self.selectionChanged)
50
51         def selectionChanged(self):
52                 self["SetupEntry"].text = self.parent.getCurrentEntry()
53                 self["SetupValue"].text = self.parent.getCurrentValue()
54
55 class Setup(ConfigListScreen, Screen):
56
57         ALLOW_SUSPEND = True
58
59         def removeNotifier(self):
60                 config.usage.setup_level.notifiers.remove(self.levelChanged)
61
62         def levelChanged(self, configElement):
63                 list = []
64                 self.refill(list)
65                 self["config"].setList(list)
66
67         def refill(self, list):
68                 xmldata = setupdom.childNodes[0]
69                 entries = xmldata.childNodes
70                 for x in entries:             #walk through the actual nodelist
71                         if x.nodeType != xml.dom.minidom.Element.nodeType:
72                                 continue
73                         elif x.tagName == 'setup':
74                                 if x.getAttribute("key") != self.setup:
75                                         continue
76                                 self.addItems(list, x.childNodes);
77                                 self.setup_title = x.getAttribute("title").encode("UTF-8")
78
79         def __init__(self, session, setup):
80                 Screen.__init__(self, session)
81
82                 # for the skin: first try a setup_<setupID>, then Setup
83                 self.skinName = ["setup_" + setup, "Setup" ]
84
85                 self.onChangedEntry = [ ]
86
87                 self.setup = setup
88                 list = []
89                 self.refill(list)
90
91                 #check for list.entries > 0 else self.close
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                                 "save": self.keySave,
103                         }, -2)
104
105                 ConfigListScreen.__init__(self, list, session = session, on_change = self.changedEntry)
106
107                 self.changedEntry()
108
109         # for summary:
110         def changedEntry(self):
111                 for x in self.onChangedEntry:
112                         x()
113
114         def getCurrentEntry(self):
115                 return self["config"].getCurrent()[0]
116
117         def getCurrentValue(self):
118                 return str(self["config"].getCurrent()[1].getText())
119
120         def createSummary(self):
121                 return SetupSummary
122
123         def addItems(self, list, childNode):
124                 for x in childNode:
125                         if x.nodeType != xml.dom.minidom.Element.nodeType:
126                                 continue
127                         elif x.tagName == 'item':
128                                 item_level = int(x.getAttribute("level") or "0")
129
130                                 if not self.levelChanged in config.usage.setup_level.notifiers:
131                                         config.usage.setup_level.notifiers.append(self.levelChanged)
132                                         self.onClose.append(self.removeNotifier)
133
134                                 if item_level > config.usage.setup_level.index:
135                                         continue
136
137                                 requires = x.getAttribute("requires")
138                                 if requires and not SystemInfo.get(requires, False):
139                                         continue;
140
141                                 item_text = _(x.getAttribute("text").encode("UTF-8") or "??")
142                                 b = eval(XMLTools.mergeText(x.childNodes));
143                                 if b == "":
144                                         continue
145                                 #add to configlist
146                                 item = b
147                                 # the first b is the item itself, ignored by the configList.
148                                 # the second one is converted to string.
149                                 if not isinstance(item, ConfigNothing):
150                                         list.append( (item_text, item) )
151
152 def getSetupTitle(id):
153         xmldata = setupdom.childNodes[0].childNodes
154         for x in elementsWithTag(xmldata, "setup"):
155                 if x.getAttribute("key") == id:
156                         return x.getAttribute("title").encode("UTF-8")
157         raise "unknown setup id '%s'!" % repr(id)