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 _
16 import NavigationInstance
22 PARAM_DISABLETIMER = 3
24 PARAM_INCREASETIMER = 5
25 PARAM_DECREASETIMER = 6
27 PARAM_ENABLETIMERONCE = 8
30 class KiddyTimerScreen(Screen):
32 def __init__(self, session):
33 Screen.__init__(self, session)
34 self.skin = KTglob.SKIN
35 self.onShow.append(self.movePosition)
37 self.skin_path = KTglob.plugin_path
39 self["TimerGraph"] = KTmultiPixmap()
40 self["TimerText"] = Label(_("??:??"))
41 self["TimerSlider"] = ProgressBar()
42 self["TimerSliderText"] = Label(_("??:??"))
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)
51 if config.plugins.KiddyTimer.timerStyle.value == "clock":
52 self["TimerGraph"].show()
53 self["TimerText"].show()
54 self["TimerSlider"].hide()
55 self["TimerSliderText"].hide()
57 self["TimerGraph"].hide()
58 self["TimerText"].hide()
59 self["TimerSlider"].show()
60 self["TimerSliderText"].show()
62 def movePosition(self):
65 self.instance.move(ePoint(config.plugins.KiddyTimer.position_x.value, config.plugins.KiddyTimer.position_y.value))
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)
73 ##############################################################################
82 self.iServiceReference = None
85 self.sessionStartTime = None
87 self.loopTimerStep = 1000
88 self.loopTimer = eTimer()
89 self.loopTimer.callback.append(self.calculateTimes)
91 config.misc.standbyCounter.addNotifier(self.enterStandby, initial_call = False)
93 def gotSession(self, session):
94 self.session = session
97 def enterStandby(self,configElement):
98 Standby.inStandby.onClose.append(self.endStandby)
101 def endStandby(self):
104 def startTimer(self,bForceStart=False,iRemainingSeconds=0):
105 bDoStandardInit = True
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
118 elif iRemainingSeconds > 0:
119 self.resetTimer(setTime=iRemainingSeconds)
120 bDoStandardInit = False
121 elif self.currentDay != config.plugins.KiddyTimer.lastStartDay.getValue():
123 bDoStandardInit = False
125 self.setCurrentDayTime()
126 self.setSessionTime(config.plugins.KiddyTimer.remainingTime.getValue())
127 self.setRemainingTime(self.sessionTime)
128 self.setSessionStartTime()
130 self.setPluginStatus("RUNNING")
131 self.toggleActiveState(True)
133 self.askForActivation()
140 self.toggleActiveState(False)
142 self.setPluginStatus("SHUTDOWN")
143 self.iServiceReference = None
146 def resetTimer(self,**kwargs):
147 if "setTime" in kwargs.keys():
148 self.setCurrentDayTime(kwargs["setTime"])
150 self.setCurrentDayTime()
152 self.setSessionTime(self.currentDayTime)
153 self.setRemainingTime(self.currentDayTime)
154 self.setSessionStartTime()
156 def timerHasToRun(self):
157 curStartTime = self.sessionStartTime or time.localtime()
158 iPluginStart = KTglob.getSecondsFromClock( [curStartTime[3],curStartTime[4]] )
159 iMonitorEnd = KTglob.getSecondsFromClock(config.plugins.KiddyTimer.monitorEndTime.getValue())
160 iMonitorStart = KTglob.getSecondsFromClock(config.plugins.KiddyTimer.monitorStartTime.getValue())
161 return (iPluginStart < iMonitorEnd) & (iPluginStart > iMonitorStart)
164 self.loopTimer.start(self.loopTimerStep,1)
167 self.loopTimer.stop()
169 def detectCheatAttempt(self):
170 sLastStatus = config.plugins.KiddyTimer.lastStatus.value
171 if (sLastStatus == "RUNNING"):
172 # cheat detected: RUNNING is NEVER a valid entry when checking.
173 Notifications.AddNotification(MessageBox, _("Cheat attempt detected. \nBox has not been shudown correctly. \nRemaining time was set to 0."), MessageBox.TYPE_WARNING, 0)
178 def setPluginStatus(self,sStatus):
179 # Set values for detection of cheat attempts
180 config.plugins.KiddyTimer.lastStatus.value = sStatus
181 config.plugins.KiddyTimer.lastStatus.save()
184 def askForActivation(self):
185 iTimeOut = config.plugins.KiddyTimer.activationDialogTimeout.getValue()
186 Notifications.AddNotificationWithCallback(self.callbackAskForActivation, MessageBox, _("Do you want to start the kiddytimer- plugin now."), MessageBox.TYPE_YESNO, iTimeOut)
188 def callbackAskForActivation(self, value):
190 self.callbackParameter = PARAM_STOPTIMER
196 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"))
198 def getTriesEntry(self):
199 return config.ParentalControl.retries.setuppin
201 def pinEntered(self, result):
205 if self.callbackParameter == PARAM_STOPTIMER:
207 elif self.callbackParameter == PARAM_DISABLETIMER:
208 self.toggleEnabledState(False)
209 elif self.callbackParameter == PARAM_INCREASETIMER:
210 self.session.openWithCallback(self.modifySessionTime, MinuteInput)
211 elif self.callbackParameter == PARAM_SETTIMER:
212 self.session.openWithCallback(self.callbackSetTimer, MinuteInput)
213 elif self.callbackParameter == PARAM_RESETTIMER:
215 elif self.callbackParameter == PARAM_ENABLETIMERONCE:
216 self.session.openWithCallback(self.callbackEnableTimerOnce, MinuteInput)
218 def setCurrentDayTime(self,iDayTime=PARAM_NONE):
219 if iDayTime == PARAM_NONE:
220 iDayTime = KTglob.getTodaysTimeInSeconds()
221 self.currentDayTime = iDayTime
223 def setSessionStartTime(self):
224 self.sessionStartTime = time.localtime()
226 def modifySessionTime(self, iMinutes):
227 iSeconds = iMinutes * 60
228 if self.callbackParameter == PARAM_INCREASETIMER:
229 iSeconds += self.sessionTime
231 iSeconds = self.sessionTime - iSeconds
232 self.setSessionTime(iSeconds)
235 def setSessionTime(self, iSeconds):
236 self.sessionTime = iSeconds
237 if self.sessionTime > self.currentDayTime:
238 self.setCurrentDayTime(self.sessionTime)
239 if self.sessionTime < 0:
242 def setRemainingTime(self,iRemaining):
245 self.remainingTime = iRemaining
246 self.remainingPercentage = iRemaining / self.currentDayTime
248 def callbackSetTimer(self, iMinutes):
249 iSeconds = iMinutes * 60
250 self.resetTimer(setTime=iSeconds)
252 def callbackEnableTimerOnce(self, iMinutes):
253 iSeconds = iMinutes * 60
255 #If timer is active: stop it first to prohibit conflicts
258 self.startTimer(True, iSeconds)
260 def toggleActiveState(self , bStatus):
262 if self.dialog == None and bStatus:
263 self.dialog = self.session.instantiateDialog(KiddyTimerScreen)
264 self.active = bStatus
268 if self.dialog != None:
271 def toggleEnabledState(self, bStatus):
272 config.plugins.KiddyTimer.enabled.value = bStatus
273 self.enabled = bStatus
274 config.plugins.KiddyTimer.enabled.save()
281 def calculateTimes(self):
283 if self.active == True:
284 odtEnd = time.mktime(time.localtime())
285 iDiff = odtEnd - time.mktime(self.sessionStartTime)
286 iRemaining = self.sessionTime - iDiff
289 self.remainingTime = iRemaining
290 if self.currentDayTime > 0:
291 self.remainingPercentage = iRemaining / self.currentDayTime
293 self.remainingPercentage = 0
295 self.setImageNumber()
297 if self.remainingTime == 0:
298 self.iServiceReference = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
299 NavigationInstance.instance.stopService()
300 self.dialog.renderScreen()
305 def setImageNumber(self):
306 iCurPercent = int( self.remainingPercentage * 1000 )
308 for iPercent in self.dialog.percentageList:
309 if iCurPercent <= iPercent:
316 def saveValues(self):
317 if hasattr(self,"currentDay"):
318 config.plugins.KiddyTimer.lastStartDay.value = self.currentDay
319 config.plugins.KiddyTimer.lastStartDay.save()
320 if hasattr(self,"remainingTime"):
321 config.plugins.KiddyTimer.remainingTime.value = int(self.remainingTime)
322 config.plugins.KiddyTimer.remainingTime.save()
324 def showExtensionsMenu(self):
328 keyList.append((_("Stop KiddyTimer (this session only)"),PARAM_STOPTIMER))
329 keyList.append((_("Increase remaining time"),PARAM_INCREASETIMER))
330 keyList.append((_("Decrease remaining time"),PARAM_DECREASETIMER))
331 keyList.append((_("Set remaining time"),PARAM_SETTIMER))
332 keyList.append((_("Reset todays remaining time"),PARAM_RESETTIMER))
334 keyList.append((_("Start KiddyTimer"),PARAM_STARTTIMER))
335 keyList.append((_("Enable KiddyTimer for x minutes"),PARAM_ENABLETIMERONCE))
336 keyList.append((_("Disable KiddyTimer"),PARAM_DISABLETIMER))
338 keyList.append((_("Enable KiddyTimer"),PARAM_ENABLETIMER))
339 keyList.append((_("Enable KiddyTimer for x minutes"),PARAM_ENABLETIMERONCE))
340 self.session.openWithCallback(self.DoSelectionExtensionsMenu,ChoiceBox,_("Please select your KiddyTimer- option"),keyList)
342 def DoSelectionExtensionsMenu(self,answer):
343 self.callbackParameter = PARAM_NONE
346 elif answer[1] in [PARAM_DISABLETIMER,PARAM_STOPTIMER,PARAM_INCREASETIMER,PARAM_SETTIMER,PARAM_ENABLETIMERONCE,PARAM_RESETTIMER]:
347 self.callbackParameter = answer[1]
349 elif answer[1] == PARAM_STARTTIMER:
351 elif answer[1] == PARAM_ENABLETIMER:
352 self.toggleEnabledState(True)
353 elif answer[1] == PARAM_DECREASETIMER:
354 self.session.openWithCallback(self.modifySessionTime, MinuteInput)
356 self.session.open(MessageBox,_("Invalid selection"), MessageBox.TYPE_ERROR, 5)
358 # Assign global variable kiddyTimer
359 kiddyTimer = KiddyTimer()