show terrestrial frontend infos in ServiceInfo dialog
[enigma2.git] / lib / python / Screens / MediaPlayer.py
1 from enigma import eTimer, iPlayableService, eServiceCenter, iServiceInformation, eSize
2 from Screens.Screen import Screen
3 from Screens.MessageBox import MessageBox
4 from Components.ActionMap import NumberActionMap
5 from Components.Label import Label
6 from Components.Input import Input
7 from Components.GUIComponent import *
8 from Components.Pixmap import Pixmap
9 from Components.Label import Label
10 from Components.FileList import FileEntryComponent, FileList
11 from Components.MediaPlayer import PlayList, PlaylistEntryComponent
12 from Plugins.Plugin import PluginDescriptor
13 from Tools.Directories import resolveFilename, SCOPE_MEDIA
14 from Components.ServicePosition import ServicePositionGauge
15 from Screens.ChoiceBox import ChoiceBox
16 from Components.ServiceEventTracker import ServiceEventTracker
17 from Screens.InfoBarGenerics import InfoBarSeek
18
19 import os
20
21 class MediaPlayer(Screen, InfoBarSeek):
22         def __init__(self, session, args = None):
23                 Screen.__init__(self, session)
24                 self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
25                 self.session.nav.stopService()
26                 
27                 self.filelist = FileList(resolveFilename(SCOPE_MEDIA), matchingPattern = "^.*\.(mp3|ogg|ts|wav|wave)", useServiceRef = True)
28                 self["filelist"] = self.filelist
29
30                 self.playlist = PlayList()
31                 self["playlist"] = self.playlist
32                 
33                 self["PositionGauge"] = ServicePositionGauge(self.session.nav)
34                 
35                 self["currenttext"] = Label("")
36                 
37                 self["artisttext"] = Label(_("Artist:"))
38                 self["artist"] = Label("")
39                 self["titletext"] = Label(_("Title:"))
40                 self["title"] = Label("")
41                 self["albumtext"] = Label(_("Album:"))
42                 self["album"] = Label("")
43                 self["yeartext"] = Label(_("Year:"))
44                 self["year"] = Label("")
45                 self["genretext"] = Label(_("Genre:"))
46                 self["genre"] = Label("")
47                 
48                 #self["text"] = Input("1234", maxSize=True, type=Input.NUMBER)
49
50                 class MoviePlayerActionMap(NumberActionMap):
51                         def __init__(self, player, contexts = [ ], actions = { }, prio=0):
52                                 NumberActionMap.__init__(self, contexts, actions, prio)
53                                 self.player = player
54
55                         def action(self, contexts, action):
56                                 self.player.show()
57                                 return NumberActionMap.action(self, contexts, action)
58
59                 self["actions"] = MoviePlayerActionMap(self, ["OkCancelActions", "DirectionActions", "NumberActions", "MediaPlayerSeekActions"],
60                 {
61                         "ok": self.ok,
62                         "cancel": self.exit,
63                         
64                         "right": self.rightDown,
65                         "rightRepeated": self.doNothing,
66                         "rightUp": self.rightUp,
67                         "left": self.leftDown,
68                         "leftRepeated": self.doNothing,
69                         "leftUp": self.leftUp,
70                         
71                         "up": self.up,
72                         "upRepeated": self.up,
73                         "down": self.down,
74                         "downRepeated": self.down,
75                         
76                         "play": self.playEntry,
77                         "pause": self.pauseEntry,
78                         "stop": self.stopEntry,
79                         
80                         "previous": self.previousEntry,
81                         "next": self.nextEntry,
82                         
83                         "menu": self.showMenu,
84                         
85             "1": self.keyNumberGlobal,
86             "2": self.keyNumberGlobal,
87             "3": self.keyNumberGlobal,
88             "4": self.keyNumberGlobal,
89             "5": self.keyNumberGlobal,
90             "6": self.keyNumberGlobal,
91             "7": self.keyNumberGlobal,
92             "8": self.keyNumberGlobal,
93             "9": self.keyNumberGlobal,
94             "0": self.keyNumberGlobal
95         }, -2)
96
97                 InfoBarSeek.__init__(self)
98
99                 self.__event_tracker = ServiceEventTracker(screen=self, eventmap=
100                         {
101 #                               iPlayableService.evSeekableStatusChanged: self.__seekableStatusChanged,
102 #                               iPlayableService.evStart: self.__serviceStarted,
103                                 
104                                 iPlayableService.evEOF: self.__evEOF,
105 #                               iPlayableService.evSOF: self.__evSOF,
106                         })
107
108                 self.onClose.append(self.delMPTimer)
109                 self.onClose.append(self.__onClose)
110                 
111                 self.righttimer = False
112                 self.rightKeyTimer = eTimer()
113                 self.rightKeyTimer.timeout.get().append(self.rightTimerFire)
114
115                 self.lefttimer = False
116                 self.leftKeyTimer = eTimer()
117                 self.leftKeyTimer.timeout.get().append(self.leftTimerFire)
118                 
119                 self.infoTimer = eTimer()
120                 self.infoTimer.timeout.get().append(self.infoTimerFire)
121                 self.infoTimer.start(500)
122                 
123                 self.currList = "filelist"
124                 
125         def doNothing(self):
126                 pass
127         
128         def exit(self):
129                 self.close()
130         
131         def checkSkipShowHideLock(self):
132                 self.updatedSeekState()
133         
134         def __evEOF(self):
135                 self.nextEntry()
136                 
137         def __onClose(self):
138                 self.session.nav.playService(self.oldService)
139         
140         def delMPTimer(self):
141                 del self.rightKeyTimer
142                 del self.leftKeyTimer
143                 del self.infoTimer
144                 
145         def infoTimerFire(self):
146                 currPlay = self.session.nav.getCurrentService()
147                 if currPlay is not None:
148                         self.updateMusicInformation( artist = currPlay.info().getInfoString(iServiceInformation.sArtist),
149                                                                                  title = currPlay.info().getInfoString(iServiceInformation.sTitle),
150                                                                                  album = currPlay.info().getInfoString(iServiceInformation.sAlbum),
151                                                                                  genre = currPlay.info().getInfoString(iServiceInformation.sGenre),
152                                                                                  clear = True)
153                 else:
154                         self.updateMusicInformation()
155         
156         def updateMusicInformation(self, artist = "", title = "", album = "", year = "", genre = "", clear = False):
157                 self.updateSingleMusicInformation("artist", artist, clear)
158                 self.updateSingleMusicInformation("title", title, clear)
159                 self.updateSingleMusicInformation("album", album, clear)
160                 self.updateSingleMusicInformation("year", year, clear)
161                 self.updateSingleMusicInformation("genre", genre, clear)
162
163         def updateSingleMusicInformation(self, name, info, clear):
164                 if info != "" or clear:
165                         if self[name].getText() != info:
166                                 self[name].setText(info)
167
168         def fwdTimerFire(self):
169                 self.fwdKeyTimer.stop()
170                 self.fwdtimer = False
171                 self.nextEntry()
172         
173         def rwdTimerFire(self):
174                 self.rwdKeyTimer.stop()
175                 self.rwdtimer = False
176                 self.previousEntry()
177         
178         def leftDown(self):
179                 self.lefttimer = True
180                 self.leftKeyTimer.start(1000)
181
182         def rightDown(self):
183                 self.righttimer = True
184                 self.rightKeyTimer.start(1000)
185                 
186         def leftUp(self):
187                 if self.lefttimer:
188                         self.leftKeyTimer.stop()
189                         self.lefttimer = False
190                         self[self.currList].pageUp()
191     
192         def rightUp(self):
193                 if self.righttimer:
194                         self.rightKeyTimer.stop()
195                         self.righttimer = False
196                         self[self.currList].pageDown()
197                         
198         def leftTimerFire(self):
199                 self.leftKeyTimer.stop()
200                 self.lefttimer = False
201                 self.switchToFileList()
202         
203         def rightTimerFire(self):
204                 self.rightKeyTimer.stop()
205                 self.righttimer = False
206                 self.switchToPlayList()
207                                 
208         def switchToFileList(self):
209                 self.currList = "filelist"
210                 self.filelist.selectionEnabled(1)
211                 self.playlist.selectionEnabled(0)
212                 self.updateCurrentInfo()
213                 
214         def switchToPlayList(self):
215                 if len(self.playlist) != 0:
216                         self.currList = "playlist"
217                         self.filelist.selectionEnabled(0)
218                         self.playlist.selectionEnabled(1)
219                         self.updateCurrentInfo()
220                 
221         def up(self):
222                 self[self.currList].up()
223                 self.updateCurrentInfo()
224
225         def down(self):
226                 self[self.currList].down()
227                 self.updateCurrentInfo()
228
229         def updateCurrentInfo(self):
230                 text = ""
231                 if self.currList == "filelist":
232                         if not self.filelist.canDescent():
233                                 text = self.filelist.getServiceRef().getPath()
234                 if self.currList == "playlist":
235                         text = self.playlist.getSelection().getPath()
236                 
237                 self["currenttext"].setText(os.path.basename(text))
238     
239         def ok(self):
240                 if self.currList == "filelist":
241                         if self.filelist.canDescent():
242                                 self.filelist.descent()
243                                 self.updateCurrentInfo()
244                         else:
245                                 self.copyFile()
246                 if self.currList == "playlist":
247                         selection = self["playlist"].getSelection()
248                         self.changeEntry(self.playlist.getSelectionIndex())
249
250         def keyNumberGlobal(self, number):
251                 pass
252         
253         def showMenu(self):
254                 menu = []
255                 if self.currList == "filelist":
256                         menu.append((_("switch to playlist"), "playlist"))
257                         if self.filelist.canDescent():
258                                 menu.append((_("add directory to playlist"), "copydir"))
259                         else:
260                                 menu.append((_("add file to playlist"), "copy"))
261                 else:
262                         menu.append((_("switch to filelist"), "filelist"))
263                         menu.append((_("delete"), "delete"))
264                         menu.append((_("clear playlist"), "clear"))
265                 menu.append((_("hide player"), "hide"));
266                 self.session.openWithCallback(self.menuCallback, ChoiceBox, title="", list=menu)
267                 
268         def menuCallback(self, choice):
269                 if choice is None:
270                         return
271                 
272                 if choice[1] == "copydir":
273                         self.copyDirectory(self.filelist.getSelection()[0])
274                 elif choice[1] == "copy":
275                         self.copyFile()
276                 elif choice[1] == "playlist":
277                         self.switchToPlayList()
278                 elif choice[1] == "filelist":
279                         self.switchToFileList()
280                 elif choice[1] == "delete":
281                         self.deleteEntry()
282                 elif choice[1] == "clear":
283                         self.stopEntry()
284                         self.playlist.clear()
285                         self.switchToFileList()
286                 elif choice[1] == "hide":
287                         self.hide()
288
289         def copyDirectory(self, directory):
290                 filelist = FileList(directory, useServiceRef = True, isTop = True)
291                 
292                 for x in filelist.getFileList():
293                         if x[0][1] == True: #isDir
294                                 self.copyDirectory(x[0][0])
295                         else:
296                                 self.playlist.addFile(x[0][0])
297                 self.playlist.updateList()
298         
299         def copyFile(self):
300                 self.playlist.addFile(self.filelist.getServiceRef())
301                 self.playlist.updateList()
302                 if len(self.playlist) == 1:
303                         self.changeEntry(0)
304
305         def nextEntry(self):
306                 next = self.playlist.getCurrentIndex() + 1
307                 if next < len(self.playlist):
308                         self.changeEntry(next)
309         
310         def previousEntry(self):
311                 next = self.playlist.getCurrentIndex() - 1
312                 if next >= 0:
313                         self.changeEntry(next)
314                         
315         def deleteEntry(self):
316                 self.playlist.deleteFile(self.playlist.getSelectionIndex())
317                 self.playlist.updateList()
318                 if len(self.playlist) == 0:
319                         self.switchToFileList()
320                 
321         def changeEntry(self, index):
322                 self.playlist.setCurrentPlaying(index)
323                 self.playEntry()
324         
325         def playEntry(self):
326                 if len(self.playlist.getServiceRefList()):
327                         currref = self.playlist.getServiceRefList()[self.playlist.getCurrentIndex()]
328                         if self.session.nav.getCurrentlyPlayingServiceReference() is None or currref != self.session.nav.getCurrentlyPlayingServiceReference():
329                                 self.session.nav.playService(self.playlist.getServiceRefList()[self.playlist.getCurrentIndex()])
330                                 info = eServiceCenter.getInstance().info(currref)
331                                 description = info and info.getInfoString(currref, iServiceInformation.sDescription) or ""
332                                 self["title"].setText(description)
333                         self.unPauseService()
334                                 
335         def updatedSeekState(self):
336                 if self.seekstate == self.SEEK_STATE_PAUSE:
337                         self.playlist.pauseFile()
338                 elif self.seekstate == self.SEEK_STATE_PLAY:
339                         self.playlist.playFile()
340                 elif self.seekstate in ( self.SEEK_STATE_FF_2X,
341                                                                  self.SEEK_STATE_FF_4X,
342                                                                  self.SEEK_STATE_FF_8X,
343                                                                  self.SEEK_STATE_FF_32X,
344                                                                  self.SEEK_STATE_FF_64X,
345                                                                  self.SEEK_STATE_FF_128X):
346                         self.playlist.forwardFile()
347                 elif self.seekstate in ( self.SEEK_STATE_BACK_16X,
348                                                                  self.SEEK_STATE_BACK_32X,
349                                                                  self.SEEK_STATE_BACK_64X,
350                                                                  self.SEEK_STATE_BACK_128X,):
351                         self.playlist.rewindFile()
352         
353         def pauseEntry(self):
354                 self.pauseService()
355                 
356         def stopEntry(self):
357                 self.playlist.stopFile()
358                 self.session.nav.playService(None)
359
360