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