commit missing code for pixmaps in mainmenu
[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                 entryID = node.getAttribute("entryID") or "undefined"
121                 x = node.getAttribute("flushConfigOnClose")
122                 if x:
123                         a = boundFunction(self.session.openWithCallback, self.menuClosedWithConfigFlush, Menu, node, node.childNodes)
124                 else:
125                         a = boundFunction(self.session.openWithCallback, self.menuClosed, Menu, node, node.childNodes)
126                 #TODO add check if !empty(node.childNodes)
127                 destList.append((MenuTitle, a, entryID))
128
129         def menuClosedWithConfigFlush(self, *res):
130                 configfile.save()
131                 self.menuClosed(*res)
132
133         def menuClosed(self, *res):
134                 if len(res) and res[0]:
135                         self.close(True)
136
137         def addItem(self, destList, node):
138                 item_text = node.getAttribute("text").encode("UTF-8")
139                 entryID = node.getAttribute("entryID") or "undefined"
140                 for x in node.childNodes:
141                         if x.nodeType != xml.dom.minidom.Element.nodeType:
142                                 continue
143                         elif x.tagName == 'screen':
144                                 module = x.getAttribute("module") or None
145                                 screen = x.getAttribute("screen") or None
146
147                                 if screen is None:
148                                         screen = module
149
150                                 print module, screen
151                                 if module:
152                                         module = "Screens." + module
153                                 else:
154                                         module = ""
155
156                                 # check for arguments. they will be appended to the
157                                 # openDialog call
158                                 args = XMLTools.mergeText(x.childNodes)
159                                 screen += ", " + args
160
161                                 destList.append((_(item_text or "??"), boundFunction(self.runScreen, (module, screen)), entryID))
162                                 return
163                         elif x.tagName == 'code':
164                                 destList.append((_(item_text or "??"), boundFunction(self.execText, XMLTools.mergeText(x.childNodes)), entryID))
165                                 return
166                         elif x.tagName == 'setup':
167                                 id = x.getAttribute("id")
168                                 if item_text == "":
169                                         item_text = _(getSetupTitle(id)) + "..."
170                                 else:
171                                         item_text = _(item_text)
172                                 destList.append((item_text, boundFunction(self.openSetup, id), entryID))
173                                 return
174                 destList.append((item_text, self.nothing, entryID))
175
176
177         def __init__(self, session, parent, childNode):
178                 Screen.__init__(self, session)
179                 
180                 list = []
181                 
182                 menuID = None
183                 for x in childNode:                                             #walk through the actual nodelist
184                         if x.nodeType != xml.dom.minidom.Element.nodeType:
185                             continue
186                         elif x.tagName == 'item':
187                                 self.addItem(list, x)
188                                 count += 1
189                         elif x.tagName == 'menu':
190                                 self.addMenu(list, x)
191                                 count += 1
192                         elif x.tagName == "id":
193                                 menuID = x.getAttribute("val")
194                                 count = 0
195
196                         if menuID is not None:
197                                 # menuupdater?
198                                 if menuupdater.updatedMenuAvailable(menuID):
199                                         for x in menuupdater.getUpdatedMenu(menuID):
200                                                 if x[1] == count:
201                                                         list.append((x[0], boundFunction(self.runScreen, (x[2], x[3] + ", "))))
202                                                         count += 1
203
204                 if menuID is not None:
205                         # plugins
206                         for l in plugins.getPluginsForMenu(menuID):
207                                 list.append((l[0], boundFunction(l[1], self.session)))
208
209                 # for the skin: first try a menu_<menuID>, then Menu
210                 self.skinName = [ ]
211                 if menuID is not None:
212                         self.skinName.append("menu_" + menuID)
213                 self.skinName.append("Menu")
214
215                 self["menu"] = List(list)
216
217                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
218                         {
219                                 "ok": self.okbuttonClick,
220                                 "cancel": self.closeNonRecursive,
221                                 "menu": self.closeRecursive
222                         })
223                 
224                 a = parent.getAttribute("title").encode("UTF-8") or None
225                 if a is None:
226                         a = _(parent.getAttribute("text").encode("UTF-8"))
227                 self["title"] = Header(a)
228                 self.menu_title = a
229
230         def closeNonRecursive(self):
231                 self.close(False)
232
233         def closeRecursive(self):
234                 self.close(True)
235
236         def createSummary(self):
237                 return MenuSummary
238
239 class MainMenu(Menu):
240         #add file load functions for the xml-file
241         
242         def __init__(self, *x):
243                 self.skinName = "Menu"
244                 Menu.__init__(self, *x)