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
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, "")))
27 config.ParentalControl.servicepin = ConfigSubList()
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, ""))))
33 class ParentalControl:
36 self.serviceLevel = {}
39 def addWhitelistService(self, service):
40 self.whitelist.append(service)
42 def addBlacklistService(self, service):
43 self.blacklist.append(service)
45 def setServiceLevel(self, service, level):
46 self.serviceLevel[service] = level
48 def deleteWhitelistService(self, service):
49 self.whitelist.remove(service)
50 if self.serviceLevel.has_key(service):
51 self.serviceLevel.remove(service)
53 def deleteBlacklistService(self, service):
54 self.blacklist.remove(service)
55 if self.serviceLevel.has_key(service):
56 self.serviceLevel.remove(service)
58 def isServicePlayable(self, service, callback):
59 if not config.ParentalControl.configured.value:
61 print "whitelist:", self.whitelist
62 print "blacklist:", self.blacklist
63 print "config.ParentalControl.type.value:", config.ParentalControl.type.value
64 print "not in whitelist:", (service not in self.whitelist)
65 print "checking parental control for service:", service
66 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
67 self.callback = callback
68 print "service:", ServiceReference(service).getServiceName()
70 if self.serviceLevel.has_key(service):
71 levelNeeded = self.serviceLevel[service]
72 pinList = self.getPinList()[:levelNeeded + 1]
73 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"))
78 def protectService(self, service):
80 print "config.ParentalControl.type.value:", config.ParentalControl.type.value
81 if config.ParentalControl.type.value == "whitelist":
82 if service in self.whitelist:
83 self.deleteWhitelistService(service)
85 if service not in self.blacklist:
86 self.addBlacklistService(service)
87 print "whitelist:", self.whitelist
88 print "blacklist:", self.blacklist
91 def unProtectService(self, service):
93 print "config.ParentalControl.type.value:", config.ParentalControl.type.value
94 if config.ParentalControl.type.value == "whitelist":
95 if service not in self.whitelist:
96 self.addWhitelistService(service)
98 if service in self.blacklist:
99 self.deleteBlacklistService(service)
100 print "whitelist:", self.whitelist
101 print "blacklist:", self.blacklist
103 def getProtectionLevel(self, service):
104 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
105 if self.serviceLevel.has_key(service):
106 return self.serviceLevel[service]
112 def getPinList(self):
114 for x in config.ParentalControl.servicepin:
115 pinList.append(x.value)
118 def servicePinEntered(self, service, result):
120 #if self.serviceLevel.has_key(service):
121 #levelNeeded = self.serviceLevel[service]
123 #print "getPinList():", self.getPinList()
124 #pinList = self.getPinList()[:levelNeeded + 1]
125 #print "pinList:", pinList
127 # print "pin entered for service", service, "and pin was", pin
128 #if pin is not None and int(pin) in pinList:
129 if result[0] is not None and result[0]:
130 print "pin ok, playing service"
132 self.callback(ref = ServiceReference(service).ref)
134 self.tries = result[1]
135 if result[0] is not None:
136 Notifications.AddNotification(MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
137 print "wrong pin entered"
139 def saveWhitelist(self):
140 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'w')
141 for x in self.whitelist:
145 def openWhitelist(self):
148 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'r')
149 lines = file.readlines()
151 self.whitelist.append(x.strip())
156 def saveBlacklist(self):
157 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'w')
158 for x in self.blacklist:
162 def openBlacklist(self):
165 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'r')
166 lines = file.readlines()
168 self.blacklist.append(x.strip())
181 parentalControl = ParentalControl()