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