add parental control setup to the start wizard
[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 def setEPGLanguage():
8         print "language set to", language.getLanguage()
9         eServiceEvent.setEPGLanguage(language.getLanguage())
10         
11 language.addCallback(setEPGLanguage)
12
13 import traceback
14 import Screens.InfoBar
15 from Screens.SimpleSummary import SimpleSummary
16
17 import sys
18 import time
19
20 import ServiceReference
21
22 from Components.ParentalControl import InitParentalControl
23 InitParentalControl()
24
25 from Navigation import Navigation
26
27 from skin import readSkin, applyAllAttributes
28
29 from Tools.Directories import InitFallbackFiles, resolveFilename, SCOPE_PLUGINS, SCOPE_SKIN_IMAGE
30 from Components.config import config, ConfigText, configfile, ConfigSubsection, ConfigInteger
31 InitFallbackFiles()
32 eDVBDB.getInstance().reloadBouquets()
33
34 config.misc.radiopic = ConfigText(default = resolveFilename(SCOPE_SKIN_IMAGE)+"radio.mvi")
35
36 try:
37         import e2reactor
38         e2reactor.install()
39         
40         import twisted.python.runtime
41         twisted.python.runtime.platform.supportsThreads = lambda: False
42         
43         from twisted.internet import reactor
44         
45         def runReactor():
46                 reactor.run()
47 except ImportError:
48         print "twisted not available"
49         def runReactor():
50                 runMainloop()
51
52 # initialize autorun plugins and plugin menu entries
53 from Components.PluginComponent import plugins
54
55 from Screens.Wizard import wizardManager
56 from Screens.ImageWizard import *
57 from Screens.StartWizard import *
58 from Screens.TutorialWizard import *
59 from Tools.BoundFunction import boundFunction
60 from Plugins.Plugin import PluginDescriptor
61
62 had = dict()
63
64 def dump(dir, p = ""):
65         if isinstance(dir, dict):
66                 for (entry, val) in dir.items():
67                         dump(val, p + "(dict)/" + entry)
68         if hasattr(dir, "__dict__"):
69                 for name, value in dir.__dict__.items():
70                         if not had.has_key(str(value)):
71                                 had[str(value)] = 1
72                                 dump(value, p + "/" + str(name))
73                         else:
74                                 print p + "/" + str(name) + ":" + str(dir.__class__) + "(cycle)"
75         else:
76                 print p + ":" + str(dir)
77
78 # + ":" + str(dir.__class__)
79
80 # display
81
82 class OutputDevice:
83         def create(self, screen): pass
84
85 # display: HTML
86
87 class HTMLOutputDevice(OutputDevice):
88         def create(self, comp):
89                 print comp.produceHTML()
90
91 html = HTMLOutputDevice()
92
93 class GUIOutputDevice(OutputDevice):
94         parent = None
95         def create(self, comp, desktop):
96                 comp.createGUIScreen(self.parent, desktop)
97
98 # Session.open:
99 # * push current active dialog ('current_dialog') onto stack
100 # * call execEnd for this dialog
101 #   * clear in_exec flag
102 #   * hide screen
103 # * instantiate new dialog into 'current_dialog'
104 #   * create screens, components
105 #   * read, apply skin
106 #   * create GUI for screen
107 # * call execBegin for new dialog
108 #   * set in_exec
109 #   * show gui screen
110 #   * call components' / screen's onExecBegin
111 # ... screen is active, until it calls 'close'...
112 # Session.close:
113 # * assert in_exec
114 # * save return value
115 # * start deferred close handler ('onClose')
116 # * execEnd
117 #   * clear in_exec
118 #   * hide screen
119 # .. a moment later:
120 # Session.doClose:
121 # * destroy screen
122
123 class Session:
124         def __init__(self, desktop = None, summary_desktop = None, navigation = None):
125                 self.desktop = desktop
126                 self.summary_desktop = summary_desktop
127                 self.nav = navigation
128                 self.delay_timer = eTimer()
129                 self.delay_timer.timeout.get().append(self.processDelay)
130                 
131                 self.current_dialog = None
132                 
133                 self.dialog_stack = [ ]
134                 self.summary_stack = [ ]
135                 self.summary = None
136                 
137                 self.in_exec = False
138                 
139                 for p in plugins.getPlugins(PluginDescriptor.WHERE_SESSIONSTART):
140                         p(reason=0, session=self)
141         
142         def processDelay(self):
143                 callback = self.current_dialog.callback
144
145                 retval = self.current_dialog.returnValue
146
147                 if self.current_dialog.isTmp:
148                         self.current_dialog.doClose()
149 #                       dump(self.current_dialog)
150                         del self.current_dialog
151                 else:
152                         del self.current_dialog.callback
153                 
154                 self.popCurrent()
155                 if callback is not None:
156                         callback(*retval)
157
158         def execBegin(self, first=True):
159                 assert not self.in_exec 
160                 self.in_exec = True
161                 c = self.current_dialog
162                 
163                 # when this is an execbegin after a execend of a "higher" dialog,
164                 # popSummary already did the right thing.
165                 if first:
166                         self.pushSummary()
167                         summary = c.createSummary() or SimpleSummary
168                         self.summary = self.instantiateSummaryDialog(summary, c)
169                         self.summary.show()
170                         c.addSummary(self.summary)
171
172                 c.execBegin()
173
174                 # when execBegin opened a new dialog, don't bother showing the old one.
175                 if c == self.current_dialog:
176                         c.show()
177                 
178         def execEnd(self, last=True):
179                 assert self.in_exec
180                 self.in_exec = False
181
182                 self.current_dialog.execEnd()
183                 self.current_dialog.hide()
184                 
185                 if last:
186                         self.current_dialog.removeSummary(self.summary)
187                         self.popSummary()
188         
189         def create(self, screen, arguments, **kwargs):
190                 # creates an instance of 'screen' (which is a class)
191                 try:
192                         return screen(self, *arguments, **kwargs)
193                 except:
194                         errstr = "Screen %s(%s, %s): %s" % (str(screen), str(arguments), str(kwargs), sys.exc_info()[0])
195                         print errstr
196                         traceback.print_exc(file=sys.stdout)
197                         quitMainloop(5)
198         
199         def instantiateDialog(self, screen, *arguments, **kwargs):
200                 return self.doInstantiateDialog(screen, arguments, kwargs, self.desktop)
201         
202         def deleteDialog(self, screen):
203                 screen.hide()
204                 screen.doClose()
205         
206         def instantiateSummaryDialog(self, screen, *arguments, **kwargs):
207                 return self.doInstantiateDialog(screen, arguments, kwargs, self.summary_desktop)
208         
209         def doInstantiateDialog(self, screen, arguments, kwargs, desktop):
210                 # create dialog
211                 
212                 try:
213                         dlg = self.create(screen, arguments, **kwargs)
214                 except:
215                         print 'EXCEPTION IN DIALOG INIT CODE, ABORTING:'
216                         print '-'*60
217                         traceback.print_exc(file=sys.stdout)
218                         quitMainloop(5)
219                         print '-'*60
220                 
221                 if dlg is None:
222                         return
223
224                 # read skin data
225                 readSkin(dlg, None, dlg.skinName, desktop)
226
227                 # create GUI view of this dialog
228                 assert desktop is not None
229                 
230                 z = 0
231                 title = ""
232                 for (key, value) in dlg.skinAttributes:
233                         if key == "zPosition":
234                                 z = int(value)
235                         elif key == "title":
236                                 title = value
237                 
238                 dlg.instance = eWindow(desktop, z)
239                 dlg.title = title
240                 applyAllAttributes(dlg.instance, desktop, dlg.skinAttributes)
241                 gui = GUIOutputDevice()
242                 gui.parent = dlg.instance
243                 gui.create(dlg, desktop)
244                 
245                 return dlg
246          
247         def pushCurrent(self):
248                 if self.current_dialog is not None:
249                         self.dialog_stack.append(self.current_dialog)
250                         self.execEnd(last=False)
251         
252         def popCurrent(self):
253                 if len(self.dialog_stack):
254                         self.current_dialog = self.dialog_stack.pop()
255                         self.execBegin(first=False)
256                 else:
257                         self.current_dialog = None
258
259         def execDialog(self, dialog):
260                 self.pushCurrent()
261                 self.current_dialog = dialog
262                 self.current_dialog.isTmp = False
263                 self.current_dialog.callback = None # would cause re-entrancy problems.
264                 self.execBegin()
265
266         def openWithCallback(self, callback, screen, *arguments, **kwargs):
267                 dlg = self.open(screen, *arguments, **kwargs)
268                 dlg.callback = callback
269                 return dlg
270
271         def open(self, screen, *arguments, **kwargs):
272                 if len(self.dialog_stack) and not self.in_exec:
273                         raise "modal open are allowed only from a screen which is modal!"
274                         # ...unless it's the very first screen.
275                 
276                 self.pushCurrent()
277                 dlg = self.current_dialog = self.instantiateDialog(screen, *arguments, **kwargs)
278                 dlg.isTmp = True
279                 dlg.callback = None
280                 self.execBegin()
281                 return dlg
282
283         def close(self, screen, *retval):
284                 if not self.in_exec:
285                         print "close after exec!"
286                         return
287                 
288                 # be sure that the close is for the right dialog!
289                 # if it's not, you probably closed after another dialog
290                 # was opened. this can happen if you open a dialog
291                 # onExecBegin, and forget to do this only once.
292                 # after close of the top dialog, the underlying will
293                 # gain focus again (for a short time), thus triggering
294                 # the onExec, which opens the dialog again, closing the loop.
295                 assert screen == self.current_dialog
296                 
297                 self.current_dialog.returnValue = retval
298                 self.delay_timer.start(0, 1)
299                 self.execEnd()
300
301         def pushSummary(self):
302                 if self.summary is not None:
303                         self.summary.hide()
304                 self.summary_stack.append(self.summary)
305                 self.summary = None
306
307         def popSummary(self):
308                 if self.summary is not None:
309                         self.summary.doClose()
310                 self.summary = self.summary_stack.pop()
311                 if self.summary is not None:
312                         self.summary.show()
313
314 from Screens.Volume import Volume
315 from Screens.Mute import Mute
316 from GlobalActions import globalActionMap
317
318 #TODO .. move this to a own .py file
319 class VolumeControl:
320         """Volume control, handles volUp, volDown, volMute actions and display
321         a corresponding dialog"""
322         def __init__(self, session):
323                 global globalActionMap
324                 globalActionMap.actions["volumeUp"]=self.volUp
325                 globalActionMap.actions["volumeDown"]=self.volDown
326                 globalActionMap.actions["volumeMute"]=self.volMute
327
328                 config.audio = ConfigSubsection()
329                 config.audio.volume = ConfigInteger(default = 100, limits = (0, 100))
330
331                 self.volumeDialog = session.instantiateDialog(Volume)
332                 self.muteDialog = session.instantiateDialog(Mute)
333
334                 self.hideVolTimer = eTimer()
335                 self.hideVolTimer.timeout.get().append(self.volHide)
336
337                 vol = config.audio.volume.value
338                 print "volume is", vol
339                 self.volumeDialog.setValue(vol)
340                 eDVBVolumecontrol.getInstance().setVolume(vol, vol)
341
342         def volSave(self):
343                 config.audio.volume.value = eDVBVolumecontrol.getInstance().getVolume()
344                 config.audio.volume.save()
345
346         def     volUp(self):
347                 if (eDVBVolumecontrol.getInstance().isMuted()):
348                         self.volMute()
349                 eDVBVolumecontrol.getInstance().volumeUp()
350                 self.volumeDialog.show()
351                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
352                 self.volSave()
353                 self.hideVolTimer.start(3000, True)
354
355         def     volDown(self):
356                 if (eDVBVolumecontrol.getInstance().isMuted()):
357                         self.volMute()
358                 eDVBVolumecontrol.getInstance().volumeDown()
359                 self.volumeDialog.show()
360                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
361                 self.volSave()
362                 self.hideVolTimer.start(3000, True)
363
364         def volHide(self):
365                 self.volumeDialog.hide()
366
367         def     volMute(self):
368                 eDVBVolumecontrol.getInstance().volumeToggleMute()
369                 self.volumeDialog.setValue(eDVBVolumecontrol.getInstance().getVolume())
370
371                 if (eDVBVolumecontrol.getInstance().isMuted()):
372                         self.muteDialog.show()
373                 else:
374                         self.muteDialog.hide()
375
376 from Screens.Standby import Standby
377
378 class PowerKey:
379         """ PowerKey stuff - handles the powerkey press and powerkey release actions"""
380         
381         def __init__(self, session):
382                 self.session = session
383                 self.powerKeyTimer = eTimer()
384                 self.powerKeyTimer.timeout.get().append(self.powertimer)
385                 globalActionMap.actions["powerdown"]=self.powerdown
386                 globalActionMap.actions["powerup"]=self.powerup
387                 self.standbyblocked = 0
388 #               self["PowerKeyActions"] = HelpableActionMap(self, "PowerKeyActions",
389                         #{
390                                 #"powerdown": self.powerdown,
391                                 #"powerup": self.powerup,
392                                 #"discreteStandby": (self.standby, "Go standby"),
393                                 #"discretePowerOff": (self.quit, "Go to deep standby"),
394                         #})
395
396         def powertimer(self):   
397                 print "PowerOff - Now!"
398                 self.quit()
399         
400         def powerdown(self):
401                 self.standbyblocked = 0
402                 self.powerKeyTimer.start(3000, True)
403
404         def powerup(self):
405                 self.powerKeyTimer.stop()
406                 if self.standbyblocked == 0:
407                         self.standbyblocked = 1
408                         self.standby()
409
410         def standby(self):
411                 if self.session.current_dialog and self.session.current_dialog.ALLOW_SUSPEND:
412                         self.session.open(Standby, self)
413
414         def quit(self):
415                 # halt
416                 quitMainloop(1)
417
418 def runScreenTest():
419         plugins.readPluginList(resolveFilename(SCOPE_PLUGINS))
420
421         session = Session(desktop = getDesktop(0), summary_desktop = getDesktop(1), navigation = Navigation())
422         
423         screensToRun = [ ]
424         
425         for p in plugins.getPlugins(PluginDescriptor.WHERE_WIZARD):
426                 screensToRun.append(p.__call__)
427         
428         screensToRun += wizardManager.getWizards()
429         
430         screensToRun.append(Screens.InfoBar.InfoBar)
431
432         ePythonConfigQuery.setQueryFunc(configfile.getResolvedKey)
433
434         def runNextScreen(session, screensToRun, *result):
435                 if result:
436                         quitMainloop(*result)
437                         return
438         
439                 screen = screensToRun[0]
440                 
441                 if len(screensToRun):
442                         session.openWithCallback(boundFunction(runNextScreen, session, screensToRun[1:]), screen)
443                 else:
444                         session.open(screen)
445         
446         runNextScreen(session, screensToRun)
447         
448         vol = VolumeControl(session)
449         power = PowerKey(session)
450         
451         runReactor()
452         
453         configfile.save()
454         
455         from Tools.DreamboxHardware import setFPWakeuptime
456         from time import time
457         nextRecordingTime = session.nav.RecordTimer.getNextRecordingTime()
458         if nextRecordingTime != -1:
459                 if (nextRecordingTime - time() < 330): # no time to switch box back on
460                         setFPWakeuptime(time() + 30) # so switch back on in 30 seconds
461                 else:
462                         setFPWakeuptime(nextRecordingTime - (300))
463         
464         session.nav.stopService()
465         session.nav.shutdown()
466         
467         return 0
468
469 import keymapparser
470 keymapparser.readKeymap()
471 import skin
472 skin.loadSkinData(getDesktop(0))
473
474 import Components.InputDevice
475 Components.InputDevice.InitInputDevices()
476
477 import Components.AVSwitch
478 Components.AVSwitch.InitAVSwitch()
479
480 import Components.RecordingConfig
481 Components.RecordingConfig.InitRecordingConfig()
482
483 import Components.UsageConfig
484 Components.UsageConfig.InitUsageConfig()
485
486 import Components.Network
487 Components.Network.InitNetwork()
488
489 import Components.Lcd
490 Components.Lcd.InitLcd()
491
492 import Components.SetupDevices
493 Components.SetupDevices.InitSetupDevices()
494
495 import Components.RFmod
496 Components.RFmod.InitRFmod()
497
498 import Components.NimManager
499
500 import Screens.Ci
501 Screens.Ci.InitCiConfig()
502
503 # first, setup a screen
504 try:
505         runScreenTest()
506
507         plugins.shutdown()
508         
509         from Components.ParentalControl import parentalControl
510         parentalControl.save()
511 except:
512         print 'EXCEPTION IN PYTHON STARTUP CODE:'
513         print '-'*60
514         traceback.print_exc(file=sys.stdout)
515         quitMainloop(5)
516         print '-'*60