recursive close with menu key in menu
[enigma2.git] / lib / python / Screens / Menu.py
1 from Screen import *
2 from Components.MenuList import MenuList
3 from Components.ActionMap import ActionMap
4 from Components.Header import Header
5 from Components.Button import Button
6 from Components.Label import Label
7 from Components.ProgressBar import ProgressBar
8
9 from Tools.Directories import resolveFilename, SCOPE_SKIN
10
11 from enigma import quitMainloop
12
13 import xml.dom.minidom
14 from xml.dom import EMPTY_NAMESPACE
15 from skin import elementsWithTag
16
17 from Screens.Setup import *
18
19 from Tools import XMLTools
20
21 # some screens
22 def doGlobal(screen):
23         screen["clock"] = Clock()
24
25
26 #               <item text="TV-Mode">self.setModeTV()</item>
27 #               <item text="Radio-Mode">self.setModeRadio()</item>
28 #               <item text="File-Mode">self.setModeFile()</item>
29 #               <item text="Scart">self.openDialog(ScartLoopThrough)</item>
30 #                       <item text="Sleep Timer"></item>
31
32
33 # read the menu
34 menufile = file(resolveFilename(SCOPE_SKIN, 'menu.xml'), 'r')
35 mdom = xml.dom.minidom.parseString(menufile.read())
36 menufile.close()
37
38
39 def getValbyAttr(x, attr):
40         for p in range(x.attributes.length):
41                 a = x.attributes.item(p)
42                 attrib = str(a.name)
43                 value = str(a.value)
44                 if attrib == attr:
45                         return value
46                         
47         return ""
48
49 class boundFunction:
50         def __init__(self, fnc, *args):
51                 self.fnc = fnc
52                 self.args = args
53         def __call__(self):
54                 self.fnc(*self.args)
55                 
56 class MenuUpdater:
57         def __init__(self):
58                 self.updatedMenuItems = {}
59         
60         def addMenuItem(self, id, pos, text, module, screen):
61                 if not self.updatedMenuAvailable(id):
62                         self.updatedMenuItems[id] = []
63                 self.updatedMenuItems[id].append([text, pos, module, screen])
64         
65         def delMenuItem(self, id, pos, text, module, screen):
66                 self.updatedMenuItems[id].remove([text, pos, module, screen])
67         
68         def updatedMenuAvailable(self, id):
69                 return self.updatedMenuItems.has_key(id)
70         
71         def getUpdatedMenu(self, id):
72                 return self.updatedMenuItems[id]
73         
74 menuupdater = MenuUpdater()
75                 
76 class Menu(Screen):
77         def okbuttonClick(self):
78                 print "okbuttonClick"
79                 selection = self["menu"].getCurrent()
80                 selection[1]()
81
82         def execText(self, text):
83                 exec text
84                 
85         def runScreen(self, arg):
86                 # arg[0] is the module (as string)
87                 # arg[1] is Screen inside this module 
88                 #        plus possible arguments, as 
89                 #        string (as we want to reference 
90                 #        stuff which is just imported)
91                 # FIXME. somehow
92                 print arg
93                 if arg[0] != "":
94                         exec "from " + arg[0] + " import *"
95                         
96                 self.openDialog(*eval(arg[1]))
97
98         def nothing(self):                                                                                                                                      #dummy
99                 pass
100
101         def openDialog(self, *dialog):                          # in every layer needed
102                 self.session.open(*dialog)
103
104         def openSetup(self, dialog):
105                 self.session.openWithCallback(self.menuClosed, Setup, dialog)
106
107         def addMenu(self, destList, node):
108                 MenuTitle = _(getValbyAttr(node, "text"))
109                 if MenuTitle != "":                                                                                                                                     #check for title
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))
113
114         def menuClosed(self, *res):
115                 if len(res) and res[0]:
116                         self.close(True)
117
118         def addItem(self, destList, node):
119                 ItemText = _(getValbyAttr(node, "text"))
120                 if ItemText != "":                                                                                                                                      #check for name
121                         for x in node.childNodes:
122                                 if x.nodeType != xml.dom.minidom.Element.nodeType:
123                                         continue
124                                 elif x.tagName == 'screen':
125                                         module = getValbyAttr(x, "module")
126                                         screen = getValbyAttr(x, "screen")
127
128                                         if len(screen) == 0:
129                                                 screen = module
130
131                                         if module != "":
132                                                 module = "Screens." + module
133                                         
134                                         # check for arguments. they will be appended to the 
135                                         # openDialog call
136                                         args = XMLTools.mergeText(x.childNodes)
137                                         screen += ", " + args
138                                         
139                                         destList.append((ItemText, boundFunction(self.runScreen, (module, screen))))
140                                         return
141                                 elif x.tagName == 'code':
142                                         destList.append((ItemText, boundFunction(self.execText, XMLTools.mergeText(x.childNodes))))
143                                         return
144                                 elif x.tagName == 'setup':
145                                         id = getValbyAttr(x, "id")
146                                         destList.append((ItemText, boundFunction(self.openSetup, id)))
147                                         return
148                         
149                         destList.append((ItemText,self.nothing))
150
151
152         def __init__(self, session, parent, childNode):
153                 Screen.__init__(self, session)
154                 
155                 list = []
156                 menuID = ""
157
158                 menuID = -1
159                 for x in childNode:                                             #walk through the actual nodelist
160                         if x.nodeType != xml.dom.minidom.Element.nodeType:
161                             continue
162                         elif x.tagName == 'item':
163                                 self.addItem(list, x)
164                                 count += 1
165                         elif x.tagName == 'menu':
166                                 self.addMenu(list, x)
167                                 count += 1
168                         elif x.tagName == "id":
169                                 menuID = getValbyAttr(x, "val")
170                                 count = 0
171                         if menuID != -1:
172                                 if menuupdater.updatedMenuAvailable(menuID):
173                                         for x in menuupdater.getUpdatedMenu(menuID):
174                                                 if x[1] == count:
175                                                         list.append((x[0], boundFunction(self.runScreen, (x[2], x[3] + ", "))))
176                                                         count += 1
177
178
179                 self["menu"] = MenuList(list)   
180                                                         
181                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
182                         {
183                                 "ok": self.okbuttonClick,
184                                 "cancel": self.closeNonRecursive,
185                                 "menu": self.closeRecursive
186                         })
187                 
188                 a = getValbyAttr(parent, "title")
189                 if a == "":                                                                                                             #if empty use name
190                         a = _(getValbyAttr(parent, "text"))
191                 self["title"] = Header(a)
192
193         def closeNonRecursive(self):
194                 self.close(False)
195
196         def closeRecursive(self):
197                 self.close(True)
198
199 class MainMenu(Menu):
200         #add file load functions for the xml-file
201         
202         def __init__(self, *x):
203                 Menu.__init__(self, *x)
204                 self.skinName = "Menu"
205
206         def openDialog(self, dialog):
207                 self.session.open(dialog)
208
209         def openSetup(self, dialog):
210                 self.session.open(Setup, dialog)
211
212         def setModeTV(self):
213                 print "set Mode to TV"
214                 pass
215
216         def setModeRadio(self):
217                 print "set Mode to Radio"
218                 pass
219
220         def setModeFile(self):
221                 print "set Mode to File"
222                 pass