1390b51a74ad0e99d2f14cbf177829fccae10c4d
[enigma2.git] / 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
11 from Tools.Directories import resolveFilename, SCOPE_SKIN
12
13 from enigma import quitMainloop
14
15 import xml.dom.minidom
16 from xml.dom import EMPTY_NAMESPACE
17 from skin import elementsWithTag
18
19 from Screens.Setup import Setup, getSetupTitle
20
21 from Tools import XMLTools
22
23 #               <item text="TV-Mode">self.setModeTV()</item>
24 #               <item text="Radio-Mode">self.setModeRadio()</item>
25 #               <item text="File-Mode">self.setModeFile()</item>
26 #               <item text="Scart">self.openDialog(ScartLoopThrough)</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="0,4" size="132,21" font="Regular;18" />
66                 <widget name="MenuEntry" position="0,25" size="132,21" font="Regular;16" />
67                 <widget source="CurrentTime" render="Label" position="50,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                 menuID = ""
187
188                 menuID = -1
189                 for x in childNode:                                             #walk through the actual nodelist
190                         if x.nodeType != xml.dom.minidom.Element.nodeType:
191                             continue
192                         elif x.tagName == 'item':
193                                 self.addItem(list, x)
194                                 count += 1
195                         elif x.tagName == 'menu':
196                                 self.addMenu(list, x)
197                                 count += 1
198                         elif x.tagName == "id":
199                                 menuID = x.getAttribute("val")
200                                 count = 0
201                         if menuID != -1:
202                                 if menuupdater.updatedMenuAvailable(menuID):
203                                         for x in menuupdater.getUpdatedMenu(menuID):
204                                                 if x[1] == count:
205                                                         list.append((x[0], boundFunction(self.runScreen, (x[2], x[3] + ", "))))
206                                                         count += 1
207
208
209                 self["menu"] = MenuList(list)   
210                                                         
211                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
212                         {
213                                 "ok": self.okbuttonClick,
214                                 "cancel": self.closeNonRecursive,
215                                 "menu": self.closeRecursive
216                         })
217                 
218                 a = parent.getAttribute("title").encode("UTF-8") or None
219                 if a is None:
220                         a = _(parent.getAttribute("text").encode("UTF-8"))
221                 self["title"] = Header(a)
222                 self.menu_title = a
223
224         def closeNonRecursive(self):
225                 self.close(False)
226
227         def closeRecursive(self):
228                 self.close(True)
229
230         def createSummary(self):
231                 return MenuSummary
232
233 class MainMenu(Menu):
234         #add file load functions for the xml-file
235         
236         def __init__(self, *x):
237                 Menu.__init__(self, *x)
238                 self.skinName = "Menu"
239
240         def openDialog(self, dialog):
241                 self.session.open(dialog)
242
243         def openSetup(self, dialog):
244                 self.session.open(Setup, dialog)
245
246         def setModeTV(self):
247                 print "set Mode to TV"
248                 pass
249
250         def setModeRadio(self):
251                 print "set Mode to Radio"
252                 pass
253
254         def setModeFile(self):
255                 print "set Mode to File"
256                 pass