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