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