Version 1.0: Complete code- cleanup. New Feature "cheat- detection" for kids switchin...
[enigma2-plugins.git] / kiddytimer / src / KTmain.py
1 from Components.Label import Label
2 from Components.ProgressBar import ProgressBar
3 from KTMultiPixmap import KTmultiPixmap
4 from Components.config import config, configfile
5 from Screens.ChoiceBox import ChoiceBox
6 from Screens.InputBox import PinInput
7 from Screens.MessageBox import MessageBox
8 from Screens.MinuteInput import MinuteInput
9 from Screens.Screen import Screen
10 from Screens import Standby
11 from Tools.BoundFunction import boundFunction
12 from Tools import Notifications
13 from enigma import ePoint, eTimer
14 from __init__ import _
15 import KTglob
16 import NavigationInstance
17 import time
18
19 PARAM_NONE = -1
20 PARAM_STOPTIMER = 1
21 PARAM_STARTTIMER = 2
22 PARAM_DISABLETIMER = 3
23 PARAM_ENABLETIMER = 4
24 PARAM_INCREASETIMER = 5
25 PARAM_DECREASETIMER = 6
26 PARAM_SETTIMER = 7
27 PARAM_ENABLETIMERONCE = 8
28 PARAM_RESETTIMER = 9
29
30 class KiddyTimerScreen(Screen):    
31
32     def __init__(self, session):
33         Screen.__init__(self, session)
34         self.skin = KTglob.SKIN
35         self.onShow.append(self.movePosition)
36         
37         self.skin_path = KTglob.plugin_path
38
39         self["TimerGraph"] = KTmultiPixmap()
40         self["TimerText"] = Label(_("??:??"))
41         self["TimerSlider"] = ProgressBar()
42         self["TimerSliderText"] = Label(_("??:??"))
43         
44     def renderScreen(self):
45         self["TimerSlider"].setValue(int(kiddyTimer.remainingPercentage*100)) 
46         self["TimerGraph"].setPixmapNum(kiddyTimer.curImg)
47         self.sTimeLeft = KTglob.getTimeFromSeconds( (kiddyTimer.remainingTime + 59) , False ) # Add 59 Seconds to show one minute if less than 1 minute left...
48         self["TimerText"].setText(self.sTimeLeft)
49         self["TimerSliderText"].setText(self.sTimeLeft)
50
51         if config.plugins.KiddyTimer.timerStyle.value == "clock":
52             self["TimerGraph"].show()
53             self["TimerText"].show()
54             self["TimerSlider"].hide()    
55             self["TimerSliderText"].hide()
56         else:
57             self["TimerGraph"].hide()
58             self["TimerText"].hide()
59             self["TimerSlider"].show()
60             self["TimerSliderText"].show()
61
62     def movePosition(self):
63         if self.instance:
64             self.getPixmapList()
65             self.instance.move(ePoint(config.plugins.KiddyTimer.position_x.value, config.plugins.KiddyTimer.position_y.value))
66
67     def getPixmapList(self):
68         self.percentageList = []
69         for sPixmap in self["TimerGraph"].pixmapFiles:
70             i = int(sPixmap[-8:-4])
71             self.percentageList.append(i)
72       
73 ##############################################################################
74
75 class KiddyTimer():
76
77     def __init__(self):
78         self.session = None 
79         self.dialog = None
80         self.active = False
81
82         self.iServiceReference = None
83         self.curImg = 0
84
85         self.sessionStartTime = None
86
87         self.loopTimerStep = 1000
88         self.loopTimer = eTimer()
89         self.loopTimer.callback.append(self.calculateTimes)
90
91         config.misc.standbyCounter.addNotifier(self.enterStandby, initial_call = False)
92
93     def gotSession(self, session):
94         self.session = session
95         self.startTimer()   
96          
97     def enterStandby(self,configElement):
98         Standby.inStandby.onClose.append(self.endStandby)
99         self.stopTimer()    
100       
101     def endStandby(self):
102         self.startTimer()
103         
104     def startTimer(self,bForceStart=False,iRemainingSeconds=0):
105         bDoStandardInit = True
106         if bForceStart:
107             self.enabled = True
108         else:
109             self.enabled = config.plugins.KiddyTimer.enabled.value
110         if (self.enabled == True and self.timerHasToRun()) or bForceStart:   
111             # Date of the current day
112             self.currentDay = time.strftime("%d.%m.%Y" , time.localtime())
113             # First check for Cheat- attenpts by kids
114             if self.detectCheatAttempt():
115                 config.plugins.KiddyTimer.remainingTime.value = 0
116                 configfile.save()
117                 bForceStart = True
118             elif iRemainingSeconds > 0:
119                 self.resetTimer(setTime=iRemainingSeconds)
120                 bDoStandardInit = False
121             elif self.currentDay != config.plugins.KiddyTimer.lastStartDay.getValue():
122                 self.resetTimer()
123                 bDoStandardInit = False
124             if bDoStandardInit:             
125                 self.setCurrentDayTime()
126                 self.setSessionTime(config.plugins.KiddyTimer.remainingTime.getValue())
127                 self.setRemainingTime(self.sessionTime)
128                 self.setSessionStartTime()
129
130             self.setPluginStatus("RUNNING")
131             self.toggleActiveState(True)
132             if not bForceStart:
133                 self.askForActivation()
134             else:
135                 self.startLoop()
136
137     def stopTimer(self):
138         self.toggleActiveState(False)
139         self.stopLoop()
140         self.saveValues()
141         self.setPluginStatus("SHUTDOWN")
142         self.iServiceReference = None
143         self.dialog = None
144                 
145     def resetTimer(self,**kwargs):
146         if "setTime" in kwargs.keys():
147             self.setCurrentDayTime(kwargs["setTime"])
148         else:            
149             self.setCurrentDayTime()
150         
151         self.setSessionTime(self.currentDayTime)
152         self.setRemainingTime(self.currentDayTime)
153         self.setSessionStartTime()
154
155     def timerHasToRun(self):
156         curStartTime = self.sessionStartTime or time.localtime()
157         iPluginStart = KTglob.getSecondsFromClock( [curStartTime[3],curStartTime[4]] )
158         iMonitorEnd = KTglob.getSecondsFromClock(config.plugins.KiddyTimer.monitorEndTime.getValue())  
159         iMonitorStart = KTglob.getSecondsFromClock(config.plugins.KiddyTimer.monitorStartTime.getValue())  
160         return (iPluginStart < iMonitorEnd) & (iPluginStart > iMonitorStart)
161
162     def startLoop(self):
163         self.loopTimer.start(self.loopTimerStep,1)
164     
165     def stopLoop(self):
166         self.loopTimer.stop()
167                            
168     def detectCheatAttempt(self):
169         sLastStatus = config.plugins.KiddyTimer.lastStatus.value
170         if (sLastStatus == "RUNNING"):
171             # cheat detected: RUNNING is NEVER a valid entry when checking.
172             Notifications.AddNotification(MessageBox, _("Cheat attempt detected. \nBox has not been shudown correctly. \nRemaining time was set to 0."), MessageBox.TYPE_WARNING, 0)
173             return True
174         else:
175             return False
176
177     def setPluginStatus(self,sStatus):
178             # Set values for detection of cheat attempts
179             config.plugins.KiddyTimer.lastStatus.value = sStatus
180             config.plugins.KiddyTimer.lastStatus.save()
181             configfile.save()
182
183     def askForActivation(self):
184         iTimeOut = config.plugins.KiddyTimer.activationDialogTimeout.getValue()
185         Notifications.AddNotificationWithCallback(self.callbackAskForActivation, MessageBox, _("Do you want to start the kiddytimer- plugin now."), MessageBox.TYPE_YESNO, iTimeOut)
186
187     def callbackAskForActivation(self, value):
188         if not value:
189             self.callbackParameter = PARAM_STOPTIMER
190             self.askForPIN()
191
192         self.startLoop()
193
194     def askForPIN(self):
195         self.session.openWithCallback( self.pinEntered, PinInput, pinList = [config.plugins.KiddyTimer.pin.getValue()], triesEntry = self.getTriesEntry(), title = _("Please enter the correct pin code"), windowTitle = _("Enter pin code"))
196     
197     def getTriesEntry(self):
198         return config.ParentalControl.retries.setuppin
199
200     def pinEntered(self, result):
201         if not result:
202             pass
203         else:
204             if self.callbackParameter == PARAM_STOPTIMER:
205                 self.stopTimer()      
206             elif self.callbackParameter == PARAM_DISABLETIMER:
207                 self.toggleEnabledState(False)
208             elif self.callbackParameter == PARAM_INCREASETIMER:
209                 self.session.openWithCallback(self.modifySessionTime, MinuteInput)
210             elif self.callbackParameter == PARAM_SETTIMER:
211                 self.session.openWithCallback(self.callbackSetTimer, MinuteInput)
212             elif self.callbackParameter == PARAM_RESETTIMER:
213                 self.resetTimer()
214             elif self.callbackParameter == PARAM_ENABLETIMERONCE:
215                 self.session.openWithCallback(self.callbackEnableTimerOnce, MinuteInput)
216
217     def setCurrentDayTime(self,iDayTime=PARAM_NONE):
218         if iDayTime == PARAM_NONE:
219             iDayTime = KTglob.getTodaysTimeInSeconds()
220         self.currentDayTime = iDayTime
221
222     def setSessionStartTime(self):
223         self.sessionStartTime = time.localtime()
224                 
225     def modifySessionTime(self, iMinutes):
226         iSeconds = iMinutes * 60
227         if self.callbackParameter == PARAM_INCREASETIMER:
228             iSeconds += self.sessionTime
229         else:
230             iSeconds = self.sessionTime - iSeconds 
231         self.setSessionTime(iSeconds)
232         
233
234     def setSessionTime(self, iSeconds):
235         self.sessionTime = iSeconds
236         if self.sessionTime > self.currentDayTime:
237             self.setCurrentDayTime(self.sessionTime)
238         if self.sessionTime < 0:
239             self.sessionTime = 0
240
241     def setRemainingTime(self,iRemaining):
242         if iRemaining < 0:
243             iRemaining = 0
244         self.remainingTime = iRemaining
245         self.remainingPercentage = iRemaining / self.currentDayTime
246
247     def callbackSetTimer(self, iMinutes):
248         iSeconds = iMinutes * 60
249         self.resetTimer(setTime=iSeconds)
250                 
251     def callbackEnableTimerOnce(self, iMinutes):
252         iSeconds = iMinutes * 60
253         if iSeconds > 0:
254             #If timer is active: stop it first to prohibit conflicts
255             if self.active:
256                 self.stopTimer()
257             self.startTimer(True, iSeconds)
258     
259     def toggleActiveState(self , bStatus):
260         # Initialize dialog
261         if self.dialog == None and bStatus:
262             self.dialog = self.session.instantiateDialog(KiddyTimerScreen)
263         self.active = bStatus
264         if bStatus == True:
265             self.dialog.show()
266         else:
267             if self.dialog != None:
268                 self.dialog.hide()
269
270     def toggleEnabledState(self, bStatus):
271         config.plugins.KiddyTimer.enabled.value = bStatus
272         self.enabled = bStatus
273         config.plugins.KiddyTimer.enabled.save()
274         configfile.save()
275         if self.enabled:
276             self.startTimer()
277         else:
278             self.stopTimer()
279
280     def calculateTimes(self):
281         self.stopLoop()
282         if self.active == True:
283             odtEnd = time.mktime(time.localtime())
284             iDiff = odtEnd - time.mktime(self.sessionStartTime)
285             iRemaining = self.sessionTime - iDiff
286             if iRemaining < 0:
287                 iRemaining = 0
288             self.remainingTime = iRemaining
289             if self.currentDayTime > 0:
290                 self.remainingPercentage = iRemaining / self.currentDayTime
291             else:
292                 self.remainingPercentage = 0
293
294             self.setImageNumber()
295             
296             if self.remainingTime == 0:
297                 self.iServiceReference = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
298                 NavigationInstance.instance.stopService()
299             self.dialog.renderScreen()
300             self.startLoop()
301         else:
302             self.stopTimer()
303
304     def setImageNumber(self):
305         iCurPercent = int( self.remainingPercentage * 1000 )
306         iCount = 0
307         for iPercent in self.dialog.percentageList:
308            if iCurPercent <= iPercent:
309                iCount = iCount + 1
310         iCount = iCount - 1
311         if iCount < 0:
312             iCount = 0
313         self.curImg = iCount
314         
315     def saveValues(self):
316         config.plugins.KiddyTimer.lastStartDay.value = self.currentDay
317         config.plugins.KiddyTimer.lastStartDay.save()
318         config.plugins.KiddyTimer.remainingTime.value = int(self.remainingTime)
319         config.plugins.KiddyTimer.remainingTime.save()
320
321     def showExtensionsMenu(self):
322         keyList = []
323         if self.enabled:
324             if self.active:
325                 keyList.append((_("Stop KiddyTimer (this session only)"),PARAM_STOPTIMER))
326                 keyList.append((_("Increase remaining time"),PARAM_INCREASETIMER))
327                 keyList.append((_("Decrease remaining time"),PARAM_DECREASETIMER))
328                 keyList.append((_("Set remaining time"),PARAM_SETTIMER))
329                 keyList.append((_("Reset todays remaining time"),PARAM_RESETTIMER))
330             else:
331                 keyList.append((_("Start KiddyTimer"),PARAM_STARTTIMER))
332             keyList.append((_("Enable KiddyTimer for x minutes"),PARAM_ENABLETIMERONCE))
333             keyList.append((_("Disable KiddyTimer"),PARAM_DISABLETIMER))
334         else:
335             keyList.append((_("Enable KiddyTimer"),PARAM_ENABLETIMER))
336             keyList.append((_("Enable KiddyTimer for x minutes"),PARAM_ENABLETIMERONCE))
337         self.session.openWithCallback(self.DoSelectionExtensionsMenu,ChoiceBox,_("Please select your KiddyTimer- option"),keyList)
338         
339     def DoSelectionExtensionsMenu(self,answer):
340         self.callbackParameter = PARAM_NONE
341         if answer is None:
342             pass
343         elif answer[1] in [PARAM_DISABLETIMER,PARAM_STOPTIMER,PARAM_INCREASETIMER,PARAM_SETTIMER,PARAM_ENABLETIMERONCE,PARAM_RESETTIMER]:
344             self.callbackParameter = answer[1]
345             self.askForPIN()
346         elif  answer[1] == PARAM_STARTTIMER:
347             self.startTimer()
348         elif  answer[1] == PARAM_ENABLETIMER: 
349             self.toggleEnabledState(True)
350         elif answer[1] == PARAM_DECREASETIMER:
351             self.session.openWithCallback(self.modifySessionTime, MinuteInput)
352         else:
353             self.session.open(MessageBox,_("Invalid selection"), MessageBox.TYPE_ERROR, 5)
354
355 # Assign global variable kiddyTimer
356 kiddyTimer = KiddyTimer()