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.
11 #include "StringUtils.h"
13 #include "utils/XBMCTinyXML.h"
14 #include "utils/XMLUtils.h"
19 const unsigned int CFanart::max_fanart_colors
=3;
22 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
24 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26 CFanart::CFanart() = default;
30 // Take our data and pack it into the m_xml string
32 TiXmlElement
fanart("fanart");
33 for (std::vector
<SFanartData
>::const_iterator it
= m_fanart
.begin(); it
!= m_fanart
.end(); ++it
)
35 TiXmlElement
thumb("thumb");
36 thumb
.SetAttribute("colors", it
->strColors
.c_str());
37 thumb
.SetAttribute("preview", it
->strPreview
.c_str());
38 TiXmlText
text(it
->strImage
);
39 thumb
.InsertEndChild(text
);
40 fanart
.InsertEndChild(thumb
);
45 void CFanart::AddFanart(const std::string
& image
, const std::string
& preview
, const std::string
& colors
)
48 info
.strPreview
= preview
;
49 info
.strImage
= image
;
50 ParseColors(colors
, info
.strColors
);
51 m_fanart
.push_back(std::move(info
));
60 bool CFanart::Unpack()
67 TiXmlElement
*fanart
= doc
.FirstChildElement("fanart");
70 std::string url
= XMLUtils::GetAttribute(fanart
, "url");
71 TiXmlElement
*fanartThumb
= fanart
->FirstChildElement("thumb");
74 if (!fanartThumb
->NoChildren())
79 data
.strImage
= fanartThumb
->FirstChild()->ValueStr();
80 data
.strPreview
= XMLUtils::GetAttribute(fanartThumb
, "preview");
84 data
.strImage
= URIUtils::AddFileToFolder(url
, fanartThumb
->FirstChild()->ValueStr());
85 if (fanartThumb
->Attribute("preview"))
86 data
.strPreview
= URIUtils::AddFileToFolder(url
, fanartThumb
->Attribute("preview"));
88 ParseColors(XMLUtils::GetAttribute(fanartThumb
, "colors"), data
.strColors
);
89 m_fanart
.push_back(data
);
91 fanartThumb
= fanartThumb
->NextSiblingElement("thumb");
93 fanart
= fanart
->NextSiblingElement("fanart");
98 std::string
CFanart::GetImageURL(unsigned int index
) const
100 if (index
>= m_fanart
.size())
103 return m_fanart
[index
].strImage
;
106 std::string
CFanart::GetPreviewURL(unsigned int index
) const
108 if (index
>= m_fanart
.size())
111 return m_fanart
[index
].strPreview
.empty() ? m_fanart
[index
].strImage
: m_fanart
[index
].strPreview
;
114 const std::string
CFanart::GetColor(unsigned int index
) const
116 if (index
>= max_fanart_colors
|| m_fanart
.empty() ||
117 m_fanart
[0].strColors
.size() < index
*9+8)
120 // format is AARRGGBB,AARRGGBB etc.
121 return m_fanart
[0].strColors
.substr(index
*9, 8);
124 bool CFanart::SetPrimaryFanart(unsigned int index
)
126 if (index
>= m_fanart
.size())
129 std::iter_swap(m_fanart
.begin()+index
, m_fanart
.begin());
137 unsigned int CFanart::GetNumFanarts() const
139 return m_fanart
.size();
142 bool CFanart::ParseColors(const std::string
&colorsIn
, std::string
&colorsOut
)
145 // 0: XBMC ARGB Hexadecimal string comma separated "FFFFFFFF,DDDDDDDD,AAAAAAAA"
146 // 1: The TVDB RGB Int Triplets, pipe separate with leading/trailing pipes "|68,69,59|69,70,58|78,78,68|"
148 // Essentially we read the colors in using the proper format, and store them in our own fixed temporary format (3 DWORDS), and then
149 // write them back in the specified format.
151 if (colorsIn
.empty())
154 // check for the TVDB RGB triplets "|68,69,59|69,70,58|78,78,68|"
155 if (colorsIn
[0] == '|')
158 std::vector
<std::string
> strColors
= StringUtils::Split(colorsIn
, "|");
159 for (int i
= 0; i
< std::min((int)strColors
.size()-1, (int)max_fanart_colors
); i
++)
160 { // split up each color
161 std::vector
<std::string
> strTriplets
= StringUtils::Split(strColors
[i
+1], ",");
162 if (strTriplets
.size() == 3)
164 if (colorsOut
.size())
166 colorsOut
+= StringUtils::Format("FF{:2x}{:2x}{:2x}", std::stol(strTriplets
[0]),
167 std::stol(strTriplets
[1]), std::stol(strTriplets
[2]));
172 { // assume is our format
173 colorsOut
= colorsIn
;