1 /*
2  *  Copyright (C) 2005-2021 Team Kodi (https://kodi.tv)
3  *
4  *  SPDX-License-Identifier: GPL-2.0-or-later
5  *  See LICENSE.md for more information.
6  */
7 
8 #include "ChannelEpg.h"
9 
10 #include "../utilities/XMLUtils.h"
11 
12 #include <kodi/tools/StringUtils.h>
13 
14 using namespace kodi::tools;
15 using namespace iptvsimple;
16 using namespace iptvsimple::data;
17 using namespace pugi;
18 
UpdateFrom(const xml_node & channelNode,Channels & channels)19 bool ChannelEpg::UpdateFrom(const xml_node& channelNode, Channels& channels)
20 {
21   if (!GetAttributeValue(channelNode, "id", m_id) || m_id.empty())
22     return false;
23 
24   bool foundChannel = false;
25   bool haveDisplayNames = false;
26   for (const auto& displayNameNode : channelNode.children("display-name"))
27   {
28     haveDisplayNames = true;
29 
30     const std::string name = displayNameNode.child_value();
31     if (channels.FindChannel(m_id, name))
32     {
33       foundChannel = true;
34       AddDisplayName(name);
35     }
36   }
37 
38   // If there are no display names just check if the id matches a channel
39   if (!haveDisplayNames && channels.FindChannel(m_id, ""))
40     foundChannel = true;
41 
42   if (!foundChannel)
43     return false;
44 
45   // get icon if available
46   const auto& iconNode = channelNode.child("icon");
47   std::string iconPath = m_iconPath;
48   if (!iconNode || !GetAttributeValue(iconNode, "src", iconPath))
49     m_iconPath.clear();
50   else
51     m_iconPath = iconPath;
52 
53   return true;
54 }
55 
CombineNamesAndIconPathFrom(const ChannelEpg & right)56 bool ChannelEpg::CombineNamesAndIconPathFrom(const ChannelEpg& right)
57 {
58   bool combined = false;
59 
60   for (const DisplayNamePair& namePair : right.m_displayNames)
61   {
62     AddDisplayName(namePair.m_displayName);
63     combined = true;
64   }
65 
66   if (m_iconPath.empty() && !right.m_iconPath.empty())
67   {
68     m_iconPath = right.m_iconPath;
69     combined = true;
70   }
71 
72   return combined;
73 }
74 
AddDisplayName(const std::string & value)75 void ChannelEpg::AddDisplayName(const std::string& value)
76 {
77   DisplayNamePair pair;
78   pair.m_displayName = value;
79   pair.m_displayNameWithUnderscores = value;
80   StringUtils::Replace(pair.m_displayNameWithUnderscores, ' ', '_');
81   m_displayNames.emplace_back(pair);
82 }
83 
GetJoinedDisplayNames()84 std::string ChannelEpg::GetJoinedDisplayNames()
85 {
86   std::vector<std::string> names;
87   for (auto& displayNamePair : m_displayNames)
88     names.emplace_back(displayNamePair.m_displayName);
89 
90   return StringUtils::Join(names, EPG_STRING_TOKEN_SEPARATOR);
91 }
92