namezap: fix crash is infobar.instance is none
[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.style = {"number": self.STYLE_NUMBER, "name": self.STYLE_NAME, "both": self.STYLE_BOTH}[config.plugins.namezap.style.value]
39
40                 if self.style == self.STYLE_NUMBER:
41                         self.skinName = "NumberZap"
42
43                 self["name"] = Label("")
44                 self.serviceHandler = eServiceCenter.getInstance()
45                 self.updateServiceName(int(self.field))
46
47         def keyNumberGlobal(self, number):
48                 NumberZap.keyNumberGlobal(self, number)
49                 if self.style != self.STYLE_NUMBER:
50                         self.updateServiceName(int(self.field))
51
52         def searchNumberHelper(self, serviceHandler, num, bouquet):
53                 servicelist = self.serviceHandler.list(bouquet)
54                 if not servicelist is None:
55                         while num:
56                                 serviceIterator = servicelist.getNext()
57                                 if not serviceIterator.valid(): #check end of list
58                                         break
59                                 playable = not (serviceIterator.flags & (eServiceReference.isMarker|eServiceReference.isDirectory))
60                                 if playable:
61                                         num -= 1;
62                         if not num: #found service with searched number ?
63                                 return serviceIterator, 0
64                 return None, num
65
66         def updateServiceName(self, number):
67                 bouquet = InfoBar.instance.servicelist.bouquet_root
68                 service = None
69                 serviceHandler = self.serviceHandler
70                 if not config.usage.multibouquet.value:
71                         service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
72                 else:
73                         bouquetlist = serviceHandler.list(bouquet)
74                         if not bouquetlist is None:
75                                 while number:
76                                         bouquet = bouquetlist.getNext()
77                                         if not bouquet.valid(): #check end of list
78                                                 break
79                                         if bouquet.flags & eServiceReference.isDirectory:
80                                                 service, number = self.searchNumberHelper(serviceHandler, number, bouquet)
81                 if service is not None:
82                         info = serviceHandler.info(service)
83
84                         sname = info.getName(service).replace('\xc2\x86', '').replace('\xc2\x87', '')
85                         if self.style == self.STYLE_BOTH:
86                                 self["name"].setText("%s. %s" % (self.field, sname))
87                         else:
88                                 self["name"].setText(sname)
89                 else:
90                         sname = _("Unknown Service")
91                         if self.style == self.STYLE_BOTH:
92                                 self["name"].setText("%s. %s" % (self.field, sname))
93                         else:
94                                 self["name"].setText("%s (%s)" % (sname, self.field))
95
96 def autostart(reason, *args, **kwargs):
97         if reason == 0:
98                 InfoBarGenerics.NumberZap = NameZap
99
100 def main(session, *args, **kwargs):
101         session.open(NamezapSetup)
102
103 def menu(menuid):
104         if menuid != "system":
105                 return []
106         return [(_("NameZAP Setup"), main, "namezap_setup", None)]
107
108 def Plugins(**kwargs):
109         return [
110                 PluginDescriptor(
111                         where=PluginDescriptor.WHERE_AUTOSTART,
112                         fnc=autostart,
113                         needsRestart=False,
114                 ),
115                 PluginDescriptor(
116                         where=PluginDescriptor.WHERE_MENU,
117                         fnc=menu,
118                         needsRestart=False,
119                 ),
120         ]