Merge branch 'bug_617_default_favlist_handling_fix' 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 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
33                 Screen.__init__(self, session, parent = parent)
34                 self["SetupTitle"] = StaticText(_(parent.setup_title))
35                 self["SetupEntry"] = StaticText("")
36                 self["SetupValue"] = StaticText("")
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.getroot()
67                 for x in xmldata.findall("setup"):
68                         if x.get("key") != self.setup:
69                                 continue
70                         self.addItems(list, x);
71                         self.setup_title = x.get("title", "").encode("UTF-8")
72
73         def __init__(self, session, setup):
74                 Screen.__init__(self, session)
75                 # for the skin: first try a setup_<setupID>, then Setup
76                 self.skinName = ["setup_" + setup, "Setup" ]
77
78                 self.onChangedEntry = [ ]
79
80                 self.setup = setup
81                 list = []
82                 self.refill(list)
83
84                 #check for list.entries > 0 else self.close
85                 self["key_red"] = StaticText(_("Cancel"))
86                 self["key_green"] = StaticText(_("OK"))
87
88                 self["actions"] = NumberActionMap(["SetupActions"], 
89                         {
90                                 "cancel": self.keyCancel,
91                                 "save": self.keySave,
92                         }, -2)
93
94                 ConfigListScreen.__init__(self, list, session = session, on_change = self.changedEntry)
95
96                 self.changedEntry()
97                 self.onLayoutFinish.append(self.layoutFinished)
98
99         def layoutFinished(self):
100                 self.setTitle(_(self.setup_title))
101
102         # for summary:
103         def changedEntry(self):
104                 for x in self.onChangedEntry:
105                         x()
106
107         def getCurrentEntry(self):
108                 return self["config"].getCurrent()[0]
109
110         def getCurrentValue(self):
111                 return str(self["config"].getCurrent()[1].getText())
112
113         def createSummary(self):
114                 return SetupSummary
115
116         def addItems(self, list, parentNode):
117                 for x in parentNode:
118                         if x.tag == 'item':
119                                 item_level = int(x.get("level", 0))
120
121                                 if not self.levelChanged in config.usage.setup_level.notifiers:
122                                         config.usage.setup_level.notifiers.append(self.levelChanged)
123                                         self.onClose.append(self.removeNotifier)
124
125                                 if item_level > config.usage.setup_level.index:
126                                         continue
127
128                                 requires = x.get("requires")
129                                 if requires and not SystemInfo.get(requires, False):
130                                         continue;
131
132                                 item_text = _(x.get("text", "??").encode("UTF-8"))
133                                 b = eval(x.text or "");
134                                 if b == "":
135                                         continue
136                                 #add to configlist
137                                 item = b
138                                 # the first b is the item itself, ignored by the configList.
139                                 # the second one is converted to string.
140                                 if not isinstance(item, ConfigNothing):
141                                         list.append( (item_text, item) )
142
143 def getSetupTitle(id):
144         xmldata = setupdom.getroot()
145         for x in xmldata.findall("setup"):
146                 if x.get("key") == id:
147                         return x.get("title", "").encode("UTF-8")
148         raise SetupError("unknown setup id '%s'!" % repr(id))