Merge branch 'experimental' of git.opendreambox.org:/git/enigma2 into experimental
[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.Sources.StaticText import StaticText
7
8 import xml.etree.cElementTree
9
10 # FIXME: use resolveFile!
11 # read the setupmenu
12 try:
13         # first we search in the current path
14         setupfile = file('data/setup.xml', 'r')
15 except:
16         # if not found in the current path, we use the global datadir-path
17         setupfile = file('/usr/share/enigma2/setup.xml', 'r')
18 setupdom = xml.etree.cElementTree.parse(setupfile)
19 setupfile.close()
20
21 class SetupError(Exception):
22     def __init__(self, message):
23         self.msg = message
24
25     def __str__(self):
26         return self.msg
27
28 class SetupSummary(Screen):
29
30         def __init__(self, session, parent):
31
32                 Screen.__init__(self, session, parent = parent)
33                 self["SetupTitle"] = StaticText(_(parent.setup_title))
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.onChangedEntry.append(self.selectionChanged)
41                 self.parent["config"].onSelectionChanged.append(self.selectionChanged)
42                 self.selectionChanged()
43
44         def removeWatcher(self):
45                 self.parent.onChangedEntry.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.notifiers.remove(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
77                 self.onChangedEntry = [ ]
78
79                 self.setup = setup
80                 list = []
81                 self.refill(list)
82
83                 #check for list.entries > 0 else self.close
84                 self["key_red"] = StaticText(_("Cancel"))
85                 self["key_green"] = StaticText(_("OK"))
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                 self.onLayoutFinish.append(self.layoutFinished)
97
98         def layoutFinished(self):
99                 self.setTitle(_(self.setup_title))
100
101         # for summary:
102         def changedEntry(self):
103                 for x in self.onChangedEntry:
104                         x()
105
106         def getCurrentEntry(self):
107                 return self["config"].getCurrent()[0]
108
109         def getCurrentValue(self):
110                 return str(self["config"].getCurrent()[1].getText())
111
112         def createSummary(self):
113                 return SetupSummary
114
115         def addItems(self, list, parentNode):
116                 for x in parentNode:
117                         if x.tag == 'item':
118                                 item_level = int(x.get("level", 0))
119
120                                 if not self.levelChanged in config.usage.setup_level.notifiers:
121                                         config.usage.setup_level.notifiers.append(self.levelChanged)
122                                         self.onClose.append(self.removeNotifier)
123
124                                 if item_level > config.usage.setup_level.index:
125                                         continue
126
127                                 requires = x.get("requires")
128                                 if requires and not SystemInfo.get(requires, False):
129                                         continue;
130
131                                 item_text = _(x.get("text", "??").encode("UTF-8"))
132                                 b = eval(x.text or "");
133                                 if b == "":
134                                         continue
135                                 #add to configlist
136                                 item = b
137                                 # the first b is the item itself, ignored by the configList.
138                                 # the second one is converted to string.
139                                 if not isinstance(item, ConfigNothing):
140                                         list.append( (item_text, item) )
141
142 def getSetupTitle(id):
143         xmldata = setupdom.getroot()
144         for x in xmldata.findall("setup"):
145                 if x.get("key") == id:
146                         return x.get("title", "").encode("UTF-8")
147         raise SetupError("unknown setup id '%s'!" % repr(id))