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 parentalControl = None
25 def InitParentalControl():
26 global parentalControl
27 parentalControl = ParentalControl()
28 config.ParentalControl = ConfigSubsection()
29 config.ParentalControl.configured = ConfigYesNo(default = False)
30 config.ParentalControl.mode = ConfigSelection(default = "simple", choices = [("simple", _("simple")), ("complex", _("complex"))])
31 config.ParentalControl.storeservicepin = ConfigSelection(default = "never", choices = [("never", _("never")), ("5", _("5 minutes")), ("30", _("30 minutes")), ("60", _("60 minutes")), ("standby", _("until standby/restart"))])
32 config.ParentalControl.storeservicepincancel = ConfigSelection(default = "never", choices = [("never", _("never")), ("5", _("5 minutes")), ("30", _("30 minutes")), ("60", _("60 minutes")), ("standby", _("until standby/restart"))])
33 config.ParentalControl.servicepinactive = ConfigYesNo(default = False)
34 config.ParentalControl.setuppinactive = ConfigYesNo(default = False)
35 config.ParentalControl.type = ConfigSelection(default = "blacklist", choices = [(LIST_WHITELIST, _("whitelist")), (LIST_BLACKLIST, _("blacklist"))])
36 config.ParentalControl.setuppin = ConfigPIN(default = -1)
38 config.ParentalControl.retries = ConfigSubsection()
39 config.ParentalControl.retries.setuppin = ConfigSubsection()
40 config.ParentalControl.retries.setuppin.tries = ConfigInteger(default = 3)
41 config.ParentalControl.retries.setuppin.time = ConfigInteger(default = 3)
42 config.ParentalControl.retries.servicepin = ConfigSubsection()
43 config.ParentalControl.retries.servicepin.tries = ConfigInteger(default = 3)
44 config.ParentalControl.retries.servicepin.time = ConfigInteger(default = 3)
45 # config.ParentalControl.configured = configElement("config.ParentalControl.configured", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
46 #config.ParentalControl.mode = configElement("config.ParentalControl.mode", configSelection, 0, (("simple", _("simple")), ("complex", _("complex"))))
47 #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"))))
48 #config.ParentalControl.servicepinactive = configElement("config.ParentalControl.servicepinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
49 #config.ParentalControl.setuppinactive = configElement("config.ParentalControl.setuppinactive", configSelection, 1, (("yes", _("yes")), ("no", _("no"))))
50 #config.ParentalControl.type = configElement("config.ParentalControl.type", configSelection, 0, (("whitelist", _("whitelist")), ("blacklist", _("blacklist"))))
51 #config.ParentalControl.setuppin = configElement("config.ParentalControl.setuppin", configSequence, "0000", configSequenceArg().get("PINCODE", (4, "")))
53 config.ParentalControl.servicepin = ConfigSubList()
56 config.ParentalControl.servicepin.append(ConfigPIN(default = -1))
57 #config.ParentalControl.servicepin.append(configElement("config.ParentalControl.servicepin.level" + str(i), configSequence, "0000", configSequenceArg().get("PINCODE", (4, ""))))
59 class ParentalControl:
61 #Do not call open on init, because bouquets are not ready at that moment
63 self.serviceLevel = {}
64 #Instead: Use Flags to see, if we already initialized config and called open
65 self.configInitialized = False
66 #This is the timer that is used to see, if the time for caching the pin is over
67 #Of course we could also work without a timer and compare the times every
68 #time we call isServicePlayable. But this might probably slow down zapping,
69 #That's why I decided to use a timer
70 self.sessionPinTimer = eTimer()
71 self.sessionPinTimer.callback.append(self.resetSessionPin)
73 def serviceMethodWrapper(self, service, method, *args):
74 #This method is used to call all functions that need a service as Parameter:
75 #It takes either a Service- Reference or a Bouquet- Reference and passes
76 #Either the service or all services contained in the bouquet to the method given
77 #That way all other functions do not need to distinguish between service and bouquet.
78 if "FROM BOUQUET" in service:
79 method( service , TYPE_BOUQUET , *args )
80 servicelist = self.readServicesFromBouquet(service,"C")
81 for ref in servicelist:
83 method( sRef , TYPE_BOUQUETSERVICE , *args )
85 ref = ServiceReference(service)
87 method( sRef , TYPE_SERVICE , *args )
89 def setServiceLevel(self, service, type, level):
90 self.serviceLevel[service] = level
92 def isServicePlayable(self, ref, callback):
93 if not config.ParentalControl.configured.value or not config.ParentalControl.servicepinactive.value:
95 #Check if configuration has already been read or if the significant values have changed.
96 #If true: read the configuration
97 if self.configInitialized == False or self.storeServicePin != config.ParentalControl.storeservicepin.value or self.storeServicePinCancel != config.ParentalControl.storeservicepincancel.value:
98 self.getConfigValues()
99 service = ref.toCompareString()
100 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)):
101 #Check if the session pin is cached and return the cached value, if it is.
102 if self.sessionPinCached == True:
103 #As we can cache successful pin- entries as well as canceled pin- entries,
104 #We give back the last action
105 return self.sessionPinCachedValue
106 self.callback = callback
107 #Someone started to implement different levels of protection. Seems they were never completed
108 #I did not throw out this code, although it is of no use at the moment
110 if self.serviceLevel.has_key(service):
111 levelNeeded = self.serviceLevel[service]
112 pinList = self.getPinList()[:levelNeeded + 1]
113 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"))
118 def protectService(self, service):
119 if config.ParentalControl.type.value == LIST_WHITELIST:
120 if self.whitelist.has_key(service):
121 self.serviceMethodWrapper(service, self.removeServiceFromList, self.whitelist)
122 #self.deleteWhitelistService(service)
124 if not self.blacklist.has_key(service):
125 self.serviceMethodWrapper(service, self.addServiceToList, self.blacklist)
126 #self.addBlacklistService(service)
127 #print "whitelist:", self.whitelist
128 #print "blacklist:", self.blacklist
130 def unProtectService(self, service):
132 #print "config.ParentalControl.type.value:", config.ParentalControl.type.value
133 if config.ParentalControl.type.value == LIST_WHITELIST:
134 if not self.whitelist.has_key(service):
135 self.serviceMethodWrapper(service, self.addServiceToList, self.whitelist)
136 #self.addWhitelistService(service)
138 if self.blacklist.has_key(service):
139 self.serviceMethodWrapper(service, self.removeServiceFromList, self.blacklist)
140 #self.deleteBlacklistService(service)
141 #print "whitelist:", self.whitelist
142 #print "blacklist:", self.blacklist
144 def getProtectionLevel(self, service):
145 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)):
146 if self.serviceLevel.has_key(service):
147 return self.serviceLevel[service]
153 def getProtectionType(self, service):
154 #New method used in ParentalControlList: This method does not only return
155 #if a service is protected or not, it also returns, why (whitelist or blacklist, service or bouquet)
157 if (config.ParentalControl.type.value == LIST_WHITELIST):
158 if self.whitelist.has_key(service):
159 if TYPE_SERVICE in self.whitelist[service]:
160 sImage = IMG_WHITESERVICE
162 sImage = IMG_WHITEBOUQUET
163 elif (config.ParentalControl.type.value == LIST_BLACKLIST):
164 if self.blacklist.has_key(service):
165 if TYPE_SERVICE in self.blacklist[service]:
166 sImage = IMG_BLACKSERVICE
168 sImage = IMG_BLACKBOUQUET
169 bLocked = self.getProtectionLevel(service) != -1
170 return (bLocked,sImage)
172 def getConfigValues(self):
173 #Read all values from configuration
174 self.checkPinInterval = False
175 self.checkPinIntervalCancel = False
176 self.checkSessionPin = False
177 self.checkSessionPinCancel = False
179 self.sessionPinCached = False
180 self.pinIntervalSeconds = 0
181 self.pinIntervalSecondsCancel = 0
183 self.storeServicePin = config.ParentalControl.storeservicepin.value
184 self.storeServicePinCancel = config.ParentalControl.storeservicepincancel.value
186 if self.storeServicePin == "never":
188 elif self.storeServicePin == "standby":
189 self.checkSessionPin = True
191 self.checkPinInterval = True
192 iMinutes = float(self.storeServicePin)
193 iSeconds = iMinutes*60
194 self.pinIntervalSeconds = iSeconds
196 if self.storeServicePinCancel == "never":
198 elif self.storeServicePinCancel == "standby":
199 self.checkSessionPinCancel = True
201 self.checkPinIntervalCancel = True
202 iMinutes = float(self.storeServicePinCancel)
203 iSeconds = iMinutes*60
204 self.pinIntervalSecondsCancel = iSeconds
206 self.configInitialized = True
207 # Reset PIN cache on standby: Use StandbyCounter- Config- Callback
208 config.misc.standbyCounter.addNotifier(self.standbyCounterCallback, initial_call = False)
210 def standbyCounterCallback(self, configElement):
211 self.resetSessionPin()
213 def resetSessionPin(self):
214 #Reset the session pin, stop the timer
215 self.sessionPinCached = False
216 self.sessionPinTimer.stop()
218 def getCurrentTimeStamp(self):
221 def getPinList(self):
222 return [ x.value for x in config.ParentalControl.servicepin ]
224 def servicePinEntered(self, service, result):
226 if result is not None and result:
227 #This is the new function of caching the service pin
228 #save last session and time of last entered pin...
229 if self.checkSessionPin == True:
230 self.sessionPinCached = True
231 self.sessionPinCachedValue = True
232 if self.checkPinInterval == True:
233 self.sessionPinCached = True
234 self.sessionPinCachedValue = True
235 self.sessionPinTimer.start(self.pinIntervalSeconds*1000,1)
236 self.callback(ref = service)
238 #This is the new function of caching cancelling of service pin
239 if result is not None:
240 Notifications.AddNotification(MessageBox, _("The pin code you entered is wrong."), MessageBox.TYPE_ERROR)
242 if self.checkSessionPinCancel == True:
243 self.sessionPinCached = True
244 self.sessionPinCachedValue = False
245 if self.checkPinIntervalCancel == True:
246 self.sessionPinCached = True
247 self.sessionPinCachedValue = False
248 self.sessionPinTimer.start(self.pinIntervalSecondsCancel*1000,1)
250 def saveListToFile(self,sWhichList):
251 #Replaces saveWhiteList and saveBlackList:
252 #I don't like to have two functions with identical code...
253 if sWhichList == LIST_BLACKLIST:
254 vList = self.blacklist
256 vList = self.whitelist
257 file = open(resolveFilename(SCOPE_CONFIG, sWhichList), 'w')
258 for sService,sType in vList.iteritems():
259 #Only Services that are selected directly and Bouqets are saved.
260 #Services that are added by a bouquet are not saved.
261 #This is the reason for the change in self.whitelist and self.blacklist
262 if TYPE_SERVICE in sType or TYPE_BOUQUET in sType:
263 file.write(str(sService) + "\n")
266 def openListFromFile(self,sWhichList):
267 #Replaces openWhiteList and openBlackList:
268 #I don't like to have two functions with identical code...
269 if sWhichList == LIST_BLACKLIST:
271 vList = self.blacklist
274 vList = self.whitelist
276 file = open(resolveFilename(SCOPE_CONFIG, sWhichList ), 'r')
277 lines = file.readlines()
280 self.serviceMethodWrapper(sPlain, self.addServiceToList, vList)
285 def addServiceToList(self, service, type, vList):
286 #Replaces addWhitelistService and addBlacklistService
287 #The lists are not only lists of service references any more.
288 #They are named lists with the service as key and an array of types as value:
290 if vList.has_key(service):
291 if not type in vList[service]:
292 vList[service].append(type)
294 vList[service] = [type]
296 def removeServiceFromList(self, service, type, vList):
297 #Replaces deleteWhitelistService and deleteBlacklistService
298 if vList.has_key(service):
299 if type in vList[service]:
300 vList[service].remove(type)
301 if not vList[service]:
303 if self.serviceLevel.has_key(service):
304 self.serviceLevel.remove(service)
306 def readServicesFromBouquet(self,sBouquetSelection,formatstring):
307 #This method gives back a list of services for a given bouquet
308 from enigma import eServiceCenter, eServiceReference
309 from Screens.ChannelSelection import service_types_tv
310 serviceHandler = eServiceCenter.getInstance()
311 refstr = sBouquetSelection
312 root = eServiceReference(refstr)
313 list = serviceHandler.list(root)
315 services = list.getContent("CN", True) #(servicecomparestring, name)
319 # we need to open the files in case we havent's read them yet
320 self.saveListToFile(LIST_BLACKLIST)
321 self.saveListToFile(LIST_WHITELIST)
324 self.openListFromFile(LIST_BLACKLIST)
325 self.openListFromFile(LIST_WHITELIST)