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.toCompareString())
42 def addBlacklistService(self, service):
43 self.blacklist.append(service.toCompareString())
45 def setServiceLevel(self, service, level):
46 self.serviceLevel[service.toCompareString()] = level
48 def deleteWhitelistService(self, service):
49 self.whitelist.remove(service.toCompareString())
50 if self.serviceLevel.has_key(service.toCompareString()):
51 self.serviceLevel.remove(service.toCompareString())
53 def deleteBlacklistService(self, service):
54 self.blacklist.remove(service.toCompareString())
55 if self.serviceLevel.has_key(service.toCompareString()):
56 self.serviceLevel.remove(service.toCompareString())
58 def isServicePlayable(self, serviceref, callback):
59 service = serviceref.toCompareString()
60 if not config.ParentalControl.configured.value:
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()
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"))
79 def protectService(self, service):
81 print "config.ParentalControl.type.value:", config.ParentalControl.type.value
82 if config.ParentalControl.type.value == "whitelist":
83 if service.toCompareString() in self.whitelist:
84 self.deleteWhitelistService(service)
86 if service.toCompareString() not in self.blacklist:
87 self.addBlacklistService(service)
88 print "whitelist:", self.whitelist
89 print "blacklist:", self.blacklist
92 def unProtectService(self, service):
94 print "config.ParentalControl.type.value:", config.ParentalControl.type.value
95 if config.ParentalControl.type.value == "whitelist":
96 if service.toCompareString() not in self.whitelist:
97 self.addWhitelistService(service)
99 if service.toCompareString() in self.blacklist:
100 self.deleteBlacklistService(service)
101 print "whitelist:", self.whitelist
102 print "blacklist:", self.blacklist
104 def getProtectionLevel(self, serviceref):
105 service = serviceref.toCompareString()
106 if (config.ParentalControl.type.value == "whitelist" and service not in self.whitelist) or (config.ParentalControl.type.value == "blacklist" and service in self.blacklist):
107 if self.serviceLevel.has_key(service):
108 return self.serviceLevel[service]
114 def getPinList(self):
116 for x in config.ParentalControl.servicepin:
117 pinList.append(x.value)
120 def servicePinEntered(self, service, result):
122 #if self.serviceLevel.has_key(service):
123 #levelNeeded = self.serviceLevel[service]
125 #print "getPinList():", self.getPinList()
126 #pinList = self.getPinList()[:levelNeeded + 1]
127 #print "pinList:", pinList
129 # print "pin entered for service", service, "and pin was", pin
130 #if pin is not None and int(pin) in pinList:
131 if result[0] is not None and result[0]:
132 print "pin ok, playing service"
134 self.callback(ref = ServiceReference(service).ref)
136 self.tries = result[1]
137 if result[0] is not None:
138 Notifications.AddNotification(MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
139 print "wrong pin entered"
141 def saveWhitelist(self):
142 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'w')
143 for x in self.whitelist:
147 def openWhitelist(self):
150 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'r')
151 lines = file.readlines()
153 self.whitelist.append(x.strip())
158 def saveBlacklist(self):
159 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'w')
160 for x in self.blacklist:
164 def openBlacklist(self):
167 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'r')
168 lines = file.readlines()
170 self.blacklist.append(x.strip())
183 parentalControl = ParentalControl()