[PVR][Estuary] Timer settings dialog: Show client name in timer type selection dialog...
[xbmc.git] / xbmc / utils / Fanart.cpp
blob5e385eebe97d4d2836ca503afec675b0499f01ea
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 "Fanart.h"
11 #include "StringUtils.h"
12 #include "URIUtils.h"
13 #include "utils/XBMCTinyXML.h"
14 #include "utils/XMLUtils.h"
16 #include <algorithm>
17 #include <functional>
19 const unsigned int CFanart::max_fanart_colors=3;
22 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
23 /// CFanart Functions
24 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
26 CFanart::CFanart() = default;
28 void CFanart::Pack()
30 // Take our data and pack it into the m_xml string
31 m_xml.clear();
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);
42 m_xml << fanart;
45 void CFanart::AddFanart(const std::string& image, const std::string& preview, const std::string& colors)
47 SFanartData info;
48 info.strPreview = preview;
49 info.strImage = image;
50 ParseColors(colors, info.strColors);
51 m_fanart.push_back(std::move(info));
54 void CFanart::Clear()
56 m_fanart.clear();
57 m_xml.clear();
60 bool CFanart::Unpack()
62 CXBMCTinyXML doc;
63 doc.Parse(m_xml);
65 m_fanart.clear();
67 TiXmlElement *fanart = doc.FirstChildElement("fanart");
68 while (fanart)
70 std::string url = XMLUtils::GetAttribute(fanart, "url");
71 TiXmlElement *fanartThumb = fanart->FirstChildElement("thumb");
72 while (fanartThumb)
74 if (!fanartThumb->NoChildren())
76 SFanartData data;
77 if (url.empty())
79 data.strImage = fanartThumb->FirstChild()->ValueStr();
80 data.strPreview = XMLUtils::GetAttribute(fanartThumb, "preview");
82 else
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");
95 return true;
98 std::string CFanart::GetImageURL(unsigned int index) const
100 if (index >= m_fanart.size())
101 return "";
103 return m_fanart[index].strImage;
106 std::string CFanart::GetPreviewURL(unsigned int index) const
108 if (index >= m_fanart.size())
109 return "";
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)
118 return "FFFFFFFF";
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())
127 return false;
129 std::iter_swap(m_fanart.begin()+index, m_fanart.begin());
131 // repack our data
132 Pack();
134 return true;
137 unsigned int CFanart::GetNumFanarts() const
139 return m_fanart.size();
142 bool CFanart::ParseColors(const std::string &colorsIn, std::string &colorsOut)
144 // Formats:
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())
152 return false;
154 // check for the TVDB RGB triplets "|68,69,59|69,70,58|78,78,68|"
155 if (colorsIn[0] == '|')
156 { // need conversion
157 colorsOut.clear();
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)
163 { // convert
164 if (colorsOut.size())
165 colorsOut += ",";
166 colorsOut += StringUtils::Format("FF{:2x}{:2x}{:2x}", std::stol(strTriplets[0]),
167 std::stol(strTriplets[1]), std::stol(strTriplets[2]));
171 else
172 { // assume is our format
173 colorsOut = colorsIn;
175 return true;