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