fix showing timer symbol in epglist..
[enigma2.git] / mytest.py
1 from Tools import RedirectOutput
2 from enigma import *
3 from tools import *
4
5 from Components.Language import language
6
7 import traceback
8 import Screens.InfoBar
9 from Screens.SimpleSummary import SimpleSummary
10
11 import sys
12 import time
13
14 import ServiceReference
15
16 from Navigation import Navigation
17
18 from skin import readSkin, applyAllAttributes
19
20 from Components.config import configfile
21 from Tools.Directories import InitFallbackFiles, resolveFilename, SCOPE_PLUGINS
22 InitFallbackFiles()
23 eDVBDB.getInstance().reloadBouquets()
24
25 try:
26         import e2reactor
27         e2reactor.install()
28         
29         from twisted.internet import reactor
30         
31         def runReactor():
32                 reactor.run()
33 except ImportError:
34         print "twisted not available"
35         def runReactor():
36                 runMainloop()
37
38 # initialize autorun plugins and plugin menu entries
39 from Components.PluginComponent import plugins
40
41 from Screens.Wizard import wizardManager
42 from Screens.ImageWizard import *
43 from Screens.StartWizard import *
44 from Screens.TutorialWizard import *
45 from Tools.BoundFunction import boundFunction
46 from Plugins.Plugin import PluginDescriptor
47
48 had = dict()
49
50 def dump(dir, p = ""):
51         if isinstance(dir, dict):
52                 for (entry, val) in dir.items():
53                         dump(val, p + "(dict)/" + entry)
54         if hasattr(dir, "__dict__"):
55                 for name, value in dir.__dict__.items():
56                         if not had.has_key(str(value)):
57                                 had[str(value)] = 1
58                                 dump(value, p + "/" + str(name))
59                         else:
60                                 print p + "/" + str(name) + ":" + str(dir.__class__) + "(cycle)"
61         else:
62                 print p + ":" + str(dir)
63
64 # + ":" + str(dir.__class__)
65
66 # display
67
68 class OutputDevice:
69         def create(self, screen): pass
70
71 # display: HTML
72
73 class HTMLOutputDevice(OutputDevice):
74         def create(self, comp):
75                 print comp.produceHTML()
76
77 html = HTMLOutputDevice()
78
79 class GUIOutputDevice(OutputDevice):
80         parent = None
81         def create(self, comp, desktop):
82                 comp.createGUIScreen(self.parent, desktop)
83
84 class Session:
85         def __init__(self, desktop = None, summary_desktop = None, navigation = None):
86                 self.desktop = desktop
87                 self.summary_desktop = summary_desktop
88                 self.nav = navigation
89                 self.delay_timer = eTimer()
90                 self.delay_timer.timeout.get().append(self.processDelay)
91                 
92                 self.current_dialog = None
93                 
94                 self.dialog_stack = [ ]
95                 self.summary_stack = [ ]
96                 self.summary = None
97                 
98                 for p in plugins.getPlugins(PluginDescriptor.WHERE_SESSIONSTART):
99                         p(reason=0, session=self)
100         
101         def processDelay(self):
102                 self.execEnd()
103                 
104                 callback = self.current_dialog.callback
105
106                 retval = self.current_dialog.returnValue
107
108                 if self.current_dialog.isTmp:
109                         self.current_dialog.doClose()
110 #                       dump(self.current_dialog)
111                         del self.current_dialog
112                 else:
113                         del self.current_dialog.callback
114                 
115                 self.popCurrent()
116                 if callback is not None:
117                         callback(*retval)
118
119         def execBegin(self):
120                 c = self.current_dialog
121                 
122                 self.pushSummary()
123
124                 summary = c.createSummary() or SimpleSummary
125                 self.summary = self.instantiateSummaryDialog(summary, c)
126                 self.summary.show()
127
128                 c.addSummary(self.summary)
129                 c.execBegin()
130
131                 # when execBegin opened a new dialog, don't bother showing the old one.
132                 if c == self.current_dialog:
133                         c.show()
134                 
135         def execEnd(self):
136                 self.current_dialog.execEnd()
137                 self.current_dialog.hide()
138                 self.current_dialog.removeSummary(self.summary)
139                 self.popSummary()
140         
141         def create(self, screen, arguments, **kwargs):
142                 # creates an instance of 'screen' (which is a class)
143                 try:
144                         return screen(self, *arguments, **kwargs)
145                 except:
146                         errstr = "Screen %s(%s, %s): %s" % (str(screen), str(arguments), str(kwargs), sys.exc_info()[0])
147                         print errstr
148                         traceback.print_exc(file=sys.stdout)
149                         quitMainloop(5)
150         
151         def instantiateDialog(self, screen, *arguments, **kwargs):
152                 return self.doInstantiateDialog(screen, arguments, kwargs, self.desktop)
153         
154         def instantiateSummaryDialog(self, screen, *arguments, **kwargs):
155                 return self.doInstantiateDialog(screen, arguments, kwargs, self.summary_desktop)
156         
157         def doInstantiateDialog(self, screen, arguments, kwargs, desktop):
158                 # create dialog
159                 
160                 try:
161                         dlg = self.create(screen, arguments, **kwargs)
162                 except:
163                         print 'EXCEPTION IN DIALOG INIT CODE, ABORTING:'
164                         print '-'*60
165                         traceback.print_exc(file=sys.stdout)
166                         quitMainloop(5)
167                         print '-'*60
168                 
169                 if dlg is None:
170                         return
171
172                 # read skin data
173                 readSkin(dlg, None, dlg.skinName, desktop)
174
175                 # create GUI view of this dialog
176                 assert desktop is not None
177                 
178                 z = 0
179                 title = ""
180                 for (key, value) in dlg.skinAttributes:
181                         if key == "zPosition":
182                                 z = int(value)
183                         elif key == "title":
184                                 title = value
185                 
186                 dlg.instance = eWindow(desktop, z)
187                 dlg.title = title
188                 applyAllAttributes(dlg.instance, desktop, dlg.skinAttributes)
189                 gui = GUIOutputDevice()
190                 gui.parent = dlg.instance
191                 gui.create(dlg, desktop)
192                 
193                 return dlg
194          
195         def pushCurrent(self):
196                 if self.current_dialog:
197                         self.dialog_stack.append(self.current_dialog)
198                         self.execEnd()
199         
200         def popCurrent(self):
201                 if len(self.dialog_stack):
202                         self.current_dialog = self.dialog_stack.pop()
203                         self.execBegin()
204                 else:
205                         self.current_dialog = None
206
207         def execDialog(self, dialog):
208                 self.pushCurrent()
209                 self.current_dialog = dialog
210                 self.current_dialog.isTmp = False
211                 self.current_dialog.callback = None # would cause re-entrancy problems.
212                 self.execBegin()
213
214         def openWithCallback(self, callback, screen, *arguments, **kwargs):
215                 dlg = self.open(screen, *arguments, **kwargs)
216                 dlg.callback = callback
217
218         def open(self, screen, *arguments, **kwargs):
219                 self.pushCurrent()
220                 dlg = self.current_dialog = self.instantiateDialog(screen, *arguments, **kwargs)
221                 dlg.isTmp = True
222                 dlg.callback = None
223                 self.execBegin()
224                 return dlg
225
226         def keyEvent(self, code):
227                 print "code " + str(code)
228
229         def close(self, *retval):
230                 self.current_dialog.returnValue = retval
231                 self.delay_timer.start(0, 1)
232
233         def pushSummary(self):
234                 if self.summary is not None:
235                         self.summary.hide()
236                 self.summary_stack.append(self.summary)
237                 self.summary = None
238
239         def popSummary(self):
240                 if self.summary is not None:
241                         self.summary.doClose()
242                 self.summary = self.summary_stack.pop()
243                 if self.summary is not None:
244                         self.summary.show()
245
246 from Screens.Volume import Volume
247 from Screens.Mute import Mute
248 from GlobalActions import globalActionMap
249 from Components.config import ConfigSubsection, configSequence, configElement, configsequencearg
250
251 #TODO .. move this to a own .py file
252 class VolumeControl:
253         """Volume control, handles volUp, volDown, volMute actions and display
254         a corresponding dialog"""
255         def __init__(self, session):
256                 global globalActionMap
257                 globalActionMap.actions["volumeUp"]=self.volUp
258                 globalActionMap.actions["volumeDown"]=self.volDown
259                 globalActionMap.actions["volumeMute"]=self.volMute
260
261                 config.audio = ConfigSubsection()
262                 config.audio.volume = configElement("config.audio.volume", configSequence, [100], configsequencearg.get("INTEGER", (0, 100)))
263
264                 self.volumeDialog = session.instantiateDialog(Volume)
265                 self.muteDialog = session.instantiateDialog(Mute)
266
267                 self.hideVolTimer = eTimer()
268                 self.hideVolTimer.timeout.get().append(self.volHide)
269
270                 vol = config.audio.volume.value[0]
271                 self.volumeDialog.setValue(vol)
272                 eDVBVolumecontrol.getInstance().setVolume(vol, vol)
273
274         def volSave(self):
275                 config.audio.volume.value = eDVBVolumecontrol.getInstance().getVolume()
276                 config.audio.volume.save()
277
278         def     volUp(self):
279                 if (eDVBVolumecontrol.getInstance().isMuted()):
280                         self.volMute()
281                 eDVBVolumecontrol.getInstance().volumeUp()
282                 self.volumeDialog.show()
283                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
284                 self.volSave()
285                 self.hideVolTimer.start(3000, True)
286
287         def     volDown(self):
288                 if (eDVBVolumecontrol.getInstance().isMuted()):
289                         self.volMute()
290                 eDVBVolumecontrol.getInstance().volumeDown()
291                 self.volumeDialog.show()
292                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
293                 self.volSave()
294                 self.hideVolTimer.start(3000, True)
295
296         def volHide(self):
297                 self.volumeDialog.hide()
298
299         def     volMute(self):
300                 eDVBVolumecontrol.getInstance().volumeToggleMute()
301                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
302
303                 if (eDVBVolumecontrol.getInstance().isMuted()):
304                         self.muteDialog.show()
305                 else:
306                         self.muteDialog.hide()
307
308 def runScreenTest():
309         plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
310
311         session = Session(desktop = getDesktop(0), summary_desktop = getDesktop(1), navigation = Navigation())
312         
313         screensToRun = [ ]
314         
315         for p in plugins.getPlugins(PluginDescriptor.WHERE_WIZARD):
316                 screensToRun.append(p.__call__)
317         
318         screensToRun += wizardManager.getWizards()
319         
320         screensToRun.append(Screens.InfoBar.InfoBar)
321
322         def runNextScreen(session, screensToRun, *result):
323                 if result:
324                         quitMainloop(*result)
325                         return
326         
327                 screen = screensToRun[0]
328                 
329                 if len(screensToRun):
330                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen)
331                 else:
332                         session.open(screen)
333         
334         runNextScreen(session, screensToRun)
335         
336         CONNECT(keyPressedSignal(), session.keyEvent)
337         
338         vol = VolumeControl(session)
339         
340         runReactor()
341         
342         configfile.save()
343         
344         from Tools.DreamboxHardware import setFPWakeuptime
345         from time import time
346         nextRecordingTime = session.nav.RecordTimer.getNextRecordingTime()
347         if nextRecordingTime != -1:
348                 if (nextRecordingTime - time() < 330): # no time to switch box back on
349                         setFPWakeuptime(time() + 30) # so switch back on in 30 seconds
350                 else:
351                         setFPWakeuptime(nextRecordingTime - (300))
352         
353         session.nav.shutdown()
354         
355         return 0
356
357 import keymapparser
358 keymapparser.readKeymap()
359 import skin
360 skin.loadSkin(getDesktop(0))
361
362 import Components.InputDevice
363 Components.InputDevice.InitInputDevices()
364
365 import Components.AVSwitch
366 Components.AVSwitch.InitAVSwitch()
367
368 import Components.RecordingConfig
369 Components.RecordingConfig.InitRecordingConfig()
370
371 import Components.UsageConfig
372 Components.UsageConfig.InitUsageConfig()
373
374 import Components.Network
375 Components.Network.InitNetwork()
376
377 import Components.Lcd
378 Components.Lcd.InitLcd()
379
380 import Components.SetupDevices
381 Components.SetupDevices.InitSetupDevices()
382
383 import Components.RFmod
384 Components.RFmod.InitRFmod()
385
386 import Components.NimManager
387
388 # first, setup a screen
389 try:
390         runScreenTest()
391
392         plugins.shutdown()
393 except:
394         print 'EXCEPTION IN PYTHON STARTUP CODE:'
395         print '-'*60
396         traceback.print_exc(file=sys.stdout)
397         quitMainloop(5)
398         print '-'*60