2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "PlayListWPL.h"
13 #include "filesystem/File.h"
14 #include "utils/StringUtils.h"
15 #include "utils/URIUtils.h"
16 #include "utils/XBMCTinyXML2.h"
17 #include "utils/XMLUtils.h"
18 #include "utils/log.h"
23 using namespace XFILE
;
25 /* ------------------------ example wpl playlist file ---------------------------------
29 <meta name="Generator" content="Microsoft Windows Media Player -- 10.0.0.3646"/>
31 <title>Playlist</title>
35 <media src="E:\MP3\Track1.mp3"/>
36 <media src="E:\MP3\Track2.mp3"/>
37 <media src="E:\MP3\track3.mp3"/>
41 ------------------------ end of example wpl playlist file ---------------------------------*/
42 //Note: File is utf-8 encoded by default
44 namespace KODI::PLAYLIST
47 CPlayListWPL::CPlayListWPL(void) = default;
49 CPlayListWPL::~CPlayListWPL(void) = default;
52 bool CPlayListWPL::LoadData(std::istream
& stream
)
56 std::string
wplStream(std::istreambuf_iterator
<char>(stream
), {});
57 xmlDoc
.Parse(wplStream
);
61 CLog::Log(LOGERROR
, "Unable to parse WPL info Error: {}", xmlDoc
.ErrorStr());
65 auto* pRootElement
= xmlDoc
.RootElement();
69 auto* pHeadElement
= pRootElement
->FirstChildElement("head");
72 auto* pTitelElement
= pHeadElement
->FirstChildElement("title");
74 m_strPlayListName
= pTitelElement
->Value();
77 auto* pBodyElement
= pRootElement
->FirstChildElement("body");
81 auto* pSeqElement
= pBodyElement
->FirstChildElement("seq");
85 auto* pMediaElement
= pSeqElement
->FirstChildElement("media");
92 std::string strFileName
= XMLUtils::GetAttribute(pMediaElement
, "src");
93 if (!strFileName
.empty())
95 std::string strFileNameClean
= URIUtils::SubstitutePath(strFileName
);
96 CUtil::GetQualifiedFilename(m_strBasePath
, strFileNameClean
);
97 std::string strDescription
= URIUtils::GetFileName(strFileNameClean
);
98 CFileItemPtr
newItem(new CFileItem(strDescription
));
99 newItem
->SetPath(strFileNameClean
);
102 pMediaElement
= pMediaElement
->NextSiblingElement();
107 void CPlayListWPL::Save(const std::string
& strFileName
) const
109 if (!m_vecItems
.size()) return ;
110 std::string strPlaylist
= CUtil::MakeLegalPath(strFileName
);
112 if (!file
.OpenForWrite(strPlaylist
, true))
114 CLog::Log(LOGERROR
, "Could not save WPL playlist: [{}]", strPlaylist
);
118 write
+= StringUtils::Format("<?wpl version={}1.0{}>\n", 34, 34);
119 write
+= StringUtils::Format("<smil>\n");
120 write
+= StringUtils::Format(" <head>\n");
121 write
+= StringUtils::Format(" <meta name={}Generator{} content={}Microsoft Windows Media "
122 "Player -- 10.0.0.3646{}/>\n",
124 write
+= StringUtils::Format(" <author/>\n");
125 write
+= StringUtils::Format(" <title>{}</title>\n", m_strPlayListName
.c_str());
126 write
+= StringUtils::Format(" </head>\n");
127 write
+= StringUtils::Format(" <body>\n");
128 write
+= StringUtils::Format(" <seq>\n");
129 for (int i
= 0; i
< (int)m_vecItems
.size(); ++i
)
131 CFileItemPtr item
= m_vecItems
[i
];
132 write
+= StringUtils::Format(" <media src={}{}{}/>", 34, item
->GetPath(), 34);
134 write
+= StringUtils::Format(" </seq>\n");
135 write
+= StringUtils::Format(" </body>\n");
136 write
+= StringUtils::Format("</smil>\n");
137 file
.Write(write
.c_str(), write
.size());
141 } // namespace KODI::PLAYLIST