| 1 |
/* |
| 2 |
* Copyright (C) 2005-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "PlayListWPL.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "Util.h" |
| 13 |
#include "filesystem/File.h" |
| 14 |
#include "utils/StringUtils.h" |
| 15 |
#include "utils/URIUtils.h" |
| 16 |
#include "utils/XBMCTinyXML.h" |
| 17 |
#include "utils/XMLUtils.h" |
| 18 |
#include "utils/log.h" |
| 19 |
|
| 20 |
#include <iostream> |
| 21 |
#include <string> |
| 22 |
|
| 23 |
using namespace XFILE; |
| 24 |
using namespace PLAYLIST; |
| 25 |
|
| 26 |
/* ------------------------ example wpl playlist file --------------------------------- |
| 27 |
<?wpl version="1.0"?> |
| 28 |
<smil> |
| 29 |
<head> |
| 30 |
<meta name="Generator" content="Microsoft Windows Media Player -- 10.0.0.3646"/> |
| 31 |
<author/> |
| 32 |
<title>Playlist</title> |
| 33 |
</head> |
| 34 |
<body> |
| 35 |
<seq> |
| 36 |
<media src="E:\MP3\Track1.mp3"/> |
| 37 |
<media src="E:\MP3\Track2.mp3"/> |
| 38 |
<media src="E:\MP3\track3.mp3"/> |
| 39 |
</seq> |
| 40 |
</body> |
| 41 |
</smil> |
| 42 |
------------------------ end of example wpl playlist file ---------------------------------*/ |
| 43 |
//Note: File is utf-8 encoded by default |
| 44 |
|
| 45 |
CPlayListWPL::CPlayListWPL(void) = default; |
| 46 |
|
| 47 |
CPlayListWPL::~CPlayListWPL(void) = default; |
| 48 |
|
| 49 |
|
| 50 |
bool CPlayListWPL::LoadData(std::istream& stream) |
| 51 |
{ |
| 52 |
CXBMCTinyXML xmlDoc; |
| 53 |
|
| 54 |
stream >> xmlDoc; |
| 55 |
if (xmlDoc.Error()) |
| 56 |
{ |
| 57 |
CLog::Log(LOGERROR, "Unable to parse B4S info Error: {}", xmlDoc.ErrorDesc()); |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
TiXmlElement* pRootElement = xmlDoc.RootElement(); |
| 62 |
if (!pRootElement ) return false; |
| 63 |
|
| 64 |
TiXmlElement* pHeadElement = pRootElement->FirstChildElement("head"); |
| 65 |
if (pHeadElement ) |
| 66 |
{ |
| 67 |
TiXmlElement* pTitelElement = pHeadElement->FirstChildElement("title"); |
| 68 |
if (pTitelElement ) |
| 69 |
m_strPlayListName = pTitelElement->Value(); |
| 70 |
} |
| 71 |
|
| 72 |
TiXmlElement* pBodyElement = pRootElement->FirstChildElement("body"); |
| 73 |
if (!pBodyElement ) return false; |
| 74 |
|
| 75 |
TiXmlElement* pSeqElement = pBodyElement->FirstChildElement("seq"); |
| 76 |
if (!pSeqElement ) return false; |
| 77 |
|
| 78 |
TiXmlElement* pMediaElement = pSeqElement->FirstChildElement("media"); |
| 79 |
|
| 80 |
if (!pMediaElement) return false; |
| 81 |
while (pMediaElement) |
| 82 |
{ |
| 83 |
std::string strFileName = XMLUtils::GetAttribute(pMediaElement, "src"); |
| 84 |
if (!strFileName.empty()) |
| 85 |
{ |
| 86 |
std::string strFileNameClean = URIUtils::SubstitutePath(strFileName); |
| 87 |
CUtil::GetQualifiedFilename(m_strBasePath, strFileNameClean); |
| 88 |
std::string strDescription = URIUtils::GetFileName(strFileNameClean); |
| 89 |
CFileItemPtr newItem(new CFileItem(strDescription)); |
| 90 |
newItem->SetPath(strFileNameClean); |
| 91 |
Add(newItem); |
| 92 |
} |
| 93 |
pMediaElement = pMediaElement->NextSiblingElement(); |
| 94 |
} |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
void CPlayListWPL::Save(const std::string& strFileName) const |
| 99 |
{ |
| 100 |
if (!m_vecItems.size()) return ; |
| 101 |
std::string strPlaylist = CUtil::MakeLegalPath(strFileName); |
| 102 |
CFile file; |
| 103 |
if (!file.OpenForWrite(strPlaylist, true)) |
| 104 |
{ |
| 105 |
CLog::Log(LOGERROR, "Could not save WPL playlist: [{}]", strPlaylist); |
| 106 |
return ; |
| 107 |
} |
| 108 |
std::string write; |
| 109 |
write += StringUtils::Format("<?wpl version={}1.0{}>\n", 34, 34); |
| 110 |
write += StringUtils::Format("<smil>\n"); |
| 111 |
write += StringUtils::Format(" <head>\n"); |
| 112 |
write += StringUtils::Format(" <meta name={}Generator{} content={}Microsoft Windows Media " |
| 113 |
"Player -- 10.0.0.3646{}/>\n", |
| 114 |
34, 34, 34, 34); |
| 115 |
write += StringUtils::Format(" <author/>\n"); |
| 116 |
write += StringUtils::Format(" <title>{}</title>\n", m_strPlayListName.c_str()); |
| 117 |
write += StringUtils::Format(" </head>\n"); |
| 118 |
write += StringUtils::Format(" <body>\n"); |
| 119 |
write += StringUtils::Format(" <seq>\n"); |
| 120 |
for (int i = 0; i < (int)m_vecItems.size(); ++i) |
| 121 |
{ |
| 122 |
CFileItemPtr item = m_vecItems[i]; |
| 123 |
write += StringUtils::Format(" <media src={}{}{}/>", 34, item->GetPath(), 34); |
| 124 |
} |
| 125 |
write += StringUtils::Format(" </seq>\n"); |
| 126 |
write += StringUtils::Format(" </body>\n"); |
| 127 |
write += StringUtils::Format("</smil>\n"); |
| 128 |
file.Write(write.c_str(), write.size()); |
| 129 |
file.Close(); |
| 130 |
} |