use multicontent list and show also the current event
[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.Label import Label
5 from Components.Language import language
6 from Components.MenuList import MenuList
7 from Components.MultiContent import MultiContentEntryText
8 from enigma import eListboxPythonMultiContent, eServiceCenter, gFont
9 from os import environ
10 from Plugins.Plugin import PluginDescriptor
11 from Screens.Screen import Screen
12 from Tools.Directories import resolveFilename, SCOPE_LANGUAGE, SCOPE_PLUGINS
13 import gettext
14
15 ################################################
16
17 def localeInit():
18         lang = language.getLanguage()
19         environ["LANGUAGE"] = lang[:2]\r
20         gettext.bindtextdomain("enigma2", resolveFilename(SCOPE_LANGUAGE))\r
21         gettext.textdomain("enigma2")
22         gettext.bindtextdomain("ZapHistoryBrowser", "%s%s" % (resolveFilename(SCOPE_PLUGINS), "Extensions/ZapHistoryBrowser/locale/"))
23
24 def _(txt):\r
25         t = gettext.dgettext("ZapHistoryBrowser", txt)\r
26         if t == txt:\r
27                 t = gettext.gettext(txt)\r
28         return t
29
30 localeInit()\r
31 language.addCallback(localeInit)
32
33 ################################################
34
35 class ZapHistoryBrowserList(MenuList):
36         def __init__(self, list, enableWrapAround=False):
37                 MenuList.__init__(self, list, enableWrapAround, eListboxPythonMultiContent)
38                 self.l.setItemHeight(40)
39                 self.l.setFont(0, gFont("Regular", 20))
40                 self.l.setFont(1, gFont("Regular", 18))
41
42 def ZapHistoryBrowserListEntry(serviceName, eventName):
43         res = [serviceName]
44         res.append(MultiContentEntryText(pos=(0, 0), size=(560, 22), font=0, text=serviceName))
45         res.append(MultiContentEntryText(pos=(0, 22), size=(560, 18), font=1, text=eventName))
46         return res
47
48 ################################################
49
50 class ZapHistoryBrowser(Screen):
51         skin = """
52         <screen position="center,center" size="560,440" title="%s" >
53                 <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" transparent="1" alphatest="on" />
54                 <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" transparent="1" alphatest="on" />
55                 <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" transparent="1" alphatest="on" />
56                 <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" transparent="1" alphatest="on" />
57                 <widget name="key_red" position="0,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
58                 <widget name="key_green" position="140,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
59                 <widget name="key_yellow" position="280,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
60                 <widget name="key_blue" position="420,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
61                 <widget name="list" position="0,40" size="560,400" scrollbarMode="showOnDemand" />
62         </screen>""" % _("Zap-History Browser")
63
64         def __init__(self, session, servicelist):
65                 Screen.__init__(self, session)
66                 self.session = session
67                 
68                 self.servicelist = servicelist
69                 self.serviceHandler = eServiceCenter.getInstance()
70                 
71                 self["list"] = ZapHistoryBrowserList([])
72                 self["key_red"] = Label(_("Clear"))
73                 self["key_green"] = Label(_("Delete"))
74                 self["key_yellow"] = Label(_("Zap & Close"))
75                 self["key_blue"] = Label("")
76                 
77                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
78                         {
79                                 "ok": self.zap,
80                                 "cancel": self.close,
81                                 "red": self.clear,
82                                 "green": self.delete,
83                                 "yellow": self.zapAndClose
84                         }, prio=-1)
85                 
86                 self.onLayoutFinish.append(self.buildList)
87
88         def buildList(self):
89                 list = []
90                 for x in self.servicelist.history:
91                         if len(x) == 2: # Single-Bouquet
92                                 ref = x[1]
93                         else: # Multi-Bouquet
94                                 ref = x[2]
95                         info = self.serviceHandler.info(ref)
96                         name = info.getName(ref).replace('\xc2\x86', '').replace('\xc2\x87', '')
97                         event = info.getEvent(ref)
98                         if event is not None:
99                                 eventName = event.getEventName()
100                                 if eventName is None:
101                                         eventName = ""
102                         else:
103                                 eventName = ""
104                         list.append(ZapHistoryBrowserListEntry(name, eventName))
105                 list.reverse()
106                 self["list"].setList(list)
107
108         def zap(self):
109                 length = len(self.servicelist.history)
110                 if length > 0:
111                         self.servicelist.history_pos = (length - self["list"].getSelectionIndex()) - 1
112                         self.servicelist.setHistoryPath()
113
114         def clear(self):
115                 for i in range(0, len(self.servicelist.history)):
116                         del self.servicelist.history[0]
117                 self.buildList()
118                 self.servicelist.history_pos = 0
119
120         def delete(self):
121                 length = len(self.servicelist.history)
122                 if length > 0:
123                         idx = (length - self["list"].getSelectionIndex()) - 1
124                         del self.servicelist.history[idx]
125                         self.buildList()
126                         
127                         # We must check if the current service is still in the zap-history
128                         currRef = self.session.nav.getCurrentlyPlayingServiceReference()
129                         idx = 0
130                         for x in self.servicelist.history:
131                                 if len(x) == 2: # Single-Bouquet
132                                         ref = x[1]
133                                 else: # Multi-Bouquet
134                                         ref = x[2]
135                                 
136                                 if ref == currRef:
137                                         self.servicelist.history_pos = idx
138                                         break
139                                 else:
140                                         idx += 1
141
142         def zapAndClose(self):
143                 self.zap()
144                 self.close()
145
146 ################################################
147
148 def main(session, servicelist, **kwargs):
149         session.open(ZapHistoryBrowser, servicelist)
150
151 def Plugins(**kwargs):
152         return PluginDescriptor(name=_("Zap-History Browser"), where=PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)