Merge pull request #26350 from jjd-uk/estuary_media_align
[xbmc.git] / xbmc / guilib / listproviders / StaticProvider.cpp
blob35acff0bfc677202f7558c6c23d3d76192d546b0
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 "StaticProvider.h"
11 #include "utils/StringUtils.h"
12 #include "utils/TimeUtils.h"
13 #include "utils/XMLUtils.h"
15 CStaticListProvider::CStaticListProvider(const TiXmlElement *element, int parentID)
16 : IListProvider(parentID),
17 m_defaultItem(-1),
18 m_defaultAlways(false),
19 m_updateTime(0)
21 assert(element);
23 const TiXmlElement *item = element->FirstChildElement("item");
24 while (item)
26 if (item->FirstChild())
28 CGUIStaticItemPtr newItem(new CGUIStaticItem(item, parentID));
29 m_items.push_back(newItem);
31 item = item->NextSiblingElement("item");
34 if (XMLUtils::GetInt(element, "default", m_defaultItem))
36 const char *always = element->FirstChildElement("default")->Attribute("always");
37 if (always && StringUtils::CompareNoCase(always, "true", 4) == 0)
38 m_defaultAlways = true;
42 CStaticListProvider::CStaticListProvider(const std::vector<CGUIStaticItemPtr> &items)
43 : IListProvider(0),
44 m_defaultItem(-1),
45 m_defaultAlways(false),
46 m_updateTime(0),
47 m_items(items)
51 CStaticListProvider::CStaticListProvider(const CStaticListProvider& other)
52 : IListProvider(other.m_parentID),
53 m_defaultItem(other.m_defaultItem),
54 m_defaultAlways(other.m_defaultAlways),
55 m_updateTime(other.m_updateTime)
57 for (const auto& item : other.m_items)
59 std::shared_ptr<CGUIListItem> control(item->Clone());
60 if (!control)
61 continue;
63 std::shared_ptr<CGUIStaticItem> newItem = std::dynamic_pointer_cast<CGUIStaticItem>(control);
64 if (!newItem)
65 continue;
67 m_items.emplace_back(std::move(newItem));
71 CStaticListProvider::~CStaticListProvider() = default;
73 std::unique_ptr<IListProvider> CStaticListProvider::Clone()
75 return std::make_unique<CStaticListProvider>(*this);
78 bool CStaticListProvider::Update(bool forceRefresh)
80 bool changed = forceRefresh;
81 if (!m_updateTime)
82 m_updateTime = CTimeUtils::GetFrameTime();
83 else if (CTimeUtils::GetFrameTime() - m_updateTime > 1000)
85 m_updateTime = CTimeUtils::GetFrameTime();
86 for (auto& i : m_items)
87 i->UpdateProperties(m_parentID);
89 for (auto& i : m_items)
90 changed |= i->UpdateVisibility(m_parentID);
91 return changed; //! @todo Also returned changed if properties are changed (if so, need to update scroll to letter).
94 void CStaticListProvider::Fetch(std::vector<std::shared_ptr<CGUIListItem>>& items)
96 items.clear();
97 for (const auto& i : m_items)
99 if (i->IsVisible())
100 items.push_back(i);
104 void CStaticListProvider::SetDefaultItem(int item, bool always)
106 m_defaultItem = item;
107 m_defaultAlways = always;
110 int CStaticListProvider::GetDefaultItem() const
112 if (m_defaultItem >= 0)
114 unsigned int offset = 0;
115 for (const auto& i : m_items)
117 if (i->IsVisible())
119 if (i->m_iprogramCount == m_defaultItem)
120 return offset;
121 offset++;
125 return -1;
128 bool CStaticListProvider::AlwaysFocusDefaultItem() const
130 return m_defaultAlways;
133 bool CStaticListProvider::OnClick(const std::shared_ptr<CGUIListItem>& item)
135 CGUIStaticItem *staticItem = static_cast<CGUIStaticItem*>(item.get());
136 return staticItem->GetClickActions().ExecuteActions(0, m_parentID);