add support for opening m3u, pls, extended m3u and extended pls playlists
authorStefan Pluecken <stefan.pluecken@multimedia-labs.de>
Sun, 18 Jun 2006 22:24:10 +0000 (22:24 +0000)
committerStefan Pluecken <stefan.pluecken@multimedia-labs.de>
Sun, 18 Jun 2006 22:24:10 +0000 (22:24 +0000)
lib/python/Components/FileList.py
lib/python/Components/Playlist.py
lib/python/Screens/MediaPlayer.py

index ce1f236..0d8245c 100644 (file)
@@ -95,7 +95,7 @@ class FileList(MenuList, HTMLComponent, GUIComponent):
                        tmpfiles = files[:]
                        for x in tmpfiles:
                                if os.path.isdir(directory + x):
                        tmpfiles = files[:]
                        for x in tmpfiles:
                                if os.path.isdir(directory + x):
-                                       directories.append(x)
+                                       directories.append(directory + x + "/")
                                        files.remove(x)
                
                if directory != "/" and self.showDirectories and not self.isTop:
                                        files.remove(x)
                
                if directory != "/" and self.showDirectories and not self.isTop:
index 704b2f8..2279969 100644 (file)
@@ -1,4 +1,5 @@
 from ServiceReference import ServiceReference
 from ServiceReference import ServiceReference
+import os
 
 class PlaylistIO:      
        def __init__(self):
 
 class PlaylistIO:      
        def __init__(self):
@@ -49,4 +50,63 @@ class PlaylistIOInternal(PlaylistIO):
                        file.write(str(x) + "\n")
                file.close()
                
                        file.write(str(x) + "\n")
                file.close()
                
-               return self.OK
\ No newline at end of file
+               return self.OK
+       
+class PlaylistIOM3U(PlaylistIO):
+       def __init__(self):
+               PlaylistIO.__init__(self)
+       
+       def open(self, filename):
+               self.clear()
+               try:
+                       file = open(filename, "r")
+               except IOError:
+                       return None
+               while True:
+                       entry = file.readline().strip()
+                       if entry == "":
+                               break
+                       if entry[0] != "#":
+                               # TODO: use e2 facilities to create a service ref from file
+                               if entry[0] == "/":
+                                       self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + entry))
+                               else:
+                                       self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + os.path.dirname(filename) + "/" + entry))
+               file.close()
+               return self.list
+               
+       def save(self, filename = None):
+               return self.ERROR
+       
+class PlaylistIOPLS(PlaylistIO):
+       def __init__(self):
+               PlaylistIO.__init__(self)
+       
+       def open(self, filename):
+               self.clear()
+               try:
+                       file = open(filename, "r")
+               except IOError:
+                       return None
+               entry = file.readline().strip()
+               if entry == "[playlist]": # extended pls
+                       while True:
+                               entry = file.readline().strip()
+                               if entry == "":
+                                       break
+                               if entry[0:4] == "File":
+                                       pos = entry.find('=') + 1
+                                       newentry = entry[pos:]
+                                       # TODO: use e2 facilities to create a service ref from file
+                                       if newentry[0] == "/":
+                                               self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + newentry))
+                                       else:
+                                               self.addService(ServiceReference("4097:0:0:0:0:0:0:0:0:0:" + os.path.dirname(filename) + "/" + newentry))
+               else:
+                       playlist = PlaylistIOM3U()
+                       return playlist.open(filename)
+               file.close()
+               return self.list
+               
+       def save(self, filename = None):
+               return self.ERROR
\ No newline at end of file
index e064bb9..f042a5c 100644 (file)
@@ -14,9 +14,10 @@ from Tools.Directories import resolveFilename, SCOPE_MEDIA, SCOPE_CONFIG
 from Components.ServicePosition import ServicePositionGauge
 from Screens.ChoiceBox import ChoiceBox
 from Components.ServiceEventTracker import ServiceEventTracker
 from Components.ServicePosition import ServicePositionGauge
 from Screens.ChoiceBox import ChoiceBox
 from Components.ServiceEventTracker import ServiceEventTracker
-from Components.Playlist import PlaylistIOInternal
+from Components.Playlist import PlaylistIOInternal, PlaylistIOM3U, PlaylistIOPLS
 from Screens.InfoBarGenerics import InfoBarSeek
 from ServiceReference import ServiceReference
 from Screens.InfoBarGenerics import InfoBarSeek
 from ServiceReference import ServiceReference
+from Screens.ChoiceBox import ChoiceBox
 
 import os
 
 
 import os
 
@@ -25,8 +26,8 @@ class MediaPlayer(Screen, InfoBarSeek):
                Screen.__init__(self, session)
                self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
                self.session.nav.stopService()
                Screen.__init__(self, session)
                self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
                self.session.nav.stopService()
-               
-               self.filelist = FileList(resolveFilename(SCOPE_MEDIA), matchingPattern = "^.*\.(mp3|ogg|ts|wav|wave)", useServiceRef = True)
+
+               self.filelist = FileList(resolveFilename(SCOPE_MEDIA), matchingPattern = "^.*\.(mp3|ogg|ts|wav|wave|m3u|pls)", useServiceRef = True)
                self["filelist"] = self.filelist
 
                self.playlist = PlayList()
                self["filelist"] = self.filelist
 
                self.playlist = PlayList()
@@ -311,11 +312,37 @@ class MediaPlayer(Screen, InfoBarSeek):
                                self.playlist.addFile(x[0][0])
                self.playlist.updateList()
        
                                self.playlist.addFile(x[0][0])
                self.playlist.updateList()
        
+       ADDPLAYLIST = 0
+       REPLACEPLAYLIST = 1
+       
        def copyFile(self):
        def copyFile(self):
-               self.playlist.addFile(self.filelist.getServiceRef())
-               self.playlist.updateList()
-               if len(self.playlist) == 1:
-                       self.changeEntry(0)
+               if self.filelist.getServiceRef().type == 4098: # playlist
+                       list = []
+                       list.append((_("Add files to playlist"), (self.ADDPLAYLIST, self.filelist.getServiceRef())))
+                       list.append((_("Replace current playlist"), (self.REPLACEPLAYLIST, self.filelist.getServiceRef())))
+                       self.session.openWithCallback(self.playlistCallback, ChoiceBox, title=_("You selected a playlist"), list = list)
+               else:
+                       self.playlist.addFile(self.filelist.getServiceRef())
+                       self.playlist.updateList()
+                       if len(self.playlist) == 1:
+                               self.changeEntry(0)
+
+       def playlistCallback(self, answer):
+               if answer is not None:
+                       extension = answer[1][1].getPath()[-3:]
+                       if extension == "m3u":
+                               playlist = PlaylistIOM3U()
+                       elif extension == "pls":
+                               playlist = PlaylistIOPLS()
+                       if answer[1][0] == self.REPLACEPLAYLIST:
+                               self.stopEntry()
+                               self.playlist.clear()
+                               self.switchToFileList()
+                       if answer[1][0] == self.REPLACEPLAYLIST or answer[1][0] == self.ADDPLAYLIST:
+                               list = playlist.open(answer[1][1].getPath())
+                               for x in list:
+                                       self.playlist.addFile(x.ref)
+                               
 
        def nextEntry(self):
                next = self.playlist.getCurrentIndex() + 1
 
        def nextEntry(self):
                next = self.playlist.getCurrentIndex() + 1