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