Merge pull request #26350 from jjd-uk/estuary_media_align
[xbmc.git] / xbmc / guilib / listproviders / MultiProvider.cpp
blob7a9a350cfe34662c03e7125fe807f3bb3236a74b
1 /*
2 * Copyright (C) 2013-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 "MultiProvider.h"
11 #include "utils/XBMCTinyXML.h"
13 #include <mutex>
15 CMultiProvider::CMultiProvider(const TiXmlNode *first, int parentID)
16 : IListProvider(parentID)
18 for (const TiXmlNode *content = first; content; content = content->NextSiblingElement("content"))
20 IListProviderPtr sub(IListProvider::CreateSingle(content, parentID));
21 if (sub)
22 m_providers.push_back(std::move(sub));
26 CMultiProvider::CMultiProvider(const CMultiProvider& other) : IListProvider(other.m_parentID)
28 for (const auto& provider : other.m_providers)
30 std::unique_ptr<IListProvider> newProvider = provider->Clone();
31 if (newProvider)
32 m_providers.emplace_back(std::move(newProvider));
36 std::unique_ptr<IListProvider> CMultiProvider::Clone()
38 return std::make_unique<CMultiProvider>(*this);
41 bool CMultiProvider::Update(bool forceRefresh)
43 bool result = false;
44 for (auto& provider : m_providers)
45 result |= provider->Update(forceRefresh);
46 return result;
49 void CMultiProvider::Fetch(std::vector<std::shared_ptr<CGUIListItem>>& items)
51 std::unique_lock<CCriticalSection> lock(m_section);
52 std::vector<std::shared_ptr<CGUIListItem>> subItems;
53 items.clear();
54 m_itemMap.clear();
55 for (auto const& provider : m_providers)
57 provider->Fetch(subItems);
58 for (auto& item : subItems)
60 auto key = GetItemKey(item);
61 m_itemMap[key] = provider.get();
62 items.push_back(item);
64 subItems.clear();
68 bool CMultiProvider::IsUpdating() const
70 bool result = false;
71 for (auto const& provider : m_providers)
72 result |= provider->IsUpdating();
73 return result;
76 void CMultiProvider::Reset()
79 std::unique_lock<CCriticalSection> lock(m_section);
80 m_itemMap.clear();
83 for (auto const& provider : m_providers)
84 provider->Reset();
87 bool CMultiProvider::OnClick(const std::shared_ptr<CGUIListItem>& item)
89 std::unique_lock<CCriticalSection> lock(m_section);
90 auto key = GetItemKey(item);
91 auto it = m_itemMap.find(key);
92 if (it != m_itemMap.end())
93 return it->second->OnClick(item);
94 else
95 return false;
98 bool CMultiProvider::OnInfo(const std::shared_ptr<CGUIListItem>& item)
100 std::unique_lock<CCriticalSection> lock(m_section);
101 auto key = GetItemKey(item);
102 auto it = m_itemMap.find(key);
103 if (it != m_itemMap.end())
104 return it->second->OnInfo(item);
105 else
106 return false;
109 bool CMultiProvider::OnContextMenu(const std::shared_ptr<CGUIListItem>& item)
111 std::unique_lock<CCriticalSection> lock(m_section);
112 auto key = GetItemKey(item);
113 auto it = m_itemMap.find(key);
114 if (it != m_itemMap.end())
115 return it->second->OnContextMenu(item);
116 else
117 return false;
120 CMultiProvider::item_key_type CMultiProvider::GetItemKey(std::shared_ptr<CGUIListItem> const& item)
122 return reinterpret_cast<item_key_type>(item.get());