use service compare strings instead of eServiceReferences in parental control
[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)
41         
42         def addBlacklistService(self, service):
43                 self.blacklist.append(service)
44
45         def setServiceLevel(self, service, level):
46                 self.serviceLevel[service] = level
47
48         def deleteWhitelistService(self, service):
49                 self.whitelist.remove(service)
50                 if self.serviceLevel.has_key(service):
51                         self.serviceLevel.remove(service)
52         
53         def deleteBlacklistService(self, service):
54                 self.blacklist.remove(service)
55                 if self.serviceLevel.has_key(service):
56                         self.serviceLevel.remove(service)
57                                 
58         def isServicePlayable(self, service, callback):
59                 if not config.ParentalControl.configured.value:
60                         return True
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()
69                         levelNeeded = 0
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"))
74                         return False
75                 else:
76                         return True
77                 
78         def protectService(self, service):
79                 print "protect"
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)
84                 else: # blacklist
85                         if service not in self.blacklist:
86                                 self.addBlacklistService(service)
87                 print "whitelist:", self.whitelist
88                 print "blacklist:", self.blacklist
89
90                                 
91         def unProtectService(self, service):
92                 print "unprotect"
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)
97                 else: # blacklist
98                         if service in self.blacklist:
99                                 self.deleteBlacklistService(service)
100                 print "whitelist:", self.whitelist
101                 print "blacklist:", self.blacklist
102
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]
107                         else:
108                                 return 0
109                 else:
110                         return -1
111         
112         def getPinList(self):
113                 pinList = []
114                 for x in config.ParentalControl.servicepin:
115                         pinList.append(x.value)
116                 return pinList
117         
118         def servicePinEntered(self, service, result):
119 #               levelNeeded = 0
120                 #if self.serviceLevel.has_key(service):
121                         #levelNeeded = self.serviceLevel[service]
122 #               
123                 #print "getPinList():", self.getPinList()
124                 #pinList = self.getPinList()[:levelNeeded + 1]
125                 #print "pinList:", pinList
126 #               
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"
131                         self.tries = 3
132                         self.callback(ref = ServiceReference(service).ref)
133                 else:
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"
138                         
139         def saveWhitelist(self):
140                 file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'w')
141                 for x in self.whitelist:
142                         file.write(x + "\n")
143                 file.close
144         
145         def openWhitelist(self):
146                 self.whitelist = []
147                 try:
148                         file = open(resolveFilename(SCOPE_CONFIG, "whitelist"), 'r')
149                         lines = file.readlines()
150                         for x in lines:
151                                 self.whitelist.append(x.strip())
152                         file.close
153                 except:
154                         pass
155                 
156         def saveBlacklist(self):
157                 file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'w')
158                 for x in self.blacklist:
159                         file.write(x + "\n")
160                 file.close
161
162         def openBlacklist(self):
163                 self.blacklist = []
164                 try:
165                         file = open(resolveFilename(SCOPE_CONFIG, "blacklist"), 'r')
166                         lines = file.readlines()
167                         for x in lines:
168                                 self.blacklist.append(x.strip())
169                         file.close
170                 except:
171                         pass
172                 
173         def save(self):
174                 self.saveBlacklist()
175                 self.saveWhitelist()
176                 
177         def open(self):
178                 self.openBlacklist()
179                 self.openWhitelist()
180
181 parentalControl = ParentalControl()