do not delete timer.. (sefault in some situations)
[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
9 from enigma import quitMainloop
10
11 import xml.dom.minidom
12 from xml.dom import EMPTY_NAMESPACE
13 from skin import elementsWithTag
14
15 from Screens.Setup import *
16
17 from Tools import XMLTools
18
19 # some screens
20 def doGlobal(screen):
21         screen["clock"] = Clock()
22
23
24 #               <item text="TV-Mode">self.setModeTV()</item>
25 #               <item text="Radio-Mode">self.setModeRadio()</item>
26 #               <item text="File-Mode">self.setModeFile()</item>
27 #               <item text="Scart">self.openDialog(ScartLoopThrough)</item>
28 #                       <item text="Sleep Timer"></item>
29
30
31 # read the menu
32 try:
33         # first we search in the current path
34         menufile = file('data/menu.xml', 'r')
35 except IOError:
36         # if not found in the current path, we use the global datadir-path
37         menufile = file('/usr/share/enigma2/menu.xml', 'r')
38 mdom = xml.dom.minidom.parseString(menufile.read())
39 menufile.close()
40
41
42 def getValbyAttr(x, attr):
43         for p in range(x.attributes.length):
44                 a = x.attributes.item(p)
45                 attrib = str(a.name)
46                 value = str(a.value)
47                 if attrib == attr:
48                         return value
49                         
50         return ""
51
52 class boundFunction:
53         def __init__(self, fnc, *args):
54                 self.fnc = fnc
55                 self.args = args
56         def __call__(self):
57                 self.fnc(*self.args)
58
59 class Menu(Screen):
60         def okbuttonClick(self):
61                 print "okbuttonClick"
62                 selection = self["menu"].getCurrent()
63                 selection[1]()
64
65         def execText(self, text):
66                 exec text
67                 
68         def runScreen(self, arg):
69                 # arg[0] is the module (as string)
70                 # arg[1] is Screen inside this module 
71                 #        plus possible arguments, as 
72                 #        string (as we want to reference 
73                 #        stuff which is just imported)
74                 # FIXME. somehow.
75                 if arg[0] != "":
76                         exec "from Screens." + arg[0] + " import *"
77                 
78                 self.openDialog(*eval(arg[1]))
79
80         def nothing(self):                                                                                                                                      #dummy
81                 pass
82
83         def openDialog(self, *dialog):                          # in every layer needed
84                 self.session.open(*dialog)
85
86         def openSetup(self, dialog):
87                 self.session.open(Setup, dialog)
88
89         def addMenu(self, destList, node):
90                 MenuTitle = _(getValbyAttr(node, "text"))
91                 if MenuTitle != "":                                                                                                                                     #check for title
92                         a = boundFunction(self.session.open, Menu, node, node.childNodes)
93                         #TODO add check if !empty(node.childNodes)
94                         destList.append((MenuTitle, a))
95                 
96         def addItem(self, destList, node):
97                 ItemText = _(getValbyAttr(node, "text"))
98                 if ItemText != "":                                                                                                                                      #check for name
99                         for x in node.childNodes:
100                                 if x.nodeType != xml.dom.minidom.Element.nodeType:
101                                         continue
102                                 elif x.tagName == 'screen':
103                                         module = getValbyAttr(x, "module")
104                                         screen = getValbyAttr(x, "screen")
105
106                                         if len(screen) == 0:
107                                                 screen = module
108                                         
109                                         # check for arguments. they will be appended to the 
110                                         # openDialog call
111                                         args = XMLTools.mergeText(x.childNodes)
112                                         screen += ", " + args
113                                         
114                                         destList.append((ItemText, boundFunction(self.runScreen, (module, screen))))
115                                         return
116                                 elif x.tagName == 'code':
117                                         destList.append((ItemText, boundFunction(self.execText, XMLTools.mergeText(x.childNodes))))
118                                         return
119                                 elif x.tagName == 'setup':
120                                         id = getValbyAttr(x, "id")
121                                         destList.append((ItemText, boundFunction(self.openSetup, id)))
122                                         return
123                         
124                         destList.append((ItemText,self.nothing))
125
126
127         def __init__(self, session, parent, childNode):
128                 Screen.__init__(self, session)
129                 
130                 list = []
131
132                 for x in childNode:                                                     #walk through the actual nodelist
133                         if x.nodeType != xml.dom.minidom.Element.nodeType:
134                             continue
135                         elif x.tagName == 'item':
136                                 self.addItem(list, x)
137                         elif x.tagName == 'menu':
138                                 self.addMenu(list, x)
139
140                 self["menu"] = MenuList(list)   
141                                                         
142                 self["actions"] = ActionMap(["OkCancelActions"], 
143                         {
144                                 "ok": self.okbuttonClick,
145                                 "cancel": self.close
146                         })
147                 
148                 a = getValbyAttr(parent, "title")
149                 if a == "":                                                                                                             #if empty use name
150                         a = _(getValbyAttr(parent, "text"))
151                 self["title"] = Header(a)
152
153 class MainMenu(Menu):
154         #add file load functions for the xml-file
155         
156         def __init__(self, *x):
157                 Menu.__init__(self, *x)
158                 self.skinName = "Menu"
159
160         def openDialog(self, dialog):
161                 self.session.open(dialog)
162
163         def openSetup(self, dialog):
164                 self.session.open(Setup, dialog)
165
166         def setModeTV(self):
167                 print "set Mode to TV"
168                 pass
169
170         def setModeRadio(self):
171                 print "set Mode to Radio"
172                 pass
173
174         def setModeFile(self):
175                 print "set Mode to File"
176                 pass