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