diff options
Diffstat (limited to 'xbmc/utils/Fanart.cpp')
| -rw-r--r-- | xbmc/utils/Fanart.cpp | 175 |
1 files changed, 175 insertions, 0 deletions
diff --git a/xbmc/utils/Fanart.cpp b/xbmc/utils/Fanart.cpp new file mode 100644 index 0000000..fbc3774 --- /dev/null +++ b/xbmc/utils/Fanart.cpp | |||
| @@ -0,0 +1,175 @@ | |||
| 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 "Fanart.h" | ||
| 10 | |||
| 11 | #include "StringUtils.h" | ||
| 12 | #include "URIUtils.h" | ||
| 13 | #include "utils/XBMCTinyXML.h" | ||
| 14 | #include "utils/XMLUtils.h" | ||
| 15 | |||
| 16 | #include <algorithm> | ||
| 17 | #include <functional> | ||
| 18 | |||
| 19 | const unsigned int CFanart::max_fanart_colors=3; | ||
| 20 | |||
| 21 | |||
| 22 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 23 | /// CFanart Functions | ||
| 24 | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||
| 25 | |||
| 26 | CFanart::CFanart() = default; | ||
| 27 | |||
| 28 | void CFanart::Pack() | ||
| 29 | { | ||
| 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) | ||
| 34 | { | ||
| 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); | ||
| 41 | } | ||
| 42 | m_xml << fanart; | ||
| 43 | } | ||
| 44 | |||
| 45 | void CFanart::AddFanart(const std::string& image, const std::string& preview, const std::string& colors) | ||
| 46 | { | ||
| 47 | SFanartData info; | ||
| 48 | info.strPreview = preview; | ||
| 49 | info.strImage = image; | ||
| 50 | ParseColors(colors, info.strColors); | ||
| 51 | m_fanart.push_back(std::move(info)); | ||
| 52 | } | ||
| 53 | |||
| 54 | void CFanart::Clear() | ||
| 55 | { | ||
| 56 | m_fanart.clear(); | ||
| 57 | m_xml.clear(); | ||
| 58 | } | ||
| 59 | |||
| 60 | bool CFanart::Unpack() | ||
| 61 | { | ||
| 62 | CXBMCTinyXML doc; | ||
| 63 | doc.Parse(m_xml); | ||
| 64 | |||
| 65 | m_fanart.clear(); | ||
| 66 | |||
| 67 | TiXmlElement *fanart = doc.FirstChildElement("fanart"); | ||
| 68 | while (fanart) | ||
| 69 | { | ||
| 70 | std::string url = XMLUtils::GetAttribute(fanart, "url"); | ||
| 71 | TiXmlElement *fanartThumb = fanart->FirstChildElement("thumb"); | ||
| 72 | while (fanartThumb) | ||
| 73 | { | ||
| 74 | if (!fanartThumb->NoChildren()) | ||
| 75 | { | ||
| 76 | SFanartData data; | ||
| 77 | if (url.empty()) | ||
| 78 | { | ||
| 79 | data.strImage = fanartThumb->FirstChild()->ValueStr(); | ||
| 80 | data.strPreview = XMLUtils::GetAttribute(fanartThumb, "preview"); | ||
| 81 | } | ||
| 82 | else | ||
| 83 | { | ||
| 84 | data.strImage = URIUtils::AddFileToFolder(url, fanartThumb->FirstChild()->ValueStr()); | ||
| 85 | if (fanartThumb->Attribute("preview")) | ||
| 86 | data.strPreview = URIUtils::AddFileToFolder(url, fanartThumb->Attribute("preview")); | ||
| 87 | } | ||
| 88 | ParseColors(XMLUtils::GetAttribute(fanartThumb, "colors"), data.strColors); | ||
| 89 | m_fanart.push_back(data); | ||
| 90 | } | ||
| 91 | fanartThumb = fanartThumb->NextSiblingElement("thumb"); | ||
| 92 | } | ||
| 93 | fanart = fanart->NextSiblingElement("fanart"); | ||
| 94 | } | ||
| 95 | return true; | ||
| 96 | } | ||
| 97 | |||
| 98 | std::string CFanart::GetImageURL(unsigned int index) const | ||
| 99 | { | ||
| 100 | if (index >= m_fanart.size()) | ||
| 101 | return ""; | ||
| 102 | |||
| 103 | return m_fanart[index].strImage; | ||
| 104 | } | ||
| 105 | |||
| 106 | std::string CFanart::GetPreviewURL(unsigned int index) const | ||
| 107 | { | ||
| 108 | if (index >= m_fanart.size()) | ||
| 109 | return ""; | ||
| 110 | |||
| 111 | return m_fanart[index].strPreview.empty() ? m_fanart[index].strImage : m_fanart[index].strPreview; | ||
| 112 | } | ||
| 113 | |||
| 114 | const std::string CFanart::GetColor(unsigned int index) const | ||
| 115 | { | ||
| 116 | if (index >= max_fanart_colors || m_fanart.empty() || | ||
| 117 | m_fanart[0].strColors.size() < index*9+8) | ||
| 118 | return "FFFFFFFF"; | ||
| 119 | |||
| 120 | // format is AARRGGBB,AARRGGBB etc. | ||
| 121 | return m_fanart[0].strColors.substr(index*9, 8); | ||
| 122 | } | ||
| 123 | |||
| 124 | bool CFanart::SetPrimaryFanart(unsigned int index) | ||
| 125 | { | ||
| 126 | if (index >= m_fanart.size()) | ||
| 127 | return false; | ||
| 128 | |||
| 129 | std::iter_swap(m_fanart.begin()+index, m_fanart.begin()); | ||
| 130 | |||
| 131 | // repack our data | ||
| 132 | Pack(); | ||
| 133 | |||
| 134 | return true; | ||
| 135 | } | ||
| 136 | |||
| 137 | unsigned int CFanart::GetNumFanarts() const | ||
| 138 | { | ||
| 139 | return m_fanart.size(); | ||
| 140 | } | ||
| 141 | |||
| 142 | bool CFanart::ParseColors(const std::string &colorsIn, std::string &colorsOut) | ||
| 143 | { | ||
| 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|" | ||
| 147 | |||
| 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 in the specified format. | ||
| 150 | |||
| 151 | if (colorsIn.empty()) | ||
| 152 | return false; | ||
| 153 | |||
| 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%2lx%2lx%2lx", atol(strTriplets[0].c_str()), atol(strTriplets[1].c_str()), atol(strTriplets[2].c_str())); | ||
| 167 | } | ||
| 168 | } | ||
| 169 | } | ||
| 170 | else | ||
| 171 | { // assume is our format | ||
| 172 | colorsOut = colorsIn; | ||
| 173 | } | ||
| 174 | return true; | ||
| 175 | } | ||
