(ralfk) add getCurrentEvent to Filelist
[enigma2.git] / lib / python / Components / MediaPlayer.py
1 from HTMLComponent import *
2 from GUIComponent import *
3
4 from MenuList import MenuList
5
6 from Tools.Directories import SCOPE_SKIN_IMAGE, resolveFilename
7 from os import path
8
9 from enigma import eListboxPythonMultiContent, eListbox, RT_VALIGN_CENTER, loadPNG, gFont
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 = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_play.png"))
19 PauseIcon = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_pause.png"))
20 StopIcon = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_stop.png"))
21 RewindIcon = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_rewind.png"))
22 ForwardIcon = loadPNG(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_forward.png"))
23
24 def PlaylistEntryComponent(serviceref, state):
25         res = [ serviceref ]
26         res.append((eListboxPythonMultiContent.TYPE_TEXT,25, 0, 470, 32, 0, RT_VALIGN_CENTER, path.split(serviceref.getPath().split('/')[-1])[1]))
27         png = None
28         if state == STATE_PLAY:
29                 png = PlayIcon
30         elif state == STATE_PAUSE:
31                 png = PauseIcon
32         elif state == STATE_STOP:
33                 png = StopIcon
34         elif state == STATE_REWIND:
35                 png = RewindIcon
36         elif state == STATE_FORWARD:
37                 png = ForwardIcon
38
39         if png is not None:
40                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 5, 0, 16, 16, png))
41     
42         return res
43
44 class PlayList(MenuList, HTMLComponent, GUIComponent):
45         def __init__(self):
46                 GUIComponent.__init__(self)
47                 self.l = eListboxPythonMultiContent()
48                 self.list = []
49                 self.l.setList(self.list)
50                 self.l.setFont(0, gFont("Regular", 18))
51                 self.l.setItemHeight(22)
52                 self.currPlaying = -1
53                 self.oldCurrPlaying = -1
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         GUI_WIDGET = eListbox
62
63         def postWidgetCreate(self, instance):
64                 instance.setContent(self.l)
65
66         def getSelection(self):
67                 return self.l.getCurrentSelection()[0]
68                 
69         def getSelectionIndex(self):
70                 return self.l.getCurrentSelectionIndex()
71
72         def addFile(self, serviceref):
73                 self.list.append(PlaylistEntryComponent(serviceref, 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         
84         def updateState(self, state):
85                 if len(self.list) > self.oldCurrPlaying and self.oldCurrPlaying != -1:
86                         self.list[self.oldCurrPlaying] = PlaylistEntryComponent(self.list[self.oldCurrPlaying][0], STATE_NONE)
87                 if self.currPlaying != -1 and self.currPlaying < len(self.list):
88                         self.list[self.currPlaying] = PlaylistEntryComponent(self.list[self.currPlaying][0], state)
89                 self.updateList()
90
91         def playFile(self):
92                 self.updateState(STATE_PLAY)
93
94         def pauseFile(self):
95                 self.updateState(STATE_PAUSE)
96                 
97         def stopFile(self):
98                 self.updateState(STATE_STOP)
99
100         def rewindFile(self):
101                 self.updateState(STATE_REWIND)
102                 
103         def forwardFile(self):
104                 self.updateState(STATE_FORWARD)
105         
106         def updateList(self):
107                 self.l.setList(self.list)
108                 
109         def getCurrentIndex(self):
110                 return self.currPlaying
111         
112         def getServiceRefList(self):
113                 return [ x[0] for x in self.list ]
114         
115         def __len__(self):
116                 return len(self.list)