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