4bf18067750170a066e6d3af071a211da2299780
[enigma2.git] / 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, service, callback):
66                 if not config.ParentalControl.configured.value or not config.ParentalControl.setuppinactive.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:", service
73                 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
74                         self.callback = callback
75                         #print "service:", ServiceReference(service).getServiceName()
76                         levelNeeded = 0
77                         if self.serviceLevel.has_key(service):
78                                 levelNeeded = self.serviceLevel[service]
79                         pinList = self.getPinList()[:levelNeeded + 1]
80                         Notifications.AddNotificationWithCallback(boundFunction(self.servicePinEntered, service), PinInput, triesEntry = config.ParentalControl.retries.servicepin, pinList = pinList, service = ServiceReference(service).getServiceName(), title = _("this service is protected by a parental control pin"), windowTitle = _("Parental control"))
81                         return False
82                 else:
83                         return True
84                 
85         def protectService(self, service):
86                 #print "protect"
87                 #print "config.ParentalControl.type.value:", config.ParentalControl.type.value
88                 if config.ParentalControl.type.value == "whitelist":
89                         if service in self.whitelist:
90                                 self.deleteWhitelistService(service)
91                 else: # blacklist
92                         if service not in self.blacklist:
93                                 self.addBlacklistService(service)
94                 #print "whitelist:", self.whitelist
95                 #print "blacklist:", self.blacklist
96
97                                 
98         def unProtectService(self, service):
99                 #print "unprotect"
100                 #print "config.ParentalControl.type.value:", config.ParentalControl.type.value
101                 if config.ParentalControl.type.value == "whitelist":
102                         if service not in self.whitelist:
103                                 self.addWhitelistService(service)
104                 else: # blacklist
105                         if service in self.blacklist:
106                                 self.deleteBlacklistService(service)
107                 #print "whitelist:", self.whitelist
108                 #print "blacklist:", self.blacklist
109
110         def getProtectionLevel(self, service):
111                 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
112                         if self.serviceLevel.has_key(service):
113                                 return self.serviceLevel[service]
114                         else:
115                                 return 0
116                 else:
117                         return -1
118         
119         def getPinList(self):
120                 pinList = []
121                 for x in config.ParentalControl.servicepin:
122                         pinList.append(x.value)
123                 return pinList
124         
125         def servicePinEntered(self, service, result):
126 #               levelNeeded = 0
127                 #if self.serviceLevel.has_key(service):
128                         #levelNeeded = self.serviceLevel[service]
129 #               
130                 #print "getPinList():", self.getPinList()
131                 #pinList = self.getPinList()[:levelNeeded + 1]
132                 #print "pinList:", pinList
133 #               
134 #               print "pin entered for service", service, "and pin was", pin
135                 #if pin is not None and int(pin) in pinList:
136                 if result is not None and result:
137                         #print "pin ok, playing service"
138                         self.callback(ref = ServiceReference(service).ref)
139                 else:
140                         if result is not None:
141                                 Notifications.AddNotification(MessageBox,  _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
142                         #print "wrong pin entered"
143                         
144         def saveWhitelist(self):
145                 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'w')
146                 for x in self.whitelist:
147                         file.write(x + "\n")
148                 file.close
149         
150         def openWhitelist(self):
151                 self.whitelist = []
152                 try:
153                         file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'r')
154                         lines = file.readlines()
155                         for x in lines:
156                                 self.whitelist.append(x.strip())
157                         file.close
158                 except:
159                         pass
160                 
161         def saveBlacklist(self):
162                 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'w')
163                 for x in self.blacklist:
164                         file.write(x + "\n")
165                 file.close
166
167         def openBlacklist(self):
168                 self.blacklist = []
169                 try:
170                         file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'r')
171                         lines = file.readlines()
172                         for x in lines:
173                                 self.blacklist.append(x.strip())
174                         file.close
175                 except:
176                         pass
177                 
178         def save(self):
179                 self.saveBlacklist()
180                 self.saveWhitelist()
181                 
182         def open(self):
183                 self.openBlacklist()
184                 self.openWhitelist()
185
186 parentalControl = ParentalControl()