sort list properly in ParentalControlSetup
[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
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                 
44                 ProtectedScreen.__init__(self)
45                 
46                 self["actions"] = NumberActionMap(["SetupActions"],
47                 {
48                         "cancel": self.keyCancel
49                 }, -2)
50                 
51                 self.list = []
52                 ConfigListScreen.__init__(self, self.list)
53                 self.createSetup()
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 class ParentalControlEditor(Screen):
123         def __init__(self, session):
124                 Screen.__init__(self, session)
125
126                 self.list = []
127                 self.servicelist = ParentalControlList(self.list)
128                 self["servicelist"] = self.servicelist;
129                 
130                 #self.onShown.append(self.chooseLetter)
131                 self.currentLetter = ''
132                 
133                 self.readServiceList()
134                 
135                 self["actions"] = NumberActionMap(["DirectionActions", "ColorActions", "OkCancelActions", "NumberActions"],
136                 {
137                         "ok": self.select,
138                         "cancel": self.cancel,
139                         "red": self.chooseLetter,
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 cancel(self):
155                 parentalControl.save()
156                 self.close()
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                 
169                 self.servicesList = {}
170                 
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 s[1][0]=='\xc2' and s[1][1]=='\x86': # 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 = '&'
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 == '&':
191                                 x = ("special characters", x)
192                         else:
193                                 x = (x, x)
194                         mylist.append(x)
195                 mylist.sort(key=operator.itemgetter(1))
196                 print self.servicesList.keys()
197                 self.session.openWithCallback(self.letterChosen, ChoiceBox, title=_("Show services beginning with"), list=mylist)
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
208 class ParentalControlChangePin(Screen, ConfigListScreen, ProtectedScreen):
209         def __init__(self, session, pin, pinname):
210                 Screen.__init__(self, session)
211
212                 self.pin = pin
213
214                 self.list = []
215                 self.pin1 = ConfigPIN(default = 1111, censor = "*")
216                 self.pin2 = ConfigPIN(default = 1112, censor = "*")
217                 self.list.append(getConfigListEntry(_("New pin"), NoSave(self.pin1)))
218                 self.list.append(getConfigListEntry(_("Reenter new pin"), NoSave(self.pin2)))
219                 ConfigListScreen.__init__(self, self.list)
220                 
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