Merge commit 'origin/translations' into experimental
[enigma2.git] / 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 = _("Change 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                 if config.ParentalControl.configured.value:
76                         #self.list.append(getConfigListEntry(_("Configuration mode"), config.ParentalControl.mode))
77                         self.list.append(getConfigListEntry(_("Protect setup"), config.ParentalControl.setuppinactive))
78                         if config.ParentalControl.setuppinactive.value:
79                                 self.changeSetupPin = getConfigListEntry(_("Change setup pin"), NoSave(ConfigNothing()))
80                                 self.list.append(self.changeSetupPin)
81                         self.list.append(getConfigListEntry(_("Protect services"), config.ParentalControl.servicepinactive))
82                         if config.ParentalControl.servicepinactive.value:
83                                 self.list.append(getConfigListEntry(_("Parental control type"), config.ParentalControl.type))
84                                 if config.ParentalControl.mode.value == "complex":
85                                         self.changePin = getConfigListEntry(_("Change service pins"), NoSave(ConfigNothing()))
86                                         self.list.append(self.changePin)
87                                 elif config.ParentalControl.mode.value == "simple":     
88                                         self.changePin = getConfigListEntry(_("Change service pin"), NoSave(ConfigNothing()))
89                                         self.list.append(self.changePin)
90                                 #self.list.append(getConfigListEntry(_("Remember service pin"), config.ParentalControl.storeservicepin))        
91                                 self.editListEntry = getConfigListEntry(_("Edit services list"), NoSave(ConfigNothing()))
92                                 self.list.append(self.editListEntry)
93
94                 self["config"].list = self.list
95                 self["config"].setList(self.list)
96
97         def keyOK(self):
98                 print "self[\"config\"].l.getCurrentSelection()", self["config"].l.getCurrentSelection()
99                 if self["config"].l.getCurrentSelection() == self.editListEntry:
100                         self.session.open(ParentalControlEditor)
101                 elif self["config"].l.getCurrentSelection() == self.changePin:
102                         if config.ParentalControl.mode.value == "complex":
103                                 pass
104                         else:
105                                 self.session.open(ParentalControlChangePin, config.ParentalControl.servicepin[0], _("service pin"))
106                 elif self["config"].l.getCurrentSelection() == self.changeSetupPin:
107                         self.session.open(ParentalControlChangePin, config.ParentalControl.setuppin, _("setup pin"))
108                 else:
109                         ConfigListScreen.keyRight(self)
110                         print "current selection:", self["config"].l.getCurrentSelection()
111                         self.createSetup()
112
113         def keyLeft(self):
114                 ConfigListScreen.keyLeft(self)
115                 print "current selection:", self["config"].l.getCurrentSelection()
116                 self.createSetup()
117
118         def keyRight(self):
119                 ConfigListScreen.keyRight(self)
120                 print "current selection:", self["config"].l.getCurrentSelection()
121                 self.createSetup()
122
123         def SetupPinMessageCallback(self, value):
124                 if value:
125                         self.session.openWithCallback(self.cancelCB, ParentalControlChangePin, config.ParentalControl.setuppin, _("setup pin"))
126                 else:
127                         config.ParentalControl.setuppinactive.value = False
128                         self.keyCancel()
129
130         def ServicePinMessageCallback(self, value):
131                 if value:
132                         self.session.openWithCallback(self.cancelCB, ParentalControlChangePin, config.ParentalControl.servicepin[0], _("service pin"))
133                 else:
134                         config.ParentalControl.servicepinactive.value = False
135                         self.keyCancel()
136
137         def cancelCB(self,value):
138                 self.keyCancel()
139
140         def keyCancel(self):
141                 if config.ParentalControl.setuppinactive.value and config.ParentalControl.setuppin.value == 'aaaa':
142                         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)
143                 elif config.ParentalControl.servicepinactive.value and config.ParentalControl.servicepin[0].value == 'aaaa':
144                         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)
145                 else:
146                         for x in self["config"].list:
147                                 x[1].save()
148                         self.close()
149
150         def keyNumberGlobal(self, number):
151                 pass
152         # for summary:
153         def changedEntry(self):
154                 for x in self.onChangedEntry:
155                         x()
156
157         def getCurrentEntry(self):
158                 return self["config"].getCurrent()[0]
159
160         def getCurrentValue(self):
161                 return str(self["config"].getCurrent()[1].getText())
162
163         def createSummary(self):
164                 from Screens.Setup import SetupSummary
165                 return SetupSummary
166
167 SPECIAL_CHAR = 96
168 class ParentalControlEditor(Screen):
169         def __init__(self, session):
170                 Screen.__init__(self, session)
171                 self.list = []
172                 self.servicelist = ParentalControlList(self.list)
173                 self["servicelist"] = self.servicelist;
174                 #self.onShown.append(self.chooseLetter)
175                 self.currentLetter = chr(SPECIAL_CHAR)
176                 self.readServiceList()
177                 self.chooseLetterTimer = eTimer()
178                 self.chooseLetterTimer.callback.append(self.chooseLetter)
179                 self.onLayoutFinish.append(self.LayoutFinished)
180
181                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions", "NumberActions"],
182                 {
183                         "ok": self.select,
184                         "cancel": self.cancel,
185                         #"left": self.keyLeft,
186                         #"right": self.keyRight,
187                         "1": self.keyNumberGlobal,
188                         "2": self.keyNumberGlobal,
189                         "3": self.keyNumberGlobal,
190                         "4": self.keyNumberGlobal,
191                         "5": self.keyNumberGlobal,
192                         "6": self.keyNumberGlobal,
193                         "7": self.keyNumberGlobal,
194                         "8": self.keyNumberGlobal,
195                         "9": self.keyNumberGlobal,
196                         "0": self.keyNumberGlobal
197                 }, -1)
198
199         def LayoutFinished(self):
200                 self.chooseLetterTimer.start(0, True)
201
202         def cancel(self):
203                 self.chooseLetter()
204
205         def select(self):
206                 self.servicelist.toggleSelectedLock()
207
208         def keyNumberGlobal(self, number):
209                 pass
210
211         def readServiceList(self):
212                 serviceHandler = eServiceCenter.getInstance()
213                 refstr = '%s ORDER BY name' % (service_types_tv)
214                 self.root = eServiceReference(refstr)
215                 self.servicesList = {}
216                 list = serviceHandler.list(self.root)
217                 if list is not None:
218                         services = list.getContent("CN", True) #(servicecomparestring, name)
219                         for s in services:
220                                 key = s[1].lower()[0]
221                                 if key < 'a' or key > 'z':
222                                         key = chr(SPECIAL_CHAR)
223                                 #key = str(key)
224                                 if not self.servicesList.has_key(key):
225                                         self.servicesList[key] = []
226                                 self.servicesList[key].append(s)
227
228         def chooseLetter(self):
229                 print "choose letter"
230                 mylist = []
231                 for x in self.servicesList.keys():
232                         if x == chr(SPECIAL_CHAR):
233                                 x = ("special characters", x)
234                         else:
235                                 x = (x, x)
236                         mylist.append(x)
237                 mylist.sort(key=itemgetter(1))
238                 sel = ord(self.currentLetter) - SPECIAL_CHAR
239                 self.session.openWithCallback(self.letterChosen, ChoiceBox, title=_("Show services beginning with"), list=mylist, keys = [], selection = sel)
240
241         def letterChosen(self, result):
242                 if result is not None:
243                         print "result:", result
244                         self.currentLetter = result[1]
245                         self.list = [ParentalControlEntryComponent(x[0], x[1], parentalControl.getProtectionLevel(x[0]) != -1) for x in self.servicesList[result[1]]]
246                         self.servicelist.setList(self.list)
247                 else:
248                         parentalControl.save()
249                         self.close()
250
251 class ParentalControlChangePin(Screen, ConfigListScreen, ProtectedScreen):
252         def __init__(self, session, pin, pinname):
253                 Screen.__init__(self, session)
254                 # for the skin: first try ParentalControlChangePin, then Setup, this allows individual skinning
255                 self.skinName = ["ParentalControlChangePin", "Setup" ]
256                 self.setup_title = _("Change pin code")
257                 self.onChangedEntry = [ ]
258
259                 self.pin = pin
260                 self.list = []
261                 self.pin1 = ConfigPIN(default = 1111, censor = "*")
262                 self.pin2 = ConfigPIN(default = 1112, censor = "*")
263                 self.pin1.addEndNotifier(boundFunction(self.valueChanged, 1))
264                 self.pin2.addEndNotifier(boundFunction(self.valueChanged, 2))
265                 self.list.append(getConfigListEntry(_("New pin"), NoSave(self.pin1)))
266                 self.list.append(getConfigListEntry(_("Reenter new pin"), NoSave(self.pin2)))
267                 ConfigListScreen.__init__(self, self.list, session = self.session, on_change = self.changedEntry)
268 #               print "old pin:", pin
269                 #if pin.value != "aaaa":
270                         #self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.pinEntered, PinInput, pinList = [self.pin.value], title = _("please enter the old pin"), windowTitle = _("Change pin code")))
271                 ProtectedScreen.__init__(self)
272
273                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions"],
274                 {
275                         "cancel": self.cancel,
276                         "red": self.cancel,
277                         "save": self.keyOK,
278                 }, -1)
279                 self["key_red"] = StaticText(_("Cancel"))
280                 self["key_green"] = StaticText(_("OK"))
281                 self.onLayoutFinish.append(self.layoutFinished)
282
283         def layoutFinished(self):
284                 self.setTitle(self.setup_title)
285
286         def valueChanged(self, pin, value):
287                 if pin == 1:
288                         self["config"].setCurrentIndex(1)
289                 elif pin == 2:
290                         self.keyOK()
291
292         def getPinText(self):
293                 return _("Please enter the old pin code")
294
295         def isProtected(self):
296                 return (self.pin.value != "aaaa")
297
298         def protectedWithPin(self):
299                 return self.pin.value
300
301 #       def pinEntered(self, result):
302                 #if result[0] is None:
303                         #self.close()
304                 #if not result[0]:
305                         #print result, "-", self.pin.value
306                         #self.session.openWithCallback(self.close, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
307
308         def keyOK(self):
309                 if self.pin1.value == self.pin2.value:
310                         self.pin.value = self.pin1.value
311                         self.pin.save()
312                         self.session.openWithCallback(self.close, MessageBox, _("The pin code has been changed successfully."), MessageBox.TYPE_INFO)
313                 else:
314                         self.session.open(MessageBox, _("The pin codes you entered are different."), MessageBox.TYPE_ERROR)
315
316         def cancel(self):
317                 self.close(None)
318
319         def keyNumberGlobal(self, number):
320                 ConfigListScreen.keyNumberGlobal(self, number)
321
322         # for summary:
323         def changedEntry(self):
324                 for x in self.onChangedEntry:
325                         x()
326
327         def getCurrentEntry(self):
328                 return self["config"].getCurrent()[0]
329
330         def getCurrentValue(self):
331                 return str(self["config"].getCurrent()[1].getText())
332
333         def createSummary(self):
334                 from Screens.Setup import SetupSummary
335                 return SetupSummary