parental locking fixes
[vuplus_dvbapp] / lib / python / Screens / ParentalControlSetup.py
1 from Screen import Screen
2 from Components.ConfigList import ConfigList, ConfigListScreen
3 from Components.ActionMap import NumberActionMap
4 from Components.config import config, getConfigListEntry, ConfigNothing, NoSave, ConfigPIN
5 from Components.ServiceList import ServiceList
6 from Components.ParentalControlList import ParentalControlEntryComponent, ParentalControlList 
7 from Components.ParentalControl import parentalControl
8 from Screens.ChoiceBox import ChoiceBox
9 from Screens.MessageBox import MessageBox
10 from Screens.InputBox import InputBox, Input, PinInput
11 from Screens.ChannelSelection import service_types_tv
12 from Tools.BoundFunction import boundFunction
13 from ServiceReference import ServiceReference
14 from enigma import eServiceCenter, eServiceReference, eTimer
15 from operator import itemgetter
16
17 class ProtectedScreen:
18         def __init__(self):
19                 if self.isProtected():
20                         self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.pinEntered, PinInput, pinList = [self.protectedWithPin()], triesEntry = self.getTriesEntry(), title = self.getPinText(), windowTitle = _("Change pin code")))
21
22         def getTriesEntry(self):
23                 return config.ParentalControl.retries.setuppin
24
25         def getPinText(self):
26                 return _("Please enter the correct pin code")
27
28         def isProtected(self):
29                 return True
30
31         def protectedWithPin(self):
32                 return config.ParentalControl.setuppin.value
33
34         def pinEntered(self, result):
35                 if result is None:
36                         self.close()
37                 elif not result:
38                         self.session.openWithCallback(self.close, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
39
40 class ParentalControlSetup(Screen, ConfigListScreen, ProtectedScreen):
41         def __init__(self, session):
42                 Screen.__init__(self, session)
43                 ProtectedScreen.__init__(self)
44                 self.list = []
45                 ConfigListScreen.__init__(self, self.list)
46                 self.createSetup()
47                 
48                 self["actions"] = NumberActionMap(["SetupActions"],
49                 {
50                         "cancel": self.keyCancel
51                 }, -2)
52
53         def isProtected(self):
54                 return config.ParentalControl.setuppinactive.value and config.ParentalControl.configured.value
55         
56         def createSetup(self):
57                 self.editListEntry = None
58                 self.changePin = None
59                 self.changeSetupPin = None
60                 
61                 self.list = []
62                 self.list.append(getConfigListEntry(_("Enable parental control"), config.ParentalControl.configured))
63                 print "config.ParentalControl.configured.value", config.ParentalControl.configured.value
64                 if config.ParentalControl.configured.value:
65                         #self.list.append(getConfigListEntry(_("Configuration mode"), config.ParentalControl.mode))
66                         self.list.append(getConfigListEntry(_("Protect setup"), config.ParentalControl.setuppinactive))
67                         if config.ParentalControl.setuppinactive.value:
68                                 self.changeSetupPin = getConfigListEntry(_("Change setup pin"), NoSave(ConfigNothing()))
69                                 self.list.append(self.changeSetupPin)
70                         self.list.append(getConfigListEntry(_("Protect services"), config.ParentalControl.servicepinactive))
71                         if config.ParentalControl.servicepinactive.value:
72                                 self.list.append(getConfigListEntry(_("Parental control type"), config.ParentalControl.type))
73                                 if config.ParentalControl.mode.value == "complex":
74                                         self.changePin = getConfigListEntry(_("Change service pins"), NoSave(ConfigNothing()))
75                                         self.list.append(self.changePin)
76                                 elif config.ParentalControl.mode.value == "simple":     
77                                         self.changePin = getConfigListEntry(_("Change service pin"), NoSave(ConfigNothing()))
78                                         self.list.append(self.changePin)
79                                 #self.list.append(getConfigListEntry(_("Remember service pin"), config.ParentalControl.storeservicepin))        
80                                 self.editListEntry = getConfigListEntry(_("Edit services list"), NoSave(ConfigNothing()))
81                                 self.list.append(self.editListEntry)
82                                 
83                 self["config"].list = self.list
84                 self["config"].setList(self.list)
85
86         def keyOK(self):
87                 print "self[\"config\"].l.getCurrentSelection()", self["config"].l.getCurrentSelection()
88                 if self["config"].l.getCurrentSelection() == self.editListEntry:
89                         self.session.open(ParentalControlEditor)
90                 elif self["config"].l.getCurrentSelection() == self.changePin:
91                         if config.ParentalControl.mode.value == "complex":
92                                 pass
93                         else:
94                                 self.session.open(ParentalControlChangePin, config.ParentalControl.servicepin[0], _("service pin"))
95                 elif self["config"].l.getCurrentSelection() == self.changeSetupPin:
96                         self.session.open(ParentalControlChangePin, config.ParentalControl.setuppin, _("setup pin"))
97                 else:
98                         ConfigListScreen.keyRight(self)
99                         print "current selection:", self["config"].l.getCurrentSelection()
100                         self.createSetup()
101
102         def keyLeft(self):
103                 ConfigListScreen.keyLeft(self)
104                 print "current selection:", self["config"].l.getCurrentSelection()
105                 self.createSetup()
106
107         def keyRight(self):
108                 ConfigListScreen.keyRight(self)
109                 print "current selection:", self["config"].l.getCurrentSelection()
110                 self.createSetup()
111
112         def SetupPinMessageCallback(self, value):
113                 if value:
114                         self.session.openWithCallback(self.cancelCB, ParentalControlChangePin, config.ParentalControl.setuppin, _("setup pin"))
115                 else:
116                         config.ParentalControl.setuppinactive.value = False
117                         self.keyCancel()
118
119         def ServicePinMessageCallback(self, value):
120                 if value:
121                         self.session.openWithCallback(self.cancelCB, ParentalControlChangePin, config.ParentalControl.servicepin[0], _("service pin"))
122                 else:
123                         config.ParentalControl.servicepinactive.value = False
124                         self.keyCancel()
125
126         def cancelCB(self,value):
127                 self.keyCancel()
128
129         def keyCancel(self):
130                 if config.ParentalControl.setuppinactive.value and config.ParentalControl.setuppin.value == 'aaaa':
131                         self.session.openWithCallback(self.SetupPinMessageCallback, MessageBox, _("No valid setup PIN found!\nDo you like to change the setup PIN now?\nWhen you say 'No' here the setup protection stay disabled!"), MessageBox.TYPE_YESNO)
132                 elif config.ParentalControl.servicepinactive.value and config.ParentalControl.servicepin[0].value == 'aaaa':
133                         self.session.openWithCallback(self.ServicePinMessageCallback, MessageBox, _("No valid service PIN found!\nDo you like to change the service PIN now?\nWhen you say 'No' here the service protection stay disabled!"), MessageBox.TYPE_YESNO)
134                 else:
135                         for x in self["config"].list:
136                                 x[1].save()
137                         self.close()
138
139         def keyNumberGlobal(self, number):
140                 pass
141
142 SPECIAL_CHAR = 96
143 class ParentalControlEditor(Screen):
144         def __init__(self, session):
145                 Screen.__init__(self, session)
146                 self.list = []
147                 self.servicelist = ParentalControlList(self.list)
148                 self["servicelist"] = self.servicelist;
149                 #self.onShown.append(self.chooseLetter)
150                 self.currentLetter = chr(SPECIAL_CHAR)
151                 self.readServiceList()
152                 self.chooseLetterTimer = eTimer()
153                 self.chooseLetterTimer.timeout.get().append(self.chooseLetter)
154                 self.onLayoutFinish.append(self.LayoutFinished)
155
156                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions", "NumberActions"],
157                 {
158                         "ok": self.select,
159                         "cancel": self.cancel,
160                         #"left": self.keyLeft,
161                         #"right": self.keyRight,
162                         "1": self.keyNumberGlobal,
163                         "2": self.keyNumberGlobal,
164                         "3": self.keyNumberGlobal,
165                         "4": self.keyNumberGlobal,
166                         "5": self.keyNumberGlobal,
167                         "6": self.keyNumberGlobal,
168                         "7": self.keyNumberGlobal,
169                         "8": self.keyNumberGlobal,
170                         "9": self.keyNumberGlobal,
171                         "0": self.keyNumberGlobal
172                 }, -1)
173
174         def LayoutFinished(self):
175                 self.chooseLetterTimer.start(0, True)
176
177         def cancel(self):
178                 self.chooseLetter()
179
180         def select(self):
181                 self.servicelist.toggleSelectedLock()
182
183         def keyNumberGlobal(self, number):
184                 pass
185
186         def readServiceList(self):
187                 serviceHandler = eServiceCenter.getInstance()
188                 refstr = '%s ORDER BY name' % (service_types_tv)
189                 self.root = eServiceReference(refstr)
190                 self.servicesList = {}
191                 list = serviceHandler.list(self.root)
192                 if list is not None:
193                         services = list.getContent("CN", True) #(servicecomparestring, name)
194                         for s in services:
195                                 key = s[1].lower()[0]
196                                 if key < 'a' or key > 'z':
197                                         key = chr(SPECIAL_CHAR)
198                                 #key = str(key)
199                                 if not self.servicesList.has_key(key):
200                                         self.servicesList[key] = []
201                                 self.servicesList[key].append(s)
202
203         def chooseLetter(self):
204                 print "choose letter"
205                 mylist = []
206                 for x in self.servicesList.keys():
207                         if x == chr(SPECIAL_CHAR):
208                                 x = ("special characters", x)
209                         else:
210                                 x = (x, x)
211                         mylist.append(x)
212                 mylist.sort(key=itemgetter(1))
213                 sel = ord(self.currentLetter) - SPECIAL_CHAR
214                 self.session.openWithCallback(self.letterChosen, ChoiceBox, title=_("Show services beginning with"), list=mylist, keys = [], selection = sel)
215
216         def letterChosen(self, result):
217                 if result is not None:
218                         print "result:", result
219                         self.currentLetter = result[1]
220                         self.list = []
221                         for x in self.servicesList[result[1]]:
222                                 self.list.append(ParentalControlEntryComponent(x[0], x[1], parentalControl.getProtectionLevel(x[0]) != -1))
223                         self.servicelist.setList(self.list)
224                 else:
225                         parentalControl.save()
226                         self.close()
227
228 class ParentalControlChangePin(Screen, ConfigListScreen, ProtectedScreen):
229         def __init__(self, session, pin, pinname):
230                 Screen.__init__(self, session)
231                 self.pin = pin
232                 self.list = []
233                 self.pin1 = ConfigPIN(default = 1111, censor = "*")
234                 self.pin2 = ConfigPIN(default = 1112, censor = "*")
235                 self.list.append(getConfigListEntry(_("New pin"), NoSave(self.pin1)))
236                 self.list.append(getConfigListEntry(_("Reenter new pin"), NoSave(self.pin2)))
237                 ConfigListScreen.__init__(self, self.list)
238 #               print "old pin:", pin
239                 #if pin.value != "aaaa":
240                         #self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.pinEntered, PinInput, pinList = [self.pin.value], title = _("please enter the old pin"), windowTitle = _("Change pin code")))
241                 ProtectedScreen.__init__(self)
242                 
243                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions"],
244                 {
245                         "cancel": self.cancel,
246                 }, -1)
247
248         def getPinText(self):
249                 return _("Please enter the old pin code")
250
251         def isProtected(self):
252                 return (self.pin.value != "aaaa")
253
254         def protectedWithPin(self):
255                 return self.pin.value
256
257 #       def pinEntered(self, result):
258                 #if result[0] is None:
259                         #self.close()
260                 #if not result[0]:
261                         #print result, "-", self.pin.value
262                         #self.session.openWithCallback(self.close, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
263
264         def keyOK(self):
265                 if self.pin1.value == self.pin2.value:
266                         self.pin.value = self.pin1.value
267                         self.pin.save()
268                         self.session.openWithCallback(self.close, MessageBox, _("The pin code has been changed successfully."), MessageBox.TYPE_INFO)
269                 else:
270                         self.session.open(MessageBox, _("The pin codes you entered are different."), MessageBox.TYPE_ERROR)
271
272         def cancel(self):
273                 self.close(None)
274
275         def keyNumberGlobal(self, number):
276                 ConfigListScreen.keyNumberGlobal(self, number)
277