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