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