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