add possibility to remove timers in epglist
[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.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('/usr/share/enigma2/setup.xml', 'r')
19 setupdom = xml.etree.cElementTree.parse(setupfile)
20 setupfile.close()
21
22 class SetupSummary(Screen):
23         skin = """
24         <screen position="6,0" size="120,64">
25                 <widget name="SetupTitle" position="6,0" size="120,16" font="Regular;12" />
26                 <widget name="SetupEntry" position="6,16" size="120,32" font="Regular;12" />
27                 <widget name="SetupValue" position="6,48" size="120,16" font="Regular;12" />
28         </screen>"""
29
30         def __init__(self, session, parent):
31                 Screen.__init__(self, session)
32                 self["SetupTitle"] = Label(_(parent.setup_title))
33                 self["SetupEntry"] = Label("")
34                 self["SetupValue"] = Label("")
35                 self.parent = parent
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
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["title"] = Label(_(self.setup_title))
86
87                 self["oktext"] = Label(_("OK"))
88                 self["canceltext"] = Label(_("Cancel"))
89                 self["ok"] = Pixmap()
90                 self["cancel"] = Pixmap()
91                 
92                 self["actions"] = NumberActionMap(["SetupActions"], 
93                         {
94                                 "cancel": self.keyCancel,
95                                 "save": self.keySave,
96                         }, -2)
97
98                 ConfigListScreen.__init__(self, list, session = session, on_change = self.changedEntry)
99
100                 self.changedEntry()
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 "unknown setup id '%s'!" % repr(id)