[videodb] remove unused seasons table from episode_view
[xbmc.git] / xbmc / playlists / PlayListB4S.cpp
blob921b03e3b16e35daf97d2ee2af1d10268e05f81f
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 "PlayListB4S.h"
11 #include "FileItem.h"
12 #include "Util.h"
13 #include "filesystem/File.h"
14 #include "music/tags/MusicInfoTag.h"
15 #include "utils/StringUtils.h"
16 #include "utils/URIUtils.h"
17 #include "utils/XBMCTinyXML2.h"
18 #include "utils/XMLUtils.h"
19 #include "utils/log.h"
21 #include <iostream>
22 #include <string>
24 using namespace XFILE;
26 /* ------------------------ example b4s playlist file ---------------------------------
27 <?xml version="1.0" encoding='UTF-8' standalone="yes"?>
28 <WinampXML>
29 <!-- Generated by: Nullsoft Winamp3 version 3.0d -->
30 <playlist num_entries="2" label="Playlist 001">
31 <entry Playstring="file:E:\Program Files\Winamp3\demo.mp3">
32 <Name>demo</Name>
33 <Length>5982</Length>
34 </entry>
35 <entry Playstring="file:E:\Program Files\Winamp3\demo.mp3">
36 <Name>demo</Name>
37 <Length>5982</Length>
38 </entry>
39 </playlist>
40 </WinampXML>
41 ------------------------ end of example b4s playlist file ---------------------------------*/
43 namespace KODI::PLAYLIST
46 CPlayListB4S::CPlayListB4S(void) = default;
48 CPlayListB4S::~CPlayListB4S(void) = default;
51 bool CPlayListB4S::LoadData(std::istream& stream)
53 CXBMCTinyXML2 xmlDoc;
55 std::string b4sStream(std::istreambuf_iterator<char>(stream), {});
57 xmlDoc.Parse(b4sStream);
59 if (xmlDoc.Error())
61 CLog::Log(LOGERROR, "Unable to parse B4S info Error: {}", xmlDoc.ErrorStr());
62 return false;
65 auto* pRootElement = xmlDoc.RootElement();
66 if (!pRootElement)
67 return false;
69 auto* pPlayListElement = pRootElement->FirstChildElement("playlist");
70 if (!pPlayListElement)
71 return false;
73 m_strPlayListName = XMLUtils::GetAttribute(pPlayListElement, "label");
75 auto* pEntryElement = pPlayListElement->FirstChildElement("entry");
77 if (!pEntryElement)
78 return false;
80 while (pEntryElement)
82 std::string strFileName = XMLUtils::GetAttribute(pEntryElement, "Playstring");
83 size_t iColon = strFileName.find(':');
84 if (iColon != std::string::npos)
86 iColon++;
87 strFileName.erase(0, iColon);
89 if (strFileName.size())
91 auto* pNodeInfo = pEntryElement->FirstChildElement("Name");
92 auto* pNodeLength = pEntryElement->FirstChildElement("Length");
93 long lDuration = 0;
94 if (pNodeLength)
96 lDuration = atol(pNodeLength->FirstChild()->Value());
98 if (pNodeInfo)
100 std::string strInfo = pNodeInfo->FirstChild()->Value();
101 strFileName = URIUtils::SubstitutePath(strFileName);
102 CUtil::GetQualifiedFilename(m_strBasePath, strFileName);
103 CFileItemPtr newItem(new CFileItem(strInfo));
104 newItem->SetPath(strFileName);
105 newItem->GetMusicInfoTag()->SetDuration(lDuration);
106 Add(newItem);
109 pEntryElement = pEntryElement->NextSiblingElement();
111 return true;
114 void CPlayListB4S::Save(const std::string& strFileName) const
116 if (!m_vecItems.size()) return ;
117 std::string strPlaylist = strFileName;
118 strPlaylist = CUtil::MakeLegalPath(std::move(strPlaylist));
119 CFile file;
120 if (!file.OpenForWrite(strPlaylist, true))
122 CLog::Log(LOGERROR, "Could not save B4S playlist: [{}]", strPlaylist);
123 return ;
125 std::string write;
126 write += StringUtils::Format("<?xml version={}1.0{} encoding='UTF-8' standalone={}yes{}?>\n", 34,
127 34, 34, 34);
128 write += StringUtils::Format("<WinampXML>\n");
129 write += StringUtils::Format(" <playlist num_entries=\"{0}\" label=\"{1}\">\n",
130 m_vecItems.size(), m_strPlayListName);
131 for (int i = 0; i < (int)m_vecItems.size(); ++i)
133 const CFileItemPtr item = m_vecItems[i];
134 write += StringUtils::Format(" <entry Playstring={}file:{}{}>\n", 34, item->GetPath(), 34);
135 write += StringUtils::Format(" <Name>{}</Name>\n", item->GetLabel().c_str());
136 write +=
137 StringUtils::Format(" <Length>{}</Length>\n", item->GetMusicInfoTag()->GetDuration());
139 write += StringUtils::Format(" </playlist>\n");
140 write += StringUtils::Format("</WinampXML>\n");
141 file.Write(write.c_str(), write.size());
142 file.Close();
145 } // namespace KODI::PLAYLIST