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