get rid of unneede global keywords
[enigma2.git] / 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                                 if ord(s[1][0])==0xc2 and ord(s[1][1])==0x86: # ignore shortname brackets
196                                         key = s[1].lower()[2]
197                                 else:
198                                         key = s[1].lower()[0]
199                                 if key < 'a' or key > 'z':
200                                         key = chr(SPECIAL_CHAR)
201                                 #key = str(key)
202                                 if not self.servicesList.has_key(key):
203                                         self.servicesList[key] = []
204                                 self.servicesList[key].append(s)
205
206         def chooseLetter(self):
207                 print "choose letter"
208                 mylist = []
209                 for x in self.servicesList.keys():
210                         if x == chr(SPECIAL_CHAR):
211                                 x = ("special characters", x)
212                         else:
213                                 x = (x, x)
214                         mylist.append(x)
215                 mylist.sort(key=itemgetter(1))
216                 sel = ord(self.currentLetter) - SPECIAL_CHAR
217                 self.session.openWithCallback(self.letterChosen, ChoiceBox, title=_("Show services beginning with"), list=mylist, keys = [], selection = sel)
218
219         def letterChosen(self, result):
220                 if result is not None:
221                         print "result:", result
222                         self.currentLetter = result[1]
223                         self.list = []
224                         for x in self.servicesList[result[1]]:
225                                 self.list.append(ParentalControlEntryComponent(x[0], x[1], parentalControl.getProtectionLevel(x[0]) != -1))
226                         self.servicelist.setList(self.list)
227                 else:
228                         parentalControl.save()
229                         self.close()
230
231 class ParentalControlChangePin(Screen, ConfigListScreen, ProtectedScreen):
232         def __init__(self, session, pin, pinname):
233                 Screen.__init__(self, session)
234                 self.pin = pin
235                 self.list = []
236                 self.pin1 = ConfigPIN(default = 1111, censor = "*")
237                 self.pin2 = ConfigPIN(default = 1112, censor = "*")
238                 self.list.append(getConfigListEntry(_("New pin"), NoSave(self.pin1)))
239                 self.list.append(getConfigListEntry(_("Reenter new pin"), NoSave(self.pin2)))
240                 ConfigListScreen.__init__(self, self.list)
241 #               print "old pin:", pin
242                 #if pin.value != "aaaa":
243                         #self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.pinEntered, PinInput, pinList = [self.pin.value], title = _("please enter the old pin"), windowTitle = _("Change pin code")))
244                 ProtectedScreen.__init__(self)
245                 
246                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions"],
247                 {
248                         "cancel": self.cancel,
249                 }, -1)
250
251         def getPinText(self):
252                 return _("Please enter the old pin code")
253
254         def isProtected(self):
255                 return (self.pin.value != "aaaa")
256
257         def protectedWithPin(self):
258                 return self.pin.value
259
260 #       def pinEntered(self, result):
261                 #if result[0] is None:
262                         #self.close()
263                 #if not result[0]:
264                         #print result, "-", self.pin.value
265                         #self.session.openWithCallback(self.close, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
266
267         def keyOK(self):
268                 if self.pin1.value == self.pin2.value:
269                         self.pin.value = self.pin1.value
270                         self.pin.save()
271                         self.session.openWithCallback(self.close, MessageBox, _("The pin code has been changed successfully."), MessageBox.TYPE_INFO)
272                 else:
273                         self.session.open(MessageBox, _("The pin codes you entered are different."), MessageBox.TYPE_ERROR)
274
275         def cancel(self):
276                 self.close(None)
277
278         def keyNumberGlobal(self, number):
279                 ConfigListScreen.keyNumberGlobal(self, number)
280