Debian Codegraph @

xbmc/playlists/PlayListB4S.cpp

raw
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 "PlayListB4S.h"
10
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/XBMCTinyXML.h"
18 #include "utils/XMLUtils.h"
19 #include "utils/log.h"
20
21 #include <iostream>
22 #include <string>
23
24 using namespace XFILE;
25 using namespace PLAYLIST;
26
27 /* ------------------------ example b4s playlist file ---------------------------------
28 <?xml version="1.0" encoding='UTF-8' standalone="yes"?>
29 <WinampXML>
30 <!-- Generated by: Nullsoft Winamp3 version 3.0d -->
31 <playlist num_entries="2" label="Playlist 001">
32 <entry Playstring="file:E:\Program Files\Winamp3\demo.mp3">
33 <Name>demo</Name>
34 <Length>5982</Length>
35 </entry>
36 <entry Playstring="file:E:\Program Files\Winamp3\demo.mp3">
37 <Name>demo</Name>
38 <Length>5982</Length>
39 </entry>
40 </playlist>
41 </WinampXML>
42 ------------------------ end of example b4s playlist file ---------------------------------*/
43 CPlayListB4S::CPlayListB4S(void) = default;
44
45 CPlayListB4S::~CPlayListB4S(void) = default;
46
47
48 bool CPlayListB4S::LoadData(std::istream& stream)
49 {
50 CXBMCTinyXML xmlDoc;
51
52 stream >> xmlDoc;
53
54 if (xmlDoc.Error())
55 {
56 CLog::Log(LOGERROR, "Unable to parse B4S info Error: {}", xmlDoc.ErrorDesc());
57 return false;
58 }
59
60 TiXmlElement* pRootElement = xmlDoc.RootElement();
61 if (!pRootElement ) return false;
62
63 TiXmlElement* pPlayListElement = pRootElement->FirstChildElement("playlist");
64 if (!pPlayListElement ) return false;
65 m_strPlayListName = XMLUtils::GetAttribute(pPlayListElement, "label");
66
67 TiXmlElement* pEntryElement = pPlayListElement->FirstChildElement("entry");
68
69 if (!pEntryElement) return false;
70 while (pEntryElement)
71 {
72 std::string strFileName = XMLUtils::GetAttribute(pEntryElement, "Playstring");
73 size_t iColon = strFileName.find(':');
74 if (iColon != std::string::npos)
75 {
76 iColon++;
77 strFileName.erase(0, iColon);
78 }
79 if (strFileName.size())
80 {
81 TiXmlNode* pNodeInfo = pEntryElement->FirstChild("Name");
82 TiXmlNode* pNodeLength = pEntryElement->FirstChild("Length");
83 long lDuration = 0;
84 if (pNodeLength)
85 {
86 lDuration = atol(pNodeLength->FirstChild()->Value());
87 }
88 if (pNodeInfo)
89 {
90 std::string strInfo = pNodeInfo->FirstChild()->Value();
91 strFileName = URIUtils::SubstitutePath(strFileName);
92 CUtil::GetQualifiedFilename(m_strBasePath, strFileName);
93 CFileItemPtr newItem(new CFileItem(strInfo));
94 newItem->SetPath(strFileName);
95 newItem->GetMusicInfoTag()->SetDuration(lDuration);
96 Add(newItem);
97 }
98 }
99 pEntryElement = pEntryElement->NextSiblingElement();
100 }
101 return true;
102 }
103
104 void CPlayListB4S::Save(const std::string& strFileName) const
105 {
106 if (!m_vecItems.size()) return ;
107 std::string strPlaylist = strFileName;
108 strPlaylist = CUtil::MakeLegalPath(std::move(strPlaylist));
109 CFile file;
110 if (!file.OpenForWrite(strPlaylist, true))
111 {
112 CLog::Log(LOGERROR, "Could not save B4S playlist: [{}]", strPlaylist);
113 return ;
114 }
115 std::string write;
116 write += StringUtils::Format("<?xml version={}1.0{} encoding='UTF-8' standalone={}yes{}?>\n", 34,
117 34, 34, 34);
118 write += StringUtils::Format("<WinampXML>\n");
119 write += StringUtils::Format(" <playlist num_entries=\"{0}\" label=\"{1}\">\n",
120 m_vecItems.size(), m_strPlayListName);
121 for (int i = 0; i < (int)m_vecItems.size(); ++i)
122 {
123 const CFileItemPtr item = m_vecItems[i];
124 write += StringUtils::Format(" <entry Playstring={}file:{}{}>\n", 34, item->GetPath(), 34);
125 write += StringUtils::Format(" <Name>{}</Name>\n", item->GetLabel().c_str());
126 write +=
127 StringUtils::Format(" <Length>{}</Length>\n", item->GetMusicInfoTag()->GetDuration());
128 }
129 write += StringUtils::Format(" </playlist>\n");
130 write += StringUtils::Format("</WinampXML>\n");
131 file.Write(write.c_str(), write.size());
132 file.Close();
133 }