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