epgrefresh: BackgroundAdapter -> PipAdapter
[enigma2-plugins.git] / epgrefresh / src / PipAdapter.py
1 from Screens.PictureInPicture import PictureInPicture
2 from Components.SystemInfo import SystemInfo
3 from enigma import ePoint, eSize
4
5 # MessageBox
6 from Screens.MessageBox import MessageBox
7 from Tools import Notifications
8
9 # Config
10 from Components.config import config
11
12 class PipAdapter:
13         def __init__(self, session):
14                 if SystemInfo.get("NumVideoDecoders", 1) < 2:
15                         self.pipAvail = False
16                         return
17
18                 self.session = session
19                 self.pipAvail = True
20                 if config.plugins.epgrefresh.enablemessage.value:
21                         Notifications.AddNotification(MessageBox, _("EPG refresh started in background.\nPlease don't use PiP meanwhile!"), type=MessageBox.TYPE_INFO, timeout=4)
22                 if session.pipshown:
23                         # Hijack PiP
24                         self.wasShown = True
25                         self.previousService = self.session.pip.getCurrentService()
26                         self.previousPath = self.session.pip.servicePath
27                         del self.session.pip
28                 else:
29                         self.wasShown = False
30                 self.initPiP()
31
32         def hidePiP(self):
33                 # set pip size to 1 pixel
34                 print "[EPGRefresh.PipAdapter.hidePiP]"
35                 x = y = 0
36                 w = h = 1
37                 self.session.pip.instance.move(ePoint(x, y))
38                 self.session.pip.instance.resize(eSize(w, y))
39                 self.session.pip["video"].instance.resize(eSize(w, y))
40
41         def initPiP(self):
42                 # Instantiate PiP
43                 self.session.pip = self.session.instantiateDialog(PictureInPicture)
44                 self.session.pip.show()
45                 self.hidePiP()
46                 self.session.pipshown = True # Always pretends it's shown (since the ressources are present)
47                 newservice = self.session.nav.getCurrentlyPlayingServiceReference()
48                 if self.session.pip.playService(newservice):
49                         self.session.pip.servicePath = newservice.getPath()
50
51         def play(self, service):
52                 print "[EPGRefresh.PipAdapter.play]"
53                 if not self.pipAvail: return False
54
55                 if not self.session.pipshown: # make sure pip still exists
56                         self.initPiP()
57
58                 if self.session.pip.playService(service):
59                         self.session.pip.servicePath = service.getPath()
60                         return True
61                 return False
62
63         def stop(self):
64                 if not self.pipAvail: return
65
66                 if config.plugins.epgrefresh.enablemessage.value:
67                         Notifications.AddNotification(MessageBox, _("EPG refresh finished. PiP available now."), type=MessageBox.TYPE_INFO, timeout=4)
68
69                 # remove pip preemptively
70                 try: del self.session.pip
71                 except Exception: pass
72
73                 # reset pip and remove it if unable to play service
74                 if self.wasShown:
75                         self.session.pip = self.session.instantiateDialog(PictureInPicture)
76                         self.session.pip.show()
77                         self.session.pipshown = True
78                         if self.session.pip.playService(self.previousService):
79                                 self.session.pip.servicePath = self.previousPath
80                         else:
81                                 self.session.pipshown = False
82                                 del self.session.pip
83                 else:
84                         self.session.pipshown = False
85