Debian Codegraph @

xbmc/pvr/channels/PVRChannelGroupAllChannels.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 "PVRChannelGroupAllChannels.h"
10
11 #include "ServiceBroker.h"
12 #include "guilib/LocalizeStrings.h"
13 #include "pvr/PVRDatabase.h"
14 #include "pvr/PVRManager.h"
15 #include "pvr/addons/PVRClients.h"
16 #include "pvr/channels/PVRChannel.h"
17 #include "pvr/channels/PVRChannelGroupMember.h"
18 #include "pvr/epg/EpgContainer.h"
19 #include "utils/Variant.h"
20 #include "utils/log.h"
21
22 #include <algorithm>
23 #include <iterator>
24 #include <mutex>
25 #include <string>
26 #include <utility>
27 #include <vector>
28
29 using namespace PVR;
30
31 CPVRChannelGroupAllChannels::CPVRChannelGroupAllChannels(bool bRadio)
32 : CPVRChannelGroupFromUser(
33 CPVRChannelsPath(bRadio, g_localizeStrings.Get(19287), PVR_GROUP_CLIENT_ID_LOCAL), nullptr)
34 {
35 }
36
37 CPVRChannelGroupAllChannels::CPVRChannelGroupAllChannels(const CPVRChannelsPath& path)
38 : CPVRChannelGroupFromUser(path, nullptr)
39 {
40 }
41
42 CPVRChannelGroupAllChannels::~CPVRChannelGroupAllChannels()
43 {
44 }
45
46 void CPVRChannelGroupAllChannels::CheckGroupName()
47 {
48 //! @todo major design flaw to fix: channel and group URLs must not contain the group name!
49
50 // Ensure the group name is still correct, or channels may fail to load after a locale change
51 if (!IsUserSetName())
52 SetGroupName(g_localizeStrings.Get(19287));
53 }
54
55 bool CPVRChannelGroupAllChannels::UpdateFromClients(
56 const std::vector<std::shared_ptr<CPVRClient>>& clients)
57 {
58 // get the channels from the given clients
59 std::vector<std::shared_ptr<CPVRChannel>> channels;
60 CServiceBroker::GetPVRManager().Clients()->GetChannels(clients, IsRadio(), channels,
61 m_failedClients);
62
63 // create group members for the channels
64 std::vector<std::shared_ptr<CPVRChannelGroupMember>> groupMembers;
65 std::transform(channels.cbegin(), channels.cend(), std::back_inserter(groupMembers),
66 [this](const auto& channel) {
67 return std::make_shared<CPVRChannelGroupMember>(GroupID(), GroupName(),
68 GetClientID(), channel);
69 });
70
71 return UpdateGroupEntries(groupMembers);
72 }
73
74 std::vector<std::shared_ptr<CPVRChannelGroupMember>> CPVRChannelGroupAllChannels::
75 RemoveDeletedGroupMembers(
76 const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers)
77 {
78 std::vector<std::shared_ptr<CPVRChannelGroupMember>> removedMembers =
79 CPVRChannelGroup::RemoveDeletedGroupMembers(groupMembers);
80 if (!removedMembers.empty())
81 {
82 const std::shared_ptr<CPVRDatabase> database = CServiceBroker::GetPVRManager().GetTVDatabase();
83 if (!database)
84 {
85 CLog::LogF(LOGERROR, "No TV database");
86 }
87 else
88 {
89 std::vector<std::shared_ptr<CPVREpg>> epgsToRemove;
90 for (const auto& member : removedMembers)
91 {
92 const auto channel = member->Channel();
93 const auto epg = channel->GetEPG();
94 if (epg)
95 epgsToRemove.emplace_back(epg);
96
97 // Note: We need to obtain a lock for every channel instance before we can lock
98 // the TV db. This order is important. Otherwise deadlocks may occur.
99 channel->Lock();
100 }
101
102 // Note: We must lock the db the whole time, otherwise races may occur.
103 database->Lock();
104
105 bool commitPending = false;
106
107 for (const auto& member : removedMembers)
108 {
109 // since channel was not found in the all channels group, it was deleted from the backend
110
111 const auto channel = member->Channel();
112 commitPending |= channel->QueueDelete();
113 channel->Unlock();
114
115 size_t queryCount = database->GetDeleteQueriesCount();
116 if (queryCount > CHANNEL_COMMIT_QUERY_COUNT_LIMIT)
117 database->CommitDeleteQueries();
118 }
119
120 if (commitPending)
121 database->CommitDeleteQueries();
122
123 database->Unlock();
124
125 // delete the EPG data for the removed channels
126 CServiceBroker::GetPVRManager().EpgContainer().QueueDeleteEpgs(epgsToRemove);
127 }
128 }
129 return removedMembers;
130 }
131
132 bool CPVRChannelGroupAllChannels::AppendToGroup(
133 const std::shared_ptr<const CPVRChannelGroupMember>& groupMember)
134 {
135 if (IsGroupMember(groupMember))
136 return false;
137
138 groupMember->Channel()->SetHidden(false, true);
139
140 SortAndRenumber();
141 return true;
142 }
143
144 bool CPVRChannelGroupAllChannels::RemoveFromGroup(
145 const std::shared_ptr<const CPVRChannelGroupMember>& groupMember)
146 {
147 if (!IsGroupMember(groupMember))
148 return false;
149
150 groupMember->Channel()->SetHidden(true, true);
151
152 SortAndRenumber();
153 return true;
154 }
155
156 bool CPVRChannelGroupAllChannels::IsGroupMember(
157 const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) const
158 {
159 return !groupMember->Channel()->IsHidden();
160 }