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