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