Merge remote branch 'remotes/origin/bug_600_softwaremanager_small_fixes'
[vuplus_dvbapp] / lib / python / Screens / ParentalControlSetup.py
1 from Screen import Screen
2 from Components.ConfigList import ConfigListScreen
3 from Components.ActionMap import NumberActionMap
4 from Components.config import config, getConfigListEntry, ConfigNothing, NoSave, ConfigPIN
5 from Components.ParentalControlList import ParentalControlEntryComponent, ParentalControlList 
6 from Components.ParentalControl import parentalControl
7 from Components.Sources.StaticText import StaticText
8 from Screens.ChoiceBox import ChoiceBox
9 from Screens.MessageBox import MessageBox
10 from Screens.InputBox import PinInput
11 from Screens.ChannelSelection import service_types_tv
12 from Tools.BoundFunction import boundFunction
13 from enigma import eServiceCenter, eTimer, eServiceReference
14 from operator import itemgetter
15
16 class ProtectedScreen:
17         def __init__(self):
18                 if self.isProtected():
19                         self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.pinEntered, PinInput, pinList = [self.protectedWithPin()], triesEntry = self.getTriesEntry(), title = self.getPinText(), windowTitle = _("Enter pin code")))
20
21         def getTriesEntry(self):
22                 return config.ParentalControl.retries.setuppin
23
24         def getPinText(self):
25                 return _("Please enter the correct pin code")
26
27         def isProtected(self):
28                 return True
29
30         def protectedWithPin(self):
31                 return config.ParentalControl.setuppin.value
32
33         def pinEntered(self, result):
34                 if result is None:
35                         self.close()
36                 elif not result:
37                         self.session.openWithCallback(self.close, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
38
39 class ParentalControlSetup(Screen, ConfigListScreen, ProtectedScreen):
40         def __init__(self, session):
41                 Screen.__init__(self, session)
42                 ProtectedScreen.__init__(self)
43                 # for the skin: first try ParentalControlSetup, then Setup, this allows individual skinning
44                 self.skinName = ["ParentalControlSetup", "Setup" ]
45                 self.setup_title = _("Parental control setup")
46                 self.onChangedEntry = [ ]
47
48                 self.list = []
49                 ConfigListScreen.__init__(self, self.list, session = self.session, on_change = self.changedEntry)
50                 self.createSetup()
51                 
52                 self["actions"] = NumberActionMap(["SetupActions"],
53                 {
54                   "cancel": self.keyCancel,
55                   "save": self.keyCancel
56                 }, -2)
57                 self["key_red"] = StaticText(_("Cancel"))
58                 self["key_green"] = StaticText(_("OK"))
59                 self.onLayoutFinish.append(self.layoutFinished)
60
61         def layoutFinished(self):
62                 self.setTitle(self.setup_title)
63
64         def isProtected(self):
65                 return config.ParentalControl.setuppinactive.value and config.ParentalControl.configured.value
66         
67         def createSetup(self):
68                 self.editListEntry = None
69                 self.changePin = None
70                 self.changeSetupPin = None
71                 
72                 self.list = []
73                 self.list.append(getConfigListEntry(_("Enable parental control"), config.ParentalControl.configured))
74                 print "config.ParentalControl.configured.value", config.ParentalControl.configured.value
75                 self.editBouquetListEntry = -1
76                 self.reloadLists = -1
77                 if config.ParentalControl.configured.value:
78                         #self.list.append(getConfigListEntry(_("Configuration mode"), config.ParentalControl.mode))
79                         self.list.append(getConfigListEntry(_("Protect setup"), config.ParentalControl.setuppinactive))
80                         if config.ParentalControl.setuppinactive.value:
81                                 self.changeSetupPin = getConfigListEntry(_("Change setup PIN"), NoSave(ConfigNothing()))
82                                 self.list.append(self.changeSetupPin)
83                         self.list.append(getConfigListEntry(_("Protect services"), config.ParentalControl.servicepinactive))
84                         if config.ParentalControl.servicepinactive.value:
85                                 self.list.append(getConfigListEntry(_("Parental control type"), config.ParentalControl.type))
86                                 if config.ParentalControl.mode.value == "complex":
87                                         self.changePin = getConfigListEntry(_("Change service PINs"), NoSave(ConfigNothing()))
88                                         self.list.append(self.changePin)
89                                 elif config.ParentalControl.mode.value == "simple":     
90                                         self.changePin = getConfigListEntry(_("Change service PIN"), NoSave(ConfigNothing()))
91                                         self.list.append(self.changePin)
92                                 #Added Option to remember the service pin
93                                 self.list.append(getConfigListEntry(_("Remember service PIN"), config.ParentalControl.storeservicepin)) 
94                                 #Added Option to remember the cancellation of service pin entry
95                                 self.list.append(getConfigListEntry(_("Remember service PIN cancel"), config.ParentalControl.storeservicepincancel))    
96                                 self.editListEntry = getConfigListEntry(_("Edit services list"), NoSave(ConfigNothing()))
97                                 self.list.append(self.editListEntry)
98                                 #New funtion: Possibility to add Bouquets to whitelist / blacklist
99                                 self.editBouquetListEntry = getConfigListEntry(_("Edit bouquets list"), NoSave(ConfigNothing()))
100                                 self.list.append(self.editBouquetListEntry)
101                                 #New option to reload service lists (for example if bouquets have changed)
102                                 self.reloadLists = getConfigListEntry(_("Reload Black-/Whitelists"), NoSave(ConfigNothing()))
103                                 self.list.append(self.reloadLists)
104                                 
105                 self["config"].list = self.list
106                 self["config"].setList(self.list)
107
108         def keyOK(self):
109                 print "self[\"config\"].l.getCurrentSelection()", self["config"].l.getCurrentSelection()
110                 if self["config"].l.getCurrentSelection() == self.editListEntry:
111                         self.session.open(ParentalControlEditor)
112                 elif self["config"].l.getCurrentSelection() == self.editBouquetListEntry:
113                         self.session.open(ParentalControlBouquetEditor)
114                 elif self["config"].l.getCurrentSelection() == self.changePin:
115                         if config.ParentalControl.mode.value == "complex":
116                                 pass
117                         else:
118                                 self.session.open(ParentalControlChangePin, config.ParentalControl.servicepin[0], _("service PIN"))
119                 elif self["config"].l.getCurrentSelection() == self.changeSetupPin:
120                         self.session.open(ParentalControlChangePin, config.ParentalControl.setuppin, _("setup PIN"))
121                 elif self["config"].l.getCurrentSelection() == self.reloadLists:
122                         parentalControl.open()
123                 else:
124                         ConfigListScreen.keyRight(self)
125                         print "current selection:", self["config"].l.getCurrentSelection()
126                         self.createSetup()
127
128         def keyLeft(self):
129                 ConfigListScreen.keyLeft(self)
130                 print "current selection:", self["config"].l.getCurrentSelection()
131                 self.createSetup()
132
133         def keyRight(self):
134                 ConfigListScreen.keyRight(self)
135                 print "current selection:", self["config"].l.getCurrentSelection()
136                 self.createSetup()
137
138         def SetupPinMessageCallback(self, value):
139                 if value:
140                         self.session.openWithCallback(self.cancelCB, ParentalControlChangePin, config.ParentalControl.setuppin, _("setup PIN"))
141                 else:
142                         config.ParentalControl.setuppinactive.value = False
143                         self.keyCancel()
144
145         def ServicePinMessageCallback(self, value):
146                 if value:
147                         self.session.openWithCallback(self.cancelCB, ParentalControlChangePin, config.ParentalControl.servicepin[0], _("service PIN"))
148                 else:
149                         config.ParentalControl.servicepinactive.value = False
150                         self.keyCancel()
151
152         def cancelCB(self,value):
153                 self.keyCancel()
154
155         def keyCancel(self):
156                 if config.ParentalControl.setuppinactive.value and config.ParentalControl.setuppin.value == 'aaaa':
157                         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)
158                 elif config.ParentalControl.servicepinactive.value and config.ParentalControl.servicepin[0].value == 'aaaa':
159                         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)
160                 else:
161                         for x in self["config"].list:
162                                 x[1].save()
163                         self.close()
164
165         def keyNumberGlobal(self, number):
166                 pass
167
168         # for summary:
169         def changedEntry(self):
170                 for x in self.onChangedEntry:
171                         x()
172
173         def getCurrentEntry(self):
174                 return self["config"].getCurrent()[0]
175
176         def getCurrentValue(self):
177                 return str(self["config"].getCurrent()[1].getText())
178
179         def createSummary(self):
180                 from Screens.Setup import SetupSummary
181                 return SetupSummary
182
183 SPECIAL_CHAR = 96
184 class ParentalControlEditor(Screen):
185         def __init__(self, session):
186                 Screen.__init__(self, session)
187                 self.list = []
188                 self.servicelist = ParentalControlList(self.list)
189                 self["servicelist"] = self.servicelist;
190                 #self.onShown.append(self.chooseLetter)
191                 self.currentLetter = chr(SPECIAL_CHAR)
192                 self.readServiceList()
193                 self.chooseLetterTimer = eTimer()
194                 self.chooseLetterTimer.callback.append(self.chooseLetter)
195                 self.onLayoutFinish.append(self.LayoutFinished)
196
197                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions", "NumberActions"],
198                 {
199                         "ok": self.select,
200                         "cancel": self.cancel,
201                         #"left": self.keyLeft,
202                         #"right": self.keyRight,
203                         "1": self.keyNumberGlobal,
204                         "2": self.keyNumberGlobal,
205                         "3": self.keyNumberGlobal,
206                         "4": self.keyNumberGlobal,
207                         "5": self.keyNumberGlobal,
208                         "6": self.keyNumberGlobal,
209                         "7": self.keyNumberGlobal,
210                         "8": self.keyNumberGlobal,
211                         "9": self.keyNumberGlobal,
212                         "0": self.keyNumberGlobal
213                 }, -1)
214
215         def LayoutFinished(self):
216                 self.chooseLetterTimer.start(0, True)
217
218         def cancel(self):
219                 self.chooseLetter()
220
221         def select(self):
222                 self.servicelist.toggleSelectedLock()
223
224         def keyNumberGlobal(self, number):
225                 pass
226
227         def readServiceList(self):
228                 serviceHandler = eServiceCenter.getInstance()
229                 refstr = '%s ORDER BY name' % (service_types_tv)
230                 self.root = eServiceReference(refstr)
231                 self.servicesList = {}
232                 list = serviceHandler.list(self.root)
233                 if list is not None:
234                         services = list.getContent("CN", True) #(servicecomparestring, name)
235                         for s in services:
236                                 key = s[1].lower()[0]
237                                 if key < 'a' or key > 'z':
238                                         key = chr(SPECIAL_CHAR)
239                                 #key = str(key)
240                                 if not self.servicesList.has_key(key):
241                                         self.servicesList[key] = []
242                                 self.servicesList[key].append(s)
243                         
244         def chooseLetter(self):
245                 print "choose letter"
246                 mylist = []
247                 for x in self.servicesList.keys():
248                         if x == chr(SPECIAL_CHAR):
249                                 x = (_("special characters"), x)
250                         else:
251                                 x = (x, x)
252                         mylist.append(x)
253                 mylist.sort(key=itemgetter(1))
254                 sel = ord(self.currentLetter) - SPECIAL_CHAR
255                 self.session.openWithCallback(self.letterChosen, ChoiceBox, title=_("Show services beginning with"), list=mylist, keys = [], selection = sel)
256
257         def letterChosen(self, result):
258                 if result is not None:
259                         print "result:", result
260                         self.currentLetter = result[1]
261                         #Replace getProtectionLevel by new getProtectionType
262                         self.list = [ParentalControlEntryComponent(x[0], x[1], parentalControl.getProtectionType(x[0])) for x in self.servicesList[result[1]]]
263                         self.servicelist.setList(self.list)
264                 else:
265                         parentalControl.save()
266                         self.close()
267
268 class ParentalControlBouquetEditor(Screen):
269         #This new class allows adding complete bouquets to black- and whitelists
270         #The servicereference that is stored for bouquets is their refstr as listed in bouquets.tv
271         def __init__(self, session):
272                 Screen.__init__(self, session)
273                 self.skinName = "ParentalControlEditor"
274                 self.list = []
275                 self.bouquetslist = ParentalControlList(self.list)
276                 self["servicelist"] = self.bouquetslist;
277                 self.readBouquetList()
278                 self.onLayoutFinish.append(self.selectBouquet)
279
280                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions"],
281                 {
282                         "ok": self.select,
283                         "cancel": self.cancel
284                 }, -1)
285
286         def cancel(self):
287                 parentalControl.save()
288                 self.close()
289
290         def select(self):
291                 self.bouquetslist.toggleSelectedLock()
292
293         def readBouquetList(self):
294                 serviceHandler = eServiceCenter.getInstance()
295                 refstr = '1:134:1:0:0:0:0:0:0:0:FROM BOUQUET \"bouquets.tv\" ORDER BY bouquet'
296                 bouquetroot = eServiceReference(refstr)
297                 self.bouquetlist = {}
298                 list = serviceHandler.list(bouquetroot)
299                 if list is not None:
300                         self.bouquetlist = list.getContent("CN", True)
301         
302         def selectBouquet(self):
303                 self.list = [ParentalControlEntryComponent(x[0], x[1], parentalControl.getProtectionType(x[0])) for x in self.bouquetlist]
304                 self.bouquetslist.setList(self.list)
305
306 class ParentalControlChangePin(Screen, ConfigListScreen, ProtectedScreen):
307         def __init__(self, session, pin, pinname):
308                 Screen.__init__(self, session)
309                 # for the skin: first try ParentalControlChangePin, then Setup, this allows individual skinning
310                 self.skinName = ["ParentalControlChangePin", "Setup" ]
311                 self.setup_title = _("Change pin code")
312                 self.onChangedEntry = [ ]
313
314                 self.pin = pin
315                 self.list = []
316                 self.pin1 = ConfigPIN(default = 1111, censor = "*")
317                 self.pin2 = ConfigPIN(default = 1112, censor = "*")
318                 self.pin1.addEndNotifier(boundFunction(self.valueChanged, 1))
319                 self.pin2.addEndNotifier(boundFunction(self.valueChanged, 2))
320                 self.list.append(getConfigListEntry(_("New PIN"), NoSave(self.pin1)))
321                 self.list.append(getConfigListEntry(_("Reenter new PIN"), NoSave(self.pin2)))
322                 ConfigListScreen.__init__(self, self.list)
323 #               print "old pin:", pin
324                 #if pin.value != "aaaa":
325                         #self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.pinEntered, PinInput, pinList = [self.pin.value], title = _("please enter the old pin"), windowTitle = _("Change pin code")))
326                 ProtectedScreen.__init__(self)
327                 
328                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions"],
329                 {
330                         "cancel": self.cancel,
331                         "red": self.cancel,
332                         "save": self.keyOK,
333                 }, -1)
334                 self["key_red"] = StaticText(_("Cancel"))
335                 self["key_green"] = StaticText(_("OK"))
336                 self.onLayoutFinish.append(self.layoutFinished)
337
338         def layoutFinished(self):
339                 self.setTitle(self.setup_title)
340
341         def valueChanged(self, pin, value):
342                 if pin == 1:
343                         self["config"].setCurrentIndex(1)
344                 elif pin == 2:
345                         self.keyOK()
346
347         def getPinText(self):
348                 return _("Please enter the old PIN code")
349
350         def isProtected(self):
351                 return (self.pin.value != "aaaa")
352
353         def protectedWithPin(self):
354                 return self.pin.value
355
356 #       def pinEntered(self, result):
357                 #if result[0] is None:
358                         #self.close()
359                 #if not result[0]:
360                         #print result, "-", self.pin.value
361                         #self.session.openWithCallback(self.close, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
362
363         def keyOK(self):
364                 if self.pin1.value == self.pin2.value:
365                         self.pin.value = self.pin1.value
366                         self.pin.save()
367                         self.session.openWithCallback(self.close, MessageBox, _("The PIN code has been changed successfully."), MessageBox.TYPE_INFO)
368                 else:
369                         self.session.open(MessageBox, _("The PIN codes you entered are different."), MessageBox.TYPE_ERROR)
370
371         def cancel(self):
372                 self.close(None)
373
374         def keyNumberGlobal(self, number):
375                 ConfigListScreen.keyNumberGlobal(self, number)
376
377         # for summary:
378         def changedEntry(self):
379                 for x in self.onChangedEntry:
380                         x()
381
382         def getCurrentEntry(self):
383                 return self["config"].getCurrent()[0]
384
385         def getCurrentValue(self):
386                 return str(self["config"].getCurrent()[1].getText())
387
388         def createSummary(self):
389                 from Screens.Setup import SetupSummary
390                 return SetupSummary