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