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