86f188a4ab51017e80a0f44b70cb72df9ced5686
[enigma2.git] / lib / python / Components / ParentalControl.py
1 from Components.config import config, ConfigSubsection, ConfigSelection, ConfigPIN, ConfigYesNo, ConfigSubList
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 #       config.ParentalControl.configured = configElement("config.ParentalControl.configured", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
20         #config.ParentalControl.mode = configElement("config.ParentalControl.mode", configSelection, 0, (("simple", _("simple")), ("complex", _("complex"))))
21         #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"))))
22         #config.ParentalControl.servicepinactive = configElement("config.ParentalControl.servicepinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
23         #config.ParentalControl.setuppinactive = configElement("config.ParentalControl.setuppinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
24         #config.ParentalControl.type = configElement("config.ParentalControl.type", configSelection, 0, (("whitelist", _("whitelist")), ("blacklist", _("blacklist"))))
25         #config.ParentalControl.setuppin = configElement("config.ParentalControl.setuppin", configSequence, "0000", configSequenceArg().get("PINCODE", (4, "")))
26
27         config.ParentalControl.servicepin = ConfigSubList()
28
29         for i in range(3):
30                 config.ParentalControl.servicepin.append(ConfigPIN(default = -1))
31                 #config.ParentalControl.servicepin.append(configElement("config.ParentalControl.servicepin.level" + str(i), configSequence, "0000", configSequenceArg().get("PINCODE", (4, ""))))
32
33 class ParentalControl:
34         def __init__(self):
35                 self.open()
36                 self.serviceLevel = {}
37                 self.tries = 3
38                 
39         def addWhitelistService(self, service):
40                 self.whitelist.append(service.toString())
41         
42         def addBlacklistService(self, service):
43                 self.blacklist.append(service.toString())
44
45         def setServiceLevel(self, service, level):
46                 self.serviceLevel[service.toString()] = level
47
48         def deleteWhitelistService(self, service):
49                 self.whitelist.remove(service.toString())
50                 if self.serviceLevel.has_key(service.toString()):
51                         self.serviceLevel.remove(service.toString())
52         
53         def deleteBlacklistService(self, service):
54                 self.blacklist.remove(service.toString())
55                 if self.serviceLevel.has_key(service.toString()):
56                         self.serviceLevel.remove(service.toString())
57                                 
58         def isServicePlayable(self, serviceref, callback):
59                 service = serviceref.toString()
60                 if not config.ParentalControl.configured.value:
61                         return True
62                 print "whitelist:", self.whitelist
63                 print "blacklist:", self.blacklist
64                 print "config.ParentalControl.type.value:", config.ParentalControl.type.value
65                 print "not in whitelist:", (service not in self.whitelist)
66                 print "checking parental control for service:", service
67                 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
68                         self.callback = callback
69                         print "service:", ServiceReference(service).getServiceName()
70                         levelNeeded = 0
71                         if self.serviceLevel.has_key(service):
72                                 levelNeeded = self.serviceLevel[service]
73                         pinList = self.getPinList()[:levelNeeded + 1]
74                         Notifications.AddNotificationWithCallback(boundFunction(self.servicePinEntered, service), PinInput, tries = self.tries, pinList = pinList, service = ServiceReference(service).getServiceName(), title = _("this service is protected by a parental control pin"), windowTitle = _("Parental control"))
75                         return False
76                 else:
77                         return True
78                 
79         def protectService(self, service):
80                 print "protect"
81                 print "config.ParentalControl.type.value:", config.ParentalControl.type.value
82                 if config.ParentalControl.type.value == "whitelist":
83                         if service.toString() in self.whitelist:
84                                 self.deleteWhitelistService(service)
85                 else: # blacklist
86                         if service.toString() not in self.blacklist:
87                                 self.addBlacklistService(service)
88                 print "whitelist:", self.whitelist
89                 print "blacklist:", self.blacklist
90
91                                 
92         def unProtectService(self, service):
93                 print "unprotect"
94                 print "config.ParentalControl.type.value:", config.ParentalControl.type.value
95                 if config.ParentalControl.type.value == "whitelist":
96                         if service.toString() not in self.whitelist:
97                                 self.addWhitelistService(service)
98                 else: # blacklist
99                         if service.toString() in self.blacklist:
100                                 self.deleteBlacklistService(service)
101                 print "whitelist:", self.whitelist
102                 print "blacklist:", self.blacklist
103
104
105                 
106         def getProtectionLevel(self, serviceref):
107                 service = serviceref.toString()
108                 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
109                         if self.serviceLevel.has_key(service):
110                                 return self.serviceLevel[service]
111                         else:
112                                 return 0
113                 else:
114                         return -1
115         
116         def getPinList(self):
117                 pinList = []
118                 for x in config.ParentalControl.servicepin:
119                         pinList.append(x.value)
120                 return pinList
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[0] is not None and result[0]:
134                         print "pin ok, playing service"
135                         self.tries = 3
136                         self.callback(ref = ServiceReference(service).ref)
137                 else:
138                         self.tries = result[1]
139                         if result[0] is not None:
140                                 Notifications.AddNotification(MessageBox,  _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
141                         print "wrong pin entered"
142                         
143         def saveWhitelist(self):
144                 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'w')
145                 for x in self.whitelist:
146                         file.write(x + "\n")
147                 file.close
148         
149         def openWhitelist(self):
150                 self.whitelist = []
151                 try:
152                         file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'r')
153                         lines = file.readlines()
154                         for x in lines:
155                                 self.whitelist.append(x.strip())
156                         file.close
157                 except:
158                         pass
159                 
160         def saveBlacklist(self):
161                 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'w')
162                 for x in self.blacklist:
163                         file.write(x + "\n")
164                 file.close
165
166         def openBlacklist(self):
167                 self.blacklist = []
168                 try:
169                         file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'r')
170                         lines = file.readlines()
171                         for x in lines:
172                                 self.blacklist.append(x.strip())
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()