Debian Codegraph @

xbmc/pvr/channels/PVRChannelGroupsContainer.cpp

raw
1 /*
2 * Copyright (C) 2012-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "PVRChannelGroupsContainer.h"
10
11 #include "pvr/channels/PVRChannel.h"
12 #include "pvr/channels/PVRChannelGroupMember.h"
13 #include "pvr/channels/PVRChannelGroups.h"
14 #include "pvr/epg/EpgInfoTag.h"
15 #include "utils/log.h"
16
17 #include <memory>
18 #include <mutex>
19
20 using namespace PVR;
21
22 CPVRChannelGroupsContainer::CPVRChannelGroupsContainer() :
23 m_groupsRadio(new CPVRChannelGroups(true)),
24 m_groupsTV(new CPVRChannelGroups(false))
25 {
26 }
27
28 CPVRChannelGroupsContainer::~CPVRChannelGroupsContainer()
29 {
30 Unload();
31 delete m_groupsRadio;
32 delete m_groupsTV;
33 }
34
35 bool CPVRChannelGroupsContainer::Update(const std::vector<std::shared_ptr<CPVRClient>>& clients)
36 {
37 return LoadFromDatabase(clients) && UpdateFromClients(clients);
38 }
39
40 bool CPVRChannelGroupsContainer::LoadFromDatabase(
41 const std::vector<std::shared_ptr<CPVRClient>>& clients)
42 {
43 return m_groupsTV->LoadFromDatabase(clients) && m_groupsRadio->LoadFromDatabase(clients);
44 }
45
46 bool CPVRChannelGroupsContainer::UpdateFromClients(
47 const std::vector<std::shared_ptr<CPVRClient>>& clients, bool bChannelsOnly /* = false */)
48 {
49 std::unique_lock<CCriticalSection> lock(m_critSection);
50 if (m_bIsUpdating)
51 return false;
52 m_bIsUpdating = true;
53 lock.unlock();
54
55 CLog::LogFC(LOGDEBUG, LOGPVR, "Updating {}", bChannelsOnly ? "channels" : "channel groups");
56 bool bReturn = m_groupsTV->UpdateFromClients(clients, bChannelsOnly) &&
57 m_groupsRadio->UpdateFromClients(clients, bChannelsOnly);
58
59 lock.lock();
60 m_bIsUpdating = false;
61 lock.unlock();
62
63 return bReturn;
64 }
65
66 void CPVRChannelGroupsContainer::Unload()
67 {
68 m_groupsRadio->Unload();
69 m_groupsTV->Unload();
70 }
71
72 CPVRChannelGroups* CPVRChannelGroupsContainer::Get(bool bRadio) const
73 {
74 return bRadio ? m_groupsRadio : m_groupsTV;
75 }
76
77 std::shared_ptr<CPVRChannelGroup> CPVRChannelGroupsContainer::GetGroupAll(bool bRadio) const
78 {
79 return Get(bRadio)->GetGroupAll();
80 }
81
82 std::shared_ptr<CPVRChannelGroup> CPVRChannelGroupsContainer::GetByIdFromAll(int iGroupId) const
83 {
84 std::shared_ptr<CPVRChannelGroup> group = m_groupsTV->GetById(iGroupId);
85 if (!group)
86 group = m_groupsRadio->GetById(iGroupId);
87
88 return group;
89 }
90
91 std::shared_ptr<CPVRChannel> CPVRChannelGroupsContainer::GetChannelById(int iChannelId) const
92 {
93 std::shared_ptr<CPVRChannel> channel = m_groupsTV->GetGroupAll()->GetByChannelID(iChannelId);
94 if (!channel)
95 channel = m_groupsRadio->GetGroupAll()->GetByChannelID(iChannelId);
96
97 return channel;
98 }
99
100 std::shared_ptr<CPVRChannel> CPVRChannelGroupsContainer::GetChannelForEpgTag(
101 const std::shared_ptr<const CPVREpgInfoTag>& epgTag) const
102 {
103 if (!epgTag)
104 return {};
105
106 return Get(epgTag->IsRadio())->GetGroupAll()->GetByUniqueID(epgTag->UniqueChannelID(), epgTag->ClientID());
107 }
108
109 std::shared_ptr<CPVRChannelGroupMember> CPVRChannelGroupsContainer::GetChannelGroupMemberByPath(
110 const std::string& strPath) const
111 {
112 const CPVRChannelsPath path(strPath);
113 if (path.IsValid())
114 return Get(path.IsRadio())->GetChannelGroupMemberByPath(path);
115
116 return {};
117 }
118
119 std::shared_ptr<CPVRChannel> CPVRChannelGroupsContainer::GetByPath(const std::string& strPath) const
120 {
121 const std::shared_ptr<const CPVRChannelGroupMember> groupMember =
122 GetChannelGroupMemberByPath(strPath);
123 if (groupMember)
124 return groupMember->Channel();
125
126 return {};
127 }
128
129 std::shared_ptr<CPVRChannel> CPVRChannelGroupsContainer::GetByUniqueID(int iUniqueChannelId, int iClientID) const
130 {
131 std::shared_ptr<CPVRChannel> channel;
132 std::shared_ptr<CPVRChannelGroup> channelgroup = GetGroupAllTV();
133 if (channelgroup)
134 channel = channelgroup->GetByUniqueID(iUniqueChannelId, iClientID);
135
136 if (!channelgroup || !channel)
137 channelgroup = GetGroupAllRadio();
138 if (channelgroup)
139 channel = channelgroup->GetByUniqueID(iUniqueChannelId, iClientID);
140
141 return channel;
142 }
143
144 std::shared_ptr<CPVRChannelGroupMember> CPVRChannelGroupsContainer::
145 GetLastPlayedChannelGroupMember() const
146 {
147 std::shared_ptr<CPVRChannelGroupMember> channelTV =
148 m_groupsTV->GetGroupAll()->GetLastPlayedChannelGroupMember();
149 std::shared_ptr<CPVRChannelGroupMember> channelRadio =
150 m_groupsRadio->GetGroupAll()->GetLastPlayedChannelGroupMember();
151
152 if (!channelTV || (channelRadio &&
153 channelRadio->Channel()->LastWatched() > channelTV->Channel()->LastWatched()))
154 return channelRadio;
155
156 return channelTV;
157 }
158
159 int CPVRChannelGroupsContainer::CleanupCachedImages()
160 {
161 return m_groupsTV->CleanupCachedImages() + m_groupsRadio->CleanupCachedImages();
162 }