By Anders Holst:
[enigma2.git] / lib / python / Components / ParentalControl.py
1 from Components.config import config, ConfigSubsection, ConfigSelection, ConfigPIN, ConfigYesNo, ConfigSubList, ConfigInteger
2 from Screens.InputBox import PinInput
3 from Screens.MessageBox import MessageBox
4 from Tools.BoundFunction import boundFunction
5 from ServiceReference import ServiceReference
6 from Tools import Notifications
7 from Tools.Directories import resolveFilename, SCOPE_CONFIG
8
9 def InitParentalControl():
10         config.ParentalControl = ConfigSubsection()
11         config.ParentalControl.configured = ConfigYesNo(default = False)
12         config.ParentalControl.mode = ConfigSelection(default = "simple", choices = [("simple", _("simple")), ("complex", _("complex"))])
13         config.ParentalControl.storeservicepin = ConfigSelection(default = "never", choices = [("never", _("never")), ("5_minutes", _("5 minutes")), ("30_minutes", _("30 minutes")), ("60_minutes", _("60 minutes")), ("restart", _("until restart"))])
14         config.ParentalControl.servicepinactive = ConfigYesNo(default = False)
15         config.ParentalControl.setuppinactive = ConfigYesNo(default = False)
16         config.ParentalControl.type = ConfigSelection(default = "blacklist", choices = [("whitelist", _("whitelist")), ("blacklist", _("blacklist"))])
17         config.ParentalControl.setuppin = ConfigPIN(default = -1)
18         
19         config.ParentalControl.retries = ConfigSubsection()
20         config.ParentalControl.retries.setuppin = ConfigSubsection()
21         config.ParentalControl.retries.setuppin.tries = ConfigInteger(default = 3)
22         config.ParentalControl.retries.setuppin.time = ConfigInteger(default = 3)       
23         config.ParentalControl.retries.servicepin = ConfigSubsection()
24         config.ParentalControl.retries.servicepin.tries = ConfigInteger(default = 3)
25         config.ParentalControl.retries.servicepin.time = ConfigInteger(default = 3)
26 #       config.ParentalControl.configured = configElement("config.ParentalControl.configured", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
27         #config.ParentalControl.mode = configElement("config.ParentalControl.mode", configSelection, 0, (("simple", _("simple")), ("complex", _("complex"))))
28         #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"))))
29         #config.ParentalControl.servicepinactive = configElement("config.ParentalControl.servicepinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
30         #config.ParentalControl.setuppinactive = configElement("config.ParentalControl.setuppinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
31         #config.ParentalControl.type = configElement("config.ParentalControl.type", configSelection, 0, (("whitelist", _("whitelist")), ("blacklist", _("blacklist"))))
32         #config.ParentalControl.setuppin = configElement("config.ParentalControl.setuppin", configSequence, "0000", configSequenceArg().get("PINCODE", (4, "")))
33
34         config.ParentalControl.servicepin = ConfigSubList()
35
36         for i in (0, 1, 2):
37                 config.ParentalControl.servicepin.append(ConfigPIN(default = -1))
38                 #config.ParentalControl.servicepin.append(configElement("config.ParentalControl.servicepin.level" + str(i), configSequence, "0000", configSequenceArg().get("PINCODE", (4, ""))))
39
40 class ParentalControl:
41         def __init__(self):
42                 self.open()
43                 self.serviceLevel = {}
44                 
45         def addWhitelistService(self, service):
46                 self.whitelist.append(service)
47         
48         def addBlacklistService(self, service):
49                 self.blacklist.append(service)
50
51         def setServiceLevel(self, service, level):
52                 self.serviceLevel[service] = level
53
54         def deleteWhitelistService(self, service):
55                 self.whitelist.remove(service)
56                 if self.serviceLevel.has_key(service):
57                         self.serviceLevel.remove(service)
58         
59         def deleteBlacklistService(self, service):
60                 self.blacklist.remove(service)
61                 if self.serviceLevel.has_key(service):
62                         self.serviceLevel.remove(service)
63
64         def isServicePlayable(self, ref, callback):
65                 if not config.ParentalControl.configured.value or not config.ParentalControl.servicepinactive.value:
66                         return True
67                 #print "whitelist:", self.whitelist
68                 #print "blacklist:", self.blacklist
69                 #print "config.ParentalControl.type.value:", config.ParentalControl.type.value
70                 #print "not in whitelist:", (service not in self.whitelist)
71                 #print "checking parental control for service:", ref.toString()
72                 service = ref.toCompareString()
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, 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"))
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                 return [ x.value for x in config.ParentalControl.servicepin ]
121         
122         def servicePinEntered(self, service, result):
123 #               levelNeeded = 0
124                 #if self.serviceLevel.has_key(service):
125                         #levelNeeded = self.serviceLevel[service]
126 #               
127                 #print "getPinList():", self.getPinList()
128                 #pinList = self.getPinList()[:levelNeeded + 1]
129                 #print "pinList:", pinList
130 #               
131 #               print "pin entered for service", service, "and pin was", pin
132                 #if pin is not None and int(pin) in pinList:
133                 if result is not None and result:
134                         #print "pin ok, playing service"
135                         self.callback(ref = service)
136                 else:
137                         if result is not None:
138                                 Notifications.AddNotification(MessageBox,  _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
139                         #print "wrong pin entered"
140                         
141         def saveWhitelist(self):
142                 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'w')
143                 for x in self.whitelist:
144                         file.write(x + "\n")
145                 file.close
146         
147         def openWhitelist(self):
148                 self.whitelist = []
149                 try:
150                         file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'r')
151                         lines = file.readlines()
152                         for x in lines:
153                                 ref = ServiceReference(x.strip())
154                                 self.whitelist.append(str(ref))
155                         file.close
156                 except:
157                         pass
158                 
159         def saveBlacklist(self):
160                 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'w')
161                 for x in self.blacklist:
162                         file.write(x + "\n")
163                 file.close
164
165         def openBlacklist(self):
166                 self.blacklist = []
167                 try:
168                         file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'r')
169                         lines = file.readlines()
170                         for x in lines:
171                                 ref = ServiceReference(x.strip())
172                                 self.blacklist.append(str(ref))
173                         file.close
174                 except:
175                         pass
176                 
177         def save(self):
178                 self.saveBlacklist()
179                 self.saveWhitelist()
180                 
181         def open(self):
182                 self.openBlacklist()
183                 self.openWhitelist()
184
185 parentalControl = ParentalControl()