Merge branch 'master' of /home/tmbinc/enigma2-git into tmbinc/FixTimingBugs
[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                         global epg_bouquet
73                         epg_bouquet = bouquet
74                         epg.setServices(services)
75
76 def main(session, servicelist, **kwargs):
77         global Session
78         Session = session
79         global Servicelist
80         Servicelist = servicelist
81         bouquets = Servicelist.getBouquetList()
82         if bouquets is None:
83                 cnt = 0
84         else:
85                 cnt = len(bouquets)
86         if cnt > 1: # show bouquet list
87                 global bouquetSel
88                 bouquetSel = Session.openWithCallback(closed, BouquetSelector, bouquets, openBouquetEPG, enableWrapAround=True)
89                 dlg_stack.append(bouquetSel)
90         elif cnt == 1:
91                 if not openBouquetEPG(bouquets[0][1]):
92                         cleanup()
93
94 def Plugins(**kwargs):
95         name = _("Graphical Multi EPG")
96         descr = _("A graphical EPG for all services of an specific bouquet")
97         return [ PluginDescriptor(name=name, description=descr, where = PluginDescriptor.WHERE_EVENTINFO, fnc=main),
98           PluginDescriptor(name=name, description=descr, where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc=main) ]