fill parental control list speedup
[vuplus_dvbapp] / 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.toCompareString())
41         
42         def addBlacklistService(self, service):
43                 self.blacklist.append(service.toCompareString())
44
45         def setServiceLevel(self, service, level):
46                 self.serviceLevel[service.toCompareString()] = level
47
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())
52         
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())
57                                 
58         def isServicePlayable(self, serviceref, callback):
59                 service = serviceref.toCompareString()
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.toCompareString() in self.whitelist:
84                                 self.deleteWhitelistService(service)
85                 else: # blacklist
86                         if service.toCompareString() 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.toCompareString() not in self.whitelist:
97                                 self.addWhitelistService(service)
98                 else: # blacklist
99                         if service.toCompareString() in self.blacklist:
100                                 self.deleteBlacklistService(service)
101                 print "whitelist:", self.whitelist
102                 print "blacklist:", self.blacklist
103
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]
109                         else:
110                                 return 0
111                 else:
112                         return -1
113         
114         def getPinList(self):
115                 pinList = []
116                 for x in config.ParentalControl.servicepin:
117                         pinList.append(x.value)
118                 return pinList
119         
120         def servicePinEntered(self, service, result):
121 #               levelNeeded = 0
122                 #if self.serviceLevel.has_key(service):
123                         #levelNeeded = self.serviceLevel[service]
124 #               
125                 #print "getPinList():", self.getPinList()
126                 #pinList = self.getPinList()[:levelNeeded + 1]
127                 #print "pinList:", pinList
128 #               
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"
133                         self.tries = 3
134                         self.callback(ref = ServiceReference(service).ref)
135                 else:
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"
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                                 self.whitelist.append(x.strip())
154                         file.close
155                 except:
156                         pass
157                 
158         def saveBlacklist(self):
159                 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'w')
160                 for x in self.blacklist:
161                         file.write(x + "\n")
162                 file.close
163
164         def openBlacklist(self):
165                 self.blacklist = []
166                 try:
167                         file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'r')
168                         lines = file.readlines()
169                         for x in lines:
170                                 self.blacklist.append(x.strip())
171                         file.close
172                 except:
173                         pass
174                 
175         def save(self):
176                 self.saveBlacklist()
177                 self.saveWhitelist()
178                 
179         def open(self):
180                 self.openBlacklist()
181                 self.openWhitelist()
182
183 parentalControl = ParentalControl()