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