New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[playlist] Add support for reading XSPF Playlists #14585
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright (C) 2018 Tyler Szabo | ||
tylerszabo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* Copyright (C) 2018 Team Kodi | ||
* This file is part of Kodi - https://kodi.tv | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSES/README.md for more information. | ||
*/ | ||
|
||
#include "PlayListXSPF.h" | ||
|
||
#include "utils/log.h" | ||
#include "utils/URIUtils.h" | ||
#include "utils/XBMCTinyXML.h" | ||
#include "URL.h" | ||
|
||
using namespace PLAYLIST; | ||
|
||
namespace | ||
{ | ||
|
||
constexpr char const* LOCATION_TAGNAME = "location"; | ||
constexpr char const* PLAYLIST_TAGNAME = "playlist"; | ||
constexpr char const* TITLE_TAGNAME = "title"; | ||
constexpr char const* TRACK_TAGNAME = "track"; | ||
constexpr char const* TRACKLIST_TAGNAME = "trackList"; | ||
|
||
std::string GetXMLText(const TiXmlElement* pXmlElement) | ||
{ | ||
std::string result; | ||
if (pXmlElement) | ||
{ | ||
const char* const innerText = pXmlElement->GetText(); | ||
if (innerText) | ||
result = innerText; | ||
} | ||
return result; | ||
} | ||
|
||
tylerszabo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
CPlayListXSPF::CPlayListXSPF(void) = default; | ||
|
||
CPlayListXSPF::~CPlayListXSPF(void) = default; | ||
|
||
bool CPlayListXSPF::Load(const std::string& strFileName) | ||
{ | ||
CXBMCTinyXML xmlDoc; | ||
|
||
if (!xmlDoc.LoadFile(strFileName)) | ||
{ | ||
CLog::Log(LOGERROR, "Error parsing XML file %s (%d, %d): %s", strFileName.c_str(), xmlDoc.ErrorRow(), xmlDoc.ErrorCol(), xmlDoc.ErrorDesc()); | ||
return false; | ||
} | ||
|
||
TiXmlElement* pPlaylist = xmlDoc.FirstChildElement(PLAYLIST_TAGNAME); | ||
if (!pPlaylist) | ||
{ | ||
CLog::Log(LOGERROR, "Error parsing XML file %s: missing root element %s", strFileName.c_str(), PLAYLIST_TAGNAME); | ||
return false; | ||
} | ||
|
||
TiXmlElement* pTracklist = pPlaylist->FirstChildElement(TRACKLIST_TAGNAME); | ||
if (!pTracklist) | ||
{ | ||
CLog::Log(LOGERROR, "Error parsing XML file %s: missing element %s", strFileName.c_str(), TRACKLIST_TAGNAME); | ||
return false; | ||
} | ||
|
||
Clear(); | ||
URIUtils::GetParentPath(strFileName, m_strBasePath); | ||
|
||
m_strPlayListName = GetXMLText(pPlaylist->FirstChildElement(TITLE_TAGNAME)); | ||
|
||
TiXmlElement* pCurTrack = pTracklist->FirstChildElement(TRACK_TAGNAME); | ||
while (pCurTrack) | ||
{ | ||
std::string location = GetXMLText(pCurTrack->FirstChildElement(LOCATION_TAGNAME)); | ||
if (!location.empty()) | ||
{ | ||
std::string label = GetXMLText(pCurTrack->FirstChildElement(TITLE_TAGNAME)); | ||
|
||
CFileItemPtr newItem(new CFileItem(label)); | ||
|
||
CURL uri(location); | ||
|
||
// at the time of writing CURL doesn't handle file:// URI scheme the way | ||
// it's presented in this format, parse to local path instead | ||
std::string localpath; | ||
if (StringUtils::StartsWith(location, "file:///")) | ||
{ | ||
#ifndef TARGET_WINDOWS | ||
// Linux absolute path must start with root | ||
localpath = "/"; | ||
#endif | ||
tylerszabo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Path starts after "file:///" | ||
localpath += CURL::Decode(location.substr(8)); | ||
} | ||
else if (uri.GetProtocol().empty()) | ||
{ | ||
localpath = URIUtils::AppendSlash(m_strBasePath) + CURL::Decode(location); | ||
} | ||
|
||
if (!localpath.empty()) | ||
{ | ||
#ifdef TARGET_WINDOWS | ||
StringUtils::Replace(localpath, "/", "\\"); | ||
localpath = URIUtils::CanonicalizePath(localpath, '\\'); | ||
#else | ||
localpath = URIUtils::CanonicalizePath(localpath, '/'); | ||
#endif | ||
tylerszabo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
newItem->SetPath(localpath); | ||
} | ||
else | ||
{ | ||
newItem->SetURL(uri); | ||
} | ||
|
||
Add(newItem); | ||
} | ||
|
||
pCurTrack = pCurTrack->NextSiblingElement(TRACK_TAGNAME); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (C) 2018 Team Kodi | ||
* This file is part of Kodi - https://kodi.tv | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSES/README.md for more information. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "PlayList.h" | ||
|
||
namespace PLAYLIST | ||
{ | ||
class CPlayListXSPF : public CPlayList | ||
{ | ||
public: | ||
CPlayListXSPF(void); | ||
~CPlayListXSPF(void) override; | ||
|
||
// Implementation of CPlayList | ||
bool Load(const std::string& strFileName) override; | ||
tylerszabo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
set(SOURCES TestPlayListFactory.cpp | ||
TestPlayListXSPF.cpp) | ||
|
||
core_add_test_library(playlists_test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright (C) 2018 Tyler Szabo | ||
* Copyright (C) 2018 Team Kodi | ||
* This file is part of Kodi - https://kodi.tv | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSES/README.md for more information. | ||
*/ | ||
|
||
#include "playlists/PlayListFactory.h" | ||
|
||
#include "playlists/PlayList.h" | ||
#include "playlists/PlayListXSPF.h" | ||
#include "test/TestUtils.h" | ||
#include "URL.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
using namespace PLAYLIST; | ||
|
||
|
||
TEST(TestPlayListFactory, XSPF) | ||
{ | ||
std::string filename = XBMC_REF_FILE_PATH("/xbmc/playlists/test/newfile.xspf"); | ||
CURL url("http://example.com/playlists/playlist.xspf"); | ||
CPlayList* playlist = nullptr; | ||
|
||
EXPECT_TRUE(CPlayListFactory::IsPlaylist(url)); | ||
EXPECT_TRUE(CPlayListFactory::IsPlaylist(filename)); | ||
|
||
playlist = CPlayListFactory::Create(filename); | ||
EXPECT_NE(playlist, nullptr); | ||
|
||
if (playlist) | ||
{ | ||
EXPECT_NE(dynamic_cast<CPlayListXSPF*>(playlist), nullptr); | ||
delete playlist; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* Copyright (C) 2018 Tyler Szabo | ||
* Copyright (C) 2018 Team Kodi | ||
* This file is part of Kodi - https://kodi.tv | ||
* | ||
* SPDX-License-Identifier: GPL-2.0-or-later | ||
* See LICENSES/README.md for more information. | ||
*/ | ||
|
||
#include "playlists/PlayListXSPF.h" | ||
|
||
#include "test/TestUtils.h" | ||
#include "utils/URIUtils.h" | ||
#include "FileItem.h" | ||
#include "URL.h" | ||
|
||
#include "gtest/gtest.h" | ||
|
||
using namespace PLAYLIST; | ||
|
||
|
||
tylerszabo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
TEST(TestPlayListXSPF, Load) | ||
{ | ||
std::string filename = XBMC_REF_FILE_PATH("/xbmc/playlists/test/test.xspf"); | ||
CPlayListXSPF playlist; | ||
std::vector<std::string> pathparts; | ||
std::vector<std::string>::reverse_iterator it; | ||
|
||
EXPECT_TRUE(playlist.Load(filename)); | ||
|
||
EXPECT_EQ(playlist.size(), 5); | ||
EXPECT_STREQ(playlist.GetName().c_str(), "Various Music"); | ||
|
||
|
||
ASSERT_GT(playlist.size(), 0); | ||
EXPECT_STREQ(playlist[0]->GetLabel().c_str(), ""); | ||
EXPECT_STREQ(playlist[0]->GetURL().Get().c_str(), "http://example.com/song_1.mp3"); | ||
|
||
|
||
ASSERT_GT(playlist.size(), 1); | ||
EXPECT_STREQ(playlist[1]->GetLabel().c_str(), "Relative local file"); | ||
pathparts = URIUtils::SplitPath(playlist[1]->GetPath()); | ||
it = pathparts.rbegin(); | ||
EXPECT_STREQ((*it++).c_str(), "song_2.mp3"); | ||
EXPECT_STREQ((*it++).c_str(), "path_to"); | ||
EXPECT_STREQ((*it++).c_str(), "relative"); | ||
EXPECT_STREQ((*it++).c_str(), "test"); | ||
EXPECT_STREQ((*it++).c_str(), "playlists"); | ||
EXPECT_STREQ((*it++).c_str(), "xbmc"); | ||
|
||
|
||
ASSERT_GT(playlist.size(), 2); | ||
EXPECT_STREQ(playlist[2]->GetLabel().c_str(), "Don\xC2\x92t Worry, We\xC2\x92ll Be Watching You"); | ||
pathparts = URIUtils::SplitPath(playlist[2]->GetPath()); | ||
it = pathparts.rbegin(); | ||
EXPECT_STREQ((*it++).c_str(), "09 - Don't Worry, We'll Be Watching You.mp3"); | ||
EXPECT_STREQ((*it++).c_str(), "Making Mirrors"); | ||
EXPECT_STREQ((*it++).c_str(), "Gotye"); | ||
EXPECT_STREQ((*it++).c_str(), "Music"); | ||
EXPECT_STREQ((*it++).c_str(), "jane"); | ||
EXPECT_STREQ((*it++).c_str(), "Users"); | ||
EXPECT_STREQ((*it++).c_str(), "C:"); | ||
|
||
|
||
ASSERT_GT(playlist.size(), 3); | ||
EXPECT_STREQ(playlist[3]->GetLabel().c_str(), "Rollin' & Scratchin'"); | ||
pathparts = URIUtils::SplitPath(playlist[3]->GetPath()); | ||
it = pathparts.rbegin(); | ||
EXPECT_STREQ((*it++).c_str(), "08 - Rollin' & Scratchin'.mp3"); | ||
EXPECT_STREQ((*it++).c_str(), "Homework"); | ||
EXPECT_STREQ((*it++).c_str(), "Daft Punk"); | ||
EXPECT_STREQ((*it++).c_str(), "Music"); | ||
EXPECT_STREQ((*it++).c_str(), "jane"); | ||
EXPECT_STREQ((*it++).c_str(), "home"); | ||
|
||
|
||
ASSERT_GT(playlist.size(), 4); | ||
EXPECT_STREQ(playlist[4]->GetLabel().c_str(), ""); | ||
EXPECT_STREQ(playlist[4]->GetURL().Get().c_str(), "http://example.com/song_2.mp3"); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.