Skip to content

Commit

Permalink
A new type of list provider, MultiProvider, which allows multiple <co…
Browse files Browse the repository at this point in the history
…ntent> blocks in a single container.
  • Loading branch information
b-pass committed Oct 19, 2016
1 parent 63b8843 commit 342ed6e
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 8 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)
24 changes: 18 additions & 6 deletions xbmc/listproviders/IListProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
11 changes: 9 additions & 2 deletions xbmc/listproviders/IListProvider.h
Original file line number Diff line number Diff line change
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 Down
1 change: 1 addition & 0 deletions xbmc/listproviders/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
SRCS = DirectoryProvider.cpp
SRCS += IListProvider.cpp
SRCS += MultiProvider.cpp
SRCS += StaticProvider.cpp

LIB=listproviders.a
Expand Down
110 changes: 110 additions & 0 deletions xbmc/listproviders/MultiProvider.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
*
* 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) const
{
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)
{
m_itemMap[item.get()] = 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(bool immediately)
{
if (immediately)
{
CSingleLock lock(m_section);
m_itemMap.clear();
}

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

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

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

bool CMultiProvider::OnContextMenu(const CGUIListItemPtr &item)
{
CSingleLock lock(m_section);
auto it = m_itemMap.find(item.get());
if (it != m_itemMap.end())
return it->second->OnContextMenu(item);
else
return false;
}
51 changes: 51 additions & 0 deletions xbmc/listproviders/MultiProvider.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2013 Team XBMC
* http://xbmc.org
*
* 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);
virtual void Fetch(std::vector<CGUIListItemPtr> &items) const;
virtual bool IsUpdating() const;
virtual void Reset(bool immediately = false);
virtual bool OnClick(const CGUIListItemPtr &item);
virtual bool OnInfo(const CGUIListItemPtr &item);
virtual bool OnContextMenu(const CGUIListItemPtr &item);

protected:
std::vector<IListProviderPtr> m_providers;
mutable std::map<CGUIListItem*, IListProviderPtr> m_itemMap;
CCriticalSection m_section; // protects m_itemMap
};

0 comments on commit 342ed6e

Please sign in to comment.