use Tools.Directories to get paths
[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, text, module, screen):
61                 if not self.updatedMenuAvailable(id):
62                         self.updatedMenuItems[id] = []
63                 self.updatedMenuItems[id].append([text, module, screen])
64         
65         def delMenuItem(self, id, text, module, screen):
66                 self.updatedMenuItems[id].remove([text, 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.open(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.open, Menu, node, node.childNodes)
111                         #TODO add check if !empty(node.childNodes)
112                         destList.append((MenuTitle, a))
113                 
114         def addItem(self, destList, node):
115                 ItemText = _(getValbyAttr(node, "text"))
116                 if ItemText != "":                                                                                                                                      #check for name
117                         for x in node.childNodes:
118                                 if x.nodeType != xml.dom.minidom.Element.nodeType:
119                                         continue
120                                 elif x.tagName == 'screen':
121                                         module = getValbyAttr(x, "module")
122                                         screen = getValbyAttr(x, "screen")
123
124                                         if len(screen) == 0:
125                                                 screen = module
126
127                                         if module != "":
128                                                 module = "Screens." + module
129                                         
130                                         # check for arguments. they will be appended to the 
131                                         # openDialog call
132                                         args = XMLTools.mergeText(x.childNodes)
133                                         screen += ", " + args
134                                         
135                                         destList.append((ItemText, boundFunction(self.runScreen, (module, screen))))
136                                         return
137                                 elif x.tagName == 'code':
138                                         destList.append((ItemText, boundFunction(self.execText, XMLTools.mergeText(x.childNodes))))
139                                         return
140                                 elif x.tagName == 'setup':
141                                         id = getValbyAttr(x, "id")
142                                         destList.append((ItemText, boundFunction(self.openSetup, id)))
143                                         return
144                         
145                         destList.append((ItemText,self.nothing))
146
147
148         def __init__(self, session, parent, childNode):
149                 Screen.__init__(self, session)
150                 
151                 list = []
152                 menuID = ""
153
154                 for x in childNode:                                                     #walk through the actual nodelist
155                         if x.nodeType != xml.dom.minidom.Element.nodeType:
156                             continue
157                         elif x.tagName == 'item':
158                                 self.addItem(list, x)
159                         elif x.tagName == 'menu':
160                                 self.addMenu(list, x)
161                         elif x.tagName == "id":
162                                 menuID = getValbyAttr(x, "val")
163
164                 if menuupdater.updatedMenuAvailable(menuID):
165                         for x in menuupdater.getUpdatedMenu(menuID):
166                                 list.append((x[0], boundFunction(self.runScreen, (x[1], x[2] + ", "))))
167
168                 self["menu"] = MenuList(list)   
169                                                         
170                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
171                         {
172                                 "ok": self.okbuttonClick,
173                                 "cancel": self.close,
174                                 "menu": self.close
175                         })
176                 
177                 a = getValbyAttr(parent, "title")
178                 if a == "":                                                                                                             #if empty use name
179                         a = _(getValbyAttr(parent, "text"))
180                 self["title"] = Header(a)
181
182 class MainMenu(Menu):
183         #add file load functions for the xml-file
184         
185         def __init__(self, *x):
186                 Menu.__init__(self, *x)
187                 self.skinName = "Menu"
188
189         def openDialog(self, dialog):
190                 self.session.open(dialog)
191
192         def openSetup(self, dialog):
193                 self.session.open(Setup, dialog)
194
195         def setModeTV(self):
196                 print "set Mode to TV"
197                 pass
198
199         def setModeRadio(self):
200                 print "set Mode to Radio"
201                 pass
202
203         def setModeFile(self):
204                 print "set Mode to File"
205                 pass