added configuration variable for timeout of activation dialog
[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
5 from Screens.InputBox import PinInput
6 from Screens.MessageBox import MessageBox
7 from Screens.Screen import Screen
8 from Screens import Standby
9 from Tools.BoundFunction import boundFunction
10 from Tools import Notifications
11 from enigma import ePoint, eTimer
12 import KTglob
13 import NavigationInstance
14 import time
15
16 class KiddyTimerScreen(Screen):    
17
18     def __init__(self, session):
19         Screen.__init__(self, session)
20         self.skin = KTglob.SKIN
21         self.onShow.append(self.movePosition)
22         
23         self.skin_path = KTglob.plugin_path
24
25         self["TimerGraph"] = KTmultiPixmap()
26         self["TimerText"] = Label(_("??:??"))
27         self["TimerSlider"] = ProgressBar()
28         self["TimerSliderText"] = Label(_("??:??"))
29         
30     def renderScreen(self):
31         self["TimerSlider"].setValue(int(KTglob.oKiddyTimer.remainingPercentage*100)) 
32         self["TimerGraph"].setPixmapNum(KTglob.oKiddyTimer.curImg)
33         self.sTimeLeft = KTglob.getTimeFromSeconds( (KTglob.oKiddyTimer.remainingTime + 59) , False ) # Add 59 Seconds to show one minute if less than 1 minute left...
34         self["TimerText"].setText(self.sTimeLeft)
35         self["TimerSliderText"].setText(self.sTimeLeft)
36
37         if config.plugins.KiddyTimer.timerStyle.value == "clock":
38             self["TimerGraph"].show()
39             self["TimerText"].show()
40             self["TimerSlider"].hide()    
41             self["TimerSliderText"].hide()
42         else:
43             self["TimerGraph"].hide()
44             self["TimerText"].hide()
45             self["TimerSlider"].show()
46             self["TimerSliderText"].show()
47
48     def movePosition(self):
49         if self.instance:
50             self.getPixmapList()
51             self.instance.move(ePoint(config.plugins.KiddyTimer.position_x.value, config.plugins.KiddyTimer.position_y.value))
52
53     def getPixmapList(self):
54         self.percentageList = []
55         for sPixmap in self["TimerGraph"].pixmapFiles:
56             i = int(sPixmap[-8:-4])
57             self.percentageList.append(i)
58       
59 ##############################################################################
60
61 class KiddyTimer():
62
63     def __init__(self):
64         self.session = None 
65         self.dialog = None
66         self.dialogEnabled = False
67
68         self.iServiceReference = None
69         self.curImg = 0
70                     
71         self.loopTimerStep = 1000
72         self.loopTimer = eTimer()
73         self.loopTimer.callback.append(self.calculateTimer)
74     
75     def gotSession(self, session):
76         self.session = session
77         self.enabled = config.plugins.KiddyTimer.enabled.value
78         # Start- Time of the plugin, used for calculating the remaining time
79         self.pluginStartTime = time.localtime()            
80         # Number of the current day
81         self.dayNr = int(time.strftime("%w" , self.pluginStartTime ))
82         # Current Day to compare with the last saved day. If different -> Reset counter
83         # Number of seconds for the current day
84         iDayTime = KTglob.getSecondsFromClock( config.plugins.KiddyTimer.dayTimes[self.dayNr].timeValue.getValue() )
85         self.currentDayTime = iDayTime
86         self.currentDay = time.strftime("%d.%m.%Y" , self.pluginStartTime)
87         self.lastSavedRemainingTime = config.plugins.KiddyTimer.remainingTime.getValue()
88         self.remainingTime = self.lastSavedRemainingTime
89         if self.enabled == True:            
90             if self.currentDay != config.plugins.KiddyTimer.lastStartDay.getValue():
91                 #Reset all Timers
92                 self.resetTimer()
93             
94             if self.dialog == None:
95                 self.dialog = self.session.instantiateDialog(KiddyTimerScreen)
96                 self.dialog.hide()
97                 
98             self.setDialogStatus(self.timerHasToRun())
99             if self.dialogEnabled == True:
100                 self.askForActivation()
101             else:
102                 self.calculateTimer()
103     
104     def askForActivation(self):
105         iTimeOut = config.plugins.KiddyTimer.activationDialogTimeout.getValue()
106         Notifications.AddNotificationWithCallback(self.activationCallback, MessageBox, _("Do you want to start the kiddytimer- plugin now."), MessageBox.TYPE_YESNO, iTimeOut)
107
108     def activationCallback(self, value):
109         self.setDialogStatus(self.timerHasToRun())
110
111         if value:
112             self.setDialogStatus( True )
113             if self.dialog == None:
114                 self.dialog = self.session.instantiateDialog(KiddyTimerScreen)
115                 self.dialog.hide()
116         else:
117             self.askForPassword()
118                                        
119         self.calculateTimer()
120
121     def askForPassword(self):
122         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"))
123     
124     def getTriesEntry(self):
125         return config.ParentalControl.retries.setuppin
126         
127     def pinEntered(self, result):
128         if result is None:
129             pass
130         elif not result:
131             pass
132         else:
133             self.setDialogStatus( False )        
134     
135     def startLoop(self):
136         self.loopTimer.start(self.loopTimerStep,1)
137     
138     def stopLoop(self):
139         self.loopTimer.stop()
140     
141     def resetTimer(self):
142         iDayTime = KTglob.getSecondsFromClock( config.plugins.KiddyTimer.dayTimes[self.dayNr].timeValue.getValue() )
143         self.currentDayTime = iDayTime
144         
145         self.lastSavedRemainingTime = self.currentDayTime
146         self.remainingTime = self.currentDayTime
147                 
148         config.plugins.KiddyTimer.lastStartDay.setValue(self.currentDay)
149         self.remainingPercentage = self.remainingTime / self.currentDayTime
150         self.pluginStartTime = time.localtime()
151         
152         self.saveValues()
153     
154     def timerHasToRun(self):
155         iPluginStart = KTglob.getSecondsFromClock( [self.pluginStartTime[3],self.pluginStartTime[4]] )
156         iMonitorEnd = KTglob.getSecondsFromClock(config.plugins.KiddyTimer.monitorEndTime.getValue())  
157         return iPluginStart < iMonitorEnd 
158     
159     def setDialogStatus(self , bStatus):
160         if bStatus == True:
161             self.dialogEnabled = True
162             if self.dialog != None:
163                 self.dialog.show()
164         else:
165             self.dialogEnabled = False
166             if self.dialog != None:
167                 self.dialog.hide()
168
169     def calculateTimer(self):
170         if Standby.inStandby != None:
171             Standby.inStandby.onClose.append(self.endStandby)
172             self.stopMe()            
173         else:
174             if self.dialogEnabled == True:
175                 odtEnd = time.mktime(time.localtime())
176                 iDiff = odtEnd - time.mktime(self.pluginStartTime)
177                 iRemaining = self.lastSavedRemainingTime - iDiff
178                 if iRemaining < 0:
179                     iRemaining = 0
180                 self.remainingTime = iRemaining
181                 self.remainingPercentage = iRemaining / KTglob.oKiddyTimer.currentDayTime
182                 self.saveValues()
183                 
184                 self.setImageNumber()
185                 
186                 if self.remainingTime == 0:
187                     self.iServiceReference = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
188                     NavigationInstance.instance.stopService()
189                 self.dialog.renderScreen()
190             self.startLoop()
191
192     def showHide(self):
193         if config.plugins.KiddyTimer.enabled.value and self.timerHasToRun():
194             self.setDialogStatus(True)
195         else:
196             self.setDialogStatus(False)
197         
198     def endStandby(self):
199         self.gotSession(self.session)
200
201     def setImageNumber(self):
202         iCurPercent = int( self.remainingPercentage * 1000 )
203         iCount = 0
204         for iPercent in self.dialog.percentageList:
205            if iCurPercent <= iPercent:
206                iCount = iCount + 1
207         iCount = iCount - 1
208         if iCount < 0:
209             iCount = 0
210         self.curImg = iCount
211
212     def stopMe(self):
213         self.saveValues()
214         if self.dialog != None:
215             self.stopLoop()
216             self.dialog.hide()
217         self.iServiceReference = None
218         self.dialog = None
219         
220     def saveValues(self):
221         config.plugins.KiddyTimer.lastStartDay.save()
222         config.plugins.KiddyTimer.remainingTime.setValue(int(self.remainingTime))
223         config.plugins.KiddyTimer.remainingTime.save()
224