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