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