Merge pull request #25808 from CastagnaIT/fix_url_parse
[xbmc.git] / xbmc / playlists / PlayListWPL.cpp
blobe87dce87dec98f786e16d61481211d6059856518
1 /*
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.
7 */
9 #include "PlayListWPL.h"
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/XBMCTinyXML2.h"
17 #include "utils/XMLUtils.h"
18 #include "utils/log.h"
20 #include <iostream>
21 #include <string>
23 using namespace XFILE;
25 /* ------------------------ example wpl playlist file ---------------------------------
26 <?wpl version="1.0"?>
27 <smil>
28 <head>
29 <meta name="Generator" content="Microsoft Windows Media Player -- 10.0.0.3646"/>
30 <author/>
31 <title>Playlist</title>
32 </head>
33 <body>
34 <seq>
35 <media src="E:\MP3\Track1.mp3"/>
36 <media src="E:\MP3\Track2.mp3"/>
37 <media src="E:\MP3\track3.mp3"/>
38 </seq>
39 </body>
40 </smil>
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)
54 CXBMCTinyXML2 xmlDoc;
56 std::string wplStream(std::istreambuf_iterator<char>(stream), {});
57 xmlDoc.Parse(wplStream);
59 if (xmlDoc.Error())
61 CLog::Log(LOGERROR, "Unable to parse WPL info Error: {}", xmlDoc.ErrorStr());
62 return false;
65 auto* pRootElement = xmlDoc.RootElement();
66 if (!pRootElement)
67 return false;
69 auto* pHeadElement = pRootElement->FirstChildElement("head");
70 if (pHeadElement )
72 auto* pTitelElement = pHeadElement->FirstChildElement("title");
73 if (pTitelElement )
74 m_strPlayListName = pTitelElement->Value();
77 auto* pBodyElement = pRootElement->FirstChildElement("body");
78 if (!pBodyElement)
79 return false;
81 auto* pSeqElement = pBodyElement->FirstChildElement("seq");
82 if (!pSeqElement)
83 return false;
85 auto* pMediaElement = pSeqElement->FirstChildElement("media");
87 if (!pMediaElement)
88 return false;
90 while (pMediaElement)
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);
100 Add(newItem);
102 pMediaElement = pMediaElement->NextSiblingElement();
104 return true;
107 void CPlayListWPL::Save(const std::string& strFileName) const
109 if (!m_vecItems.size()) return ;
110 std::string strPlaylist = CUtil::MakeLegalPath(strFileName);
111 CFile file;
112 if (!file.OpenForWrite(strPlaylist, true))
114 CLog::Log(LOGERROR, "Could not save WPL playlist: [{}]", strPlaylist);
115 return ;
117 std::string write;
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",
123 34, 34, 34, 34);
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());
138 file.Close();
141 } // namespace KODI::PLAYLIST