Skip to content

Commit

Permalink
Merge pull request #10730 from b-pass/multi-content
Browse files Browse the repository at this point in the history
Allow multiple content elements in a single container
  • Loading branch information
MartijnKaijser committed Feb 18, 2017
2 parents 176ead0 + 2bdf705 commit 2c1977f
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 25 deletions.
2 changes: 2 additions & 0 deletions xbmc/listproviders/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
set(SOURCES DirectoryProvider.cpp
IListProvider.cpp
MultiProvider.cpp
StaticProvider.cpp)

set(HEADERS DirectoryProvider.h
IListProvider.h
MultiProvider.h
StaticProvider.h)

core_add_library(listproviders)
6 changes: 3 additions & 3 deletions xbmc/listproviders/DirectoryProvider.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
* Copyright (C) 2013-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -278,7 +278,7 @@ void CDirectoryProvider::Announce(AnnouncementFlag flag, const char *sender, con
}
}

void CDirectoryProvider::Fetch(std::vector<CGUIListItemPtr> &items) const
void CDirectoryProvider::Fetch(std::vector<CGUIListItemPtr> &items)
{
CSingleLock lock(m_section);
items.clear();
Expand Down
6 changes: 3 additions & 3 deletions xbmc/listproviders/DirectoryProvider.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
* Copyright (C) 2013-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -59,7 +59,7 @@ class CDirectoryProvider :

virtual bool Update(bool forceRefresh) override;
virtual void Announce(ANNOUNCEMENT::AnnouncementFlag flag, const char *sender, const char *message, const CVariant &data) override;
virtual void Fetch(std::vector<CGUIListItemPtr> &items) const override;
virtual void Fetch(std::vector<CGUIListItemPtr> &items) override;
virtual void Reset() override;
virtual bool OnClick(const CGUIListItemPtr &item) override;
bool OnInfo(const CGUIListItemPtr &item) override;
Expand Down
28 changes: 20 additions & 8 deletions xbmc/listproviders/IListProvider.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
* Copyright (C) 2013-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -22,18 +22,30 @@
#include "utils/XBMCTinyXML.h"
#include "StaticProvider.h"
#include "DirectoryProvider.h"
#include "MultiProvider.h"

IListProvider *IListProvider::Create(const TiXmlNode *node, int parentID)
{
const TiXmlElement *root = node->FirstChildElement("content");
const TiXmlNode *root = node->FirstChild("content");
if (root)
{
const TiXmlElement *item = root->FirstChildElement("item");
if (item)
return new CStaticListProvider(root, parentID);
const TiXmlNode *next = root->NextSibling("content");
if (next)
return new CMultiProvider(root, parentID);

if (!root->NoChildren())
return new CDirectoryProvider(root, parentID);
return CreateSingle(root, parentID);
}
return NULL;
}

IListProvider *IListProvider::CreateSingle(const TiXmlNode *content, int parentID)
{
const TiXmlElement *item = content->FirstChildElement("item");
if (item)
return new CStaticListProvider(content->ToElement(), parentID);

if (!content->NoChildren())
return new CDirectoryProvider(content->ToElement(), parentID);

return NULL;
}
17 changes: 12 additions & 5 deletions xbmc/listproviders/IListProvider.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
* Copyright (C) 2013-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -38,11 +38,18 @@ class IListProvider
virtual ~IListProvider() {}

/*! \brief Factory to create list providers.
\param node a TiXmlNode to create.
\param parent a parent TiXmlNode for the container.
\param parentID id of parent window for context.
\return the list provider, NULL if none.
*/
static IListProvider *Create(const TiXmlNode *node, int parentID);
static IListProvider *Create(const TiXmlNode *parent, int parentID);

/*! \brief Factory to create list providers. Cannot create a multi-provider.
\param content the TiXmlNode for the content to create.
\param parentID id of parent window for context.
\return the list provider, NULL if none.
*/
static IListProvider *CreateSingle(const TiXmlNode *content, int parentID);

/*! \brief Update the list content
\return true if the content has changed, false otherwise.
Expand All @@ -52,7 +59,7 @@ class IListProvider
/*! \brief Fetch the current list of items.
\param items [out] the list to be filled.
*/
virtual void Fetch(std::vector<CGUIListItemPtr> &items) const=0;
virtual void Fetch(std::vector<CGUIListItemPtr> &items)=0;

/*! \brief Check whether the list provider is updating content.
\return true if in the processing of updating, false otherwise.
Expand Down
118 changes: 118 additions & 0 deletions xbmc/listproviders/MultiProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2013-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#include "MultiProvider.h"
#include "utils/XBMCTinyXML.h"
#include "threads/SingleLock.h"

CMultiProvider::CMultiProvider(const TiXmlNode *first, int parentID)
: IListProvider(parentID)
{
for (const TiXmlNode *content = first; content; content = content->NextSiblingElement("content"))
{
IListProviderPtr sub(IListProvider::CreateSingle(content, parentID));
if (sub)
m_providers.push_back(sub);
}
}

bool CMultiProvider::Update(bool forceRefresh)
{
bool result = false;
for (auto& provider : m_providers)
result |= provider->Update(forceRefresh);
return result;
}

void CMultiProvider::Fetch(std::vector<CGUIListItemPtr> &items)
{
CSingleLock lock(m_section);
std::vector<CGUIListItemPtr> subItems;
items.clear();
m_itemMap.clear();
for (auto const& provider : m_providers)
{
provider->Fetch(subItems);
for (auto& item : subItems)
{
auto key = GetItemKey(item);
m_itemMap[key] = provider;
items.push_back(item);
}
subItems.clear();
}
}

bool CMultiProvider::IsUpdating() const
{
bool result = false;
for (auto const& provider : m_providers)
result |= provider->IsUpdating();
return result;
}

void CMultiProvider::Reset()
{
{
CSingleLock lock(m_section);
m_itemMap.clear();
}

for (auto const& provider : m_providers)
provider->Reset();
}

bool CMultiProvider::OnClick(const CGUIListItemPtr &item)
{
CSingleLock lock(m_section);
auto key = GetItemKey(item);
auto it = m_itemMap.find(key);
if (it != m_itemMap.end())
return it->second->OnClick(item);
else
return false;
}

bool CMultiProvider::OnInfo(const CGUIListItemPtr &item)
{
CSingleLock lock(m_section);
auto key = GetItemKey(item);
auto it = m_itemMap.find(key);
if (it != m_itemMap.end())
return it->second->OnInfo(item);
else
return false;
}

bool CMultiProvider::OnContextMenu(const CGUIListItemPtr &item)
{
CSingleLock lock(m_section);
auto key = GetItemKey(item);
auto it = m_itemMap.find(key);
if (it != m_itemMap.end())
return it->second->OnContextMenu(item);
else
return false;
}

CMultiProvider::item_key_type CMultiProvider::GetItemKey(CGUIListItemPtr const &item)
{
return reinterpret_cast<item_key_type>(item.get());
}
53 changes: 53 additions & 0 deletions xbmc/listproviders/MultiProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2013-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/

#pragma once

#include <vector>
#include <map>
#include "IListProvider.h"
#include "threads/CriticalSection.h"

typedef std::shared_ptr<IListProvider> IListProviderPtr;

/*!
\ingroup listproviders
\brief A listprovider that handles multiple individual providers.
*/
class CMultiProvider : public IListProvider
{
public:
CMultiProvider(const TiXmlNode *first, int parentID);

virtual bool Update(bool forceRefresh) override;
virtual void Fetch(std::vector<CGUIListItemPtr> &items) override;
virtual bool IsUpdating() const override;
virtual void Reset() override;
virtual bool OnClick(const CGUIListItemPtr &item) override;
virtual bool OnInfo(const CGUIListItemPtr &item) override;
virtual bool OnContextMenu(const CGUIListItemPtr &item) override;

protected:
typedef size_t item_key_type;
static item_key_type GetItemKey(CGUIListItemPtr const &item);
std::vector<IListProviderPtr> m_providers;
std::map<item_key_type, IListProviderPtr> m_itemMap;
CCriticalSection m_section; // protects m_itemMap
};
6 changes: 3 additions & 3 deletions xbmc/listproviders/StaticProvider.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
* Copyright (C) 2013-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -78,7 +78,7 @@ bool CStaticListProvider::Update(bool forceRefresh)
return changed; //! @todo Also returned changed if properties are changed (if so, need to update scroll to letter).
}

void CStaticListProvider::Fetch(std::vector<CGUIListItemPtr> &items) const
void CStaticListProvider::Fetch(std::vector<CGUIListItemPtr> &items)
{
items.clear();
for (std::vector<CGUIStaticItemPtr>::const_iterator i = m_items.begin(); i != m_items.end(); ++i)
Expand Down
6 changes: 3 additions & 3 deletions xbmc/listproviders/StaticProvider.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
* Copyright (C) 2013-2017 Team Kodi
* http://kodi.tv
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -33,7 +33,7 @@ class CStaticListProvider : public IListProvider
virtual ~CStaticListProvider();

virtual bool Update(bool forceRefresh) override;
virtual void Fetch(std::vector<CGUIListItemPtr> &items) const override;
virtual void Fetch(std::vector<CGUIListItemPtr> &items) override;
virtual bool OnClick(const CGUIListItemPtr &item) override;
bool OnInfo(const CGUIListItemPtr &item) override { return false; }
bool OnContextMenu(const CGUIListItemPtr &item) override { return false; }
Expand Down

0 comments on commit 2c1977f

Please sign in to comment.