parental locking fixes
[vuplus_dvbapp] / lib / python / Components / ParentalControl.py
1 from Components.config import config, ConfigSubsection, ConfigSelection, ConfigPIN, ConfigYesNo, ConfigSubList, ConfigInteger
2 from Components.Input import Input
3 from Screens.InputBox import InputBox, PinInput
4 from Screens.MessageBox import MessageBox
5 from Tools.BoundFunction import boundFunction
6 from ServiceReference import ServiceReference
7 from Tools import Notifications
8 from Tools.Directories import resolveFilename, SCOPE_CONFIG
9
10 def InitParentalControl():
11         config.ParentalControl = ConfigSubsection()
12         config.ParentalControl.configured = ConfigYesNo(default = False)
13         config.ParentalControl.mode = ConfigSelection(default = "simple", choices = [("simple", _("simple")), ("complex", _("complex"))])
14         config.ParentalControl.storeservicepin = ConfigSelection(default = "never", choices = [("never", _("never")), ("5_minutes", _("5 minutes")), ("30_minutes", _("30 minutes")), ("60_minutes", _("60 minutes")), ("restart", _("until restart"))])
15         config.ParentalControl.servicepinactive = ConfigYesNo(default = False)
16         config.ParentalControl.setuppinactive = ConfigYesNo(default = False)
17         config.ParentalControl.type = ConfigSelection(default = "blacklist", choices = [("whitelist", _("whitelist")), ("blacklist", _("blacklist"))])
18         config.ParentalControl.setuppin = ConfigPIN(default = -1)
19         
20         config.ParentalControl.retries = ConfigSubsection()
21         config.ParentalControl.retries.setuppin = ConfigSubsection()
22         config.ParentalControl.retries.setuppin.tries = ConfigInteger(default = 3)
23         config.ParentalControl.retries.setuppin.time = ConfigInteger(default = 3)       
24         config.ParentalControl.retries.servicepin = ConfigSubsection()
25         config.ParentalControl.retries.servicepin.tries = ConfigInteger(default = 3)
26         config.ParentalControl.retries.servicepin.time = ConfigInteger(default = 3)
27 #       config.ParentalControl.configured = configElement("config.ParentalControl.configured", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
28         #config.ParentalControl.mode = configElement("config.ParentalControl.mode", configSelection, 0, (("simple", _("simple")), ("complex", _("complex"))))
29         #config.ParentalControl.storeservicepin = configElement("config.ParentalControl.storeservicepin", configSelection, 0, (("never", _("never")), ("5_minutes", _("5 minutes")), ("30_minutes", _("30 minutes")), ("60_minutes", _("60 minutes")), ("restart", _("until restart"))))
30         #config.ParentalControl.servicepinactive = configElement("config.ParentalControl.servicepinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
31         #config.ParentalControl.setuppinactive = configElement("config.ParentalControl.setuppinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
32         #config.ParentalControl.type = configElement("config.ParentalControl.type", configSelection, 0, (("whitelist", _("whitelist")), ("blacklist", _("blacklist"))))
33         #config.ParentalControl.setuppin = configElement("config.ParentalControl.setuppin", configSequence, "0000", configSequenceArg().get("PINCODE", (4, "")))
34
35         config.ParentalControl.servicepin = ConfigSubList()
36
37         for i in range(3):
38                 config.ParentalControl.servicepin.append(ConfigPIN(default = -1))
39                 #config.ParentalControl.servicepin.append(configElement("config.ParentalControl.servicepin.level" + str(i), configSequence, "0000", configSequenceArg().get("PINCODE", (4, ""))))
40
41 class ParentalControl:
42         def __init__(self):
43                 self.open()
44                 self.serviceLevel = {}
45                 
46         def addWhitelistService(self, service):
47                 self.whitelist.append(service)
48         
49         def addBlacklistService(self, service):
50                 self.blacklist.append(service)
51
52         def setServiceLevel(self, service, level):
53                 self.serviceLevel[service] = level
54
55         def deleteWhitelistService(self, service):
56                 self.whitelist.remove(service)
57                 if self.serviceLevel.has_key(service):
58                         self.serviceLevel.remove(service)
59         
60         def deleteBlacklistService(self, service):
61                 self.blacklist.remove(service)
62                 if self.serviceLevel.has_key(service):
63                         self.serviceLevel.remove(service)
64
65         def isServicePlayable(self, ref, callback):
66                 if not config.ParentalControl.configured.value or not config.ParentalControl.servicepinactive.value:
67                         return True
68                 #print "whitelist:", self.whitelist
69                 #print "blacklist:", self.blacklist
70                 #print "config.ParentalControl.type.value:", config.ParentalControl.type.value
71                 #print "not in whitelist:", (service not in self.whitelist)
72                 #print "checking parental control for service:", ref.toString()
73                 service = ref.toCompareString()
74                 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
75                         self.callback = callback
76                         #print "service:", ServiceReference(service).getServiceName()
77                         levelNeeded = 0
78                         if self.serviceLevel.has_key(service):
79                                 levelNeeded = self.serviceLevel[service]
80                         pinList = self.getPinList()[:levelNeeded + 1]
81                         Notifications.AddNotificationWithCallback(boundFunction(self.servicePinEntered, ref), PinInput, triesEntry = config.ParentalControl.retries.servicepin, pinList = pinList, service = ServiceReference(ref).getServiceName(), title = _("this service is protected by a parental control pin"), windowTitle = _("Parental control"))
82                         return False
83                 else:
84                         return True
85                 
86         def protectService(self, service):
87                 #print "protect"
88                 #print "config.ParentalControl.type.value:", config.ParentalControl.type.value
89                 if config.ParentalControl.type.value == "whitelist":
90                         if service in self.whitelist:
91                                 self.deleteWhitelistService(service)
92                 else: # blacklist
93                         if service not in self.blacklist:
94                                 self.addBlacklistService(service)
95                 #print "whitelist:", self.whitelist
96                 #print "blacklist:", self.blacklist
97
98                                 
99         def unProtectService(self, service):
100                 #print "unprotect"
101                 #print "config.ParentalControl.type.value:", config.ParentalControl.type.value
102                 if config.ParentalControl.type.value == "whitelist":
103                         if service not in self.whitelist:
104                                 self.addWhitelistService(service)
105                 else: # blacklist
106                         if service in self.blacklist:
107                                 self.deleteBlacklistService(service)
108                 #print "whitelist:", self.whitelist
109                 #print "blacklist:", self.blacklist
110
111         def getProtectionLevel(self, service):
112                 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
113                         if self.serviceLevel.has_key(service):
114                                 return self.serviceLevel[service]
115                         else:
116                                 return 0
117                 else:
118                         return -1
119         
120         def getPinList(self):
121                 pinList = []
122                 for x in config.ParentalControl.servicepin:
123                         pinList.append(x.value)
124                 return pinList
125         
126         def servicePinEntered(self, service, result):
127 #               levelNeeded = 0
128                 #if self.serviceLevel.has_key(service):
129                         #levelNeeded = self.serviceLevel[service]
130 #               
131                 #print "getPinList():", self.getPinList()
132                 #pinList = self.getPinList()[:levelNeeded + 1]
133                 #print "pinList:", pinList
134 #               
135 #               print "pin entered for service", service, "and pin was", pin
136                 #if pin is not None and int(pin) in pinList:
137                 if result is not None and result:
138                         #print "pin ok, playing service"
139                         self.callback(ref = service)
140                 else:
141                         if result is not None:
142                                 Notifications.AddNotification(MessageBox,  _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
143                         #print "wrong pin entered"
144                         
145         def saveWhitelist(self):
146                 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'w')
147                 for x in self.whitelist:
148                         file.write(x + "\n")
149                 file.close
150         
151         def openWhitelist(self):
152                 self.whitelist = []
153                 try:
154                         file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'r')
155                         lines = file.readlines()
156                         for x in lines:
157                                 ref = ServiceReference(x.strip())
158                                 self.whitelist.append(str(ref))
159                         file.close
160                 except:
161                         pass
162                 
163         def saveBlacklist(self):
164                 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'w')
165                 for x in self.blacklist:
166                         file.write(x + "\n")
167                 file.close
168
169         def openBlacklist(self):
170                 self.blacklist = []
171                 try:
172                         file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'r')
173                         lines = file.readlines()
174                         for x in lines:
175                                 ref = ServiceReference(x.strip())
176                                 self.blacklist.append(str(ref))
177                         file.close
178                 except:
179                         pass
180                 
181         def save(self):
182                 self.saveBlacklist()
183                 self.saveWhitelist()
184                 
185         def open(self):
186                 self.openBlacklist()
187                 self.openWhitelist()
188
189 parentalControl = ParentalControl()