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