add auto vcr switching support (needs new drivers (fp.ko))
[vuplus_dvbapp] / lib / python / Screens / Menu.py
1 from Screen import Screen
2 from Components.Sources.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 from Components.Sources.Clock import Clock
10 from Components.PluginComponent import plugins
11
12 from Tools.Directories import resolveFilename, SCOPE_SKIN
13
14 from enigma import quitMainloop
15
16 import xml.dom.minidom
17 from xml.dom import EMPTY_NAMESPACE
18 from skin import elementsWithTag
19
20 from Screens.Setup import Setup, getSetupTitle
21
22 from Tools import XMLTools
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="Sleep Timer"></item>
28
29
30 # read the menu
31 menufile = file(resolveFilename(SCOPE_SKIN, 'menu.xml'), 'r')
32 mdom = xml.dom.minidom.parseString(menufile.read())
33 menufile.close()
34
35 class boundFunction:
36         def __init__(self, fnc, *args):
37                 self.fnc = fnc
38                 self.args = args
39         def __call__(self):
40                 self.fnc(*self.args)
41                 
42 class MenuUpdater:
43         def __init__(self):
44                 self.updatedMenuItems = {}
45         
46         def addMenuItem(self, id, pos, text, module, screen):
47                 if not self.updatedMenuAvailable(id):
48                         self.updatedMenuItems[id] = []
49                 self.updatedMenuItems[id].append([text, pos, module, screen])
50         
51         def delMenuItem(self, id, pos, text, module, screen):
52                 self.updatedMenuItems[id].remove([text, pos, module, screen])
53         
54         def updatedMenuAvailable(self, id):
55                 return self.updatedMenuItems.has_key(id)
56         
57         def getUpdatedMenu(self, id):
58                 return self.updatedMenuItems[id]
59         
60 menuupdater = MenuUpdater()
61
62 class MenuSummary(Screen):
63         skin = """
64         <screen position="0,0" size="132,64">
65                 <widget name="MenuTitle" position="6,4" size="120,21" font="Regular;18" />
66                 <widget name="MenuEntry" position="6,25" size="120,21" font="Regular;16" />
67                 <widget source="CurrentTime" render="Label" position="56,46" size="82,18" font="Regular;16" >
68                         <convert type="ClockToText">WithSeconds</convert>
69                 </widget>
70         </screen>"""
71
72         def __init__(self, session, parent):
73                 Screen.__init__(self, session)
74                 self["MenuTitle"] = Label(parent.menu_title)
75                 self["MenuEntry"] = Label("")
76                 self["CurrentTime"] = Clock()
77                 self.parent = parent
78                 self.onShow.append(self.addWatcher)
79                 self.onHide.append(self.removeWatcher)
80         
81         def addWatcher(self):
82                 self.parent["menu"].onSelectionChanged.append(self.selectionChanged)
83                 self.selectionChanged()
84         
85         def removeWatcher(self):
86                 self.parent["menu"].onSelectionChanged.remove(self.selectionChanged)
87
88         def selectionChanged(self):
89                 self["MenuEntry"].setText(self.parent["menu"].getCurrent()[0])
90
91 class Menu(Screen):
92
93         ALLOW_SUSPEND = True
94
95         def okbuttonClick(self):
96                 print "okbuttonClick"
97                 selection = self["menu"].getCurrent()
98                 selection[1]()
99
100         def execText(self, text):
101                 exec text
102
103         def runScreen(self, arg):
104                 # arg[0] is the module (as string)
105                 # arg[1] is Screen inside this module 
106                 #        plus possible arguments, as 
107                 #        string (as we want to reference 
108                 #        stuff which is just imported)
109                 # FIXME. somehow
110                 if arg[0] != "":
111                         exec "from " + arg[0] + " import *"
112
113                 self.openDialog(*eval(arg[1]))
114
115         def nothing(self):                                                                                                                                      #dummy
116                 pass
117
118         def openDialog(self, *dialog):                          # in every layer needed
119                 self.session.open(*dialog)
120
121         def openSetup(self, dialog):
122                 self.session.openWithCallback(self.menuClosed, Setup, dialog)
123
124         def addMenu(self, destList, node):
125                 MenuTitle = _(node.getAttribute("text").encode("UTF-8") or "??")
126                 x = node.getAttribute("flushConfigOnClose")
127                 if x:
128                         a = boundFunction(self.session.openWithCallback, self.menuClosedWithConfigFlush, Menu, node, node.childNodes)
129                 else:
130                         a = boundFunction(self.session.openWithCallback, self.menuClosed, Menu, node, node.childNodes)
131                 #TODO add check if !empty(node.childNodes)
132                 destList.append((MenuTitle, a))
133
134         def menuClosedWithConfigFlush(self, *res):
135                 configfile.save()
136                 self.menuClosed(*res)
137
138         def menuClosed(self, *res):
139                 if len(res) and res[0]:
140                         self.close(True)
141
142         def addItem(self, destList, node):
143                 item_text = node.getAttribute("text").encode("UTF-8")
144                 for x in node.childNodes:
145                         if x.nodeType != xml.dom.minidom.Element.nodeType:
146                                 continue
147                         elif x.tagName == 'screen':
148                                 module = x.getAttribute("module") or None
149                                 screen = x.getAttribute("screen") or None
150
151                                 if screen is None:
152                                         screen = module
153
154                                 print module, screen
155                                 if module:
156                                         module = "Screens." + module
157                                 else:
158                                         module = ""
159                                 
160                                 # check for arguments. they will be appended to the 
161                                 # openDialog call
162                                 args = XMLTools.mergeText(x.childNodes)
163                                 screen += ", " + args
164                                         
165                                 destList.append((_(item_text or "??"), boundFunction(self.runScreen, (module, screen))))
166                                 return
167                         elif x.tagName == 'code':
168                                 destList.append((_(item_text or "??"), boundFunction(self.execText, XMLTools.mergeText(x.childNodes))))
169                                 return
170                         elif x.tagName == 'setup':
171                                 id = x.getAttribute("id")
172                                 if item_text == "":
173                                         item_text = _(getSetupTitle(id)) + "..."
174                                 else:
175                                         item_text = _(item_text)
176                                 destList.append((item_text, boundFunction(self.openSetup, id)))
177                                 return
178                 
179                 destList.append((item_text,self.nothing))
180
181
182         def __init__(self, session, parent, childNode):
183                 Screen.__init__(self, session)
184                 
185                 list = []
186                 
187                 menuID = None
188                 for x in childNode:                                             #walk through the actual nodelist
189                         if x.nodeType != xml.dom.minidom.Element.nodeType:
190                             continue
191                         elif x.tagName == 'item':
192                                 self.addItem(list, x)
193                                 count += 1
194                         elif x.tagName == 'menu':
195                                 self.addMenu(list, x)
196                                 count += 1
197                         elif x.tagName == "id":
198                                 menuID = x.getAttribute("val")
199                                 count = 0
200
201                         if menuID is not None:
202                                 # menuupdater?
203                                 if menuupdater.updatedMenuAvailable(menuID):
204                                         for x in menuupdater.getUpdatedMenu(menuID):
205                                                 if x[1] == count:
206                                                         list.append((x[0], boundFunction(self.runScreen, (x[2], x[3] + ", "))))
207                                                         count += 1
208
209                 if menuID is not None:
210                         # plugins
211                         for l in plugins.getPluginsForMenu(menuID):
212                                 list.append((l[0], boundFunction(l[1], self.session)))
213
214                 self["menu"] = MenuList(list)   
215                                                         
216                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
217                         {
218                                 "ok": self.okbuttonClick,
219                                 "cancel": self.closeNonRecursive,
220                                 "menu": self.closeRecursive
221                         })
222                 
223                 a = parent.getAttribute("title").encode("UTF-8") or None
224                 if a is None:
225                         a = _(parent.getAttribute("text").encode("UTF-8"))
226                 self["title"] = Header(a)
227                 self.menu_title = a
228
229         def closeNonRecursive(self):
230                 self.close(False)
231
232         def closeRecursive(self):
233                 self.close(True)
234
235         def createSummary(self):
236                 return MenuSummary
237
238 class MainMenu(Menu):
239         #add file load functions for the xml-file
240         
241         def __init__(self, *x):
242                 Menu.__init__(self, *x)
243                 self.skinName = "Menu"
244
245         def openDialog(self, dialog):
246                 self.session.open(dialog)
247
248         def openSetup(self, dialog):
249                 self.session.open(Setup, dialog)
250
251         def setModeTV(self):
252                 print "set Mode to TV"
253                 pass
254
255         def setModeRadio(self):
256                 print "set Mode to Radio"
257                 pass
258
259         def setModeFile(self):
260                 print "set Mode to File"
261                 pass