| 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 |
//! @todo use Observable here, so we can use event driven operations later |
| 10 |
|
| 11 |
#include "PVRChannelGroup.h" |
| 12 |
|
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "addons/kodi-dev-kit/include/kodi/c-api/addon-instance/pvr/pvr_channel_groups.h" |
| 15 |
#include "pvr/PVRCachedImages.h" |
| 16 |
#include "pvr/PVRDatabase.h" |
| 17 |
#include "pvr/PVRManager.h" |
| 18 |
#include "pvr/addons/PVRClient.h" |
| 19 |
#include "pvr/addons/PVRClients.h" |
| 20 |
#include "pvr/channels/PVRChannel.h" |
| 21 |
#include "pvr/channels/PVRChannelGroupMember.h" |
| 22 |
#include "pvr/channels/PVRChannelsPath.h" |
| 23 |
#include "pvr/epg/Epg.h" |
| 24 |
#include "pvr/epg/EpgChannelData.h" |
| 25 |
#include "pvr/epg/EpgInfoTag.h" |
| 26 |
#include "utils/StringUtils.h" |
| 27 |
#include "utils/log.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <iterator> |
| 31 |
#include <memory> |
| 32 |
#include <mutex> |
| 33 |
#include <numeric> |
| 34 |
#include <string> |
| 35 |
#include <utility> |
| 36 |
#include <vector> |
| 37 |
|
| 38 |
using namespace PVR; |
| 39 |
|
| 40 |
CPVRChannelGroup::CPVRChannelGroup(const CPVRChannelsPath& path, |
| 41 |
const std::shared_ptr<const CPVRChannelGroup>& allChannelsGroup) |
| 42 |
: m_allChannelsGroup(allChannelsGroup), m_path(path) |
| 43 |
{ |
| 44 |
GetSettings()->RegisterCallback(this); |
| 45 |
} |
| 46 |
|
| 47 |
CPVRChannelGroup::~CPVRChannelGroup() |
| 48 |
{ |
| 49 |
GetSettings()->UnregisterCallback(this); |
| 50 |
} |
| 51 |
|
| 52 |
bool CPVRChannelGroup::operator==(const CPVRChannelGroup& right) const |
| 53 |
{ |
| 54 |
return (GroupType() == right.GroupType() && m_iGroupId == right.m_iGroupId && |
| 55 |
m_iPosition == right.m_iPosition && m_path == right.m_path && |
| 56 |
m_clientPriority == right.m_clientPriority && m_isUserSetName == right.m_isUserSetName && |
| 57 |
m_clientGroupName == right.m_clientGroupName && |
| 58 |
m_iClientPosition == right.m_iClientPosition); |
| 59 |
} |
| 60 |
|
| 61 |
bool CPVRChannelGroup::operator!=(const CPVRChannelGroup& right) const |
| 62 |
{ |
| 63 |
return !(*this == right); |
| 64 |
} |
| 65 |
|
| 66 |
void CPVRChannelGroup::FillAddonData(PVR_CHANNEL_GROUP& group) const |
| 67 |
{ |
| 68 |
group = {}; |
| 69 |
group.bIsRadio = IsRadio(); |
| 70 |
strncpy(group.strGroupName, ClientGroupName().c_str(), sizeof(group.strGroupName) - 1); |
| 71 |
group.iPosition = GetClientPosition(); |
| 72 |
} |
| 73 |
|
| 74 |
CCriticalSection CPVRChannelGroup::m_settingsSingletonCritSection; |
| 75 |
std::weak_ptr<CPVRChannelGroupSettings> CPVRChannelGroup::m_settingsSingleton; |
| 76 |
|
| 77 |
std::shared_ptr<CPVRChannelGroupSettings> CPVRChannelGroup::GetSettings() const |
| 78 |
{ |
| 79 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 80 |
if (!m_settings) |
| 81 |
{ |
| 82 |
std::unique_lock<CCriticalSection> singletonLock(m_settingsSingletonCritSection); |
| 83 |
const std::shared_ptr<CPVRChannelGroupSettings> settings = m_settingsSingleton.lock(); |
| 84 |
if (settings) |
| 85 |
{ |
| 86 |
m_settings = settings; |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
m_settings = std::make_shared<CPVRChannelGroupSettings>(); |
| 91 |
m_settingsSingleton = m_settings; |
| 92 |
} |
| 93 |
} |
| 94 |
return m_settings; |
| 95 |
} |
| 96 |
|
| 97 |
bool CPVRChannelGroup::LoadFromDatabase( |
| 98 |
const std::map<std::pair<int, int>, std::shared_ptr<CPVRChannel>>& channels, |
| 99 |
const std::vector<std::shared_ptr<CPVRClient>>& clients) |
| 100 |
{ |
| 101 |
const int iChannelCount = m_iGroupId > 0 ? LoadFromDatabase(clients) : 0; |
| 102 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Fetched {} {} group members from the database for group '{}'", |
| 103 |
iChannelCount, IsRadio() ? "radio" : "TV", GroupName()); |
| 104 |
|
| 105 |
for (const auto& groupMember : m_members) |
| 106 |
{ |
| 107 |
groupMember.second->SetGroupName(GroupName()); |
| 108 |
|
| 109 |
auto channel = groupMember.second->Channel(); |
| 110 |
if (channel) |
| 111 |
continue; |
| 112 |
|
| 113 |
auto channelIt = channels.find(groupMember.first); |
| 114 |
if (channelIt == channels.end()) |
| 115 |
{ |
| 116 |
CLog::Log(LOGERROR, "Cannot find group member '{},{}' in channels!", groupMember.first.first, |
| 117 |
groupMember.first.second); |
| 118 |
// No workaround here, please. We need to find and fix the root cause of this case! |
| 119 |
} |
| 120 |
|
| 121 |
channel = (*channelIt).second; |
| 122 |
groupMember.second->SetChannel(channel); |
| 123 |
|
| 124 |
// Create EPG for loaded channel |
| 125 |
channel->CreateEPG(); |
| 126 |
} |
| 127 |
|
| 128 |
m_bLoaded = true; |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
void CPVRChannelGroup::Unload() |
| 133 |
{ |
| 134 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 135 |
m_sortedMembers.clear(); |
| 136 |
m_members.clear(); |
| 137 |
m_failedClients.clear(); |
| 138 |
} |
| 139 |
|
| 140 |
int CPVRChannelGroup::GetClientID() const |
| 141 |
{ |
| 142 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 143 |
return m_path.GetGroupClientID(); |
| 144 |
} |
| 145 |
|
| 146 |
void CPVRChannelGroup::SetClientID(int clientID) |
| 147 |
{ |
| 148 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 149 |
if (m_path.GetGroupClientID() != clientID) |
| 150 |
{ |
| 151 |
m_path = CPVRChannelsPath(m_path.IsRadio(), m_path.GetGroupName(), clientID); |
| 152 |
if (m_bLoaded) |
| 153 |
{ |
| 154 |
m_bChanged = true; |
| 155 |
Persist(); //! @todo why must we persist immediately? |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
const CPVRChannelsPath& CPVRChannelGroup::GetPath() const |
| 161 |
{ |
| 162 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 163 |
return m_path; |
| 164 |
} |
| 165 |
|
| 166 |
void CPVRChannelGroup::SetPath(const CPVRChannelsPath& path) |
| 167 |
{ |
| 168 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 169 |
if (m_path != path) |
| 170 |
{ |
| 171 |
m_path = path; |
| 172 |
if (m_bLoaded) |
| 173 |
{ |
| 174 |
// note: path contains both the radio flag and the group name, which are stored in the db |
| 175 |
m_bChanged = true; |
| 176 |
Persist(); //! @todo why must we persist immediately? |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/********** sort methods **********/ |
| 182 |
|
| 183 |
struct sortByClientChannelNumber |
| 184 |
{ |
| 185 |
bool operator()(const std::shared_ptr<const CPVRChannelGroupMember>& channel1, |
| 186 |
const std::shared_ptr<const CPVRChannelGroupMember>& channel2) const |
| 187 |
{ |
| 188 |
if (channel1->ClientPriority() == channel2->ClientPriority()) |
| 189 |
{ |
| 190 |
if (channel1->ClientChannelNumber() == channel2->ClientChannelNumber()) |
| 191 |
return channel1->Channel()->ChannelName() < channel2->Channel()->ChannelName(); |
| 192 |
|
| 193 |
return channel1->ClientChannelNumber() < channel2->ClientChannelNumber(); |
| 194 |
} |
| 195 |
return channel1->ClientPriority() > channel2->ClientPriority(); |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
struct sortByChannelNumber |
| 200 |
{ |
| 201 |
bool operator()(const std::shared_ptr<const CPVRChannelGroupMember>& channel1, |
| 202 |
const std::shared_ptr<const CPVRChannelGroupMember>& channel2) const |
| 203 |
{ |
| 204 |
return channel1->ChannelNumber() < channel2->ChannelNumber(); |
| 205 |
} |
| 206 |
}; |
| 207 |
|
| 208 |
void CPVRChannelGroup::Sort() |
| 209 |
{ |
| 210 |
if (GetSettings()->UseBackendChannelOrder()) |
| 211 |
SortByClientChannelNumber(); |
| 212 |
else |
| 213 |
SortByChannelNumber(); |
| 214 |
} |
| 215 |
|
| 216 |
bool CPVRChannelGroup::SortAndRenumber() |
| 217 |
{ |
| 218 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 219 |
Sort(); |
| 220 |
return Renumber(); |
| 221 |
} |
| 222 |
|
| 223 |
void CPVRChannelGroup::SortByClientChannelNumber() |
| 224 |
{ |
| 225 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 226 |
std::sort(m_sortedMembers.begin(), m_sortedMembers.end(), sortByClientChannelNumber()); |
| 227 |
} |
| 228 |
|
| 229 |
void CPVRChannelGroup::SortByChannelNumber() |
| 230 |
{ |
| 231 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 232 |
std::sort(m_sortedMembers.begin(), m_sortedMembers.end(), sortByChannelNumber()); |
| 233 |
} |
| 234 |
|
| 235 |
void CPVRChannelGroup::UpdateClientPriorities() |
| 236 |
{ |
| 237 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 238 |
|
| 239 |
// Update own priority value |
| 240 |
m_clientPriority.reset(); |
| 241 |
GetClientPriority(); |
| 242 |
|
| 243 |
// Update group member's value and re-sort members |
| 244 |
if (UpdateMembersClientPriority()) |
| 245 |
SortAndRenumber(); |
| 246 |
} |
| 247 |
|
| 248 |
bool CPVRChannelGroup::UpdateMembersClientPriority() |
| 249 |
{ |
| 250 |
const std::shared_ptr<const CPVRClients> clients = CServiceBroker::GetPVRManager().Clients(); |
| 251 |
bool bChanged = false; |
| 252 |
|
| 253 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 254 |
|
| 255 |
const bool bUseBackendChannelOrder = GetSettings()->UseBackendChannelOrder(); |
| 256 |
for (auto& member : m_sortedMembers) |
| 257 |
{ |
| 258 |
int iNewPriority = 0; |
| 259 |
|
| 260 |
if (bUseBackendChannelOrder) |
| 261 |
{ |
| 262 |
const std::shared_ptr<const CPVRClient> client = |
| 263 |
clients->GetCreatedClient(member->Channel()->ClientID()); |
| 264 |
if (!client) |
| 265 |
continue; |
| 266 |
|
| 267 |
iNewPriority = client->GetPriority(); |
| 268 |
} |
| 269 |
else |
| 270 |
{ |
| 271 |
iNewPriority = 0; |
| 272 |
} |
| 273 |
|
| 274 |
bChanged |= (member->ClientPriority() != iNewPriority); |
| 275 |
member->SetClientPriority(iNewPriority); |
| 276 |
} |
| 277 |
|
| 278 |
return bChanged; |
| 279 |
} |
| 280 |
|
| 281 |
/********** getters **********/ |
| 282 |
|
| 283 |
std::shared_ptr<CPVRChannelGroupMember> CPVRChannelGroup::GetByUniqueID( |
| 284 |
const std::pair<int, int>& id) const |
| 285 |
{ |
| 286 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 287 |
const auto it = m_members.find(id); |
| 288 |
return it != m_members.end() ? it->second : std::shared_ptr<CPVRChannelGroupMember>(); |
| 289 |
} |
| 290 |
|
| 291 |
std::shared_ptr<CPVRChannel> CPVRChannelGroup::GetByUniqueID(int iUniqueChannelId, |
| 292 |
int iClientID) const |
| 293 |
{ |
| 294 |
const std::shared_ptr<const CPVRChannelGroupMember> groupMember = |
| 295 |
GetByUniqueID(std::make_pair(iClientID, iUniqueChannelId)); |
| 296 |
return groupMember ? groupMember->Channel() : std::shared_ptr<CPVRChannel>(); |
| 297 |
} |
| 298 |
|
| 299 |
std::shared_ptr<CPVRChannel> CPVRChannelGroup::GetByChannelID(int iChannelID) const |
| 300 |
{ |
| 301 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 302 |
const auto it = |
| 303 |
std::find_if(m_members.cbegin(), m_members.cend(), [iChannelID](const auto& member) { |
| 304 |
return member.second->Channel()->ChannelID() == iChannelID; |
| 305 |
}); |
| 306 |
return it != m_members.cend() ? (*it).second->Channel() : std::shared_ptr<CPVRChannel>(); |
| 307 |
} |
| 308 |
|
| 309 |
std::shared_ptr<CPVRChannelGroupMember> CPVRChannelGroup::GetLastPlayedChannelGroupMember( |
| 310 |
int iCurrentChannel /* = -1 */) const |
| 311 |
{ |
| 312 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 313 |
|
| 314 |
std::shared_ptr<CPVRChannelGroupMember> groupMember; |
| 315 |
for (const auto& memberPair : m_members) |
| 316 |
{ |
| 317 |
const std::shared_ptr<const CPVRChannel> channel = memberPair.second->Channel(); |
| 318 |
if (channel->ChannelID() != iCurrentChannel && |
| 319 |
CServiceBroker::GetPVRManager().Clients()->IsCreatedClient(channel->ClientID()) && |
| 320 |
channel->LastWatched() > 0 && |
| 321 |
(!groupMember || channel->LastWatched() > groupMember->Channel()->LastWatched())) |
| 322 |
{ |
| 323 |
groupMember = memberPair.second; |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
return groupMember; |
| 328 |
} |
| 329 |
|
| 330 |
GroupMemberPair CPVRChannelGroup::GetLastAndPreviousToLastPlayedChannelGroupMember() const |
| 331 |
{ |
| 332 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 333 |
if (m_sortedMembers.empty()) |
| 334 |
return {}; |
| 335 |
|
| 336 |
auto members = m_sortedMembers; |
| 337 |
lock.unlock(); |
| 338 |
|
| 339 |
std::sort(members.begin(), members.end(), [](const auto& a, const auto& b) { |
| 340 |
return a->Channel()->LastWatched() > b->Channel()->LastWatched(); |
| 341 |
}); |
| 342 |
|
| 343 |
std::shared_ptr<CPVRChannelGroupMember> last; |
| 344 |
std::shared_ptr<CPVRChannelGroupMember> previousToLast; |
| 345 |
if (members[0]->Channel()->LastWatched()) |
| 346 |
{ |
| 347 |
last = members[0]; |
| 348 |
if (members.size() > 1 && members[1]->Channel()->LastWatched()) |
| 349 |
previousToLast = members[1]; |
| 350 |
} |
| 351 |
|
| 352 |
return {last, previousToLast}; |
| 353 |
} |
| 354 |
|
| 355 |
CPVRChannelNumber CPVRChannelGroup::GetChannelNumber( |
| 356 |
const std::shared_ptr<const CPVRChannel>& channel) const |
| 357 |
{ |
| 358 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 359 |
const std::shared_ptr<const CPVRChannelGroupMember> member = GetByUniqueID(channel->StorageId()); |
| 360 |
return member ? member->ChannelNumber() : CPVRChannelNumber(); |
| 361 |
} |
| 362 |
|
| 363 |
CPVRChannelNumber CPVRChannelGroup::GetClientChannelNumber( |
| 364 |
const std::shared_ptr<const CPVRChannel>& channel) const |
| 365 |
{ |
| 366 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 367 |
const std::shared_ptr<const CPVRChannelGroupMember> member = GetByUniqueID(channel->StorageId()); |
| 368 |
return member ? member->ClientChannelNumber() : CPVRChannelNumber(); |
| 369 |
} |
| 370 |
|
| 371 |
std::shared_ptr<CPVRChannelGroupMember> CPVRChannelGroup::GetByChannelNumber( |
| 372 |
const CPVRChannelNumber& channelNumber) const |
| 373 |
{ |
| 374 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 375 |
const bool bUseBackendChannelNumbers = GetSettings()->UseBackendChannelNumbers(); |
| 376 |
for (const auto& member : m_sortedMembers) |
| 377 |
{ |
| 378 |
CPVRChannelNumber activeChannelNumber = |
| 379 |
bUseBackendChannelNumbers ? member->ClientChannelNumber() : member->ChannelNumber(); |
| 380 |
if (activeChannelNumber == channelNumber) |
| 381 |
return member; |
| 382 |
} |
| 383 |
|
| 384 |
return {}; |
| 385 |
} |
| 386 |
|
| 387 |
std::shared_ptr<CPVRChannelGroupMember> CPVRChannelGroup::GetNextChannelGroupMember( |
| 388 |
const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) const |
| 389 |
{ |
| 390 |
std::shared_ptr<CPVRChannelGroupMember> nextMember; |
| 391 |
|
| 392 |
if (groupMember) |
| 393 |
{ |
| 394 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 395 |
for (auto it = m_sortedMembers.cbegin(); !nextMember && it != m_sortedMembers.cend(); ++it) |
| 396 |
{ |
| 397 |
if (*it == groupMember) |
| 398 |
{ |
| 399 |
do |
| 400 |
{ |
| 401 |
if ((++it) == m_sortedMembers.cend()) |
| 402 |
it = m_sortedMembers.cbegin(); |
| 403 |
if ((*it)->Channel() && !(*it)->Channel()->IsHidden()) |
| 404 |
nextMember = *it; |
| 405 |
} while (!nextMember && *it != groupMember); |
| 406 |
|
| 407 |
break; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
return nextMember; |
| 413 |
} |
| 414 |
|
| 415 |
std::shared_ptr<CPVRChannelGroupMember> CPVRChannelGroup::GetPreviousChannelGroupMember( |
| 416 |
const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) const |
| 417 |
{ |
| 418 |
std::shared_ptr<CPVRChannelGroupMember> previousMember; |
| 419 |
|
| 420 |
if (groupMember) |
| 421 |
{ |
| 422 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 423 |
for (auto it = m_sortedMembers.crbegin(); !previousMember && it != m_sortedMembers.crend(); |
| 424 |
++it) |
| 425 |
{ |
| 426 |
if (*it == groupMember) |
| 427 |
{ |
| 428 |
do |
| 429 |
{ |
| 430 |
if ((++it) == m_sortedMembers.crend()) |
| 431 |
it = m_sortedMembers.crbegin(); |
| 432 |
if ((*it)->Channel() && !(*it)->Channel()->IsHidden()) |
| 433 |
previousMember = *it; |
| 434 |
} while (!previousMember && *it != groupMember); |
| 435 |
|
| 436 |
break; |
| 437 |
} |
| 438 |
} |
| 439 |
} |
| 440 |
return previousMember; |
| 441 |
} |
| 442 |
|
| 443 |
std::vector<std::shared_ptr<CPVRChannelGroupMember>> CPVRChannelGroup::GetMembers( |
| 444 |
Include eFilter /* = Include::ALL */) const |
| 445 |
{ |
| 446 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 447 |
if (eFilter == Include::ALL) |
| 448 |
return m_sortedMembers; |
| 449 |
|
| 450 |
std::vector<std::shared_ptr<CPVRChannelGroupMember>> members; |
| 451 |
for (const auto& member : m_sortedMembers) |
| 452 |
{ |
| 453 |
switch (eFilter) |
| 454 |
{ |
| 455 |
case Include::ONLY_HIDDEN: |
| 456 |
if (!member->Channel()->IsHidden()) |
| 457 |
continue; |
| 458 |
break; |
| 459 |
case Include::ONLY_VISIBLE: |
| 460 |
if (member->Channel()->IsHidden()) |
| 461 |
continue; |
| 462 |
break; |
| 463 |
default: |
| 464 |
break; |
| 465 |
} |
| 466 |
|
| 467 |
members.emplace_back(member); |
| 468 |
} |
| 469 |
|
| 470 |
return members; |
| 471 |
} |
| 472 |
|
| 473 |
void CPVRChannelGroup::GetChannelNumbers(std::vector<std::string>& channelNumbers) const |
| 474 |
{ |
| 475 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 476 |
const bool bUseBackendChannelNumbers = GetSettings()->UseBackendChannelNumbers(); |
| 477 |
for (const auto& member : m_sortedMembers) |
| 478 |
{ |
| 479 |
CPVRChannelNumber activeChannelNumber = |
| 480 |
bUseBackendChannelNumbers ? member->ClientChannelNumber() : member->ChannelNumber(); |
| 481 |
channelNumbers.emplace_back(activeChannelNumber.FormattedChannelNumber()); |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
int CPVRChannelGroup::LoadFromDatabase(const std::vector<std::shared_ptr<CPVRClient>>& clients) |
| 486 |
{ |
| 487 |
const std::shared_ptr<const CPVRDatabase> database( |
| 488 |
CServiceBroker::GetPVRManager().GetTVDatabase()); |
| 489 |
if (!database) |
| 490 |
return -1; |
| 491 |
|
| 492 |
const std::vector<std::shared_ptr<CPVRChannelGroupMember>> results = |
| 493 |
database->Get(*this, clients); |
| 494 |
|
| 495 |
std::vector<std::shared_ptr<CPVRChannelGroupMember>> membersToDelete; |
| 496 |
if (!results.empty()) |
| 497 |
{ |
| 498 |
const std::shared_ptr<const CPVRClients> allClients = CServiceBroker::GetPVRManager().Clients(); |
| 499 |
|
| 500 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 501 |
for (const auto& member : results) |
| 502 |
{ |
| 503 |
// Consistency checks. |
| 504 |
|
| 505 |
if (GetClientID() != PVR_GROUP_CLIENT_ID_UNKNOWN && |
| 506 |
GetClientID() != PVR_GROUP_CLIENT_ID_LOCAL && GetClientID() != member->ChannelClientID()) |
| 507 |
{ |
| 508 |
CLog::LogF(LOGWARNING, |
| 509 |
"Skipping member with channel database id {} of {} channel group '{}'. " |
| 510 |
"Channel client id does not match group client id.", |
| 511 |
member->ChannelDatabaseID(), IsRadio() ? "radio" : "TV", GroupName()); |
| 512 |
membersToDelete.emplace_back(member); |
| 513 |
continue; |
| 514 |
} |
| 515 |
|
| 516 |
if (member->ChannelClientID() > 0 && member->ChannelUID() > 0 && |
| 517 |
member->IsRadio() == IsRadio()) |
| 518 |
{ |
| 519 |
// Ignore data from unknown/disabled clients |
| 520 |
if (allClients->IsEnabledClient(member->ChannelClientID())) |
| 521 |
{ |
| 522 |
m_sortedMembers.emplace_back(member); |
| 523 |
m_members.emplace(std::make_pair(member->ChannelClientID(), member->ChannelUID()), |
| 524 |
member); |
| 525 |
} |
| 526 |
} |
| 527 |
else |
| 528 |
{ |
| 529 |
CLog::LogF(LOGWARNING, |
| 530 |
"Skipping member with channel database id {} of {} channel group '{}'. " |
| 531 |
"Channel not found in the database or radio flag changed.", |
| 532 |
member->ChannelDatabaseID(), IsRadio() ? "radio" : "TV", GroupName()); |
| 533 |
membersToDelete.emplace_back(member); |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
SortByChannelNumber(); |
| 538 |
} |
| 539 |
|
| 540 |
DeleteGroupMembersFromDb(membersToDelete); |
| 541 |
|
| 542 |
return results.size() - membersToDelete.size(); |
| 543 |
} |
| 544 |
|
| 545 |
void CPVRChannelGroup::DeleteGroupMembersFromDb( |
| 546 |
const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& membersToDelete) |
| 547 |
{ |
| 548 |
if (!membersToDelete.empty()) |
| 549 |
{ |
| 550 |
const std::shared_ptr<CPVRDatabase> database = CServiceBroker::GetPVRManager().GetTVDatabase(); |
| 551 |
if (!database) |
| 552 |
{ |
| 553 |
CLog::LogF(LOGERROR, "No TV database"); |
| 554 |
return; |
| 555 |
} |
| 556 |
|
| 557 |
// Note: We must lock the db the whole time, otherwise races may occur. |
| 558 |
database->Lock(); |
| 559 |
|
| 560 |
bool commitPending = false; |
| 561 |
|
| 562 |
for (const auto& member : membersToDelete) |
| 563 |
{ |
| 564 |
commitPending |= member->QueueDelete(); |
| 565 |
|
| 566 |
size_t queryCount = database->GetDeleteQueriesCount(); |
| 567 |
if (queryCount > CHANNEL_COMMIT_QUERY_COUNT_LIMIT) |
| 568 |
database->CommitDeleteQueries(); |
| 569 |
} |
| 570 |
|
| 571 |
if (commitPending) |
| 572 |
database->CommitDeleteQueries(); |
| 573 |
|
| 574 |
database->Unlock(); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
bool CPVRChannelGroup::UpdateFromClient(const std::shared_ptr<CPVRChannelGroupMember>& groupMember) |
| 579 |
{ |
| 580 |
bool bChanged = false; |
| 581 |
|
| 582 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 583 |
|
| 584 |
const std::shared_ptr<CPVRChannel> channel = groupMember->Channel(); |
| 585 |
const std::shared_ptr<CPVRChannelGroupMember> existingMember = |
| 586 |
GetByUniqueID(channel->StorageId()); |
| 587 |
if (existingMember) |
| 588 |
{ |
| 589 |
// update existing channel |
| 590 |
if (IsChannelsOwner() && existingMember->Channel()->UpdateFromClient(channel)) |
| 591 |
{ |
| 592 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Updated {} channel '{}' from PVR client", |
| 593 |
IsRadio() ? "radio" : "TV", channel->ChannelName()); |
| 594 |
bChanged = true; |
| 595 |
} |
| 596 |
|
| 597 |
existingMember->SetClientChannelNumber(channel->ClientChannelNumber()); |
| 598 |
existingMember->SetOrder(groupMember->Order()); |
| 599 |
|
| 600 |
if (existingMember->NeedsSave()) |
| 601 |
{ |
| 602 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Updated {} channel group member '{}' in group '{}'", |
| 603 |
IsRadio() ? "radio" : "TV", channel->ChannelName(), GroupName()); |
| 604 |
bChanged = true; |
| 605 |
} |
| 606 |
} |
| 607 |
else |
| 608 |
{ |
| 609 |
if (groupMember->GroupID() == INVALID_GROUP_ID) |
| 610 |
groupMember->SetGroupID(GroupID()); |
| 611 |
|
| 612 |
m_sortedMembers.emplace_back(groupMember); |
| 613 |
m_members.emplace(channel->StorageId(), groupMember); |
| 614 |
|
| 615 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Added {} channel group member '{}' to group '{}'", |
| 616 |
IsRadio() ? "radio" : "TV", channel->ChannelName(), GroupName()); |
| 617 |
|
| 618 |
// Create EPG for new channel |
| 619 |
channel->CreateEPG(); |
| 620 |
|
| 621 |
bChanged = true; |
| 622 |
} |
| 623 |
|
| 624 |
return bChanged; |
| 625 |
} |
| 626 |
|
| 627 |
bool CPVRChannelGroup::AddAndUpdateGroupMembers( |
| 628 |
const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers) |
| 629 |
{ |
| 630 |
return std::accumulate(groupMembers.cbegin(), groupMembers.cend(), false, |
| 631 |
[this](bool changed, const auto& groupMember) { |
| 632 |
return UpdateFromClient(groupMember) ? true : changed; |
| 633 |
}); |
| 634 |
} |
| 635 |
|
| 636 |
bool CPVRChannelGroup::HasValidDataForClient(int iClientId) const |
| 637 |
{ |
| 638 |
return std::find(m_failedClients.begin(), m_failedClients.end(), iClientId) == |
| 639 |
m_failedClients.end(); |
| 640 |
} |
| 641 |
|
| 642 |
bool CPVRChannelGroup::HasValidDataForClients( |
| 643 |
const std::vector<std::shared_ptr<CPVRClient>>& clients) const |
| 644 |
{ |
| 645 |
return m_failedClients.empty() || |
| 646 |
std::none_of(clients.cbegin(), clients.cend(), |
| 647 |
[this](const std::shared_ptr<const CPVRClient>& client) { |
| 648 |
return !HasValidDataForClient(client->GetID()); |
| 649 |
}); |
| 650 |
} |
| 651 |
|
| 652 |
bool CPVRChannelGroup::UpdateChannelNumbersFromAllChannelsGroup() |
| 653 |
{ |
| 654 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 655 |
|
| 656 |
bool bChanged = false; |
| 657 |
|
| 658 |
if (!IsChannelsOwner()) |
| 659 |
{ |
| 660 |
// Make sure the channel numbers are set from the all channels group using the non default |
| 661 |
// renumber call before sorting. |
| 662 |
if (Renumber(IGNORE_NUMBERING_FROM_ONE) || SortAndRenumber()) |
| 663 |
bChanged = true; |
| 664 |
} |
| 665 |
|
| 666 |
m_events.Publish(IsChannelsOwner() || bChanged ? PVREvent::ChannelGroupInvalidated |
| 667 |
: PVREvent::ChannelGroup); |
| 668 |
|
| 669 |
return bChanged; |
| 670 |
} |
| 671 |
|
| 672 |
std::vector<std::shared_ptr<CPVRChannelGroupMember>> CPVRChannelGroup::RemoveDeletedGroupMembers( |
| 673 |
const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers) |
| 674 |
{ |
| 675 |
std::vector<std::shared_ptr<CPVRChannelGroupMember>> membersToRemove; |
| 676 |
|
| 677 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 678 |
|
| 679 |
// put group members into map to speedup the following lookups |
| 680 |
std::map<std::pair<int, int>, std::shared_ptr<CPVRChannelGroupMember>> membersMap; |
| 681 |
std::transform(groupMembers.begin(), groupMembers.end(), |
| 682 |
std::inserter(membersMap, membersMap.end()), |
| 683 |
[](const std::shared_ptr<CPVRChannelGroupMember>& member) { |
| 684 |
return std::make_pair(member->Channel()->StorageId(), member); |
| 685 |
}); |
| 686 |
|
| 687 |
// check for deleted/invalid channels |
| 688 |
for (auto it = m_sortedMembers.begin(); it != m_sortedMembers.end();) |
| 689 |
{ |
| 690 |
const std::shared_ptr<const CPVRChannel> channel = (*it)->Channel(); |
| 691 |
|
| 692 |
if (GetClientID() >= 0 && GetClientID() != channel->ClientID()) |
| 693 |
{ |
| 694 |
CLog::LogF(LOGDEBUG, |
| 695 |
"Removed {} channel '{}' from group '{}' because it has the wrong client id", |
| 696 |
IsRadio() ? "radio" : "TV", channel->ChannelName(), GroupName()); |
| 697 |
membersToRemove.emplace_back(*it); |
| 698 |
|
| 699 |
m_members.erase(channel->StorageId()); |
| 700 |
it = m_sortedMembers.erase(it); |
| 701 |
continue; |
| 702 |
} |
| 703 |
|
| 704 |
auto mapIt = membersMap.find(channel->StorageId()); |
| 705 |
if (mapIt == membersMap.end()) |
| 706 |
{ |
| 707 |
if (HasValidDataForClient(channel->ClientID())) |
| 708 |
{ |
| 709 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Removed stale {} channel '{}' from group '{}'. clientId={}", |
| 710 |
IsRadio() ? "radio" : "TV", channel->ChannelName(), GroupName(), |
| 711 |
channel->ClientID()); |
| 712 |
membersToRemove.emplace_back(*it); |
| 713 |
|
| 714 |
m_members.erase(channel->StorageId()); |
| 715 |
it = m_sortedMembers.erase(it); |
| 716 |
continue; |
| 717 |
} |
| 718 |
} |
| 719 |
else |
| 720 |
{ |
| 721 |
membersMap.erase(mapIt); |
| 722 |
} |
| 723 |
++it; |
| 724 |
} |
| 725 |
|
| 726 |
DeleteGroupMembersFromDb(membersToRemove); |
| 727 |
|
| 728 |
return membersToRemove; |
| 729 |
} |
| 730 |
|
| 731 |
bool CPVRChannelGroup::UpdateGroupEntries( |
| 732 |
const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers) |
| 733 |
{ |
| 734 |
bool bReturn = false; |
| 735 |
bool bChanged = false; |
| 736 |
bool bRemoved = false; |
| 737 |
|
| 738 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 739 |
|
| 740 |
bRemoved = !RemoveDeletedGroupMembers(groupMembers).empty(); |
| 741 |
bChanged = AddAndUpdateGroupMembers(groupMembers) || bRemoved; |
| 742 |
bChanged |= UpdateMembersClientPriority(); |
| 743 |
|
| 744 |
if (bChanged) |
| 745 |
{ |
| 746 |
// renumber to make sure all group members have a channel number. New members were added at the |
| 747 |
// back, so they'll get the highest numbers |
| 748 |
bool bRenumbered = SortAndRenumber(); |
| 749 |
bReturn = Persist(); |
| 750 |
m_events.Publish(HasNewChannels() || bRemoved || bRenumbered ? PVREvent::ChannelGroupInvalidated |
| 751 |
: PVREvent::ChannelGroup); |
| 752 |
} |
| 753 |
else |
| 754 |
{ |
| 755 |
bReturn = true; |
| 756 |
} |
| 757 |
|
| 758 |
return bReturn; |
| 759 |
} |
| 760 |
|
| 761 |
bool CPVRChannelGroup::RemoveFromGroup( |
| 762 |
const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) |
| 763 |
{ |
| 764 |
bool bReturn = false; |
| 765 |
const auto channel = groupMember->Channel(); |
| 766 |
|
| 767 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 768 |
|
| 769 |
for (auto it = m_sortedMembers.begin(); it != m_sortedMembers.end(); ++it) |
| 770 |
{ |
| 771 |
const auto storageId = (*it)->Channel()->StorageId(); |
| 772 |
if (channel->StorageId() == storageId) |
| 773 |
{ |
| 774 |
m_members.erase(storageId); |
| 775 |
m_sortedMembers.erase(it); |
| 776 |
bReturn = true; |
| 777 |
break; |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
// no need to renumber if nothing was removed |
| 782 |
if (bReturn) |
| 783 |
Renumber(); |
| 784 |
|
| 785 |
return bReturn; |
| 786 |
} |
| 787 |
|
| 788 |
bool CPVRChannelGroup::AppendToGroup( |
| 789 |
const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) |
| 790 |
{ |
| 791 |
bool bReturn = false; |
| 792 |
|
| 793 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 794 |
|
| 795 |
if (!CPVRChannelGroup::IsGroupMember(groupMember)) |
| 796 |
{ |
| 797 |
unsigned int channelNumberMax = |
| 798 |
std::accumulate(m_sortedMembers.cbegin(), m_sortedMembers.cend(), 0, |
| 799 |
[](unsigned int last, const auto& member) { |
| 800 |
return (member->ChannelNumber().GetChannelNumber() > last) |
| 801 |
? member->ChannelNumber().GetChannelNumber() |
| 802 |
: last; |
| 803 |
}); |
| 804 |
|
| 805 |
const auto channel = groupMember->Channel(); |
| 806 |
const auto newMember = |
| 807 |
std::make_shared<CPVRChannelGroupMember>(GroupID(), GroupName(), GetClientID(), channel); |
| 808 |
newMember->SetChannelNumber(CPVRChannelNumber(channelNumberMax + 1, 0)); |
| 809 |
newMember->SetClientPriority(groupMember->ClientPriority()); |
| 810 |
|
| 811 |
m_sortedMembers.emplace_back(newMember); |
| 812 |
m_members.emplace(channel->StorageId(), newMember); |
| 813 |
|
| 814 |
SortAndRenumber(); |
| 815 |
bReturn = true; |
| 816 |
} |
| 817 |
return bReturn; |
| 818 |
} |
| 819 |
|
| 820 |
bool CPVRChannelGroup::IsGroupMember( |
| 821 |
const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) const |
| 822 |
{ |
| 823 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 824 |
return m_members.find(groupMember->Channel()->StorageId()) != m_members.end(); |
| 825 |
} |
| 826 |
|
| 827 |
bool CPVRChannelGroup::Persist() |
| 828 |
{ |
| 829 |
bool bReturn(true); |
| 830 |
const std::shared_ptr<CPVRDatabase> database(CServiceBroker::GetPVRManager().GetTVDatabase()); |
| 831 |
|
| 832 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 833 |
|
| 834 |
// do not persist if the group is not fully loaded and was saved before. |
| 835 |
if (!m_bLoaded && m_iGroupId != INVALID_GROUP_ID) |
| 836 |
return bReturn; |
| 837 |
|
| 838 |
// Mark newly created groups as loaded so future updates will also be persisted... |
| 839 |
if (m_iGroupId == INVALID_GROUP_ID) |
| 840 |
m_bLoaded = true; |
| 841 |
|
| 842 |
if (database) |
| 843 |
{ |
| 844 |
CLog::LogFC(LOGDEBUG, LOGPVR, "Persisting channel group '{}' with {} channels", GroupName(), |
| 845 |
static_cast<int>(m_members.size())); |
| 846 |
|
| 847 |
bReturn = database->Persist(*this); |
| 848 |
m_bChanged = false; |
| 849 |
} |
| 850 |
else |
| 851 |
{ |
| 852 |
bReturn = false; |
| 853 |
} |
| 854 |
|
| 855 |
return bReturn; |
| 856 |
} |
| 857 |
|
| 858 |
void CPVRChannelGroup::Delete() |
| 859 |
{ |
| 860 |
const std::shared_ptr<CPVRDatabase> database = CServiceBroker::GetPVRManager().GetTVDatabase(); |
| 861 |
if (!database) |
| 862 |
{ |
| 863 |
CLog::LogF(LOGERROR, "No TV database"); |
| 864 |
return; |
| 865 |
} |
| 866 |
|
| 867 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 868 |
|
| 869 |
if (m_iGroupId > 0) |
| 870 |
{ |
| 871 |
if (database->Delete(*this)) |
| 872 |
m_bDeleted = true; |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
bool CPVRChannelGroup::Renumber(RenumberMode mode /* = NORMAL */) |
| 877 |
{ |
| 878 |
bool bReturn(false); |
| 879 |
unsigned int iChannelNumber(0); |
| 880 |
|
| 881 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 882 |
const bool bUseBackendChannelNumbers = GetSettings()->UseBackendChannelNumbers(); |
| 883 |
const bool bStartGroupChannelNumbersFromOne = GetSettings()->StartGroupChannelNumbersFromOne(); |
| 884 |
|
| 885 |
CPVRChannelNumber currentChannelNumber; |
| 886 |
CPVRChannelNumber currentClientChannelNumber; |
| 887 |
for (auto& sortedMember : m_sortedMembers) |
| 888 |
{ |
| 889 |
const auto channel = sortedMember->Channel(); |
| 890 |
|
| 891 |
currentClientChannelNumber = sortedMember->ClientChannelNumber(); |
| 892 |
if (!currentClientChannelNumber.IsValid()) |
| 893 |
currentClientChannelNumber = channel->ClientChannelNumber(); |
| 894 |
|
| 895 |
if (bUseBackendChannelNumbers) |
| 896 |
{ |
| 897 |
currentChannelNumber = currentClientChannelNumber; |
| 898 |
} |
| 899 |
else if (channel->IsHidden()) |
| 900 |
{ |
| 901 |
currentChannelNumber = CPVRChannelNumber(0, 0); |
| 902 |
} |
| 903 |
else |
| 904 |
{ |
| 905 |
if (IsChannelsOwner() || |
| 906 |
(bStartGroupChannelNumbersFromOne && mode != IGNORE_NUMBERING_FROM_ONE)) |
| 907 |
currentChannelNumber = CPVRChannelNumber(++iChannelNumber, 0); |
| 908 |
else |
| 909 |
currentChannelNumber = m_allChannelsGroup->GetChannelNumber(channel); |
| 910 |
} |
| 911 |
|
| 912 |
if (sortedMember->ChannelNumber() != currentChannelNumber || |
| 913 |
sortedMember->ClientChannelNumber() != currentClientChannelNumber) |
| 914 |
{ |
| 915 |
bReturn = true; |
| 916 |
sortedMember->SetChannelNumber(currentChannelNumber); |
| 917 |
sortedMember->SetClientChannelNumber(currentClientChannelNumber); |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
if (bReturn) |
| 922 |
Sort(); |
| 923 |
|
| 924 |
return bReturn; |
| 925 |
} |
| 926 |
|
| 927 |
bool CPVRChannelGroup::HasNewChannels() const |
| 928 |
{ |
| 929 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 930 |
return std::any_of(m_members.cbegin(), m_members.cend(), |
| 931 |
[](const auto& member) { return member.second->Channel()->ChannelID() <= 0; }); |
| 932 |
} |
| 933 |
|
| 934 |
bool CPVRChannelGroup::HasChanges() const |
| 935 |
{ |
| 936 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 937 |
return m_bChanged; |
| 938 |
} |
| 939 |
|
| 940 |
bool CPVRChannelGroup::IsNew() const |
| 941 |
{ |
| 942 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 943 |
return m_iGroupId <= 0; |
| 944 |
} |
| 945 |
|
| 946 |
void CPVRChannelGroup::UseBackendChannelOrderChanged() |
| 947 |
{ |
| 948 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 949 |
UpdateMembersClientPriority(); |
| 950 |
OnSettingChanged(); |
| 951 |
} |
| 952 |
|
| 953 |
void CPVRChannelGroup::UseBackendChannelNumbersChanged() |
| 954 |
{ |
| 955 |
OnSettingChanged(); |
| 956 |
} |
| 957 |
|
| 958 |
void CPVRChannelGroup::StartGroupChannelNumbersFromOneChanged() |
| 959 |
{ |
| 960 |
OnSettingChanged(); |
| 961 |
} |
| 962 |
|
| 963 |
void CPVRChannelGroup::OnSettingChanged() |
| 964 |
{ |
| 965 |
//! @todo while pvr manager is starting up do accept setting changes. |
| 966 |
if (!CServiceBroker::GetPVRManager().IsStarted()) |
| 967 |
{ |
| 968 |
CLog::Log(LOGWARNING, "Channel group setting change ignored while PVR Manager is starting"); |
| 969 |
return; |
| 970 |
} |
| 971 |
|
| 972 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 973 |
|
| 974 |
CLog::LogFC(LOGDEBUG, LOGPVR, |
| 975 |
"Renumbering channel group '{}' to use the backend channel order and/or numbers", |
| 976 |
GroupName()); |
| 977 |
|
| 978 |
// Make sure the channel numbers are set from the all channels group using the non default |
| 979 |
// renumber call before sorting. |
| 980 |
Renumber(IGNORE_NUMBERING_FROM_ONE); |
| 981 |
|
| 982 |
const bool bRenumbered = SortAndRenumber(); |
| 983 |
Persist(); |
| 984 |
|
| 985 |
m_events.Publish(bRenumbered ? PVREvent::ChannelGroupInvalidated : PVREvent::ChannelGroup); |
| 986 |
} |
| 987 |
|
| 988 |
int CPVRChannelGroup::GroupID() const |
| 989 |
{ |
| 990 |
return m_iGroupId; |
| 991 |
} |
| 992 |
|
| 993 |
void CPVRChannelGroup::SetGroupID(int iGroupId) |
| 994 |
{ |
| 995 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 996 |
if (iGroupId >= 0 && m_iGroupId != iGroupId) |
| 997 |
{ |
| 998 |
m_iGroupId = iGroupId; |
| 999 |
|
| 1000 |
// propagate the new id to the group members |
| 1001 |
for (const auto& member : m_members) |
| 1002 |
member.second->SetGroupID(iGroupId); |
| 1003 |
} |
| 1004 |
} |
| 1005 |
|
| 1006 |
std::string CPVRChannelGroup::GroupName() const |
| 1007 |
{ |
| 1008 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1009 |
return m_path.GetGroupName(); |
| 1010 |
} |
| 1011 |
|
| 1012 |
void CPVRChannelGroup::SetGroupName(const std::string& strGroupName, |
| 1013 |
bool isUserSetName /* = false */) |
| 1014 |
{ |
| 1015 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1016 |
if (m_path.GetGroupName() != strGroupName) |
| 1017 |
{ |
| 1018 |
m_isUserSetName = isUserSetName; |
| 1019 |
m_path = CPVRChannelsPath(m_path.IsRadio(), strGroupName, m_path.GetGroupClientID()); |
| 1020 |
|
| 1021 |
// Update group members, for which group name is part of their path |
| 1022 |
for (auto& member : m_sortedMembers) |
| 1023 |
{ |
| 1024 |
member->SetGroupName(strGroupName); |
| 1025 |
} |
| 1026 |
|
| 1027 |
if (m_bLoaded) |
| 1028 |
{ |
| 1029 |
m_bChanged = true; |
| 1030 |
Persist(); //! @todo why must we persist immediately? |
| 1031 |
} |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
std::string CPVRChannelGroup::ClientGroupName() const |
| 1036 |
{ |
| 1037 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1038 |
return m_clientGroupName; |
| 1039 |
} |
| 1040 |
|
| 1041 |
void CPVRChannelGroup::SetClientGroupName(const std::string& groupName) |
| 1042 |
{ |
| 1043 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1044 |
if (m_clientGroupName != groupName) |
| 1045 |
{ |
| 1046 |
m_clientGroupName = groupName; |
| 1047 |
|
| 1048 |
if (m_bLoaded) |
| 1049 |
{ |
| 1050 |
m_bChanged = true; |
| 1051 |
Persist(); //! @todo why must we persist immediately? |
| 1052 |
} |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
bool CPVRChannelGroup::IsRadio() const |
| 1057 |
{ |
| 1058 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1059 |
return m_path.IsRadio(); |
| 1060 |
} |
| 1061 |
|
| 1062 |
time_t CPVRChannelGroup::LastWatched() const |
| 1063 |
{ |
| 1064 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1065 |
return m_iLastWatched; |
| 1066 |
} |
| 1067 |
|
| 1068 |
void CPVRChannelGroup::SetLastWatched(time_t iLastWatched) |
| 1069 |
{ |
| 1070 |
const std::shared_ptr<CPVRDatabase> database(CServiceBroker::GetPVRManager().GetTVDatabase()); |
| 1071 |
|
| 1072 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1073 |
|
| 1074 |
if (m_iLastWatched != iLastWatched) |
| 1075 |
{ |
| 1076 |
m_iLastWatched = iLastWatched; |
| 1077 |
if (m_bLoaded && database) |
| 1078 |
database->UpdateLastWatched(*this); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
|
| 1082 |
uint64_t CPVRChannelGroup::LastOpened() const |
| 1083 |
{ |
| 1084 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1085 |
return m_iLastOpened; |
| 1086 |
} |
| 1087 |
|
| 1088 |
void CPVRChannelGroup::SetLastOpened(uint64_t iLastOpened) |
| 1089 |
{ |
| 1090 |
const std::shared_ptr<CPVRDatabase> database(CServiceBroker::GetPVRManager().GetTVDatabase()); |
| 1091 |
|
| 1092 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1093 |
|
| 1094 |
if (m_iLastOpened != iLastOpened) |
| 1095 |
{ |
| 1096 |
m_iLastOpened = iLastOpened; |
| 1097 |
if (m_bLoaded && database) |
| 1098 |
database->UpdateLastOpened(*this); |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
size_t CPVRChannelGroup::Size() const |
| 1103 |
{ |
| 1104 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1105 |
return m_members.size(); |
| 1106 |
} |
| 1107 |
|
| 1108 |
bool CPVRChannelGroup::HasChannels() const |
| 1109 |
{ |
| 1110 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1111 |
return !m_members.empty(); |
| 1112 |
} |
| 1113 |
|
| 1114 |
bool CPVRChannelGroup::HasHiddenChannels() const |
| 1115 |
{ |
| 1116 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1117 |
return std::any_of(m_members.cbegin(), m_members.cend(), |
| 1118 |
[](const auto& member) { return member.second->Channel()->IsHidden(); }); |
| 1119 |
} |
| 1120 |
|
| 1121 |
bool CPVRChannelGroup::SetHidden(bool bHidden) |
| 1122 |
{ |
| 1123 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1124 |
|
| 1125 |
if (m_bHidden != bHidden) |
| 1126 |
{ |
| 1127 |
m_bHidden = bHidden; |
| 1128 |
if (m_bLoaded) |
| 1129 |
m_bChanged = true; |
| 1130 |
} |
| 1131 |
|
| 1132 |
return m_bChanged; |
| 1133 |
} |
| 1134 |
|
| 1135 |
bool CPVRChannelGroup::IsHidden() const |
| 1136 |
{ |
| 1137 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1138 |
return m_bHidden; |
| 1139 |
} |
| 1140 |
|
| 1141 |
int CPVRChannelGroup::GetPosition() const |
| 1142 |
{ |
| 1143 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1144 |
return m_iPosition; |
| 1145 |
} |
| 1146 |
|
| 1147 |
void CPVRChannelGroup::SetPosition(int iPosition) |
| 1148 |
{ |
| 1149 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1150 |
|
| 1151 |
if (m_iPosition != iPosition) |
| 1152 |
{ |
| 1153 |
m_iPosition = iPosition; |
| 1154 |
if (m_bLoaded) |
| 1155 |
m_bChanged = true; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
int CPVRChannelGroup::GetClientPosition() const |
| 1160 |
{ |
| 1161 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1162 |
return m_iClientPosition; |
| 1163 |
} |
| 1164 |
|
| 1165 |
void CPVRChannelGroup::SetClientPosition(int iPosition) |
| 1166 |
{ |
| 1167 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1168 |
|
| 1169 |
if (m_iClientPosition != iPosition) |
| 1170 |
{ |
| 1171 |
m_iClientPosition = iPosition; |
| 1172 |
if (m_bLoaded) |
| 1173 |
m_bChanged = true; |
| 1174 |
} |
| 1175 |
} |
| 1176 |
|
| 1177 |
int CPVRChannelGroup::CleanupCachedImages() |
| 1178 |
{ |
| 1179 |
std::vector<std::string> urlsToCheck; |
| 1180 |
{ |
| 1181 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1182 |
std::transform( |
| 1183 |
m_members.cbegin(), m_members.cend(), std::back_inserter(urlsToCheck), |
| 1184 |
[](const auto& groupMember) { return groupMember.second->Channel()->ClientIconPath(); }); |
| 1185 |
} |
| 1186 |
|
| 1187 |
const std::string owner = |
| 1188 |
StringUtils::Format(CPVRChannel::IMAGE_OWNER_PATTERN, IsRadio() ? "radio" : "tv"); |
| 1189 |
return CPVRCachedImages::Cleanup({{owner, ""}}, urlsToCheck); |
| 1190 |
} |
| 1191 |
|
| 1192 |
int CPVRChannelGroup::GetClientPriority() const |
| 1193 |
{ |
| 1194 |
if (GetClientID() == PVR_GROUP_CLIENT_ID_UNKNOWN) |
| 1195 |
return 0; // not yet fully migrated; try later. |
| 1196 |
|
| 1197 |
std::unique_lock<CCriticalSection> lock(m_critSection); |
| 1198 |
if (!m_clientPriority.has_value()) |
| 1199 |
{ |
| 1200 |
const auto client = CServiceBroker::GetPVRManager().GetClient(GetClientID()); |
| 1201 |
if (client) |
| 1202 |
m_clientPriority = client->GetPriority(); |
| 1203 |
else |
| 1204 |
m_clientPriority = 0; |
| 1205 |
} |
| 1206 |
return *m_clientPriority; |
| 1207 |
} |