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