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