fix sleeptimer stuff
[enigma2-plugins.git] / webinterface / src / WebComponents / Sources / SleepTimer.py
1 from Components.Sources.Source import Source
2 from Components.config import config
3
4 from Screens.MessageBox import MessageBox
5 #DO NOT REMOVE THE FOLLOWING IMPORT, it ensures that config.sleeptimer.defaulttime is there
6 import Screens.SleepTimerEdit
7
8 class SleepTimer(Source):
9     def __init__(self, session):
10         Source.__init__(self)
11         self.session = session
12         self.res = ( False,
13                      config.SleepTimer.defaulttime.value,
14                      config.SleepTimer.action.value,
15                      config.SleepTimer.ask.value,
16                      "Obligatory parameters missing [cmd [set,get], time [0-999], action [standby,shutdown], enabled [True,False]" )
17
18     def handleCommand(self, cmd):
19         print "[WebComponents.SleepTimer].handleCommand"
20         self.res = self.setSleeptimer(cmd)
21
22     def setSleeptimer(self, cmd):
23         print "[WebComponents.SleepTimer].setSleeptimer, cmd=%s" %cmd
24
25         from Screens.Standby import inStandby
26
27         if inStandby == None:
28             if cmd['cmd'] is None or cmd['cmd'] == "get":
29                 if self.session.nav.SleepTimer.isActive():
30                     return ( self.session.nav.SleepTimer.isActive(),
31                              self.session.nav.SleepTimer.getCurrentSleepTime(),
32                              config.SleepTimer.action.value,
33                              config.SleepTimer.ask.value,
34                              "Sleeptimer is enabled" )
35                 else:
36                     return ( self.session.nav.SleepTimer.isActive(),
37                              config.SleepTimer.defaulttime.value,
38                              config.SleepTimer.action.value,
39                              config.SleepTimer.ask.value,
40                              "Sleeptimer is disabled" )
41
42             elif cmd['cmd'] == "set":
43                 if cmd['enabled'] == 'True':
44                     enabled = True
45                 elif cmd['enabled'] == 'False':
46                     enabled = False
47                 else:
48                    return ( self.session.nav.SleepTimer.isActive(),
49                             config.SleepTimer.defaulttime.value,
50                             config.SleepTimer.action.value,
51                             config.SleepTimer.ask.value,
52                             "ERROR: Obligatory parameter 'enabled' [True,False] has unspecified value '%s'" %cmd['enabled'] )
53
54                 if cmd['time'] is None:
55                     if enabled:
56                         return ( self.session.nav.SleepTimer.isActive(),
57                                  config.SleepTimer.defaulttime.value,
58                                  config.SleepTimer.action.value,
59                                  config.SleepTimer.ask.value,
60                                  "ERROR: Obligatory parameter 'time' [0-999] is missing" )
61
62                 else:
63                     time = int(float(cmd['time']))
64
65                     if time > 999:
66                         time = 999
67                     elif time < 0:
68                         time = 0
69
70                     config.SleepTimer.defaulttime.setValue(time)
71                     config.SleepTimer.defaulttime.save()
72
73                 if cmd['confirmed'] is not None:
74                     confirmed = True if cmd['confirmed'].lower() == "true" else False
75                     config.SleepTimer.ask.value = confirmed
76                     config.SleepTimer.ask.save()
77
78                 if cmd['action'] == "shutdown":
79                     config.SleepTimer.action.value = "shutdown"
80                 else:
81                     config.SleepTimer.action.value = "standby"
82                 config.SleepTimer.action.save()
83
84                 if not enabled:
85                     self.session.nav.SleepTimer.clear()
86                     self.session.open(MessageBox, _("The sleep timer has been disabled."), MessageBox.TYPE_INFO)
87                     text = "Sleeptimer has been disabled"
88                 else:
89                     self.session.nav.SleepTimer.setSleepTime(time)
90                     self.session.open(MessageBox, _("The sleep timer has been activated for %s minutes.") %time, MessageBox.TYPE_INFO)
91                     text = "Sleeptimer set to %s minutes" % (time,)
92
93                 return ( self.session.nav.SleepTimer.isActive(),
94                          config.SleepTimer.defaulttime.value,
95                          config.SleepTimer.action.value,
96                          config.SleepTimer.ask.value,
97                          text )
98
99             else:
100                 return ( self.session.nav.SleepTimer.isActive(),
101                          config.SleepTimer.defaulttime.value,
102                          config.SleepTimer.action.value,
103                          config.SleepTimer.ask.value,
104                          "ERROR: Obligatory parameter 'cmd' [get,set] has unspecified value '%s'" %cmd['cmd'] )
105         else:
106             return ( self.session.nav.SleepTimer.isActive(),
107                      config.SleepTimer.defaulttime.value,
108                      config.SleepTimer.action.value,
109                      config.SleepTimer.ask.value,
110                      "ERROR: Cannot set SleepTimer while device is in Standby-Mode" )
111
112     def getResult(self):
113         return self.res
114
115     timer = property(getResult)