[VPS] code for starting from standby rewritten
[enigma2-plugins.git] / vps / src_py / plugin.py
1 # -*- coding: utf-8 -*-
2
3 from Plugins.Plugin import PluginDescriptor
4 from os import stat
5 from Vps import vps_timers
6 from Vps_setup import VPS_Setup
7 from Modifications import register_vps
8 from . import _
9
10 # Config
11 from Components.config import config, ConfigYesNo, ConfigSubsection, ConfigInteger, ConfigSelection, configfile
12
13 config.plugins.vps = ConfigSubsection()
14 config.plugins.vps.enabled = ConfigYesNo(default = True)
15 config.plugins.vps.initial_time = ConfigInteger(default=10, limits=(0, 120))
16 config.plugins.vps.allow_wakeup = ConfigYesNo(default = False)
17 config.plugins.vps.allow_seeking_multiple_pdc = ConfigYesNo(default = True)
18 config.plugins.vps.vps_default = ConfigSelection(choices = [("no", _("No")), ("yes_safe", _("Yes (safe mode)")), ("yes", _("Yes"))], default = "no") 
19 config.plugins.vps.instanttimer = ConfigSelection(choices = [("no", _("No")), ("yes_safe", _("Yes (safe mode)")), ("yes", _("Yes")), ("ask", _("always ask"))], default = "ask")
20 config.plugins.vps.infotext = ConfigInteger(default=0)
21 config.plugins.vps.margin_after = ConfigInteger(default=10, limits=(0, 600)) # in seconds
22 config.plugins.vps.wakeup_time = ConfigInteger(default=-1)
23
24
25 recordTimerWakeupAuto = False
26
27 def autostart(reason, **kwargs):
28         if reason == 0:
29                 if kwargs.has_key("session"):
30                         session = kwargs["session"]
31                         vps_timers.session = session
32                         vps_timers.checkTimer()
33
34                         try:
35                                 from Plugins.Extensions.WebInterface.WebChilds.Toplevel import addExternalChild
36                                 from Plugins.Extensions.WebInterface.WebChilds.Screenpage import ScreenPage
37                                 from twisted.web import static
38                                 from twisted.python import util
39                                 from enigma import eEnv
40                         except ImportError as ie:
41                                 pass
42                         else:
43                                 if hasattr(static.File, 'render_GET'):
44                                         class File(static.File):
45                                                 def render_POST(self, request):
46                                                         return self.render_GET(request)
47                                 else:
48                                         File = static.File
49
50                                 root = File(eEnv.resolve("${libdir}/enigma2/python/Plugins/SystemPlugins/vps/web-data"))
51                                 root.putChild("web", ScreenPage(session, util.sibpath(__file__, "web"), True))
52                                 addExternalChild(("vpsplugin", root, "VPS-Plugin", "1", False))
53                 else:
54                         register_vps()
55         
56         elif reason == 1:
57                 vps_timers.shutdown()
58
59                 try:            
60                         if config.plugins.vps.wakeup_time.value != -1 and config.plugins.vps.wakeup_time.value == config.misc.prev_wakeup_time.value:
61                                 # Folgendes wird nur wegen spezieller Anforderungen des Plugins gesetzt.
62                                 # Damit sich Enigma2 so verhält, wie wenn es für eine Aufnahme aus dem Standby aufwacht.
63                                 config.misc.prev_wakeup_time_type.value = 0
64                                 config.misc.prev_wakeup_time_type.save()
65                                 config.misc.isNextRecordTimerAfterEventActionAuto.value = recordTimerWakeupAuto
66                                 config.misc.isNextRecordTimerAfterEventActionAuto.save()
67                                 
68                                 # Da E2 die Configdatei noch vor dem Aufruf von autostart shutdown abspeichert, muss hier nochmal abgespeichert werden.
69                                 configfile.save()
70                 
71                 except:
72                         print "[VPS-Plugin] exception in shutdown handler, probably old enigma2"
73                 
74
75 def setup(session, **kwargs):
76         session.openWithCallback(doneConfig, VPS_Setup)
77
78 def doneConfig(session, **kwargs):
79         vps_timers.checkTimer()
80
81 def startSetup(menuid):
82         if menuid != "system":
83                 return []
84         return [(_("VPS Settings"), setup, "vps", 50)]
85         
86 def getNextWakeup():
87         global recordTimerWakeupAuto
88         t, recordTimerWakeupAuto = vps_timers.nextWakeup()
89         config.plugins.vps.wakeup_time.value = t
90         config.plugins.vps.save()
91         
92         return t
93
94 def Plugins(**kwargs):
95         return [
96                 PluginDescriptor(
97                         name = "VPS",
98                         where = [
99                                 PluginDescriptor.WHERE_AUTOSTART,
100                                 PluginDescriptor.WHERE_SESSIONSTART
101                         ],
102                         fnc = autostart,
103                         wakeupfnc = getNextWakeup,
104                         needsRestart = True
105                 ),
106                 PluginDescriptor(
107                         name = _("VPS Settings"),
108                         where = PluginDescriptor.WHERE_MENU,
109                         fnc = startSetup,
110                         needsRestart = True
111                 ),
112         ]