4.3.0r12
[enigma2.git] / usr / lib / enigma2 / 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.Sources.StaticText import StaticText
7 from enigma import eEnv
8
9 import xml.etree.cElementTree
10
11 # FIXME: use resolveFile!
12 # read the setupmenu
13 try:
14         # first we search in the current path
15         setupfile = file('data/setup.xml', 'r')
16 except:
17         # if not found in the current path, we use the global datadir-path
18         setupfile = file(eEnv.resolve('${datadir}/enigma2/setup.xml'), 'r')
19 setupdom = xml.etree.cElementTree.parse(setupfile)
20 setupfile.close()
21
22 class SetupError(Exception):
23     def __init__(self, message):
24         self.msg = message
25
26     def __str__(self):
27         return self.msg
28
29 class SetupSummary(Screen):
30
31         def __init__(self, session, parent):
32                 Screen.__init__(self, session, parent = parent)
33                 self["SetupTitle"] = StaticText(_(parent.setup_title) if parent.setup_title else parent["Title"].text)
34                 self["SetupEntry"] = StaticText("")
35                 self["SetupValue"] = StaticText("")
36                 self.onShow.append(self.addWatcher)
37                 self.onHide.append(self.removeWatcher)
38
39         def addWatcher(self):
40                 self.parent.onConfigEntryChanged.append(self.selectionChanged)
41                 self.parent["config"].onSelectionChanged.append(self.selectionChanged)
42                 self.selectionChanged()
43
44         def removeWatcher(self):
45                 self.parent.onConfigEntryChanged.remove(self.selectionChanged)
46                 self.parent["config"].onSelectionChanged.remove(self.selectionChanged)
47
48         def selectionChanged(self):
49                 self["SetupEntry"].text = self.parent.getCurrentEntry()
50                 self["SetupValue"].text = self.parent.getCurrentValue()
51
52 class Setup(ConfigListScreen, Screen):
53
54         ALLOW_SUSPEND = True
55
56         def removeNotifier(self):
57                 config.usage.setup_level.removeNotifier(self.levelChanged)
58
59         def levelChanged(self, configElement):
60                 list = []
61                 self.refill(list)
62                 self["config"].setList(list)
63
64         def refill(self, list):
65                 xmldata = setupdom.getroot()
66                 for x in xmldata.findall("setup"):
67                         if x.get("key") != self.setup:
68                                 continue
69                         self.addItems(list, x);
70                         self.setup_title = x.get("title", "").encode("UTF-8")
71
72         def __init__(self, session, setup):
73                 Screen.__init__(self, session)
74                 # for the skin: first try a setup_<setupID>, then Setup
75                 self.skinName = ["setup_" + setup, "Setup" ]
76                 ConfigListScreen.__init__(self, [], session = session)
77                 self.setup = setup
78                 self.levelChanged(None)
79
80                 #check for list.entries > 0 else self.close
81                 self["key_red"] = StaticText(_("Cancel"))
82                 self["key_green"] = StaticText(_("OK"))
83
84                 self["actions"] = NumberActionMap(["SetupActions"], 
85                         {
86                                 "cancel": self.keyCancel,
87                                 "save": self.keySave,
88                         }, -2)
89
90                 self._changedEntry()
91                 self.onLayoutFinish.append(self.layoutFinished)
92
93         def layoutFinished(self):
94                 self.setTitle(_(self.setup_title))
95
96         def getCurrentEntry(self):
97                 return self["config"].getCurrent()[0]
98
99         def getCurrentValue(self):
100                 return str(self["config"].getCurrent()[1].getText())
101
102         def createSummary(self):
103                 return SetupSummary
104
105         def addItems(self, list, parentNode):
106                 for x in parentNode:
107                         if x.tag == 'item':
108                                 item_level = int(x.get("level", 0))
109
110                                 if not self.levelChanged in config.usage.setup_level.notifiers:
111                                         config.usage.setup_level.addNotifier(self.levelChanged, initial_call = False)
112                                         self.onClose.append(self.removeNotifier)
113
114                                 if item_level > config.usage.setup_level.index:
115                                         continue
116
117                                 requires = x.get("requires")
118                                 if requires and not SystemInfo.get(requires, False):
119                                         continue;
120
121                                 item_text = _(x.get("text", "??").encode("UTF-8"))
122                                 b = eval(x.text or "str('')");
123                                 #add to configlist
124                                 item = b
125                                 # the first b is the item itself, ignored by the configList.
126                                 # the second one is converted to string.
127                                 if not isinstance(item, ConfigNothing):
128                                         if item:
129                                                 entry = (item_text, item)
130                                         else:
131                                                 entry = (item_text,)
132                                         list.append(entry)
133
134 def getSetupTitle(id):
135         xmldata = setupdom.getroot()
136         for x in xmldata.findall("setup"):
137                 if x.get("key") == id:
138                         return x.get("title", "").encode("UTF-8")
139         raise SetupError("unknown setup id '%s'!" % repr(id))