| 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 "PVRChannelGroups.h" |
| 10 |
|
| 11 |
#include "ServiceBroker.h" |
| 12 |
#include "pvr/PVRCachedImages.h" |
| 13 |
#include "pvr/PVRDatabase.h" |
| 14 |
#include "pvr/PVRManager.h" |
| 15 |
#include "pvr/addons/PVRClient.h" |
| 16 |
#include "pvr/addons/PVRClientUID.h" |
| 17 |
#include "pvr/addons/PVRClients.h" |
| 18 |
#include "pvr/channels/PVRChannel.h" |
| 19 |
#include "pvr/channels/PVRChannelGroupAllChannels.h" |
| 20 |
#include "pvr/channels/PVRChannelGroupFromClient.h" |
| 21 |
#include "pvr/channels/PVRChannelGroupFromUser.h" |
| 22 |
#include "pvr/channels/PVRChannelGroupMember.h" |
| 23 |
#include "pvr/channels/PVRChannelsPath.h" |
| 24 |
#include "settings/AdvancedSettings.h" |
| 25 |
#include "settings/Settings.h" |
| 26 |
#include "settings/SettingsComponent.h" |
| 27 |
#include "settings/lib/Setting.h" |
| 28 |
#include "utils/StringUtils.h" |
| 29 |
#include "utils/URIUtils.h" |
| 30 |
#include "utils/log.h" |
| 31 |
|
| 32 |
#include <algorithm> |
| 33 |
#include <chrono> |
| 34 |
#include <iterator> |
| 35 |
#include <memory> |
| 36 |
#include <mutex> |
| 37 |
#include <numeric> |
| 38 |
#include <string> |
| 39 |
#include <vector> |
| 40 |
|
| 41 |
using namespace PVR; |
| 42 |
|
| 43 |
CPVRChannelGroups::CPVRChannelGroups(bool bRadio) |
| 44 |
: m_bRadio(bRadio), m_settings({CSettings::SETTING_PVRMANAGER_BACKENDCHANNELGROUPSORDER}) |
| 45 |
{ |
| 46 |
m_settings.RegisterCallback(this); |
| 47 |
} |
| 48 |
|
| 49 |
CPVRChannelGroups::~CPVRChannelGroups() |
| 50 |
{ |
| 51 |
m_settings.UnregisterCallback(this); |
| 52 |
Unload(); |
| 53 |
} |
| 54 |
|
| 55 |
void CPVRChannelGroups::OnSettingChanged(const std::shared_ptr<const CSetting>& setting) |
| 56 |
{ |
| 57 |
if (setting == nullptr) |
| 58 |
{ |
| 59 |
CLog::LogF(LOGERROR, "No setting"); |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
const std::string& settingId = setting->GetId(); |
| 64 |
|
| 65 |
if (settingId == CSettings::SETTING_PVRMANAGER_BACKENDCHANNELGROUPSORDER) |
| 66 |
{ |
| 67 |
SortGroups(); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
void CPVRChannelGroups::Unload() |
| 72 |
{ |
| 73 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 74 |
for (const auto& group : m_groups) |
| 75 |
group->Unload(); |
| 76 |
|
| 77 |
CServiceBroker::GetPVRManager().Events().Unsubscribe(this); |
| 78 |
m_isSubscribed = false; |
| 79 |
|
| 80 |
m_groups.clear(); |
| 81 |
m_allChannelsGroup.reset(); |
| 82 |
m_failedClientsForChannelGroups.clear(); |
| 83 |
} |
| 84 |
|
| 85 |
bool CPVRChannelGroups::UpdateFromClient(const std::shared_ptr<CPVRChannelGroup>& group) |
| 86 |
{ |
| 87 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Got channel group '{}' from client {}.", group->GroupName(), |
| 88 |
group->GetClientID()); |
| 89 |
|
| 90 |
if (!Update(group, true)) |
| 91 |
return false; |
| 92 |
|
| 93 |
SortGroups(); |
| 94 |
return true; |
| 95 |
} |
| 96 |
|
| 97 |
bool CPVRChannelGroups::Update(const std::shared_ptr<CPVRChannelGroup>& group, |
| 98 |
bool bUpdateFromClient /* = false */) |
| 99 |
{ |
| 100 |
if (group->GroupName().empty() && group->GroupID() <= 0) |
| 101 |
return true; |
| 102 |
|
| 103 |
std::shared_ptr<CPVRChannelGroup> updateGroup; |
| 104 |
{ |
| 105 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 106 |
|
| 107 |
// There can be only one all channels group! Make sure we never push a new one! |
| 108 |
if (group->IsChannelsOwner()) |
| 109 |
updateGroup = GetGroupAll(); |
| 110 |
|
| 111 |
// try to find the group by id |
| 112 |
if (!updateGroup && group->GroupID() > 0) |
| 113 |
updateGroup = GetById(group->GroupID()); |
| 114 |
|
| 115 |
// try to find the group by name if we didn't find it yet |
| 116 |
if (!updateGroup) |
| 117 |
{ |
| 118 |
//! @todo If a group was renamed in the backend, no chance to find it here! PVR API should |
| 119 |
//! be extended by a uuid for channel groups which never must change, not even after rename. |
| 120 |
const auto it = std::find_if( |
| 121 |
m_groups.cbegin(), m_groups.cend(), [&group, bUpdateFromClient](const auto& g) { |
| 122 |
return (bUpdateFromClient ? g->ClientGroupName() == group->ClientGroupName() |
| 123 |
: g->GroupName() == group->GroupName()) && |
| 124 |
(g->GetClientID() == PVR_GROUP_CLIENT_ID_UNKNOWN || |
| 125 |
g->GetClientID() == group->GetClientID()); |
| 126 |
}); |
| 127 |
if (it != m_groups.cend()) |
| 128 |
updateGroup = *it; |
| 129 |
} |
| 130 |
|
| 131 |
if (updateGroup) |
| 132 |
{ |
| 133 |
updateGroup->SetGroupID(group->GroupID()); |
| 134 |
updateGroup->SetClientPosition(group->GetClientPosition()); |
| 135 |
updateGroup->SetClientID(group->GetClientID()); |
| 136 |
updateGroup->SetClientGroupName(group->ClientGroupName()); |
| 137 |
|
| 138 |
// don't override properties we only store locally in our PVR database |
| 139 |
if (!bUpdateFromClient) |
| 140 |
{ |
| 141 |
updateGroup->SetGroupName(group->GroupName()); |
| 142 |
updateGroup->SetLastWatched(group->LastWatched()); |
| 143 |
updateGroup->SetHidden(group->IsHidden()); |
| 144 |
updateGroup->SetLastOpened(group->LastOpened()); |
| 145 |
updateGroup->SetPosition(group->GetPosition()); |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
// only update the group name if the user hasn't changed it manually |
| 150 |
if (!updateGroup->IsUserSetName()) |
| 151 |
updateGroup->SetGroupName(group->ClientGroupName()); |
| 152 |
} |
| 153 |
} |
| 154 |
else |
| 155 |
{ |
| 156 |
updateGroup = group; |
| 157 |
m_groups.emplace_back(updateGroup); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
// persist changes |
| 162 |
if (bUpdateFromClient) |
| 163 |
return updateGroup->Persist(); |
| 164 |
|
| 165 |
return true; |
| 166 |
} |
| 167 |
|
| 168 |
int CPVRChannelGroups::GetGroupTypePriority( |
| 169 |
const std::shared_ptr<const CPVRChannelGroup>& group) const |
| 170 |
{ |
| 171 |
switch (group->GroupType()) |
| 172 |
{ |
| 173 |
case PVR_GROUP_TYPE_ALL_CHANNELS: |
| 174 |
return 0; // highest |
| 175 |
case PVR_GROUP_TYPE_LOCAL: |
| 176 |
return 1; |
| 177 |
case PVR_GROUP_TYPE_REMOTE: |
| 178 |
return 2; |
| 179 |
default: |
| 180 |
return 3; |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
int CPVRChannelGroups::GetGroupClientPriority( |
| 185 |
const std::shared_ptr<const CPVRChannelGroup>& group) const |
| 186 |
{ |
| 187 |
// if no priority was set by the user, use the client id to distinguish between the clients |
| 188 |
const int priority = group->GetClientPriority(); |
| 189 |
return priority > 0 ? -priority : -group->GetClientID(); |
| 190 |
} |
| 191 |
|
| 192 |
void CPVRChannelGroups::SortGroupsByBackendOrder() |
| 193 |
{ |
| 194 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 195 |
|
| 196 |
// sort by group type, then by client priority, then by position, last by name |
| 197 |
std::sort(m_groups.begin(), m_groups.end(), [this](const auto& group1, const auto& group2) { |
| 198 |
if (GetGroupTypePriority(group1) == GetGroupTypePriority(group2)) |
| 199 |
{ |
| 200 |
if (GetGroupClientPriority(group1) == GetGroupClientPriority(group2)) |
| 201 |
{ |
| 202 |
if (group1->GetClientPosition() == group2->GetClientPosition()) |
| 203 |
{ |
| 204 |
return group1->GroupName() < group2->GroupName(); |
| 205 |
} |
| 206 |
return group1->GetClientPosition() < group2->GetClientPosition(); |
| 207 |
} |
| 208 |
return GetGroupClientPriority(group1) < GetGroupClientPriority(group2); |
| 209 |
} |
| 210 |
return GetGroupTypePriority(group1) < GetGroupTypePriority(group2); |
| 211 |
}); |
| 212 |
} |
| 213 |
|
| 214 |
void CPVRChannelGroups::SortGroupsByLocalOrder() |
| 215 |
{ |
| 216 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 217 |
|
| 218 |
// sort by group's local position |
| 219 |
std::sort(m_groups.begin(), m_groups.end(), [](const auto& group1, const auto& group2) { |
| 220 |
return group1->GetPosition() < group2->GetPosition(); |
| 221 |
}); |
| 222 |
} |
| 223 |
|
| 224 |
void CPVRChannelGroups::SortGroups() |
| 225 |
{ |
| 226 |
const bool backendOrderSort = |
| 227 |
m_settings.GetBoolValue(CSettings::SETTING_PVRMANAGER_BACKENDCHANNELGROUPSORDER); |
| 228 |
|
| 229 |
if (backendOrderSort) |
| 230 |
SortGroupsByBackendOrder(); |
| 231 |
else |
| 232 |
SortGroupsByLocalOrder(); |
| 233 |
|
| 234 |
CServiceBroker::GetPVRManager().PublishEvent(PVREvent::ChannelGroupsInvalidated); |
| 235 |
} |
| 236 |
|
| 237 |
std::shared_ptr<CPVRChannelGroupMember> CPVRChannelGroups::GetChannelGroupMemberByPath( |
| 238 |
const CPVRChannelsPath& path) const |
| 239 |
{ |
| 240 |
if (path.IsChannel()) |
| 241 |
{ |
| 242 |
const std::shared_ptr<const CPVRChannelGroup> group = |
| 243 |
GetByName(path.GetGroupName(), path.GetGroupClientID()); |
| 244 |
if (group) |
| 245 |
return group->GetByUniqueID( |
| 246 |
{CPVRClientUID(path.GetAddonID(), path.GetInstanceID()).GetUID(), path.GetChannelUID()}); |
| 247 |
} |
| 248 |
|
| 249 |
return {}; |
| 250 |
} |
| 251 |
|
| 252 |
std::vector<std::shared_ptr<CPVRChannelGroupMember>> CPVRChannelGroups::GetMembersAvailableForGroup( |
| 253 |
const std::shared_ptr<const CPVRChannelGroup>& group) |
| 254 |
{ |
| 255 |
std::vector<std::shared_ptr<CPVRChannelGroupMember>> result; |
| 256 |
|
| 257 |
if (group->SupportsMemberAdd()) |
| 258 |
{ |
| 259 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 260 |
|
| 261 |
const auto allGroupMembers = GetGroupAll()->GetMembers(); |
| 262 |
for (const auto& groupMember : allGroupMembers) |
| 263 |
{ |
| 264 |
if (!group->IsGroupMember(groupMember) && |
| 265 |
(group->IsChannelsOwner() || !groupMember->Channel()->IsHidden())) |
| 266 |
result.emplace_back(groupMember); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return result; |
| 271 |
} |
| 272 |
|
| 273 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::GetById(int iGroupId) const |
| 274 |
{ |
| 275 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 276 |
const auto it = std::find_if(m_groups.cbegin(), m_groups.cend(), [iGroupId](const auto& group) { |
| 277 |
return group->GroupID() == iGroupId; |
| 278 |
}); |
| 279 |
return (it != m_groups.cend()) ? (*it) : std::shared_ptr<CPVRChannelGroup>(); |
| 280 |
} |
| 281 |
|
| 282 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::GetGroupByPath( |
| 283 |
const std::string& strInPath) const |
| 284 |
{ |
| 285 |
const CPVRChannelsPath path(strInPath); |
| 286 |
if (path.IsChannelGroup()) |
| 287 |
{ |
| 288 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 289 |
const auto it = std::find_if(m_groups.cbegin(), m_groups.cend(), |
| 290 |
[&path](const auto& group) { return group->GetPath() == path; }); |
| 291 |
if (it != m_groups.cend()) |
| 292 |
return (*it); |
| 293 |
} |
| 294 |
return {}; |
| 295 |
} |
| 296 |
|
| 297 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::GetByName(const std::string& strName, |
| 298 |
int clientID) const |
| 299 |
{ |
| 300 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 301 |
const auto it = |
| 302 |
std::find_if(m_groups.cbegin(), m_groups.cend(), [&strName, clientID](const auto& group) { |
| 303 |
return group->GroupName() == strName && group->GetClientID() == clientID; |
| 304 |
}); |
| 305 |
return (it != m_groups.cend()) ? (*it) : std::shared_ptr<CPVRChannelGroup>(); |
| 306 |
} |
| 307 |
|
| 308 |
bool CPVRChannelGroups::HasValidDataForClients( |
| 309 |
const std::vector<std::shared_ptr<CPVRClient>>& clients) const |
| 310 |
{ |
| 311 |
return m_failedClientsForChannelGroups.empty() || |
| 312 |
std::none_of(clients.cbegin(), clients.cend(), |
| 313 |
[this](const std::shared_ptr<const CPVRClient>& client) { |
| 314 |
return std::find(m_failedClientsForChannelGroups.cbegin(), |
| 315 |
m_failedClientsForChannelGroups.cend(), |
| 316 |
client->GetID()) != m_failedClientsForChannelGroups.cend(); |
| 317 |
}); |
| 318 |
} |
| 319 |
|
| 320 |
bool CPVRChannelGroups::UpdateFromClients(const std::vector<std::shared_ptr<CPVRClient>>& clients, |
| 321 |
bool bChannelsOnly /* = false */) |
| 322 |
{ |
| 323 |
bool bReturn = true; |
| 324 |
|
| 325 |
// sync groups |
| 326 |
if (!bChannelsOnly) |
| 327 |
{ |
| 328 |
// get channel groups from the clients |
| 329 |
CServiceBroker::GetPVRManager().Clients()->GetChannelGroups(clients, this, |
| 330 |
m_failedClientsForChannelGroups); |
| 331 |
} |
| 332 |
|
| 333 |
// sync channels in groups |
| 334 |
std::vector<std::shared_ptr<CPVRChannelGroup>> groups; |
| 335 |
{ |
| 336 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 337 |
groups = m_groups; |
| 338 |
} |
| 339 |
|
| 340 |
for (const auto& group : groups) |
| 341 |
{ |
| 342 |
if (!bChannelsOnly || group->IsChannelsOwner()) |
| 343 |
{ |
| 344 |
if (!group->UpdateFromClients(clients)) |
| 345 |
{ |
| 346 |
CLog::LogFC(LOGERROR, LOGPVR, "Failed to update channel group '{}'", group->GroupName()); |
| 347 |
bReturn = false; |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
if (bReturn && group->IsChannelsOwner() && |
| 352 |
CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_bPVRChannelIconsAutoScan) |
| 353 |
{ |
| 354 |
CServiceBroker::GetPVRManager().TriggerSearchMissingChannelIcons(group); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if (bChannelsOnly) |
| 359 |
{ |
| 360 |
// changes in the all channels group may require resorting/renumbering of other groups. |
| 361 |
// if we updated all groups this already has been done while updating the single groups. |
| 362 |
UpdateChannelNumbersFromAllChannelsGroup(); |
| 363 |
} |
| 364 |
|
| 365 |
CServiceBroker::GetPVRManager().PublishEvent(PVREvent::ChannelGroupsInvalidated); |
| 366 |
|
| 367 |
// persist changes |
| 368 |
return PersistAll() && bReturn; |
| 369 |
} |
| 370 |
|
| 371 |
bool CPVRChannelGroups::UpdateChannelNumbersFromAllChannelsGroup() |
| 372 |
{ |
| 373 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 374 |
return std::accumulate( |
| 375 |
m_groups.cbegin(), m_groups.cend(), false, [](bool changed, const auto& group) { |
| 376 |
return group->UpdateChannelNumbersFromAllChannelsGroup() ? true : changed; |
| 377 |
}); |
| 378 |
} |
| 379 |
|
| 380 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::CreateChannelGroup( |
| 381 |
int iType, const CPVRChannelsPath& path) |
| 382 |
{ |
| 383 |
if (iType == PVR_GROUP_TYPE_ALL_CHANNELS) |
| 384 |
return std::make_shared<CPVRChannelGroupAllChannels>(path); |
| 385 |
else if (iType == PVR_GROUP_TYPE_LOCAL) |
| 386 |
return std::make_shared<CPVRChannelGroupFromUser>(path, GetGroupAll()); |
| 387 |
else |
| 388 |
return std::make_shared<CPVRChannelGroupFromClient>(path, GetGroupAll()); |
| 389 |
} |
| 390 |
|
| 391 |
bool CPVRChannelGroups::LoadFromDatabase(const std::vector<std::shared_ptr<CPVRClient>>& clients) |
| 392 |
{ |
| 393 |
const std::shared_ptr<const CPVRDatabase> database( |
| 394 |
CServiceBroker::GetPVRManager().GetTVDatabase()); |
| 395 |
if (!database) |
| 396 |
return false; |
| 397 |
|
| 398 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 399 |
|
| 400 |
// Ensure we have an all channels group. It is important that the all channels group is |
| 401 |
// created before loading contents from database. |
| 402 |
if (m_groups.empty()) |
| 403 |
{ |
| 404 |
const auto internalGroup = std::make_shared<CPVRChannelGroupAllChannels>(m_bRadio); |
| 405 |
m_groups.emplace_back(internalGroup); |
| 406 |
m_allChannelsGroup = internalGroup; |
| 407 |
} |
| 408 |
|
| 409 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Loading all {} channel groups and members", |
| 410 |
m_bRadio ? "radio" : "TV"); |
| 411 |
|
| 412 |
// load all channels from the database |
| 413 |
std::map<std::pair<int, int>, std::shared_ptr<CPVRChannel>> channels; |
| 414 |
database->Get(m_bRadio, clients, channels); |
| 415 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Fetched {} {} channels from the database", channels.size(), |
| 416 |
m_bRadio ? "radio" : "TV"); |
| 417 |
|
| 418 |
// load local groups from the database |
| 419 |
int iLoaded = database->GetLocalGroups(*this); |
| 420 |
|
| 421 |
// load backend-supplied groups from the database |
| 422 |
iLoaded += database->Get(*this, clients); |
| 423 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Fetched {} {} groups from the database", iLoaded, |
| 424 |
m_bRadio ? "radio" : "TV"); |
| 425 |
|
| 426 |
// load all group members from the database |
| 427 |
for (const auto& group : m_groups) |
| 428 |
{ |
| 429 |
if (!group->LoadFromDatabase(channels, clients)) |
| 430 |
{ |
| 431 |
CLog::LogFC(LOGERROR, LOGPVR, |
| 432 |
"Failed to load members of {} channel group '{}' from the database", |
| 433 |
m_bRadio ? "radio" : "TV", group->GroupName()); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
// Hide empty groups |
| 438 |
for (auto it = m_groups.begin(); it != m_groups.end();) |
| 439 |
{ |
| 440 |
if ((*it)->Size() == 0 && !(*it)->IsChannelsOwner()) |
| 441 |
it = m_groups.erase(it); |
| 442 |
else |
| 443 |
++it; |
| 444 |
} |
| 445 |
|
| 446 |
// Register for client priority changes |
| 447 |
if (!m_isSubscribed) |
| 448 |
{ |
| 449 |
CServiceBroker::GetPVRManager().Events().Subscribe(this, &CPVRChannelGroups::OnPVRManagerEvent); |
| 450 |
m_isSubscribed = true; |
| 451 |
} |
| 452 |
|
| 453 |
SortGroups(); |
| 454 |
return true; |
| 455 |
} |
| 456 |
|
| 457 |
bool CPVRChannelGroups::PersistAll() |
| 458 |
{ |
| 459 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Persisting all channel group changes"); |
| 460 |
|
| 461 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 462 |
return std::accumulate( |
| 463 |
m_groups.cbegin(), m_groups.cend(), true, |
| 464 |
[](bool success, const auto& group) { return !group->Persist() ? false : success; }); |
| 465 |
} |
| 466 |
|
| 467 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::GetGroupAll() const |
| 468 |
{ |
| 469 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 470 |
return m_allChannelsGroup; |
| 471 |
} |
| 472 |
|
| 473 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::GetLastGroup() const |
| 474 |
{ |
| 475 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 476 |
if (!m_groups.empty()) |
| 477 |
return m_groups.back(); |
| 478 |
|
| 479 |
return std::shared_ptr<CPVRChannelGroup>(); |
| 480 |
} |
| 481 |
|
| 482 |
GroupMemberPair CPVRChannelGroups::GetLastAndPreviousToLastPlayedChannelGroupMember() const |
| 483 |
{ |
| 484 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 485 |
if (m_groups.empty()) |
| 486 |
return {}; |
| 487 |
|
| 488 |
auto groups = m_groups; |
| 489 |
lock.unlock(); |
| 490 |
|
| 491 |
std::sort(groups.begin(), groups.end(), |
| 492 |
[](const auto& a, const auto& b) { return a->LastWatched() > b->LastWatched(); }); |
| 493 |
|
| 494 |
// Last is always 'first' of last played group. |
| 495 |
const GroupMemberPair members = groups[0]->GetLastAndPreviousToLastPlayedChannelGroupMember(); |
| 496 |
std::shared_ptr<CPVRChannelGroupMember> last = members.first; |
| 497 |
|
| 498 |
// Previous to last is either 'second' of first group or 'first' of second group. |
| 499 |
std::shared_ptr<CPVRChannelGroupMember> previousToLast = members.second; |
| 500 |
if (groups.size() > 1 && groups[0]->LastWatched() && groups[1]->LastWatched() && members.second && |
| 501 |
members.second->Channel()->LastWatched()) |
| 502 |
{ |
| 503 |
if (groups[1]->LastWatched() >= members.second->Channel()->LastWatched()) |
| 504 |
{ |
| 505 |
const GroupMemberPair membersPreviousToLastPlayedGroup = |
| 506 |
groups[1]->GetLastAndPreviousToLastPlayedChannelGroupMember(); |
| 507 |
previousToLast = membersPreviousToLastPlayedGroup.first; |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
return {last, previousToLast}; |
| 512 |
} |
| 513 |
|
| 514 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::GetLastOpenedGroup() const |
| 515 |
{ |
| 516 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 517 |
return std::accumulate(m_groups.cbegin(), m_groups.cend(), std::shared_ptr<CPVRChannelGroup>{}, |
| 518 |
[](const std::shared_ptr<CPVRChannelGroup>& last, |
| 519 |
const std::shared_ptr<CPVRChannelGroup>& group) { |
| 520 |
return group->LastOpened() > 0 && |
| 521 |
(!last || group->LastOpened() > last->LastOpened()) |
| 522 |
? group |
| 523 |
: last; |
| 524 |
}); |
| 525 |
} |
| 526 |
|
| 527 |
std::vector<std::shared_ptr<CPVRChannelGroup>> CPVRChannelGroups::GetMembers( |
| 528 |
bool bExcludeHidden /* = false */) const |
| 529 |
{ |
| 530 |
std::vector<std::shared_ptr<CPVRChannelGroup>> groups; |
| 531 |
|
| 532 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 533 |
std::copy_if( |
| 534 |
m_groups.cbegin(), m_groups.cend(), std::back_inserter(groups), |
| 535 |
[bExcludeHidden](const auto& group) { return (!bExcludeHidden || !group->IsHidden()); }); |
| 536 |
return groups; |
| 537 |
} |
| 538 |
|
| 539 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::GetPreviousGroup( |
| 540 |
const CPVRChannelGroup& group) const |
| 541 |
{ |
| 542 |
{ |
| 543 |
bool bReturnNext = false; |
| 544 |
|
| 545 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 546 |
for (std::vector<std::shared_ptr<CPVRChannelGroup>>::const_reverse_iterator it = |
| 547 |
m_groups.rbegin(); |
| 548 |
it != m_groups.rend(); ++it) |
| 549 |
{ |
| 550 |
// return this entry |
| 551 |
if (bReturnNext && !(*it)->IsHidden()) |
| 552 |
return *it; |
| 553 |
|
| 554 |
// return the next entry |
| 555 |
if ((*it)->GroupID() == group.GroupID()) |
| 556 |
bReturnNext = true; |
| 557 |
} |
| 558 |
|
| 559 |
// no match return last visible group |
| 560 |
for (std::vector<std::shared_ptr<CPVRChannelGroup>>::const_reverse_iterator it = |
| 561 |
m_groups.rbegin(); |
| 562 |
it != m_groups.rend(); ++it) |
| 563 |
{ |
| 564 |
if (!(*it)->IsHidden()) |
| 565 |
return *it; |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
// no match |
| 570 |
return GetLastGroup(); |
| 571 |
} |
| 572 |
|
| 573 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::GetNextGroup( |
| 574 |
const CPVRChannelGroup& group) const |
| 575 |
{ |
| 576 |
{ |
| 577 |
bool bReturnNext = false; |
| 578 |
|
| 579 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 580 |
for (std::vector<std::shared_ptr<CPVRChannelGroup>>::const_iterator it = m_groups.begin(); |
| 581 |
it != m_groups.end(); ++it) |
| 582 |
{ |
| 583 |
// return this entry |
| 584 |
if (bReturnNext && !(*it)->IsHidden()) |
| 585 |
return *it; |
| 586 |
|
| 587 |
// return the next entry |
| 588 |
if ((*it)->GroupID() == group.GroupID()) |
| 589 |
bReturnNext = true; |
| 590 |
} |
| 591 |
|
| 592 |
// no match return first visible group |
| 593 |
for (std::vector<std::shared_ptr<CPVRChannelGroup>>::const_iterator it = m_groups.begin(); |
| 594 |
it != m_groups.end(); ++it) |
| 595 |
{ |
| 596 |
if (!(*it)->IsHidden()) |
| 597 |
return *it; |
| 598 |
} |
| 599 |
} |
| 600 |
|
| 601 |
// no match |
| 602 |
return GetFirstGroup(); |
| 603 |
} |
| 604 |
|
| 605 |
std::shared_ptr<CPVRChannelGroup> CPVRChannelGroups::AddGroup(const std::string& strName) |
| 606 |
{ |
| 607 |
bool bPersist(false); |
| 608 |
std::shared_ptr<CPVRChannelGroup> group; |
| 609 |
|
| 610 |
{ |
| 611 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 612 |
|
| 613 |
// check if there's another local group with the same name already |
| 614 |
group = GetByName(strName, PVR_GROUP_CLIENT_ID_LOCAL); |
| 615 |
if (!group) |
| 616 |
{ |
| 617 |
// create a new local group |
| 618 |
group = CreateChannelGroup(PVR_GROUP_TYPE_LOCAL, |
| 619 |
CPVRChannelsPath(m_bRadio, strName, PVR_GROUP_CLIENT_ID_LOCAL)); |
| 620 |
|
| 621 |
m_groups.emplace_back(group); |
| 622 |
SortGroups(); |
| 623 |
bPersist = true; |
| 624 |
|
| 625 |
CServiceBroker::GetPVRManager().PublishEvent(PVREvent::ChannelGroupsInvalidated); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
// persist in the db if a new group was added |
| 630 |
if (bPersist) |
| 631 |
group->Persist(); |
| 632 |
|
| 633 |
return group; |
| 634 |
} |
| 635 |
|
| 636 |
bool CPVRChannelGroups::DeleteGroup(const std::shared_ptr<CPVRChannelGroup>& group) |
| 637 |
{ |
| 638 |
if (!group->SupportsDelete()) |
| 639 |
{ |
| 640 |
CLog::LogF(LOGERROR, "Channel group {} cannot be deleted", group->GroupName()); |
| 641 |
return false; |
| 642 |
} |
| 643 |
|
| 644 |
bool bFound(false); |
| 645 |
|
| 646 |
// delete the group in this container |
| 647 |
{ |
| 648 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 649 |
for (auto it = m_groups.begin(); it != m_groups.end(); ++it) |
| 650 |
{ |
| 651 |
if (*it == group || (group->GroupID() > 0 && (*it)->GroupID() == group->GroupID())) |
| 652 |
{ |
| 653 |
m_groups.erase(it); |
| 654 |
bFound = true; |
| 655 |
break; |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
if (bFound && group->GroupID() > 0) |
| 661 |
{ |
| 662 |
// delete the group from the database |
| 663 |
group->Delete(); |
| 664 |
CServiceBroker::GetPVRManager().PublishEvent(PVREvent::ChannelGroupsInvalidated); |
| 665 |
} |
| 666 |
return bFound; |
| 667 |
} |
| 668 |
|
| 669 |
bool CPVRChannelGroups::HideGroup(const std::shared_ptr<CPVRChannelGroup>& group, bool bHide) |
| 670 |
{ |
| 671 |
bool bReturn = false; |
| 672 |
|
| 673 |
if (group) |
| 674 |
{ |
| 675 |
if (group->SetHidden(bHide)) |
| 676 |
{ |
| 677 |
// state changed |
| 678 |
CServiceBroker::GetPVRManager().PublishEvent(PVREvent::ChannelGroupsInvalidated); |
| 679 |
} |
| 680 |
bReturn = true; |
| 681 |
} |
| 682 |
return bReturn; |
| 683 |
} |
| 684 |
|
| 685 |
int CPVRChannelGroups::CleanupCachedImages() |
| 686 |
{ |
| 687 |
int iCleanedImages = 0; |
| 688 |
|
| 689 |
// cleanup channels |
| 690 |
iCleanedImages += GetGroupAll()->CleanupCachedImages(); |
| 691 |
|
| 692 |
// cleanup groups |
| 693 |
std::vector<std::string> urlsToCheck; |
| 694 |
{ |
| 695 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 696 |
std::transform(m_groups.cbegin(), m_groups.cend(), std::back_inserter(urlsToCheck), |
| 697 |
[](const auto& group) { return group->GetPath(); }); |
| 698 |
} |
| 699 |
|
| 700 |
// kodi-generated thumbnail (see CPVRThumbLoader) |
| 701 |
const std::string path = StringUtils::Format("pvr://channels/{}/", IsRadio() ? "radio" : "tv"); |
| 702 |
iCleanedImages += CPVRCachedImages::Cleanup({{"pvr", path}}, urlsToCheck, true); |
| 703 |
|
| 704 |
return iCleanedImages; |
| 705 |
} |
| 706 |
|
| 707 |
void CPVRChannelGroups::OnPVRManagerEvent(const PVR::PVREvent& event) |
| 708 |
{ |
| 709 |
if (event == PVREvent::ClientsPrioritiesInvalidated) |
| 710 |
{ |
| 711 |
// Update group client priorities |
| 712 |
std::vector<std::shared_ptr<CPVRChannelGroup>> groups; |
| 713 |
{ |
| 714 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 715 |
groups = m_groups; |
| 716 |
} |
| 717 |
|
| 718 |
for (const auto& group : groups) |
| 719 |
{ |
| 720 |
group->UpdateClientPriorities(); |
| 721 |
} |
| 722 |
|
| 723 |
SortGroups(); |
| 724 |
} |
| 725 |
} |