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