| 1 |
/* |
| 2 |
* Copyright (C) 2005-2020 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 "PlayListXML.h" |
| 10 |
|
| 11 |
#include "FileItem.h" |
| 12 |
#include "Util.h" |
| 13 |
#include "filesystem/File.h" |
| 14 |
#include "media/MediaLockState.h" |
| 15 |
#include "utils/StringUtils.h" |
| 16 |
#include "utils/URIUtils.h" |
| 17 |
#include "utils/Variant.h" |
| 18 |
#include "utils/XMLUtils.h" |
| 19 |
#include "utils/log.h" |
| 20 |
|
| 21 |
using namespace PLAYLIST; |
| 22 |
using namespace XFILE; |
| 23 |
|
| 24 |
/* |
| 25 |
Playlist example (must be stored with .pxml extension): |
| 26 |
|
| 27 |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
| 28 |
<streams> |
| 29 |
<!-- Stream definition. To have multiple streams, just add another <stream>...</stream> set. !--> |
| 30 |
<stream> |
| 31 |
<!-- Stream URL !--> |
| 32 |
<url>mms://stream02.rambler.ru/eurosport</url> |
| 33 |
<!-- Stream name - used for display !--> |
| 34 |
<name>Евроспорт</name> |
| 35 |
<!-- Stream category - currently only LIVETV is supported !--> |
| 36 |
<category>LIVETV</category> |
| 37 |
<!-- Stream language code !--> |
| 38 |
<lang>RU</lang> |
| 39 |
<!-- Stream channel number - will be used to select stream by channel number !--> |
| 40 |
<channel>1</channel> |
| 41 |
<!-- Stream is password-protected !--> |
| 42 |
<lockpassword>123</lockpassword> |
| 43 |
</stream> |
| 44 |
|
| 45 |
<stream> |
| 46 |
<url>mms://video.rfn.ru/vesti_24</url> |
| 47 |
<name>Вести 24</name> |
| 48 |
<category>LIVETV</category> |
| 49 |
<lang>RU</lang> |
| 50 |
<channel>2</channel> |
| 51 |
</stream> |
| 52 |
|
| 53 |
</streams> |
| 54 |
*/ |
| 55 |
|
| 56 |
|
| 57 |
CPlayListXML::CPlayListXML(void) = default; |
| 58 |
|
| 59 |
CPlayListXML::~CPlayListXML(void) = default; |
| 60 |
|
| 61 |
|
| 62 |
static inline std::string GetString( const TiXmlElement* pRootElement, const char *tagName ) |
| 63 |
{ |
| 64 |
std::string strValue; |
| 65 |
if ( XMLUtils::GetString(pRootElement, tagName, strValue) ) |
| 66 |
return strValue; |
| 67 |
|
| 68 |
return ""; |
| 69 |
} |
| 70 |
|
| 71 |
bool CPlayListXML::Load( const std::string& strFileName ) |
| 72 |
{ |
| 73 |
CXBMCTinyXML xmlDoc; |
| 74 |
|
| 75 |
m_strPlayListName = URIUtils::GetFileName(strFileName); |
| 76 |
URIUtils::GetParentPath(strFileName, m_strBasePath); |
| 77 |
|
| 78 |
Clear(); |
| 79 |
|
| 80 |
// Try to load the file as XML. If it does not load, return an error. |
| 81 |
if ( !xmlDoc.LoadFile( strFileName ) ) |
| 82 |
{ |
| 83 |
CLog::Log(LOGERROR, "Playlist {} has invalid format/malformed xml", strFileName); |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
TiXmlElement *pRootElement = xmlDoc.RootElement(); |
| 88 |
|
| 89 |
// If the stream does not contain "streams", still ok. Not an error. |
| 90 |
if (!pRootElement || StringUtils::CompareNoCase(pRootElement->Value(), "streams")) |
| 91 |
{ |
| 92 |
CLog::Log(LOGERROR, "Playlist {} has no <streams> root", strFileName); |
| 93 |
return false; |
| 94 |
} |
| 95 |
|
| 96 |
TiXmlElement* pSet = pRootElement->FirstChildElement("stream"); |
| 97 |
|
| 98 |
while ( pSet ) |
| 99 |
{ |
| 100 |
// Get parameters |
| 101 |
std::string url = GetString( pSet, "url" ); |
| 102 |
std::string name = GetString( pSet, "name" ); |
| 103 |
std::string category = GetString( pSet, "category" ); |
| 104 |
std::string lang = GetString( pSet, "lang" ); |
| 105 |
std::string channel = GetString( pSet, "channel" ); |
| 106 |
std::string lockpass = GetString( pSet, "lockpassword" ); |
| 107 |
|
| 108 |
// If url is empty, it doesn't make any sense |
| 109 |
if ( !url.empty() ) |
| 110 |
{ |
| 111 |
// If the name is empty, use url |
| 112 |
if ( name.empty() ) |
| 113 |
name = url; |
| 114 |
|
| 115 |
// Append language to the name, and also set as metadata |
| 116 |
if ( !lang.empty() ) |
| 117 |
name += " [" + lang + "]"; |
| 118 |
|
| 119 |
std::string info = name; |
| 120 |
CFileItemPtr newItem( new CFileItem(info) ); |
| 121 |
newItem->SetPath(url); |
| 122 |
|
| 123 |
// Set language as metadata |
| 124 |
if ( !lang.empty() ) |
| 125 |
newItem->SetProperty("language", lang.c_str() ); |
| 126 |
|
| 127 |
// Set category as metadata |
| 128 |
if ( !category.empty() ) |
| 129 |
newItem->SetProperty("category", category.c_str() ); |
| 130 |
|
| 131 |
// Set channel as extra info and as metadata |
| 132 |
if ( !channel.empty() ) |
| 133 |
{ |
| 134 |
newItem->SetProperty("remotechannel", channel.c_str() ); |
| 135 |
newItem->SetExtraInfo( "Channel: " + channel ); |
| 136 |
} |
| 137 |
|
| 138 |
if ( !lockpass.empty() ) |
| 139 |
{ |
| 140 |
newItem->m_strLockCode = lockpass; |
| 141 |
newItem->m_iHasLock = LOCK_STATE_LOCKED; |
| 142 |
newItem->m_iLockMode = LOCK_MODE_NUMERIC; |
| 143 |
} |
| 144 |
|
| 145 |
Add(newItem); |
| 146 |
} |
| 147 |
else |
| 148 |
CLog::Log(LOGERROR, "Playlist entry {} in file {} has missing <url> tag", name, strFileName); |
| 149 |
|
| 150 |
pSet = pSet->NextSiblingElement("stream"); |
| 151 |
} |
| 152 |
|
| 153 |
return true; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
void CPlayListXML::Save(const std::string& strFileName) const |
| 158 |
{ |
| 159 |
if (!m_vecItems.size()) return ; |
| 160 |
std::string strPlaylist = CUtil::MakeLegalPath(strFileName); |
| 161 |
CFile file; |
| 162 |
if (!file.OpenForWrite(strPlaylist, true)) |
| 163 |
{ |
| 164 |
CLog::Log(LOGERROR, "Could not save WPL playlist: [{}]", strPlaylist); |
| 165 |
return ; |
| 166 |
} |
| 167 |
std::string write; |
| 168 |
write += StringUtils::Format("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"); |
| 169 |
write += StringUtils::Format("<streams>\n"); |
| 170 |
for (int i = 0; i < (int)m_vecItems.size(); ++i) |
| 171 |
{ |
| 172 |
CFileItemPtr item = m_vecItems[i]; |
| 173 |
write += StringUtils::Format(" <stream>\n" ); |
| 174 |
write += StringUtils::Format(" <url>{}</url>", item->GetPath().c_str()); |
| 175 |
write += StringUtils::Format(" <name>{}</name>", item->GetLabel()); |
| 176 |
|
| 177 |
if ( !item->GetProperty("language").empty() ) |
| 178 |
write += StringUtils::Format(" <lang>{}</lang>", item->GetProperty("language").asString()); |
| 179 |
|
| 180 |
if ( !item->GetProperty("category").empty() ) |
| 181 |
write += StringUtils::Format(" <category>{}</category>", |
| 182 |
item->GetProperty("category").asString()); |
| 183 |
|
| 184 |
if ( !item->GetProperty("remotechannel").empty() ) |
| 185 |
write += StringUtils::Format(" <channel>{}</channel>", |
| 186 |
item->GetProperty("remotechannel").asString()); |
| 187 |
|
| 188 |
if (item->m_iHasLock > LOCK_STATE_NO_LOCK) |
| 189 |
write += StringUtils::Format(" <lockpassword>{}<lockpassword>", item->m_strLockCode); |
| 190 |
|
| 191 |
write += StringUtils::Format(" </stream>\n\n" ); |
| 192 |
} |
| 193 |
|
| 194 |
write += StringUtils::Format("</streams>\n"); |
| 195 |
file.Write(write.c_str(), write.size()); |
| 196 |
file.Close(); |
| 197 |
} |