namezap: another attempt to fix problems with pts
[enigma2-plugins.git] / namezap / src / plugin.py
1 # for localized messages
2 from . import _
3
4 # Plugin definition
5 from Plugins.Plugin import PluginDescriptor
6
7 from enigma import eServiceCenter, eServiceReference
8 from Components.Label import Label
9 from Screens import InfoBarGenerics
10 from Screens.InfoBar import InfoBar
11 from Components.config import config, ConfigSubsection, ConfigSelection
12
13 from NamezapSetup import NamezapSetup
14
15 config.plugins.namezap = ConfigSubsection()
16 config.plugins.namezap.style = ConfigSelection(choices = [
17                 ("number", _("Only Number")),
18                 ("name", _("Service Name")),
19                 ("both", _("Number and Name"))
20         ], default = "both"
21 )
22
23 NumberZap = InfoBarGenerics.NumberZap
24 class NameZap(NumberZap):
25         STYLE_NUMBER = 0
26         STYLE_NAME = 1
27         STYLE_BOTH = 2
28
29         skin = """<screen name="NameZap" position="center,center" size="270,60" title="Channel">
30                         <widget name="name" position="3,15" size="264,25" halign="left" font="Regular;23" />
31                 </screen>"""
32
33         def __init__(self, *args, **kwargs):
34                 NumberZap.__init__(self, *args, **kwargs)
35                 if InfoBar.instance is None:
36                         self.style = self.STYLE_NUMBER
37                 else:
38                         self.slist = InfoBar.instance.servicelist
39                         self.style = {"number": self.STYLE_NUMBER, "name": self.STYLE_NAME, "both": self.STYLE_BOTH}[config.plugins.namezap.style.value]
40
41                 if self.style == self.STYLE_NUMBER:
42                         self.skinName = "NumberZap"
43
44                 self["name"] = Label("")
45                 self.serviceHandler = eServiceCenter.getInstance()
46                 self.updateServiceName(int(self.field))
47
48         def keyNumberGlobal(self, number):
49                 NumberZap.keyNumberGlobal(self, number)
50                 if self.style != self.STYLE_NUMBER:
51                         self.updateServiceName(int(self.field))
52
53         def searchNumberHelper(self, serviceHandler, num, bouquet):
54                 servicelist = self.serviceHandler.list(bouquet)
55                 if not servicelist is None:
56                         while num:
57                                 serviceIterator = servicelist.getNext()
58                                 if not serviceIterator.valid(): #check end of list
59                                         break
60                                 playable = not (serviceIterator.flags & (eServiceReference.isMarker|eServiceReference.isDirectory))
61                                 if playable:
62                                         num -= 1;
63                         if not num: #found service with searched number ?
64                                 return serviceIterator, 0
65                 return None, num
66
67         def updateServiceName(self, number):
68                 bouquet = self.slist.bouquet_root
69                 service = None
70                 serviceHandler = self.serviceHandler
71                 if not config.usage.multibouquet.value:
72                         service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
73                 else:
74                         bouquetlist = serviceHandler.list(bouquet)
75                         if not bouquetlist is None:
76                                 while number:
77                                         bouquet = bouquetlist.getNext()
78                                         if not bouquet.valid(): #check end of list
79                                                 break
80                                         if bouquet.flags & eServiceReference.isDirectory:
81                                                 service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
82                 if service is not None:
83                         info = serviceHandler.info(service)
84
85                         sname = info.getName(service).replace('\xc2\x86', '').replace('\xc2\x87', '')
86                         if self.style == self.STYLE_BOTH:
87                                 self["name"].setText("%s. %s" % (self.field, sname))
88                         else:
89                                 self["name"].setText(sname)
90                 else:
91                         sname = _("Unknown Service")
92                         if self.style == self.STYLE_BOTH:
93                                 self["name"].setText("%s. %s" % (self.field, sname))
94                         else:
95                                 self["name"].setText("%s (%s)" % (sname, self.field))
96
97 def autostart(reason, *args, **kwargs):
98         if reason == 0:
99                 InfoBarGenerics.NumberZap = NameZap
100
101 def main(session, *args, **kwargs):
102         session.open(NamezapSetup)
103
104 def menu(menuid):
105         if menuid != "system":
106                 return []
107         return [(_("NameZAP Setup"), main, "namezap_setup", None)]
108
109 def Plugins(**kwargs):
110         return [
111                 PluginDescriptor(
112                         where=PluginDescriptor.WHERE_AUTOSTART,
113                         fnc=autostart,
114                         needsRestart=False,
115                 ),
116                 PluginDescriptor(
117                         where=PluginDescriptor.WHERE_MENU,
118                         fnc=menu,
119                         needsRestart=False,
120                 ),
121         ]