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