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