simplify summary code by using parent.menu+stringListSelection to display current...
[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 source="parent.menu" render="Label" position="6,25" size="120,21" font="Regular;16">
61                         <convert type="StringListSelection" />
62                 </widget>
63                 <widget source="global.CurrentTime" render="Label" position="56,46" size="82,18" font="Regular;16" >
64                         <convert type="ClockToText">WithSeconds</convert>
65                 </widget>
66         </screen>"""
67
68         def __init__(self, session, parent):
69                 Screen.__init__(self, session, parent)
70                 self["MenuTitle"] = Label(parent.menu_title)
71
72 class Menu(Screen):
73
74         ALLOW_SUSPEND = True
75
76         def okbuttonClick(self):
77                 print "okbuttonClick"
78                 selection = self["menu"].getCurrent()
79                 selection[1]()
80
81         def execText(self, text):
82                 exec text
83
84         def runScreen(self, arg):
85                 # arg[0] is the module (as string)
86                 # arg[1] is Screen inside this module 
87                 #        plus possible arguments, as 
88                 #        string (as we want to reference 
89                 #        stuff which is just imported)
90                 # FIXME. somehow
91                 if arg[0] != "":
92                         exec "from " + arg[0] + " import *"
93
94                 self.openDialog(*eval(arg[1]))
95
96         def nothing(self): #dummy
97                 pass
98
99         def openDialog(self, *dialog):                          # in every layer needed
100                 self.session.openWithCallback(self.menuClosed, *dialog)
101
102         def openSetup(self, dialog):
103                 self.session.openWithCallback(self.menuClosed, Setup, dialog)
104
105         def addMenu(self, destList, node):
106                 MenuTitle = _(node.getAttribute("text").encode("UTF-8") or "??")
107                 entryID = node.getAttribute("entryID") or "undefined"
108                 x = node.getAttribute("flushConfigOnClose")
109                 if x:
110                         a = boundFunction(self.session.openWithCallback, self.menuClosedWithConfigFlush, Menu, node, node.childNodes)
111                 else:
112                         a = boundFunction(self.session.openWithCallback, self.menuClosed, Menu, node, node.childNodes)
113                 #TODO add check if !empty(node.childNodes)
114                 destList.append((MenuTitle, a, entryID))
115
116         def menuClosedWithConfigFlush(self, *res):
117                 configfile.save()
118                 self.menuClosed(*res)
119
120         def menuClosed(self, *res):
121                 if len(res) and res[0]:
122                         self.close(True)
123
124         def addItem(self, destList, node):
125                 item_text = node.getAttribute("text").encode("UTF-8")
126                 entryID = node.getAttribute("entryID") or "undefined"
127                 for x in node.childNodes:
128                         if x.nodeType != xml.dom.minidom.Element.nodeType:
129                                 continue
130                         elif x.tagName == 'screen':
131                                 module = x.getAttribute("module") or None
132                                 screen = x.getAttribute("screen") or None
133
134                                 if screen is None:
135                                         screen = module
136
137                                 print module, screen
138                                 if module:
139                                         module = "Screens." + module
140                                 else:
141                                         module = ""
142
143                                 # check for arguments. they will be appended to the
144                                 # openDialog call
145                                 args = XMLTools.mergeText(x.childNodes)
146                                 screen += ", " + args
147
148                                 destList.append((_(item_text or "??"), boundFunction(self.runScreen, (module, screen)), entryID))
149                                 return
150                         elif x.tagName == 'code':
151                                 destList.append((_(item_text or "??"), boundFunction(self.execText, XMLTools.mergeText(x.childNodes)), entryID))
152                                 return
153                         elif x.tagName == 'setup':
154                                 id = x.getAttribute("id")
155                                 if item_text == "":
156                                         item_text = _(getSetupTitle(id)) + "..."
157                                 else:
158                                         item_text = _(item_text)
159                                 destList.append((item_text, boundFunction(self.openSetup, id), entryID))
160                                 return
161                 destList.append((item_text, self.nothing, entryID))
162
163
164         def __init__(self, session, parent, childNode):
165                 Screen.__init__(self, session)
166                 
167                 list = []
168                 
169                 menuID = None
170                 for x in childNode:                                             #walk through the actual nodelist
171                         if x.nodeType != xml.dom.minidom.Element.nodeType:
172                             continue
173                         elif x.tagName == 'item':
174                                 self.addItem(list, x)
175                                 count += 1
176                         elif x.tagName == 'menu':
177                                 self.addMenu(list, x)
178                                 count += 1
179                         elif x.tagName == "id":
180                                 menuID = x.getAttribute("val")
181                                 count = 0
182
183                         if menuID is not None:
184                                 # menuupdater?
185                                 if menuupdater.updatedMenuAvailable(menuID):
186                                         for x in menuupdater.getUpdatedMenu(menuID):
187                                                 if x[1] == count:
188                                                         list.append((x[0], boundFunction(self.runScreen, (x[2], x[3] + ", "))))
189                                                         count += 1
190
191                 if menuID is not None:
192                         # plugins
193                         for l in plugins.getPluginsForMenu(menuID):
194                                 list.append((l[0], boundFunction(l[1], self.session)))
195
196                 # for the skin: first try a menu_<menuID>, then Menu
197                 self.skinName = [ ]
198                 if menuID is not None:
199                         self.skinName.append("menu_" + menuID)
200                 self.skinName.append("Menu")
201
202                 self["menu"] = List(list)
203
204                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
205                         {
206                                 "ok": self.okbuttonClick,
207                                 "cancel": self.closeNonRecursive,
208                                 "menu": self.closeRecursive
209                         })
210                 
211                 a = parent.getAttribute("title").encode("UTF-8") or None
212                 if a is None:
213                         a = _(parent.getAttribute("text").encode("UTF-8"))
214                 self["title"] = Header(a)
215                 self.menu_title = a
216
217         def closeNonRecursive(self):
218                 self.close(False)
219
220         def closeRecursive(self):
221                 self.close(True)
222
223         def createSummary(self):
224                 return MenuSummary
225
226 class MainMenu(Menu):
227         #add file load functions for the xml-file
228         
229         def __init__(self, *x):
230                 self.skinName = "Menu"
231                 Menu.__init__(self, *x)