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