simplified translation-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.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="200,80" size="320,440" title="%s" >
37                 <ePixmap pixmap="skin_default/buttons/red.png" position="10,0" size="140,40" transparent="1" alphatest="on" />
38                 <ePixmap pixmap="skin_default/buttons/green.png" position="170,0" size="140,40" transparent="1" alphatest="on" />
39                 <widget name="key_red" position="10,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
40                 <widget name="key_green" position="170,0" zPosition="1" size="140,40" font="Regular;20" valign="center" halign="center" backgroundColor="#1f771f" transparent="1" />
41                 <widget name="list" position="0,40" size="320,400" scrollbarMode="showOnDemand" />
42         </screen>""" % _("Zap-History Browser")
43
44         def __init__(self, session, servicelist):
45                 Screen.__init__(self, session)
46                 self.session = session
47                 
48                 self.servicelist = servicelist
49                 self.serviceHandler = eServiceCenter.getInstance()
50                 
51                 self["list"] = MenuList([])
52                 self["key_red"] = Label(_("Clear"))
53                 self["key_green"] = Label(_("Delete"))
54                 
55                 self["actions"] = ActionMap(["OkCancelActions", "ColorActions"],
56                         {
57                                 "ok": self.zap,
58                                 "cancel": self.close,
59                                 "red": self.clear,
60                                 "green": self.delete
61                         }, prio=-1)
62                 
63                 self.onLayoutFinish.append(self.buildList)
64
65         def buildList(self):
66                 list = []
67                 for x in self.servicelist.history:
68                         if len(x) == 2: # Single-Bouquet
69                                 ref = x[1]
70                         else: # Multi-Bouquet
71                                 ref = x[2]
72                         info = self.serviceHandler.info(ref)
73                         name = info.getName(ref).replace('\xc2\x86', '').replace('\xc2\x87', '')
74                         list.append(name)
75                 list.reverse()
76                 self["list"].setList(list)
77
78         def zap(self):
79                 length = len(self.servicelist.history)
80                 if length > 0:
81                         self.servicelist.history_pos = (length - self["list"].getSelectionIndex()) - 1
82                         self.servicelist.setHistoryPath()
83
84         def clear(self):
85                 for i in range(0, len(self.servicelist.history)):
86                         del self.servicelist.history[0]
87                 self.buildList()
88                 self.servicelist.history_pos = 0
89
90         def delete(self):
91                 length = len(self.servicelist.history)
92                 if length > 0:
93                         idx = (length - self["list"].getSelectionIndex()) - 1
94                         del self.servicelist.history[idx]
95                         self.buildList()
96                         
97                         # We must check if the current service is still in the zap-history
98                         currRef = self.session.nav.getCurrentlyPlayingServiceReference()
99                         idx = 0
100                         for x in self.servicelist.history:
101                                 if len(x) == 2: # Single-Bouquet
102                                         ref = x[1]
103                                 else: # Multi-Bouquet
104                                         ref = x[2]
105                                 
106                                 if ref == currRef:
107                                         self.servicelist.history_pos = idx
108                                         break
109                                 else:
110                                         idx += 1
111
112 ################################################
113
114 def main(session, servicelist, **kwargs):
115         session.open(ZapHistoryBrowser, servicelist)
116
117 def Plugins(**kwargs):
118         return PluginDescriptor(name=_("Zap-History Browser"), where=PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main)