Merge commit 'origin/translations' into experimental
[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 PlayIcon = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_play.png"))
18 PauseIcon = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_pause.png"))
19 StopIcon = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_stop.png"))
20 RewindIcon = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "skin_default/icons/ico_mp_rewind.png"))
21 ForwardIcon = LoadPixmap(path=resolveFilename(SCOPE_CURRENT_SKIN, "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, 1, 470, 22, 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, 3, 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(23)
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                 self.moveToIndex(index)
80
81         def updateState(self, state):
82                 if len(self.list) > self.oldCurrPlaying and self.oldCurrPlaying != -1:
83                         self.list[self.oldCurrPlaying] = PlaylistEntryComponent(self.list[self.oldCurrPlaying][0], STATE_NONE)
84                 if self.currPlaying != -1 and self.currPlaying < len(self.list):
85                         self.list[self.currPlaying] = PlaylistEntryComponent(self.list[self.currPlaying][0], state)
86                 self.updateList()
87
88         def playFile(self):
89                 self.updateState(STATE_PLAY)
90
91         def pauseFile(self):
92                 self.updateState(STATE_PAUSE)
93                 
94         def stopFile(self):
95                 self.updateState(STATE_STOP)
96
97         def rewindFile(self):
98                 self.updateState(STATE_REWIND)
99
100         def forwardFile(self):
101                 self.updateState(STATE_FORWARD)
102
103         def updateList(self):
104                 self.l.setList(self.list)
105
106         def getCurrentIndex(self):
107                 return self.currPlaying
108
109         def getCurrentEvent(self):
110                 l = self.l.getCurrentSelection()
111                 return l and self.serviceHandler.info(l[0]).getEvent(l[0])
112
113         def getCurrent(self):
114                 l = self.l.getCurrentSelection()
115                 return l and l[0]
116
117         def getServiceRefList(self):
118                 return [ x[0] for x in self.list ]
119
120         def __len__(self):
121                 return len(self.list)