PicturePlayer/plugin.py: - center screens where needed.
[enigma2.git] / 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 and not SystemInfo.get(requires, False):
101                         return
102                 MenuTitle = _(node.get("text", "??").encode("UTF-8"))
103                 entryID = node.get("entryID", "undefined")
104                 weight = node.get("weight", 50)
105                 x = node.get("flushConfigOnClose")
106                 if x:
107                         a = boundFunction(self.session.openWithCallback, self.menuClosedWithConfigFlush, Menu, node)
108                 else:
109                         a = boundFunction(self.session.openWithCallback, self.menuClosed, Menu, node)
110                 #TODO add check if !empty(node.childNodes)
111                 destList.append((MenuTitle, a, entryID, weight))
112
113         def menuClosedWithConfigFlush(self, *res):
114                 configfile.save()
115                 self.menuClosed(*res)
116
117         def menuClosed(self, *res):
118                 if res and res[0]:
119                         self.close(True)
120
121         def addItem(self, destList, node):
122                 requires = node.get("requires")
123                 if requires and not SystemInfo.get(requires, False):
124                         return
125                 item_text = node.get("text", "").encode("UTF-8")
126                 entryID = node.get("entryID", "undefined")
127                 weight = node.get("weight", 50)
128                 for x in node:
129                         if x.tag == 'screen':
130                                 module = x.get("module")
131                                 screen = x.get("screen")
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 = x.text or ""
145                                 screen += ", " + args
146
147                                 destList.append((_(item_text or "??"), boundFunction(self.runScreen, (module, screen)), entryID, weight))
148                                 return
149                         elif x.tag == 'code':
150                                 destList.append((_(item_text or "??"), boundFunction(self.execText, x.text), entryID, weight))
151                                 return
152                         elif x.tag == 'setup':
153                                 id = x.get("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):
164                 Screen.__init__(self, session)
165                 
166                 list = []
167                 
168                 menuID = None
169                 for x in parent:                                                #walk through the actual nodelist
170                         if x.tag == 'item':
171                                 item_level = int(x.get("level", 0))
172                                 if item_level <= config.usage.setup_level.index:
173                                         self.addItem(list, x)
174                                         count += 1
175                         elif x.tag == 'menu':
176                                 self.addMenu(list, x)
177                                 count += 1
178                         elif x.tag == "id":
179                                 menuID = x.get("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                                 # check if a plugin overrides an existing menu
194                                 plugin_menuid = l[2]
195                                 for x in list:
196                                         if x[2] == plugin_menuid:
197                                                 list.remove(x)
198                                                 break
199                                 list.append((l[0], boundFunction(l[1], self.session), l[2], l[3] or 50))
200
201                 # for the skin: first try a menu_<menuID>, then Menu
202                 self.skinName = [ ]
203                 if menuID is not None:
204                         self.skinName.append("menu_" + menuID)
205                 self.skinName.append("Menu")
206
207                 # Sort by Weight
208                 list.sort(key=lambda x: int(x[3]))
209
210                 self["menu"] = List(list)
211
212                 self["actions"] = NumberActionMap(["OkCancelActions", "MenuActions", "NumberActions"],
213                         {
214                                 "ok": self.okbuttonClick,
215                                 "cancel": self.closeNonRecursive,
216                                 "menu": self.closeRecursive,
217                                 "1": self.keyNumberGlobal,
218                                 "2": self.keyNumberGlobal,
219                                 "3": self.keyNumberGlobal,
220                                 "4": self.keyNumberGlobal,
221                                 "5": self.keyNumberGlobal,
222                                 "6": self.keyNumberGlobal,
223                                 "7": self.keyNumberGlobal,
224                                 "8": self.keyNumberGlobal,
225                                 "9": self.keyNumberGlobal
226                         })
227
228                 a = parent.get("title", "").encode("UTF-8") or None
229                 a = a and _(a)
230                 if a is None:
231                         a = _(parent.get("text", "").encode("UTF-8"))
232                 self["title"] = StaticText(a)
233                 self.menu_title = a
234
235         def keyNumberGlobal(self, number):
236                 print "menu keyNumber:", number
237                 # Calculate index
238                 number -= 1
239
240                 if len(self["menu"].list) > number:
241                         self["menu"].setIndex(number)
242                         self.okbuttonClick()
243
244         def closeNonRecursive(self):
245                 self.close(False)
246
247         def closeRecursive(self):
248                 self.close(True)
249
250         def createSummary(self):
251                 return MenuSummary
252
253 class MainMenu(Menu):
254         #add file load functions for the xml-file
255         
256         def __init__(self, *x):
257                 self.skinName = "Menu"
258                 Menu.__init__(self, *x)