small fix
[enigma2.git] / lib / python / Plugins / Extensions / GraphMultiEPG / plugin.py
1 from Plugins.Plugin import PluginDescriptor
2 from GraphMultiEpg import GraphMultiEPG
3 from Screens.ChannelSelection import BouquetSelector
4 from enigma import eServiceCenter, eServiceReference
5 from ServiceReference import ServiceReference
6
7 Session = None
8 Servicelist = None
9
10 bouquetSel = None
11 epg_bouquet = None
12 dlg_stack = [ ]
13
14 def zapToService(service):
15         if not service is None:
16                 if Servicelist.getRoot() != epg_bouquet: #already in correct bouquet?
17                         Servicelist.clearPath()
18                         if Servicelist.bouquet_root != epg_bouquet:
19                                 Servicelist.enterPath(Servicelist.bouquet_root)
20                         Servicelist.enterPath(epg_bouquet)
21                 Servicelist.setCurrentSelection(service) #select the service in Servicelist
22                 Servicelist.zap()
23
24 def getBouquetServices(bouquet):
25         services = [ ]
26         Servicelist = eServiceCenter.getInstance().list(bouquet)
27         if not Servicelist is None:
28                 while True:
29                         service = Servicelist.getNext()
30                         if not service.valid(): #check if end of list
31                                 break
32                         if service.flags & (eServiceReference.isDirectory | eServiceReference.isMarker): #ignore non playable services
33                                 continue
34                         services.append(ServiceReference(service))
35         return services
36
37 def cleanup():
38         global Session
39         Session = None
40         global Servicelist
41         Servicelist = None
42
43 def closed(ret=False):
44         closedScreen = dlg_stack.pop()
45         global bouquetSel
46         if bouquetSel and closedScreen == bouquetSel:
47                 bouquetSel = None
48         dlgs=len(dlg_stack)
49         if ret and dlgs > 0: # recursive close wished
50                 dlg_stack[dlgs-1].close(dlgs > 1)
51         if dlgs <= 0:
52                 cleanup()
53
54 def openBouquetEPG(bouquet):
55         services = getBouquetServices(bouquet)
56         if len(services):
57                 global epg_bouquet
58                 epg_bouquet = bouquet
59                 dlg_stack.append(Session.openWithCallback(closed, GraphMultiEPG, services, zapToService, changeBouquetCB))
60                 return True
61         return False
62
63 def changeBouquetCB(direction, epg):
64         if bouquetSel:
65                 if direction > 0:
66                         bouquetSel.down()
67                 else:
68                         bouquetSel.up()
69                 bouquet = bouquetSel.getCurrent()
70                 services = getBouquetServices(bouquet)
71                 if len(services):
72                         epg_bouquet = bouquet
73                         epg.setServices(services)
74
75 def main(session, servicelist, **kwargs):
76         global Session
77         Session = session
78         global Servicelist
79         Servicelist = servicelist
80         bouquets = Servicelist.getBouquetList()
81         if bouquets is None:
82                 cnt = 0
83         else:
84                 cnt = len(bouquets)
85         if cnt > 1: # show bouquet list
86                 global bouquetSel
87                 bouquetSel = Session.openWithCallback(closed, BouquetSelector, bouquets, openBouquetEPG, enableWrapAround=True)
88                 dlg_stack.append(bouquetSel)
89         elif cnt == 1:
90                 if not openBouquetEPG(bouquets[0][1]):
91                         cleanup()
92
93 def Plugins(**kwargs):
94         name = _("Graphical Multi EPG")
95         descr = _("A graphical EPG for all services of an specific bouquet")
96         return [ PluginDescriptor(name=name, description=descr, where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main) ]