loading and saving the cable transponders to lamedb works 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 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                 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                         x = getValbyAttr(node, "flushConfigOnClose")
111                         if x == "1":
112                                 a = boundFunction(self.session.openWithCallback, self.menuClosedWithConfigFlush, Menu, node, node.childNodes)
113                         else:
114                                 a = boundFunction(self.session.openWithCallback, self.menuClosed, Menu, node, node.childNodes)
115                         #TODO add check if !empty(node.childNodes)
116                         destList.append((MenuTitle, a))
117
118         def menuClosedWithConfigFlush(self, *res):
119                 configfile.save()
120                 self.menuClosed(*res)
121
122         def menuClosed(self, *res):
123                 if len(res) and res[0]:
124                         self.close(True)
125
126         def addItem(self, destList, node):
127                 ItemText = _(getValbyAttr(node, "text"))
128                 if ItemText != "":                                                                                                                                      #check for name
129                         for x in node.childNodes:
130                                 if x.nodeType != xml.dom.minidom.Element.nodeType:
131                                         continue
132                                 elif x.tagName == 'screen':
133                                         module = getValbyAttr(x, "module")
134                                         screen = getValbyAttr(x, "screen")
135
136                                         if len(screen) == 0:
137                                                 screen = module
138
139                                         if module != "":
140                                                 module = "Screens." + module
141                                         
142                                         # check for arguments. they will be appended to the 
143                                         # openDialog call
144                                         args = XMLTools.mergeText(x.childNodes)
145                                         screen += ", " + args
146                                         
147                                         destList.append((ItemText, boundFunction(self.runScreen, (module, screen))))
148                                         return
149                                 elif x.tagName == 'code':
150                                         destList.append((ItemText, boundFunction(self.execText, XMLTools.mergeText(x.childNodes))))
151                                         return
152                                 elif x.tagName == 'setup':
153                                         id = getValbyAttr(x, "id")
154                                         destList.append((ItemText, boundFunction(self.openSetup, id)))
155                                         return
156                         
157                         destList.append((ItemText,self.nothing))
158
159
160         def __init__(self, session, parent, childNode):
161                 Screen.__init__(self, session)
162                 
163                 list = []
164                 menuID = ""
165
166                 menuID = -1
167                 for x in childNode:                                             #walk through the actual nodelist
168                         if x.nodeType != xml.dom.minidom.Element.nodeType:
169                             continue
170                         elif x.tagName == 'item':
171                                 self.addItem(list, x)
172                                 count += 1
173                         elif x.tagName == 'menu':
174                                 self.addMenu(list, x)
175                                 count += 1
176                         elif x.tagName == "id":
177                                 menuID = getValbyAttr(x, "val")
178                                 count = 0
179                         if menuID != -1:
180                                 if menuupdater.updatedMenuAvailable(menuID):
181                                         for x in menuupdater.getUpdatedMenu(menuID):
182                                                 if x[1] == count:
183                                                         list.append((x[0], boundFunction(self.runScreen, (x[2], x[3] + ", "))))
184                                                         count += 1
185
186
187                 self["menu"] = MenuList(list)   
188                                                         
189                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
190                         {
191                                 "ok": self.okbuttonClick,
192                                 "cancel": self.closeNonRecursive,
193                                 "menu": self.closeRecursive
194                         })
195                 
196                 a = getValbyAttr(parent, "title")
197                 if a == "":                                                                                                             #if empty use name
198                         a = _(getValbyAttr(parent, "text"))
199                 self["title"] = Header(a)
200
201         def closeNonRecursive(self):
202                 self.close(False)
203
204         def closeRecursive(self):
205                 self.close(True)
206
207 class MainMenu(Menu):
208         #add file load functions for the xml-file
209         
210         def __init__(self, *x):
211                 Menu.__init__(self, *x)
212                 self.skinName = "Menu"
213
214         def openDialog(self, dialog):
215                 self.session.open(dialog)
216
217         def openSetup(self, dialog):
218                 self.session.open(Setup, dialog)
219
220         def setModeTV(self):
221                 print "set Mode to TV"
222                 pass
223
224         def setModeRadio(self):
225                 print "set Mode to Radio"
226                 pass
227
228         def setModeFile(self):
229                 print "set Mode to File"
230                 pass