implement proper 'destroy' functions in Converter
[enigma2.git] / lib / python / Components / MediaPlayer.py
1 from HTMLComponent import HTMLComponent
2 from GUIComponent import GUIComponent
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, gFont, eServiceCenter
10
11 from Tools.LoadPixmap import LoadPixmap
12
13
14 STATE_PLAY = 0
15 STATE_PAUSE = 1
16 STATE_STOP = 2
17 STATE_REWIND = 3
18 STATE_FORWARD = 4
19 STATE_NONE = 5
20
21 PlayIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_play.png"))
22 PauseIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_pause.png"))
23 StopIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_stop.png"))
24 RewindIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_rewind.png"))
25 ForwardIcon = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "ico_mp_forward.png"))
26
27 def PlaylistEntryComponent(serviceref, state):
28         res = [ serviceref ]
29         res.append((eListboxPythonMultiContent.TYPE_TEXT,25, 0, 470, 32, 0, RT_VALIGN_CENTER, path.split(serviceref.getPath().split('/')[-1])[1]))
30         png = None
31         if state == STATE_PLAY:
32                 png = PlayIcon
33         elif state == STATE_PAUSE:
34                 png = PauseIcon
35         elif state == STATE_STOP:
36                 png = StopIcon
37         elif state == STATE_REWIND:
38                 png = RewindIcon
39         elif state == STATE_FORWARD:
40                 png = ForwardIcon
41
42         if png is not None:
43                 res.append((eListboxPythonMultiContent.TYPE_PIXMAP_ALPHATEST, 5, 0, 16, 16, png))
44     
45         return res
46
47 class PlayList(MenuList, HTMLComponent, GUIComponent):
48         def __init__(self):
49                 GUIComponent.__init__(self)
50                 self.l = eListboxPythonMultiContent()
51                 self.list = []
52                 self.l.setList(self.list)
53                 self.l.setFont(0, gFont("Regular", 18))
54                 self.l.setItemHeight(22)
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         GUI_WIDGET = eListbox
66
67         def postWidgetCreate(self, instance):
68                 instance.setContent(self.l)
69
70         def getSelection(self):
71                 return self.l.getCurrentSelection()[0]
72                 
73         def getSelectionIndex(self):
74                 return self.l.getCurrentSelectionIndex()
75
76         def addFile(self, serviceref):
77                 self.list.append(PlaylistEntryComponent(serviceref, STATE_NONE))
78
79         def deleteFile(self, index):
80                 if self.currPlaying >= index:
81                         self.currPlaying -= 1
82                 del self.list[index]
83
84         def setCurrentPlaying(self, index):
85                 self.oldCurrPlaying = self.currPlaying
86                 self.currPlaying = index
87         
88         def updateState(self, state):
89                 if len(self.list) > self.oldCurrPlaying and self.oldCurrPlaying != -1:
90                         self.list[self.oldCurrPlaying] = PlaylistEntryComponent(self.list[self.oldCurrPlaying][0], STATE_NONE)
91                 if self.currPlaying != -1 and self.currPlaying < len(self.list):
92                         self.list[self.currPlaying] = PlaylistEntryComponent(self.list[self.currPlaying][0], state)
93                 self.updateList()
94
95         def playFile(self):
96                 self.updateState(STATE_PLAY)
97
98         def pauseFile(self):
99                 self.updateState(STATE_PAUSE)
100                 
101         def stopFile(self):
102                 self.updateState(STATE_STOP)
103
104         def rewindFile(self):
105                 self.updateState(STATE_REWIND)
106                 
107         def forwardFile(self):
108                 self.updateState(STATE_FORWARD)
109         
110         def updateList(self):
111                 self.l.setList(self.list)
112                 
113         def getCurrentIndex(self):
114                 return self.currPlaying
115
116         def getCurrentEvent(self):
117                 l = self.l.getCurrentSelection()
118                 return l and self.serviceHandler.info(l[0]).getEvent(l[0])
119
120         def getCurrent(self):
121                 l = self.l.getCurrentSelection()
122                 return l and l[0]
123         
124         def getServiceRefList(self):
125                 return [ x[0] for x in self.list ]
126         
127         def __len__(self):
128                 return len(self.list)