From 0f856497bd2dd397024df14997f1bed4a514de04 Mon Sep 17 00:00:00 2001 From: "Jakob L. Kreuze" Date: Sun, 31 Mar 2019 16:51:14 -0400 Subject: [PATCH] mediaplayer: Implement AppendM3uToPlaylist Change-Id: I60a5146cae85ab7abb92a28ad142e5510c0a940e Reviewed-on: https://review.haiku-os.org/c/1337 Reviewed-by: waddlesplash --- src/apps/mediaplayer/playlist/Playlist.cpp | 45 ++++++++++++++++++++-- src/apps/mediaplayer/playlist/Playlist.h | 3 ++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/apps/mediaplayer/playlist/Playlist.cpp b/src/apps/mediaplayer/playlist/Playlist.cpp index 384ff181e3..998c3880ae 100644 --- a/src/apps/mediaplayer/playlist/Playlist.cpp +++ b/src/apps/mediaplayer/playlist/Playlist.cpp @@ -460,6 +460,8 @@ Playlist::AppendItems(const BMessage* refsReceivedMessage, int32 appendIndex) } else { if (_IsQuery(type)) AppendQueryToPlaylist(ref, &subPlaylist); + else if (_IsM3u(ref)) + AppendM3uToPlaylist(ref, &subPlaylist); else { if (!_ExtraMediaExists(this, ref)) { AppendToPlaylistRecursive(ref, &subPlaylist); @@ -573,6 +575,34 @@ Playlist::AppendPlaylistToPlaylist(const entry_ref& ref, Playlist* playlist) } +/*static*/ void +Playlist::AppendM3uToPlaylist(const entry_ref& ref, Playlist* playlist) +{ + BFile file(&ref, B_READ_ONLY); + FileReadWrite lineReader(&file); + + BString line; + while (lineReader.Next(line)) { + if (line.FindFirst("#") != 0) { + BPath path(line.String()); + entry_ref refPath; + status_t err; + + if ((err = get_ref_for_path(path.Path(), &refPath)) == B_OK) { + PlaylistItem* item + = new (std::nothrow) FilePlaylistItem(refPath); + if (item == NULL || !playlist->AddItem(item)) + delete item; + } else { + printf("Error - %s: [%" B_PRIx32 "]\n", strerror(err), err); + } + } + + line.Truncate(0); + } +} + + /*static*/ void Playlist::AppendQueryToPlaylist(const entry_ref& ref, Playlist* playlist) { @@ -620,7 +650,7 @@ Playlist::ExtraMediaExists(Playlist* playlist, PlaylistItem* item) Playlist::_ExtraMediaExists(Playlist* playlist, const entry_ref& ref) { BString exceptExtension = _GetExceptExtension(BPath(&ref).Path()); - + for (int32 i = 0; i < playlist->CountItems(); i++) { FilePlaylistItem* compare = dynamic_cast(playlist->ItemAt(i)); if (compare == NULL) @@ -720,6 +750,15 @@ Playlist::_IsPlaylist(const BString& mimeString) } +/*static*/ bool +Playlist::_IsM3u(const entry_ref& ref) +{ + BString path(BPath(&ref).Path()); + return path.FindLast(".m3u") == path.CountChars() - 4 + || path.FindLast(".m3u8") == path.CountChars() - 5; +} + + /*static*/ bool Playlist::_IsQuery(const BString& mimeString) { @@ -753,7 +792,7 @@ Playlist::_BindExtraMedia(PlaylistItem* item) FilePlaylistItem* fileItem = dynamic_cast(item); if (!fileItem) return; - + // If the media file is foo.mp3, _BindExtraMedia() searches foo.avi. BPath mediaFilePath(&fileItem->Ref()); BString mediaFilePathString = mediaFilePath.Path(); @@ -762,7 +801,7 @@ Playlist::_BindExtraMedia(PlaylistItem* item) BDirectory dir(dirPath.Path()); if (dir.InitCheck() != B_OK) return; - + BEntry entry; BString entryPathString; while (dir.GetNextEntry(&entry, true) == B_OK) { diff --git a/src/apps/mediaplayer/playlist/Playlist.h b/src/apps/mediaplayer/playlist/Playlist.h index 131fe5636c..b2f5982047 100644 --- a/src/apps/mediaplayer/playlist/Playlist.h +++ b/src/apps/mediaplayer/playlist/Playlist.h @@ -101,6 +101,8 @@ public: Playlist* playlist); static void AppendPlaylistToPlaylist(const entry_ref& ref, Playlist* playlist); + static void AppendM3uToPlaylist(const entry_ref& ref, + Playlist* playlist); static void AppendQueryToPlaylist(const entry_ref& ref, Playlist* playlist); @@ -123,6 +125,7 @@ private: static bool _IsTextPlaylist(const BString& mimeString); static bool _IsBinaryPlaylist(const BString& mimeString); static bool _IsPlaylist(const BString& mimeString); + static bool _IsM3u(const entry_ref& ref); static bool _IsQuery(const BString& mimeString); static BString _MIMEString(const entry_ref* ref); static void _BindExtraMedia(PlaylistItem* item);