Debian Codegraph @

xbmc/playlists/PlayListXSPF.cpp

raw
1 /*
2 * Copyright (C) 2018 Tyler Szabo
3 * Copyright (C) 2018 Team Kodi
4 * This file is part of Kodi - https://kodi.tv
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 * See LICENSES/README.md for more information.
8 */
9
10 #include "PlayListXSPF.h"
11
12 #include "FileItem.h"
13 #include "URL.h"
14 #include "utils/StringUtils.h"
15 #include "utils/URIUtils.h"
16 #include "utils/XBMCTinyXML.h"
17 #include "utils/log.h"
18
19 using namespace PLAYLIST;
20
21 namespace
22 {
23
24 constexpr char const* LOCATION_TAGNAME = "location";
25 constexpr char const* PLAYLIST_TAGNAME = "playlist";
26 constexpr char const* TITLE_TAGNAME = "title";
27 constexpr char const* TRACK_TAGNAME = "track";
28 constexpr char const* TRACKLIST_TAGNAME = "trackList";
29
30 std::string GetXMLText(const TiXmlElement* pXmlElement)
31 {
32 std::string result;
33 if (pXmlElement)
34 {
35 const char* const innerText = pXmlElement->GetText();
36 if (innerText)
37 result = innerText;
38 }
39 return result;
40 }
41
42 }
43
44 CPlayListXSPF::CPlayListXSPF(void) = default;
45
46 CPlayListXSPF::~CPlayListXSPF(void) = default;
47
48 bool CPlayListXSPF::Load(const std::string& strFileName)
49 {
50 CXBMCTinyXML xmlDoc;
51
52 if (!xmlDoc.LoadFile(strFileName))
53 {
54 CLog::Log(LOGERROR, "Error parsing XML file {} ({}, {}): {}", strFileName, xmlDoc.ErrorRow(),
55 xmlDoc.ErrorCol(), xmlDoc.ErrorDesc());
56 return false;
57 }
58
59 TiXmlElement* pPlaylist = xmlDoc.FirstChildElement(PLAYLIST_TAGNAME);
60 if (!pPlaylist)
61 {
62 CLog::Log(LOGERROR, "Error parsing XML file {}: missing root element {}", strFileName,
63 PLAYLIST_TAGNAME);
64 return false;
65 }
66
67 TiXmlElement* pTracklist = pPlaylist->FirstChildElement(TRACKLIST_TAGNAME);
68 if (!pTracklist)
69 {
70 CLog::Log(LOGERROR, "Error parsing XML file {}: missing element {}", strFileName,
71 TRACKLIST_TAGNAME);
72 return false;
73 }
74
75 Clear();
76 URIUtils::GetParentPath(strFileName, m_strBasePath);
77
78 m_strPlayListName = GetXMLText(pPlaylist->FirstChildElement(TITLE_TAGNAME));
79
80 TiXmlElement* pCurTrack = pTracklist->FirstChildElement(TRACK_TAGNAME);
81 while (pCurTrack)
82 {
83 std::string location = GetXMLText(pCurTrack->FirstChildElement(LOCATION_TAGNAME));
84 if (!location.empty())
85 {
86 std::string label = GetXMLText(pCurTrack->FirstChildElement(TITLE_TAGNAME));
87
88 CFileItemPtr newItem(new CFileItem(label));
89
90 CURL uri(location);
91
92 // at the time of writing CURL doesn't handle file:// URI scheme the way
93 // it's presented in this format, parse to local path instead
94 std::string localpath;
95 if (StringUtils::StartsWith(location, "file:///"))
96 {
97 #ifndef TARGET_WINDOWS
98 // Linux absolute path must start with root
99 localpath = "/";
100 #endif
101 // Path starts after "file:///"
102 localpath += CURL::Decode(location.substr(8));
103 }
104 else if (uri.GetProtocol().empty())
105 {
106 localpath = URIUtils::AppendSlash(m_strBasePath) + CURL::Decode(location);
107 }
108
109 if (!localpath.empty())
110 {
111 // We don't use URIUtils::CanonicalizePath because m_strBasePath may be a
112 // protocol e.g. smb
113 #ifdef TARGET_WINDOWS
114 StringUtils::Replace(localpath, "/", "\\");
115 #endif
116 localpath = URIUtils::GetRealPath(localpath);
117
118 newItem->SetPath(localpath);
119 }
120 else
121 {
122 newItem->SetURL(uri);
123 }
124
125 Add(newItem);
126 }
127
128 pCurTrack = pCurTrack->NextSiblingElement(TRACK_TAGNAME);
129 }
130
131 return true;
132 }