Fix crash when closing setup screen with prevously inactive KiddyTimer
[enigma2-plugins.git] / kiddytimer / src / KTsetup.py
1 from Components.ActionMap import ActionMap, NumberActionMap
2 from Components.Button import Button
3 from Components.ConfigList import ConfigListScreen
4 from Components.Label import Label
5 from Components.ProgressBar import ProgressBar
6 from Components.config import config, getConfigListEntry
7 from KTmain import oKiddyTimer
8 from KTMultiPixmap import KTmultiPixmap
9 from MovableScreen import MovableScreen
10 from Screens.InputBox import PinInput
11 from Screens.MessageBox import MessageBox
12 from Screens.ParentalControlSetup import ProtectedScreen
13 from Screens.Screen import Screen
14 from Tools import Notifications
15 from __init__ import _
16 import KTglob
17 import time
18
19 class KiddyTimerSetup(ConfigListScreen, Screen, ProtectedScreen):
20     skin = ("""
21     <screen position="center,center" size="560,400" title="%s Setup">
22       <ePixmap pixmap="~/img/button-red.png" position="0,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
23       <ePixmap pixmap="~/img/button-green.png" position="140,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
24       <ePixmap pixmap="~/img/button-yellow.png" position="280,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
25       <ePixmap pixmap="~/img/button-blue.png" position="420,0" zPosition="0" size="140,40" transparent="1" alphatest="on" />
26       <widget name="key_red" position="0,0" zPosition="1" size="140,40"
27         font="Regular;20" valign="center" halign="center" backgroundColor="#9f1313" transparent="1"
28         shadowColor="#000000" shadowOffset="-1,-1" />
29       <widget name="key_green" position="140,0" zPosition="1" size="140,40"
30         font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1"
31         shadowColor="#000000" shadowOffset="-1,-1" />
32       <widget name="key_yellow" position="280,0" zPosition="1" size="140,40"
33         font="Regular;20" valign="center" halign="center" backgroundColor="#a08500" transparent="1"
34         shadowColor="#000000" shadowOffset="-1,-1" />
35       <widget name="key_blue" position="420,0" zPosition="1" size="140,40"
36         font="Regular;20" valign="center" halign="center" backgroundColor="#18188b" transparent="1"
37         shadowColor="#000000" shadowOffset="-1,-1" />
38       <widget name="config" position="10,40" size="540,300" scrollbarMode="showOnDemand" />
39       <widget name="LastDayStarted" position="10,340" size="540,20" zPosition="4" font="Regular;18" foregroundColor="#cccccc" />
40       <widget name="RemainingTime" position="10,360" size="540,20" zPosition="4" font="Regular;18" foregroundColor="#cccccc" />
41       <widget name="PluginInfo" position="10,380" size="540,20" zPosition="4" font="Regular;18" foregroundColor="#cccccc" />
42     </screen>""") %KTglob.PLUGIN_BASE
43
44     def __init__(self, session):
45         self.session = session
46         Screen.__init__(self, self.session)
47         ProtectedScreen.__init__(self)
48
49         # Lets get a list of elements for the config list
50         self.list = [
51             getConfigListEntry(_("Enabled"), config.plugins.KiddyTimer.enabled),
52             getConfigListEntry(_("PIN"), config.plugins.KiddyTimer.pin),
53             getConfigListEntry(_("Don't monitor TV started after"), config.plugins.KiddyTimer.monitorEndTime ), 
54             getConfigListEntry(_("Style of timer"), config.plugins.KiddyTimer.timerStyle ),
55             getConfigListEntry(_("Timeout for activation dialog"), config.plugins.KiddyTimer.activationDialogTimeout )
56             ]
57         for i in range(0,7):
58             self.list.append(getConfigListEntry(KTglob.DAYNAMES[i], config.plugins.KiddyTimer.dayTimes[i].timeValue))                
59
60         ConfigListScreen.__init__(self, self.list)
61
62         self["config"].list = self.list
63
64         self.skin_path = KTglob.plugin_path
65         self.enabled_old = config.plugins.KiddyTimer.enabled.value
66
67         # If the oKiddyTimer- Class has no session yet, then get session
68         if oKiddyTimer.session == None:
69             oKiddyTimer.gotSession(self.session)
70         
71         # Temporarily stop timer as long as we are in the setup screen
72         if oKiddyTimer.dialog != None:
73             self.dialogEnabled_old = oKiddyTimer.dialogEnabled
74             oKiddyTimer.setDialogStatus(False)
75         else:
76             self.dialogEnabled_old = False
77             
78         # Plugin Information
79         iRemainingTime = oKiddyTimer.remainingTime
80         sRemainingTime = KTglob.getTimeFromSeconds(oKiddyTimer.remainingTime , True )
81
82         self["PluginInfo"] = Label(_("Plugin: %(plugin)s , Version: %(version)s") %dict(plugin=KTglob.PLUGIN_BASE,version=KTglob.PLUGIN_VERSION))
83         self["RemainingTime"] = Label(_("Remaining time: %s") %sRemainingTime)
84         self["LastDayStarted"] = Label(_("Last day started: %s") % config.plugins.KiddyTimer.lastStartDay.getValue())
85         
86         # BUTTONS
87         self["key_red"] = Button(_("Cancel"))
88         self["key_green"] = Button(_("Save"))
89         self["key_yellow"] = Button(_("Reset clock"))
90         self["key_blue"] = Button(_("Move clock"))
91
92         self["setupActions"] = NumberActionMap(["SetupActions", "ColorActions"],
93         {
94             "save": self.save,
95             "cancel": self.cancel,
96             "green": self.save,
97             "red": self.cancel,
98             "ok": self.save,
99             "blue": self.keyPositioner,
100             "yellow": self.resetTimer
101         }, -2)
102
103     def pinEntered(self, result):
104         if result is None:
105             self.cancel()
106         elif not result:
107             self.session.openWithCallback(self.cancel, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
108            
109     def keyPositioner(self):
110         self.session.open(KiddyTimerPositioner)
111
112     def resetTimer(self):
113         oKiddyTimer.resetTimer()
114         sRemainingTime = KTglob.getTimeFromSeconds(oKiddyTimer.remainingTime , True )
115         self["RemainingTime"].setText(_("Remaining time: %s") %sRemainingTime)
116
117     def save(self):
118         if oKiddyTimer.remainingTime > KTglob.getSecondsFromClock( config.plugins.KiddyTimer.dayTimes[oKiddyTimer.dayNr].timeValue.getValue()):
119             self.resetTimer()
120         for x in self["config"].list:
121             x[1].save()
122             
123         oKiddyTimer.currentDayTime = KTglob.getSecondsFromClock(config.plugins.KiddyTimer.dayTimes[oKiddyTimer.dayNr].timeValue.getValue())
124         if config.plugins.KiddyTimer.enabled.value:
125             if self.enabled_old != config.plugins.KiddyTimer.enabled.value:
126                 oKiddyTimer.gotSession(oKiddyTimer.session)
127             else:
128                 oKiddyTimer.setDialogStatus(oKiddyTimer.timerHasToRun())
129                 if oKiddyTimer.dialogEnabled == True:
130                     oKiddyTimer.askForActivation()
131         self.close()
132
133     def cancel(self):
134         for x in self["config"].list:
135             x[1].cancel()
136         self.close()
137         if self.dialogEnabled_old == True:
138             oKiddyTimer.showHide()
139
140     def protectedWithPin(self):
141         return config.plugins.KiddyTimer.pin.getValue()
142
143 class KiddyTimerPositioner(Screen, MovableScreen):
144     def __init__(self, session):
145         Screen.__init__(self, session)
146         self.skin = KTglob.SKIN
147
148         self.skin_path = KTglob.plugin_path
149
150         self["TimerGraph"] = KTmultiPixmap()
151         self["TimerText"] = Label(_("01:00"))
152         self["TimerSlider"] = ProgressBar()
153         self["TimerSliderText"] = Label(_("01:00"))
154         
155         if config.plugins.KiddyTimer.timerStyle.value == "clock":
156             self["TimerGraph"].show()
157             self["TimerText"].show()
158             self["TimerSlider"].hide()    
159             self["TimerSliderText"].hide()
160         else:
161             self["TimerGraph"].hide()
162             self["TimerText"].hide()
163             self["TimerSlider"].show()
164             self["TimerSliderText"].show()
165
166         self["actions"] = ActionMap(["OkCancelActions"] , 
167         {
168          "ok":      self.keyOK,
169          "cancel":  self.keyCancel
170         }, -1)
171
172         MovableScreen.__init__(self, config.plugins.KiddyTimer, [], 82 , 82)
173         self.startMoving()
174         
175     def keyOK(self):
176         self.close()
177     
178     def keyCancel(self):
179         self.close()