Added new Plugin KiddyTimer
[vuplus_dvbapp-plugin] / kiddytimer / src / KTmain.py
1 from Components.Label import Label
2 from KTMultiPixmap import KTmultiPixmap
3 from Components.config import config
4 from Screens.Screen import Screen
5 from Screens import Standby
6 from enigma import ePoint, eTimer
7 import KTglob
8 import NavigationInstance
9 import time
10
11 class KiddyTimerScreen(Screen):    
12
13     def __init__(self, session):
14         Screen.__init__(self, session)
15         self.skin = KTglob.SKIN
16         self.onShow.append(self.movePosition)
17
18         self.skin_path = KTglob.plugin_path
19
20         self["TimerGraph"] = KTmultiPixmap()
21         self["TimerText"] = Label(_("??:??"))
22         
23     def renderScreen(self):
24         self["TimerGraph"].setPixmapNum(KTglob.oKiddyTimer.curImg)
25         self["TimerGraph"].show()
26         self.sTimeLeft = KTglob.getTimeFromSeconds( (KTglob.oKiddyTimer.remainingTime + 59) , False ) # Add 59 Seconds to show one minute if less than 1 minute left...
27         self["TimerText"].setText(self.sTimeLeft)
28
29     def movePosition(self):
30         if self.instance:
31             self.getPixmapList()
32             self.instance.move(ePoint(config.plugins.KiddyTimer.position_x.value, config.plugins.KiddyTimer.position_y.value))
33
34     def getPixmapList(self):
35         self.percentageList = []
36         for sPixmap in self["TimerGraph"].pixmapFiles:
37             i = int(sPixmap[-8:-4])
38             self.percentageList.append(i)
39
40 ##############################################################################
41
42 class KiddyTimer():
43
44     def __init__(self):
45         self.session = None 
46         self.dialog = None
47         self.dialogEnabled = False
48
49         self.iServiceReference = None
50         self.curImg = 0
51                     
52         self.loopTimerStep = 1000
53         self.loopTimer = eTimer()
54         self.loopTimer.callback.append(self.calculateTimer)
55     
56     def gotSession(self, session):
57         self.session = session
58         self.initVars()
59     
60     def initVars(self):
61         self.enabled = config.plugins.KiddyTimer.enabled.value
62         # Start- Time of the plugin, used for calculating the remaining time
63         self.pluginStartTime = time.localtime()            
64         # Number of the current day
65         self.dayNr = int(time.strftime("%w" , self.pluginStartTime ))
66         # Current Day to compare with the last saved day. If different -> Reset counter
67         # Number of seconds for the current day
68         iDayTime = KTglob.getSecondsFromClock( config.plugins.KiddyTimer.dayTimes[self.dayNr].timeValue.getValue() )
69         self.currentDayTime = iDayTime
70         self.currentDay = time.strftime("%d.%m.%Y" , self.pluginStartTime)
71         self.lastSavedRemainingTime = config.plugins.KiddyTimer.remainingTime.getValue()
72         self.remainingTime = self.lastSavedRemainingTime
73         if self.enabled == True:            
74             if self.currentDay != config.plugins.KiddyTimer.lastStartDay.getValue():
75                 #Reset all Timers
76                 self.resetTimer()
77
78             if self.dialog == None:
79                 self.dialog = self.session.instantiateDialog(KiddyTimerScreen)
80                 self.dialog.hide()
81
82             self.setDialogStatus(self.timerHasToRun())            
83             self.calculateTimer()
84
85     def startLoop(self):
86         self.loopTimer.start(self.loopTimerStep,1)
87     
88     def stopLoop(self):
89         self.loopTimer.stop()
90     
91     def resetTimer(self):
92         iDayTime = KTglob.getSecondsFromClock( config.plugins.KiddyTimer.dayTimes[self.dayNr].timeValue.getValue() )
93         self.currentDayTime = iDayTime
94         
95         self.lastSavedRemainingTime = self.currentDayTime
96         self.remainingTime = self.currentDayTime
97                 
98         config.plugins.KiddyTimer.lastStartDay.setValue(self.currentDay)
99         self.remainingPercentage = self.remainingTime / self.currentDayTime
100         self.pluginStartTime = time.localtime()
101         
102         self.saveValues()
103     
104     def timerHasToRun(self):
105         iPluginStart = KTglob.getSecondsFromClock( [self.pluginStartTime[3],self.pluginStartTime[4]] )
106         iMonitorEnd = KTglob.getSecondsFromClock(config.plugins.KiddyTimer.monitorEndTime.getValue())  
107         return iPluginStart < iMonitorEnd 
108     
109     def setDialogStatus(self , bStatus):
110         if bStatus == True:
111             self.dialogEnabled = True
112             if self.dialog != None:
113                 self.dialog.show()
114         else:
115             self.dialogEnabled = False
116             if self.dialog != None:
117                 self.dialog.hide()
118
119     def calculateTimer(self):
120         if Standby.inStandby != None:
121             Standby.inStandby.onClose.append(self.endStandby)
122             self.stopMe()            
123         else:
124             if self.dialogEnabled == True:
125                 odtEnd = time.mktime(time.localtime())
126                 iDiff = odtEnd - time.mktime(self.pluginStartTime)
127                 iRemaining = self.lastSavedRemainingTime - iDiff
128                 if iRemaining < 0:
129                     iRemaining = 0
130                 self.remainingTime = iRemaining
131                 self.remainingPercentage = iRemaining / KTglob.oKiddyTimer.currentDayTime
132                 self.saveValues()
133                 
134                 self.setImageNumber()
135                 
136                 if self.remainingTime == 0:
137                     self.iServiceReference = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
138                     NavigationInstance.instance.stopService()
139                 self.dialog.renderScreen()
140             self.startLoop()
141
142     def showHide(self):
143         if config.plugins.KiddyTimer.enabled.value:
144             self.setDialogStatus(True)
145         else:
146             self.setDialogStatus(False)
147         
148     def endStandby(self):
149         self.gotSession(self.session)
150
151     def setImageNumber(self):
152         iCurPercent = int( self.remainingPercentage * 1000 )
153         iCount = 0
154         for iPercent in self.dialog.percentageList:
155            if iCurPercent <= iPercent:
156                iCount = iCount + 1
157         iCount = iCount - 1
158         if iCount < 0:
159             iCount = 0
160         self.curImg = iCount
161
162     def stopMe(self):
163         self.saveValues()
164         if self.dialog != None:
165             self.stopLoop()
166             self.dialog.hide()
167         self.iServiceReference = None
168         self.dialog = None
169         
170     def saveValues(self):
171         config.plugins.KiddyTimer.lastStartDay.save()
172         config.plugins.KiddyTimer.remainingTime.setValue(int(self.remainingTime))
173         config.plugins.KiddyTimer.remainingTime.save()
174