added parental control screen... allow changes (configuration, delete, clear) only...
[enigma2-plugins.git] / zaphistorybrowser / src / plugin.py
1 # -*- coding: UTF-8 -*-
2 ## Zap-History Browser by AliAbdul
3 from Components.ActionMap import ActionMap
4 from Components.config import config, ConfigInteger, ConfigSelection, ConfigSubsection, getConfigListEntry
5 from Components.ConfigList import ConfigListScreen
6 from Components.Label import Label
7 from Components.Language import language
8 from Components.MenuList import MenuList
9 from Components.MultiContent import MultiContentEntryText
10 from enigma import eListboxPythonMultiContent, eServiceCenter, gFont
11 from os import environ
12 from Plugins.Plugin import PluginDescriptor
13 from Screens.ChannelSelection import ChannelSelection
14 from Screens.ParentalControlSetup import ProtectedScreen
15 from Screens.Screen import Screen
16 from Tools.Directories import resolveFilename, SCOPE_LANGUAGE, SCOPE_PLUGINS
17 import gettext
18
19 ################################################
20
21 def localeInit():
22         lang = language.getLanguage()
23         environ["LANGUAGE"] = lang[:2]\r
24         gettext.bindtextdomain("enigma2", resolveFilename(SCOPE_LANGUAGE))\r
25         gettext.textdomain("enigma2")
26         gettext.bindtextdomain("ZapHistoryBrowser", "%s%s" % (resolveFilename(SCOPE_PLUGINS), "Extensions/ZapHistoryBrowser/locale/"))
27
28 def _(txt):\r
29         t = gettext.dgettext("ZapHistoryBrowser", txt)\r
30         if t == txt:\r
31                 t = gettext.gettext(txt)\r
32         return t
33
34 localeInit()\r
35 language.addCallback(localeInit)
36
37 ################################################
38
39 config.plugins.ZapHistoryConfigurator = ConfigSubsection()
40 config.plugins.ZapHistoryConfigurator.enable_zap_history = ConfigSelection(choices = {"off": _("disabled"), "on": _("enabled"), "parental_lock": _("disabled at parental lock")}, default="on")
41 config.plugins.ZapHistoryConfigurator.maxEntries_zap_history = ConfigInteger(default=20, limits=(1, 60))
42
43 ################################################
44
45 def addToHistory(instance, ref):
46         if config.plugins.ZapHistoryConfigurator.enable_zap_history.value == "off":
47                 return
48         if config.ParentalControl.configured.value and config.plugins.ZapHistoryConfigurator.enable_zap_history.value == "parental_lock":
49                 if parentalControl.getProtectionLevel(ref.toCompareString()) != -1:
50                         return
51         if instance.servicePath is not None:
52                 tmp = instance.servicePath[:]
53                 tmp.append(ref)
54                 try: del instance.history[instance.history_pos+1:]
55                 except: pass
56                 instance.history.append(tmp)
57                 hlen = len(instance.history)
58                 if hlen > config.plugins.ZapHistoryConfigurator.maxEntries_zap_history.value:
59                         del instance.history[0]
60                         hlen -= 1
61                 instance.history_pos = hlen-1
62
63 ChannelSelection.addToHistory = addToHistory
64
65 ################################################
66
67 class ZapHistoryConfigurator(ConfigListScreen, Screen):
68         skin = """
69                 <screen position="center,center" size="420,70" title="%s" >
70                         <widget name="config" position="0,0" size="420,70" scrollbarMode="showOnDemand" />
71                 </screen>""" % _("Zap-History Configurator")
72
73         def __init__(self, session):
74                 Screen.__init__(self, session)
75                 self.session = session
76                 
77                 ConfigListScreen.__init__(self, [
78                         getConfigListEntry(_("Enable zap history:"), config.plugins.ZapHistoryConfigurator.enable_zap_history),
79                         getConfigListEntry(_("Maximum zap history entries:"), config.plugins.ZapHistoryConfigurator.maxEntries_zap_history)])
80                 
81                 self["actions"] = ActionMap(["OkCancelActions"], {"ok": self.save, "cancel": self.exit}, -2)
82
83         def save(self):
84                 for x in self["config"].list:
85                         x[1].save()
86                 self.close()
87
88         def exit(self):
89                 for x in self["config"].list:
90                         x[1].cancel()
91                 self.close()
92
93 ################################################
94
95 class ZapHistoryBrowserList(MenuList):
96         def __init__(self, list, enableWrapAround=False):
97                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
98                 self.l.setItemHeight(40)
99                 self.l.setFont(0, gFont("Regular", 20))
100                 self.l.setFont(1, gFont("Regular", 18))
101
102 def ZapHistoryBrowserListEntry(serviceName, eventName):
103         res = [serviceName]
104         res.append(MultiContentEntryText(pos=(0, 0), size=(560, 22), font=0, text=serviceName))
105         res.append(MultiContentEntryText(pos=(0, 22), size=(560, 18), font=1, text=eventName))
106         return res
107
108 ################################################
109
110 class ZapHistoryBrowser(Screen, ProtectedScreen):
111         skin = """
112         <screen position="center,center" size="560,440" title="%s" >
113                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" transparent="1" alphatest="on" />
114                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" transparent="1" alphatest="on" />
115                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
116                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" transparent="1" alphatest="on" />
117                 <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
118                 <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
119                 <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
120                 <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
121                 <widget name="list" position="0,40" size="560,400" scrollbarMode="showOnDemand" />
122         </screen>""" % _("Zap-History Browser")
123
124         def __init__(self, session, servicelist):
125                 Screen.__init__(self, session)
126                 ProtectedScreen.__init__(self)
127                 self.session = session
128                 
129                 self.servicelist = servicelist
130                 self.serviceHandler = eServiceCenter.getInstance()
131                 self.allowChanges = True
132                 
133                 self["list"] = ZapHistoryBrowserList([])
134                 self["key_red"] = Label(_("Clear"))
135                 self["key_green"] = Label(_("Delete"))
136                 self["key_yellow"] = Label(_("Zap & Close"))
137                 self["key_blue"] = Label(_("Config"))
138                 
139                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
140                         {
141                                 "ok": self.zap,
142                                 "cancel": self.close,
143                                 "red": self.clear,
144                                 "green": self.delete,
145                                 "yellow": self.zapAndClose,
146                                 "blue": self.config
147                         }, prio=-1)
148                 
149                 self.onLayoutFinish.append(self.buildList)
150
151         def buildList(self):
152                 list = []
153                 for x in self.servicelist.history:
154                         if len(x) == 2: # Single-Bouquet
155                                 ref = x[1]
156                         else: # Multi-Bouquet
157                                 ref = x[2]
158                         info = self.serviceHandler.info(ref)
159                         name = info.getName(ref).replace('\xc2\x86', '').replace('\xc2\x87', '')
160                         event = info.getEvent(ref)
161                         if event is not None:
162                                 eventName = event.getEventName()
163                                 if eventName is None:
164                                         eventName = ""
165                         else:
166                                 eventName = ""
167                         list.append(ZapHistoryBrowserListEntry(name, eventName))
168                 list.reverse()
169                 self["list"].setList(list)
170
171         def zap(self):
172                 length = len(self.servicelist.history)
173                 if length > 0:
174                         self.servicelist.history_pos = (length - self["list"].getSelectionIndex()) - 1
175                         self.servicelist.setHistoryPath()
176
177         def clear(self):
178                 if self.allowChanges:
179                         for i in range(0, len(self.servicelist.history)):
180                                 del self.servicelist.history[0]
181                         self.buildList()
182                         self.servicelist.history_pos = 0
183
184         def delete(self):
185                 if self.allowChanges:
186                         length = len(self.servicelist.history)
187                         if length > 0:
188                                 idx = (length - self["list"].getSelectionIndex()) - 1
189                                 del self.servicelist.history[idx]
190                                 self.buildList()
191                                 currRef = self.session.nav.getCurrentlyPlayingServiceReference()
192                                 idx = 0
193                                 for x in self.servicelist.history:
194                                         if len(x) == 2: # Single-Bouquet
195                                                 ref = x[1]
196                                         else: # Multi-Bouquet
197                                                 ref = x[2]
198                                         if ref == currRef:
199                                                 self.servicelist.history_pos = idx
200                                                 break
201                                         else:
202                                                 idx += 1
203
204         def zapAndClose(self):
205                 self.zap()
206                 self.close()
207
208         def config(self):
209                 if self.allowChanges:
210                         self.session.open(ZapHistoryConfigurator)
211
212         def isProtected(self):
213                 return config.ParentalControl.setuppinactive.value and config.ParentalControl.configured.value
214         
215         def pinEntered(self, result):
216                 if result is None:
217                         self.allowChanges = False
218                 elif not result:
219                         self.allowChanges = False
220                 else:
221                         self.allowChanges = True
222
223 ################################################
224
225 def main(session, servicelist, **kwargs):
226         session.open(ZapHistoryBrowser, servicelist)
227
228 def Plugins(**kwargs):
229         return PluginDescriptor(name=_("Zap-History Browser"), where=PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)