Version 1.0: Complete code- cleanup. New Feature "cheat- detection" for kids switchin...
[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, configfile, getConfigListEntry
7 from KTmain import kiddyTimer
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,440" 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,330" scrollbarMode="showOnDemand" />
39       <widget name="LastDayStarted" position="10,380" size="540,20" zPosition="4" font="Regular;18" foregroundColor="#cccccc" />
40       <widget name="RemainingTime" position="10,400" size="540,20" zPosition="4" font="Regular;18" foregroundColor="#cccccc" />
41       <widget name="PluginInfo" position="10,420" 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 before"), config.plugins.KiddyTimer.monitorStartTime ), 
54             getConfigListEntry(_("Don't monitor TV started after"), config.plugins.KiddyTimer.monitorEndTime ), 
55             getConfigListEntry(_("Style of timer"), config.plugins.KiddyTimer.timerStyle ),
56             getConfigListEntry(_("Timeout for activation dialog"), config.plugins.KiddyTimer.activationDialogTimeout )
57             ]
58         for i in range(0,7):
59             self.list.append(getConfigListEntry(KTglob.DAYNAMES[i], config.plugins.KiddyTimer.dayTimes[i].timeValue))                
60
61         ConfigListScreen.__init__(self, self.list)
62
63         self["config"].list = self.list
64
65         self.skin_path = KTglob.plugin_path
66         self.kiddyTimerStopped = False;
67         
68         # Temporarily stop timer as long as we are in the setup screen
69         if kiddyTimer.active:
70             self.kiddyTimerStopped = True;
71             kiddyTimer.stopTimer()
72             
73         # Plugin Information
74         self.remainingTime = config.plugins.KiddyTimer.remainingTime.value
75         sRemainingTime = KTglob.getTimeFromSeconds(self.remainingTime , True )
76
77         self["PluginInfo"] = Label(_("Plugin: %(plugin)s , Version: %(version)s") %dict(plugin=KTglob.PLUGIN_BASE,version=KTglob.PLUGIN_VERSION))
78         self["RemainingTime"] = Label(_("Remaining time: %s") %sRemainingTime)
79         self["LastDayStarted"] = Label(_("Last day started: %s") % config.plugins.KiddyTimer.lastStartDay.getValue())
80         
81         # BUTTONS
82         self["key_red"] = Button(_("Cancel"))
83         self["key_green"] = Button(_("Save"))
84         self["key_yellow"] = Button(_("Reset clock"))
85         self["key_blue"] = Button(_("Move clock"))
86
87         self["setupActions"] = NumberActionMap(["SetupActions", "ColorActions"],
88         {
89             "save": self.save,
90             "cancel": self.cancel,
91             "green": self.save,
92             "red": self.cancel,
93             "ok": self.save,
94             "blue": self.keyPositioner,
95             "yellow": self.resetTimer
96         }, -2)
97
98     def pinEntered(self, result):
99         if result is None:
100             self.cancel()
101         elif not result:
102             self.session.openWithCallback(self.pinCancel, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
103     
104     def pinCancel(self, result):
105         self.cancel()
106                
107     def keyPositioner(self):
108         self.session.open(KiddyTimerPositioner)
109
110     def resetTimer(self):
111         self.remainingTime = KTglob.getTodaysTimeInSeconds()
112         config.plugins.KiddyTimer.remainingTime.value = int(self.remainingTime)
113         config.plugins.KiddyTimer.remainingTime.save()
114
115         sRemainingTime = KTglob.getTimeFromSeconds(self.remainingTime , True )
116         self["RemainingTime"].setText(_("Remaining time: %s") %sRemainingTime)
117
118     def save(self):
119         if self.remainingTime > KTglob.getTodaysTimeInSeconds():
120             self.resetTimer()
121         for x in self["config"].list:
122             x[1].save()
123         configfile.save() 
124             
125         if config.plugins.KiddyTimer.enabled.value:
126             kiddyTimer.startTimer()
127         self.close()
128
129     def cancel(self):
130         for x in self["config"].list:
131             x[1].cancel()
132         self.close()
133         if self.kiddyTimerStopped:
134             kiddyTimer.startTimer()
135         
136     def protectedWithPin(self):
137         return config.plugins.KiddyTimer.pin.getValue()
138
139 class KiddyTimerPositioner(Screen, MovableScreen):
140     def __init__(self, session):
141         Screen.__init__(self, session)
142         self.skin = KTglob.SKIN
143
144         self.skin_path = KTglob.plugin_path
145
146         self["TimerGraph"] = KTmultiPixmap()
147         self["TimerText"] = Label(_("01:00"))
148         self["TimerSlider"] = ProgressBar()
149         self["TimerSliderText"] = Label(_("01:00"))
150         
151         if config.plugins.KiddyTimer.timerStyle.value == "clock":
152             self["TimerGraph"].show()
153             self["TimerText"].show()
154             self["TimerSlider"].hide()    
155             self["TimerSliderText"].hide()
156         else:
157             self["TimerGraph"].hide()
158             self["TimerText"].hide()
159             self["TimerSlider"].show()
160             self["TimerSliderText"].show()
161
162         self["actions"] = ActionMap(["OkCancelActions"] , 
163         {
164          "ok":      self.keyOK,
165          "cancel":  self.keyCancel
166         }, -1)
167
168         MovableScreen.__init__(self, config.plugins.KiddyTimer, [], 82 , 82)
169         self.startMoving()
170         
171     def keyOK(self):
172         self.close()
173     
174     def keyCancel(self):
175         self.close()