fix initial menu entry on lcd
[vuplus_dvbapp] / lib / python / Screens / Menu.py
1 from Screen import *
2 from Components.MenuList import MenuList
3 from Components.ActionMap import ActionMap
4 from Components.Header import Header
5 from Components.Button import Button
6 from Components.Label import Label
7 from Components.ProgressBar import ProgressBar
8 from Components.config import configfile
9 from Components.Clock import Clock
10
11 from Tools.Directories import resolveFilename, SCOPE_SKIN
12
13 from enigma import quitMainloop
14
15 import xml.dom.minidom
16 from xml.dom import EMPTY_NAMESPACE
17 from skin import elementsWithTag
18
19 from Screens.Setup import *
20
21 from Tools import XMLTools
22
23 # some screens
24 def doGlobal(screen):
25         screen["clock"] = Clock()
26
27
28 #               <item text="TV-Mode">self.setModeTV()</item>
29 #               <item text="Radio-Mode">self.setModeRadio()</item>
30 #               <item text="File-Mode">self.setModeFile()</item>
31 #               <item text="Scart">self.openDialog(ScartLoopThrough)</item>
32 #                       <item text="Sleep Timer"></item>
33
34
35 # read the menu
36 menufile = file(resolveFilename(SCOPE_SKIN, 'menu.xml'), 'r')
37 mdom = xml.dom.minidom.parseString(menufile.read())
38 menufile.close()
39
40
41 def getValbyAttr(x, attr):
42         for p in range(x.attributes.length):
43                 a = x.attributes.item(p)
44                 attrib = str(a.name)
45                 value = str(a.value)
46                 if attrib == attr:
47                         return value
48                         
49         return ""
50
51 class boundFunction:
52         def __init__(self, fnc, *args):
53                 self.fnc = fnc
54                 self.args = args
55         def __call__(self):
56                 self.fnc(*self.args)
57                 
58 class MenuUpdater:
59         def __init__(self):
60                 self.updatedMenuItems = {}
61         
62         def addMenuItem(self, id, pos, text, module, screen):
63                 if not self.updatedMenuAvailable(id):
64                         self.updatedMenuItems[id] = []
65                 self.updatedMenuItems[id].append([text, pos, module, screen])
66         
67         def delMenuItem(self, id, pos, text, module, screen):
68                 self.updatedMenuItems[id].remove([text, pos, module, screen])
69         
70         def updatedMenuAvailable(self, id):
71                 return self.updatedMenuItems.has_key(id)
72         
73         def getUpdatedMenu(self, id):
74                 return self.updatedMenuItems[id]
75         
76 menuupdater = MenuUpdater()
77
78 class MenuSummary(Screen):
79         skin = """
80         <screen position="0,0" size="132,64">
81                 <widget name="MenuTitle" position="0,4" size="132,21" font="Regular;18" />
82                 <widget name="MenuEntry" position="0,25" size="132,21" font="Regular;16" />
83                 <widget name="Clock" position="50,46" size="82,18" font="Regular;16" />
84         </screen>"""
85
86         def __init__(self, session, parent):
87                 Screen.__init__(self, session)
88                 self["MenuTitle"] = Label(parent.menu_title)
89                 self["MenuEntry"] = Label("")
90                 self["Clock"] = Clock()
91                 parent["menu"].onSelectionChanged.append(self.selectionChanged)
92                 self.parent = parent
93                 self.selectionChanged()
94
95         def selectionChanged(self):
96                 self["MenuEntry"].setText(self.parent["menu"].getCurrent()[0])
97
98 class Menu(Screen):
99         def okbuttonClick(self):
100                 print "okbuttonClick"
101                 selection = self["menu"].getCurrent()
102                 selection[1]()
103
104         def execText(self, text):
105                 exec text
106
107         def runScreen(self, arg):
108                 # arg[0] is the module (as string)
109                 # arg[1] is Screen inside this module 
110                 #        plus possible arguments, as 
111                 #        string (as we want to reference 
112                 #        stuff which is just imported)
113                 # FIXME. somehow
114                 if arg[0] != "":
115                         exec "from " + arg[0] + " import *"
116                         
117                 self.openDialog(*eval(arg[1]))
118
119         def nothing(self):                                                                                                                                      #dummy
120                 pass
121
122         def openDialog(self, *dialog):                          # in every layer needed
123                 self.session.open(*dialog)
124
125         def openSetup(self, dialog):
126                 self.session.openWithCallback(self.menuClosed, Setup, dialog)
127
128         def addMenu(self, destList, node):
129                 MenuTitle = _(getValbyAttr(node, "text"))
130                 if MenuTitle != "":                                                                                                                                     #check for title
131                         x = getValbyAttr(node, "flushConfigOnClose")
132                         if x == "1":
133                                 a = boundFunction(self.session.openWithCallback, self.menuClosedWithConfigFlush, Menu, node, node.childNodes)
134                         else:
135                                 a = boundFunction(self.session.openWithCallback, self.menuClosed, Menu, node, node.childNodes)
136                         #TODO add check if !empty(node.childNodes)
137                         destList.append((MenuTitle, a))
138
139         def menuClosedWithConfigFlush(self, *res):
140                 configfile.save()
141                 self.menuClosed(*res)
142
143         def menuClosed(self, *res):
144                 if len(res) and res[0]:
145                         self.close(True)
146
147         def addItem(self, destList, node):
148                 ItemText = _(getValbyAttr(node, "text"))
149                 if ItemText != "":                                                                                                                                      #check for name
150                         for x in node.childNodes:
151                                 if x.nodeType != xml.dom.minidom.Element.nodeType:
152                                         continue
153                                 elif x.tagName == 'screen':
154                                         module = getValbyAttr(x, "module")
155                                         screen = getValbyAttr(x, "screen")
156
157                                         if len(screen) == 0:
158                                                 screen = module
159
160                                         if module != "":
161                                                 module = "Screens." + module
162                                         
163                                         # check for arguments. they will be appended to the 
164                                         # openDialog call
165                                         args = XMLTools.mergeText(x.childNodes)
166                                         screen += ", " + args
167                                         
168                                         destList.append((ItemText, boundFunction(self.runScreen, (module, screen))))
169                                         return
170                                 elif x.tagName == 'code':
171                                         destList.append((ItemText, boundFunction(self.execText, XMLTools.mergeText(x.childNodes))))
172                                         return
173                                 elif x.tagName == 'setup':
174                                         id = getValbyAttr(x, "id")
175                                         destList.append((ItemText, boundFunction(self.openSetup, id)))
176                                         return
177                         
178                         destList.append((ItemText,self.nothing))
179
180
181         def __init__(self, session, parent, childNode):
182                 Screen.__init__(self, session)
183                 
184                 list = []
185                 menuID = ""
186
187                 menuID = -1
188                 for x in childNode:                                             #walk through the actual nodelist
189                         if x.nodeType != xml.dom.minidom.Element.nodeType:
190                             continue
191                         elif x.tagName == 'item':
192                                 self.addItem(list, x)
193                                 count += 1
194                         elif x.tagName == 'menu':
195                                 self.addMenu(list, x)
196                                 count += 1
197                         elif x.tagName == "id":
198                                 menuID = getValbyAttr(x, "val")
199                                 count = 0
200                         if menuID != -1:
201                                 if menuupdater.updatedMenuAvailable(menuID):
202                                         for x in menuupdater.getUpdatedMenu(menuID):
203                                                 if x[1] == count:
204                                                         list.append((x[0], boundFunction(self.runScreen, (x[2], x[3] + ", "))))
205                                                         count += 1
206
207
208                 self["menu"] = MenuList(list)   
209                                                         
210                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
211                         {
212                                 "ok": self.okbuttonClick,
213                                 "cancel": self.closeNonRecursive,
214                                 "menu": self.closeRecursive
215                         })
216                 
217                 a = getValbyAttr(parent, "title")
218                 if a == "":                                                                                                             #if empty use name
219                         a = _(getValbyAttr(parent, "text"))
220                 self["title"] = Header(a)
221                 self.menu_title = a
222
223         def closeNonRecursive(self):
224                 self.close(False)
225
226         def closeRecursive(self):
227                 self.close(True)
228
229         def createSummary(self):
230                 return MenuSummary
231
232 class MainMenu(Menu):
233         #add file load functions for the xml-file
234         
235         def __init__(self, *x):
236                 Menu.__init__(self, *x)
237                 self.skinName = "Menu"
238
239         def openDialog(self, dialog):
240                 self.session.open(dialog)
241
242         def openSetup(self, dialog):
243                 self.session.open(Setup, dialog)
244
245         def setModeTV(self):
246                 print "set Mode to TV"
247                 pass
248
249         def setModeRadio(self):
250                 print "set Mode to Radio"
251                 pass
252
253         def setModeFile(self):
254                 print "set Mode to File"
255                 pass