epgrefresh: make notification a popup
[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 _, NOTIFICATIONID
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, NOTIFICATIONID)
33                 if 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                         del self.session.pip
39                 else:
40                         self.wasShown = False
41                 self.initPiP()
42                 return True
43
44         def hidePiP(self):
45                 # set pip size to 1 pixel
46                 print("[EPGRefresh.PipAdapter.hidePiP]")
47                 x = y = 0
48                 w = h = 1
49                 self.session.pip.instance.move(ePoint(x, y))
50                 self.session.pip.instance.resize(eSize(w, y))
51                 self.session.pip["video"].instance.resize(eSize(w, y))
52
53         def initPiP(self):
54                 # Instantiate PiP
55                 self.session.pip = self.session.instantiateDialog(PictureInPicture)
56                 self.session.pip.show()
57                 if self.hide: self.hidePiP()
58                 self.session.pipshown = True # Always pretends it's shown (since the ressources are present)
59                 newservice = self.session.nav.getCurrentlyPlayingServiceReference()
60                 if self.session.pip.playService(newservice):
61                         self.session.pip.servicePath = newservice.getPath()
62
63         def play(self, service):
64                 print("[EPGRefresh.PipAdapter.play]")
65                 if not self.pipAvail: return False
66
67                 if not self.session.pipshown: # make sure pip still exists
68                         self.initPiP()
69
70                 if self.session.pip.playService(service):
71                         self.session.pip.servicePath = service.getPath()
72                         return True
73                 return False
74
75         def stop(self):
76                 if not self.pipAvail: return
77
78                 if config.plugins.epgrefresh.enablemessage.value:
79                         Notifications.AddPopup(_("EPG refresh finished.") + "\n" + _("PiP available now."), MessageBox.TYPE_INFO, 4, NOTIFICATIONID)
80
81                 # remove pip preemptively
82                 try: del self.session.pip
83                 except Exception: pass
84
85                 # reset pip and remove it if unable to play service
86                 if self.wasShown:
87                         self.session.pip = self.session.instantiateDialog(PictureInPicture)
88                         self.session.pip.show()
89                         self.session.pipshown = True
90                         if self.session.pip.playService(self.previousService):
91                                 self.session.pip.servicePath = self.previousPath
92                         else:
93                                 self.session.pipshown = False
94                                 del self.session.pip
95                 else:
96                         self.session.pipshown = False
97