Support duo4k.
[vuplus_dvbapp] / lib / python / Components / MediaPlayer.py
1 from MenuList import MenuList
2
3 from Tools.Directories import SCOPE_CURRENT_SKIN, resolveFilename
4 from os import path
5
6 from enigma import eListboxPythonMultiContent, RT_VALIGN_CENTER, gFont, eServiceCenter
7
8 from Tools.LoadPixmap import LoadPixmap
9 import skin
10
11 STATE_PLAY = 0
12 STATE_PAUSE = 1
13 STATE_STOP = 2
14 STATE_REWIND = 3
15 STATE_FORWARD = 4
16 STATE_NONE = 5
17
18 PlayIcon = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_play.png"))
19 PauseIcon = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_pause.png"))
20 StopIcon = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_stop.png"))
21 RewindIcon = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_rewind.png"))
22 ForwardIcon = LoadPixmap(path=resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_forward.png"))
23
24 def PlaylistEntryComponent(serviceref, state):
25         res = [ serviceref ]
26         text = serviceref.getName()
27         if text is "":
28                 text = path.split(serviceref.getPath().split('/')[-1])[1]
29         x, y, w, h = skin.parameters.get("PlayListName",(25, 1, 470, 22))
30         res.append((eListboxPythonMultiContent.TYPE_TEXT,x, y, w, h, 0, RT_VALIGN_CENTER, text))
31         png = None
32         if state == STATE_PLAY:
33                 png = PlayIcon
34         elif state == STATE_PAUSE:
35                 png = PauseIcon
36         elif state == STATE_STOP:
37                 png = StopIcon
38         elif state == STATE_REWIND:
39                 png = RewindIcon
40         elif state == STATE_FORWARD:
41                 png = ForwardIcon
42
43         if png is not None:
44                 x, y, w, h = skin.parameters.get("PlayListIcon",(5, 3, 16, 16))
45                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, x, y, w, h, png))
46
47         return res
48
49 class PlayList(MenuList):
50         def __init__(self, enableWrapAround = False):
51                 MenuList.__init__(self, [], enableWrapAround, eListboxPythonMultiContent)
52                 font = skin.fonts.get("PlayList", ("Regular", 18, 23))
53                 self.l.setFont(0, gFont(font[0], font[1]))
54                 self.l.setItemHeight(font[2])
55                 self.currPlaying = -1
56                 self.oldCurrPlaying = -1
57                 self.serviceHandler = eServiceCenter.getInstance()
58
59         def clear(self):
60                 del self.list[:]
61                 self.l.setList(self.list)
62                 self.currPlaying = -1
63                 self.oldCurrPlaying = -1
64
65         def getSelection(self):
66                 return self.l.getCurrentSelection()[0]
67
68         def addFile(self, serviceref):
69                 self.list.append(PlaylistEntryComponent(serviceref, STATE_NONE))
70
71         def updateFile(self, index, newserviceref):
72                 if index < len(self.list):
73                     self.list[index] = PlaylistEntryComponent(newserviceref, STATE_NONE)
74
75         def deleteFile(self, index):
76                 if self.currPlaying >= index:
77                         self.currPlaying -= 1
78                 del self.list[index]
79
80         def setCurrentPlaying(self, index):
81                 self.oldCurrPlaying = self.currPlaying
82                 self.currPlaying = index
83                 self.moveToIndex(index)
84
85         def updateState(self, state):
86                 if len(self.list) > self.oldCurrPlaying and self.oldCurrPlaying != -1:
87                         self.list[self.oldCurrPlaying] = PlaylistEntryComponent(self.list[self.oldCurrPlaying][0], STATE_NONE)
88                 if self.currPlaying != -1 and self.currPlaying < len(self.list):
89                         self.list[self.currPlaying] = PlaylistEntryComponent(self.list[self.currPlaying][0], state)
90                 self.updateList()
91
92         def playFile(self):
93                 self.updateState(STATE_PLAY)
94
95         def pauseFile(self):
96                 self.updateState(STATE_PAUSE)
97                 
98         def stopFile(self):
99                 self.updateState(STATE_STOP)
100
101         def rewindFile(self):
102                 self.updateState(STATE_REWIND)
103
104         def forwardFile(self):
105                 self.updateState(STATE_FORWARD)
106
107         def updateList(self):
108                 self.l.setList(self.list)
109
110         def getCurrentIndex(self):
111                 return self.currPlaying
112
113         def getCurrentEvent(self):
114                 l = self.l.getCurrentSelection()
115                 return l and self.serviceHandler.info(l[0]).getEvent(l[0])
116
117         def getCurrent(self):
118                 l = self.l.getCurrentSelection()
119                 return l and l[0]
120
121         def getServiceRefList(self):
122                 return [ x[0] for x in self.list ]
123
124         def __len__(self):
125                 return len(self.list)