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