Do not crash if eServiceCenter.info is none
[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                         if info:
160                                 name = info.getName(ref).replace('\xc2\x86', '').replace('\xc2\x87', '')
161                                 event = info.getEvent(ref)
162                                 if event is not None:
163                                         eventName = event.getEventName()
164                                         if eventName is None:
165                                                 eventName = ""
166                                 else:
167                                         eventName = ""
168                         else:
169                                 name = "N/A"
170                                 eventName = ""
171                         list.append(ZapHistoryBrowserListEntry(name, eventName))
172                 list.reverse()
173                 self["list"].setList(list)
174
175         def zap(self):
176                 length = len(self.servicelist.history)
177                 if length > 0:
178                         self.servicelist.history_pos = (length - self["list"].getSelectionIndex()) - 1
179                         self.servicelist.setHistoryPath()
180
181         def clear(self):
182                 if self.allowChanges:
183                         for i in range(0, len(self.servicelist.history)):
184                                 del self.servicelist.history[0]
185                         self.buildList()
186                         self.servicelist.history_pos = 0
187
188         def delete(self):
189                 if self.allowChanges:
190                         length = len(self.servicelist.history)
191                         if length > 0:
192                                 idx = (length - self["list"].getSelectionIndex()) - 1
193                                 del self.servicelist.history[idx]
194                                 self.buildList()
195                                 currRef = self.session.nav.getCurrentlyPlayingServiceReference()
196                                 idx = 0
197                                 for x in self.servicelist.history:
198                                         if len(x) == 2: # Single-Bouquet
199                                                 ref = x[1]
200                                         else: # Multi-Bouquet
201                                                 ref = x[2]
202                                         if ref == currRef:
203                                                 self.servicelist.history_pos = idx
204                                                 break
205                                         else:
206                                                 idx += 1
207
208         def zapAndClose(self):
209                 self.zap()
210                 self.close()
211
212         def config(self):
213                 if self.allowChanges:
214                         self.session.open(ZapHistoryConfigurator)
215
216         def isProtected(self):
217                 return config.ParentalControl.setuppinactive.value and config.ParentalControl.configured.value
218         
219         def pinEntered(self, result):
220                 if result is None:
221                         self.allowChanges = False
222                 elif not result:
223                         self.allowChanges = False
224                 else:
225                         self.allowChanges = True
226
227 ################################################
228
229 def main(session, servicelist, **kwargs):
230         session.open(ZapHistoryBrowser, servicelist)
231
232 def Plugins(**kwargs):
233         return PluginDescriptor(name=_("Zap-History Browser"), where=PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)