1 from Components.config import config, ConfigSubsection, ConfigSelection, ConfigPIN, ConfigText, ConfigYesNo, ConfigSubList, ConfigInteger
2 #from Screens.ChannelSelection import service_types_tv
3 from Screens.InputBox import 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 from enigma import eTimer
12 TYPE_SERVICE = "SERVICE"
13 TYPE_BOUQUETSERVICE = "BOUQUETSERVICE"
14 TYPE_BOUQUET = "BOUQUET"
15 LIST_BLACKLIST = "blacklist"
16 LIST_WHITELIST = "whitelist"
18 IMG_WHITESERVICE = LIST_WHITELIST + "-" + TYPE_SERVICE
19 IMG_WHITEBOUQUET = LIST_WHITELIST + "-" + TYPE_BOUQUET
20 IMG_BLACKSERVICE = LIST_BLACKLIST + "-" + TYPE_SERVICE
21 IMG_BLACKBOUQUET = LIST_BLACKLIST + "-" + TYPE_BOUQUET
23 def InitParentalControl():
24 config.ParentalControl = ConfigSubsection()
25 config.ParentalControl.configured = ConfigYesNo(default = False)
26 config.ParentalControl.mode = ConfigSelection(default = "simple", choices = [("simple", _("simple")), ("complex", _("complex"))])
27 config.ParentalControl.storeservicepin = ConfigSelection(default = "never", choices = [("never", _("never")), ("5", _("5 minutes")), ("30", _("30 minutes")), ("60", _("60 minutes")), ("standby", _("until standby/restart"))])
28 config.ParentalControl.storeservicepincancel = ConfigSelection(default = "never", choices = [("never", _("never")), ("5", _("5 minutes")), ("30", _("30 minutes")), ("60", _("60 minutes")), ("standby", _("until standby/restart"))])
29 config.ParentalControl.servicepinactive = ConfigYesNo(default = False)
30 config.ParentalControl.setuppinactive = ConfigYesNo(default = False)
31 config.ParentalControl.type = ConfigSelection(default = "blacklist", choices = [(LIST_WHITELIST, _("whitelist")), (LIST_BLACKLIST, _("blacklist"))])
32 config.ParentalControl.setuppin = ConfigPIN(default = -1)
34 config.ParentalControl.retries = ConfigSubsection()
35 config.ParentalControl.retries.setuppin = ConfigSubsection()
36 config.ParentalControl.retries.setuppin.tries = ConfigInteger(default = 3)
37 config.ParentalControl.retries.setuppin.time = ConfigInteger(default = 3)
38 config.ParentalControl.retries.servicepin = ConfigSubsection()
39 config.ParentalControl.retries.servicepin.tries = ConfigInteger(default = 3)
40 config.ParentalControl.retries.servicepin.time = ConfigInteger(default = 3)
41 # config.ParentalControl.configured = configElement("config.ParentalControl.configured", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
42 #config.ParentalControl.mode = configElement("config.ParentalControl.mode", configSelection, 0, (("simple", _("simple")), ("complex", _("complex"))))
43 #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"))))
44 #config.ParentalControl.servicepinactive = configElement("config.ParentalControl.servicepinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
45 #config.ParentalControl.setuppinactive = configElement("config.ParentalControl.setuppinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
46 #config.ParentalControl.type = configElement("config.ParentalControl.type", configSelection, 0, (("whitelist", _("whitelist")), ("blacklist", _("blacklist"))))
47 #config.ParentalControl.setuppin = configElement("config.ParentalControl.setuppin", configSequence, "0000", configSequenceArg().get("PINCODE", (4, "")))
49 config.ParentalControl.servicepin = ConfigSubList()
52 config.ParentalControl.servicepin.append(ConfigPIN(default = -1))
53 #config.ParentalControl.servicepin.append(configElement("config.ParentalControl.servicepin.level" + str(i), configSequence, "0000", configSequenceArg().get("PINCODE", (4, ""))))
55 class ParentalControl:
57 #Do not call open on init, because bouquets are not ready at that moment
59 self.serviceLevel = {}
60 #Instead: Use Flags to see, if we already initialized config and called open
61 self.configInitialized = False
62 #This is the timer that is used to see, if the time for caching the pin is over
63 #Of course we could also work without a timer and compare the times every
64 #time we call isServicePlayable. But this might probably slow down zapping,
65 #That's why I decided to use a timer
66 self.sessionPinTimer = eTimer()
67 self.sessionPinTimer.callback.append(self.resetSessionPin)
69 def serviceMethodWrapper(self, service, method, *args):
70 #This method is used to call all functions that need a service as Parameter:
71 #It takes either a Service- Reference or a Bouquet- Reference and passes
72 #Either the service or all services contained in the bouquet to the method given
73 #That way all other functions do not need to distinguish between service and bouquet.
74 if "FROM BOUQUET" in service:
75 method( service , TYPE_BOUQUET , *args )
76 servicelist = self.readServicesFromBouquet(service,"C")
77 for ref in servicelist:
79 method( sRef , TYPE_BOUQUETSERVICE , *args )
81 ref = ServiceReference(service)
83 method( sRef , TYPE_SERVICE , *args )
85 def setServiceLevel(self, service, type, level):
86 self.serviceLevel[service] = level
88 def isServicePlayable(self, ref, callback):
89 if not config.ParentalControl.configured.value or not config.ParentalControl.servicepinactive.value:
91 #Check if configuration has already been read or if the significant values have changed.
92 #If true: read the configuration
93 if self.configInitialized == False or self.storeServicePin != config.ParentalControl.storeservicepin.value or self.storeServicePinCancel != config.ParentalControl.storeservicepincancel.value:
94 self.getConfigValues()
95 service = ref.toCompareString()
96 if (config.ParentalControl.type.value == LIST_WHITELIST and not self.whitelist.has_key(service)) or (config.ParentalControl.type.value == LIST_BLACKLIST and self.blacklist.has_key(service)):
97 #Check if the session pin is cached and return the cached value, if it is.
98 if self.sessionPinCached == True:
99 #As we can cache successful pin- entries as well as canceled pin- entries,
100 #We give back the last action
101 return self.sessionPinCachedValue
102 self.callback = callback
103 #Someone started to implement different levels of protection. Seems they were never completed
104 #I did not throw out this code, although it is of no use at the moment
106 if self.serviceLevel.has_key(service):
107 levelNeeded = self.serviceLevel[service]
108 pinList = self.getPinList()[:levelNeeded + 1]
109 Notifications.AddNotificationWithCallback(boundFunction(self.servicePinEntered, ref), PinInput, triesEntry = config.ParentalControl.retries.servicepin, pinList = pinList, service = ServiceReference(ref).getServiceName(), title = _("this service is protected by a parental control pin"), windowTitle = _("Parental control"))
114 def protectService(self, service):
115 if config.ParentalControl.type.value == LIST_WHITELIST:
116 if self.whitelist.has_key(service):
117 self.serviceMethodWrapper(service, self.removeServiceFromList, self.whitelist)
118 #self.deleteWhitelistService(service)
120 if not self.blacklist.has_key(service):
121 self.serviceMethodWrapper(service, self.addServiceToList, self.blacklist)
122 #self.addBlacklistService(service)
123 #print "whitelist:", self.whitelist
124 #print "blacklist:", self.blacklist
126 def unProtectService(self, service):
128 #print "config.ParentalControl.type.value:", config.ParentalControl.type.value
129 if config.ParentalControl.type.value == LIST_WHITELIST:
130 if not self.whitelist.has_key(service):
131 self.serviceMethodWrapper(service, self.addServiceToList, self.whitelist)
132 #self.addWhitelistService(service)
134 if self.blacklist.has_key(service):
135 self.serviceMethodWrapper(service, self.removeServiceFromList, self.blacklist)
136 #self.deleteBlacklistService(service)
137 #print "whitelist:", self.whitelist
138 #print "blacklist:", self.blacklist
140 def getProtectionLevel(self, service):
141 if (config.ParentalControl.type.value == LIST_WHITELIST and not self.whitelist.has_key(service)) or (config.ParentalControl.type.value == LIST_BLACKLIST and self.blacklist.has_key(service)):
142 if self.serviceLevel.has_key(service):
143 return self.serviceLevel[service]
149 def getProtectionType(self, service):
150 #New method used in ParentalControlList: This method does not only return
151 #if a service is protected or not, it also returns, why (whitelist or blacklist, service or bouquet)
153 if (config.ParentalControl.type.value == LIST_WHITELIST):
154 if self.whitelist.has_key(service):
155 if TYPE_SERVICE in self.whitelist[service]:
156 sImage = IMG_WHITESERVICE
158 sImage = IMG_WHITEBOUQUET
159 elif (config.ParentalControl.type.value == LIST_BLACKLIST):
160 if self.blacklist.has_key(service):
161 if TYPE_SERVICE in self.blacklist[service]:
162 sImage = IMG_BLACKSERVICE
164 sImage = IMG_BLACKBOUQUET
165 bLocked = self.getProtectionLevel(service) != -1
166 return (bLocked,sImage)
168 def getConfigValues(self):
169 #Read all values from configuration
170 self.checkPinInterval = False
171 self.checkPinIntervalCancel = False
172 self.checkSessionPin = False
173 self.checkSessionPinCancel = False
175 self.sessionPinCached = False
176 self.pinIntervalSeconds = 0
177 self.pinIntervalSecondsCancel = 0
179 self.storeServicePin = config.ParentalControl.storeservicepin.value
180 self.storeServicePinCancel = config.ParentalControl.storeservicepincancel.value
182 if self.storeServicePin == "never":
184 elif self.storeServicePin == "standby":
185 self.checkSessionPin = True
187 self.checkPinInterval = True
188 iMinutes = float(self.storeServicePin)
189 iSeconds = iMinutes*60
190 self.pinIntervalSeconds = iSeconds
192 if self.storeServicePinCancel == "never":
194 elif self.storeServicePinCancel == "standby":
195 self.checkSessionPinCancel = True
197 self.checkPinIntervalCancel = True
198 iMinutes = float(self.storeServicePinCancel)
199 iSeconds = iMinutes*60
200 self.pinIntervalSecondsCancel = iSeconds
202 self.configInitialized = True
203 # Reset PIN cache on standby: Use StandbyCounter- Config- Callback
204 config.misc.standbyCounter.addNotifier(self.standbyCounterCallback, initial_call = False)
206 def standbyCounterCallback(self, configElement):
207 self.resetSessionPin()
209 def resetSessionPin(self):
210 #Reset the session pin, stop the timer
211 self.sessionPinCached = False
212 self.sessionPinTimer.stop()
214 def getCurrentTimeStamp(self):
217 def getPinList(self):
218 return [ x.value for x in config.ParentalControl.servicepin ]
220 def servicePinEntered(self, service, result):
222 if result is not None and result:
223 #This is the new function of caching the service pin
224 #save last session and time of last entered pin...
225 if self.checkSessionPin == True:
226 self.sessionPinCached = True
227 self.sessionPinCachedValue = True
228 if self.checkPinInterval == True:
229 self.sessionPinCached = True
230 self.sessionPinCachedValue = True
231 self.sessionPinTimer.start(self.pinIntervalSeconds*1000,1)
232 self.callback(ref = service)
234 #This is the new function of caching cancelling of service pin
235 if result is not None:
236 Notifications.AddNotification(MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
238 if self.checkSessionPinCancel == True:
239 self.sessionPinCached = True
240 self.sessionPinCachedValue = False
241 if self.checkPinIntervalCancel == True:
242 self.sessionPinCached = True
243 self.sessionPinCachedValue = False
244 self.sessionPinTimer.start(self.pinIntervalSecondsCancel*1000,1)
246 def saveListToFile(self,sWhichList):
247 #Replaces saveWhiteList and saveBlackList:
248 #I don't like to have two functions with identical code...
249 if sWhichList == LIST_BLACKLIST:
250 vList = self.blacklist
252 vList = self.whitelist
253 file = open(resolveFilename(SCOPE_CONFIG, sWhichList), 'w')
254 for sService,sType in vList.iteritems():
255 #Only Services that are selected directly and Bouqets are saved.
256 #Services that are added by a bouquet are not saved.
257 #This is the reason for the change in self.whitelist and self.blacklist
258 if TYPE_SERVICE in sType or TYPE_BOUQUET in sType:
259 file.write(str(sService) + "\n")
262 def openListFromFile(self,sWhichList):
263 #Replaces openWhiteList and openBlackList:
264 #I don't like to have two functions with identical code...
265 if sWhichList == LIST_BLACKLIST:
267 vList = self.blacklist
270 vList = self.whitelist
272 file = open(resolveFilename(SCOPE_CONFIG, sWhichList ), 'r')
273 lines = file.readlines()
276 self.serviceMethodWrapper(sPlain, self.addServiceToList, vList)
281 def addServiceToList(self, service, type, vList):
282 #Replaces addWhitelistService and addBlacklistService
283 #The lists are not only lists of service references any more.
284 #They are named lists with the service as key and an array of types as value:
286 if vList.has_key(service):
287 if not type in vList[service]:
288 vList[service].append(type)
290 vList[service] = [type]
292 def removeServiceFromList(self, service, type, vList):
293 #Replaces deleteWhitelistService and deleteBlacklistService
294 if vList.has_key(service):
295 if type in vList[service]:
296 vList[service].remove(type)
297 if not vList[service]:
299 if self.serviceLevel.has_key(service):
300 self.serviceLevel.remove(service)
302 def readServicesFromBouquet(self,sBouquetSelection,formatstring):
303 #This method gives back a list of services for a given bouquet
304 from enigma import eServiceCenter, eServiceReference
305 from Screens.ChannelSelection import service_types_tv
306 serviceHandler = eServiceCenter.getInstance()
307 refstr = sBouquetSelection
308 root = eServiceReference(refstr)
309 list = serviceHandler.list(root)
311 services = list.getContent("CN", True) #(servicecomparestring, name)
315 # we need to open the files in case we havent's read them yet
316 self.saveListToFile(LIST_BLACKLIST)
317 self.saveListToFile(LIST_WHITELIST)
320 self.openListFromFile(LIST_BLACKLIST)
321 self.openListFromFile(LIST_WHITELIST)
323 parentalControl = ParentalControl()