directly show list of beginning characters
[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.Directories import resolveFilename, SCOPE_CONFIG
13 from Tools.BoundFunction import boundFunction
14 from ServiceReference import ServiceReference
15 from enigma import eServiceCenter, eServiceReference, eTimer
16 import os
17 import operator
18
19 class ProtectedScreen:
20         def __init__(self):
21                 if self.isProtected():
22                         self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.pinEntered, PinInput, pinList = [self.protectedWithPin()], title = self.getPinText(), windowTitle = _("Change pin code")))
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[0] is None:
35                         self.close()
36                 if not result[0]:
37                         print result, "-", self.protectedWithPin()
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
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 keyCancel(self):
113                 for x in self["config"].list:
114                         x[1].save()
115                 self.close()
116
117         def keyNumberGlobal(self, number):
118                 pass
119
120 class ParentalControlEditor(Screen):
121         SPECIAL_CHAR = 96
122         def __init__(self, session):
123                 Screen.__init__(self, session)
124                 self.list = []
125                 self.servicelist = ParentalControlList(self.list)
126                 self["servicelist"] = self.servicelist;
127                 #self.onShown.append(self.chooseLetter)
128                 self.currentLetter = chr(SPECIAL_CHAR)
129                 self.readServiceList()
130                 self.chooseLetterTimer = eTimer()
131                 self.chooseLetterTimer.timeout.get().append(self.chooseLetter)
132                 self.onLayoutFinish.append(self.LayoutFinished)
133
134                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions", "NumberActions"],
135                 {
136                         "ok": self.select,
137                         "cancel": self.cancel,
138                         #"left": self.keyLeft,
139                         #"right": self.keyRight,
140                         "1": self.keyNumberGlobal,
141                         "2": self.keyNumberGlobal,
142                         "3": self.keyNumberGlobal,
143                         "4": self.keyNumberGlobal,
144                         "5": self.keyNumberGlobal,
145                         "6": self.keyNumberGlobal,
146                         "7": self.keyNumberGlobal,
147                         "8": self.keyNumberGlobal,
148                         "9": self.keyNumberGlobal,
149                         "0": self.keyNumberGlobal
150                 }, -1)
151
152         def LayoutFinished(self):
153                 self.chooseLetterTimer.start(0, True)
154
155         def cancel(self):
156                 self.chooseLetter()
157
158         def select(self):
159                 self.servicelist.toggleSelectedLock()
160
161         def keyNumberGlobal(self, number):
162                 pass
163
164         def readServiceList(self):
165                 serviceHandler = eServiceCenter.getInstance()
166                 refstr = '%s ORDER BY name' % (service_types_tv)
167                 self.root = eServiceReference(refstr)
168                 self.servicesList = {}
169                 list = serviceHandler.list(self.root)
170                 if list is not None:
171                         services = list.getContent("CN", True) #(servicecomparestring, name)
172                         for s in services:
173                                 if s[1][0]=='\xc2' and s[1][1]=='\x86': # ignore shortname brackets
174                                         key = s[1].lower()[2]
175                                 else:
176                                         key = s[1].lower()[0]
177                                 if key < 'a' or key > 'z':
178                                         key = chr(SPECIAL_CHAR)
179                                 #key = str(key)
180                                 if not self.servicesList.has_key(key):
181                                         self.servicesList[key] = []
182                                 self.servicesList[key].append(s)
183
184         def chooseLetter(self):
185                 print "choose letter"
186                 mylist = []
187                 for x in self.servicesList.keys():
188                         if x == chr(SPECIAL_CHAR):
189                                 x = ("special characters", x)
190                         else:
191                                 x = (x, x)
192                         mylist.append(x)
193                 mylist.sort(key=operator.itemgetter(1))
194                 sel = ord(self.currentLetter) - SPECIAL_CHAR
195                 self.session.openWithCallback(self.letterChosen, ChoiceBox, title=_("Show services beginning with"), list=mylist, keys = [], selection = sel)
196
197         def letterChosen(self, result):
198                 if result is not None:
199                         print "result:", result
200                         self.currentLetter = result[1]
201                         self.list = []
202                         for x in self.servicesList[result[1]]:
203                                 self.list.append(ParentalControlEntryComponent(x[0], x[1], parentalControl.getProtectionLevel(x[0]) != -1))
204                         self.servicelist.setList(self.list)
205                 else:
206                         parentalControl.save()
207                         self.close()
208
209 class ParentalControlChangePin(Screen, ConfigListScreen, ProtectedScreen):
210         def __init__(self, session, pin, pinname):
211                 Screen.__init__(self, session)
212                 self.pin = pin
213                 self.list = []
214                 self.pin1 = ConfigPIN(default = 1111, censor = "*")
215                 self.pin2 = ConfigPIN(default = 1112, censor = "*")
216                 self.list.append(getConfigListEntry(_("New pin"), NoSave(self.pin1)))
217                 self.list.append(getConfigListEntry(_("Reenter new pin"), NoSave(self.pin2)))
218                 ConfigListScreen.__init__(self, self.list)
219 #               print "old pin:", pin
220                 #if pin.value != "aaaa":
221                         #self.onFirstExecBegin.append(boundFunction(self.session.openWithCallback, self.pinEntered, PinInput, pinList = [self.pin.value], title = _("please enter the old pin"), windowTitle = _("Change pin code")))
222                 ProtectedScreen.__init__(self)
223                 
224                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions"],
225                 {
226                         "cancel": self.cancel,
227                 }, -1)
228
229         def getPinText(self):
230                 return _("Please enter the old pin code")
231
232         def isProtected(self):
233                 return (self.pin.value != "aaaa")
234
235         def protectedWithPin(self):
236                 return self.pin.value
237
238 #       def pinEntered(self, result):
239                 #if result[0] is None:
240                         #self.close()
241                 #if not result[0]:
242                         #print result, "-", self.pin.value
243                         #self.session.openWithCallback(self.close, MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
244
245         def keyOK(self):
246                 if self.pin1.value == self.pin2.value:
247                         self.pin.value = self.pin1.value
248                         self.pin.save()
249                         self.session.openWithCallback(self.close, MessageBox, _("The pin code has been changed successfully."), MessageBox.TYPE_INFO)
250                 else:
251                         self.session.open(MessageBox, _("The pin codes you entered are different."), MessageBox.TYPE_ERROR)
252
253         def cancel(self):
254                 self.close(None)
255
256         def keyNumberGlobal(self, number):
257                 ConfigListScreen.keyNumberGlobal(self, number)
258