/
PVRChannelGroups.cpp
569 lines (473 loc) · 16.1 KB
/
PVRChannelGroups.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
* Copyright (C) 2012-2015 Team Kodi
* http://kodi.tv
*
* 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 Kodi; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "PVRChannelGroups.h"
#include <algorithm>
#include "FileItem.h"
#include "ServiceBroker.h"
#include "URL.h"
#include "settings/Settings.h"
#include "utils/StringUtils.h"
#include "utils/URIUtils.h"
#include "utils/log.h"
#include "pvr/PVRDatabase.h"
#include "pvr/PVRManager.h"
#include "pvr/addons/PVRClients.h"
#include "pvr/channels/PVRChannelGroupInternal.h"
using namespace PVR;
CPVRChannelGroups::CPVRChannelGroups(bool bRadio) :
m_bRadio(bRadio)
{
}
CPVRChannelGroups::~CPVRChannelGroups(void)
{
Clear();
}
void CPVRChannelGroups::Clear(void)
{
CSingleLock lock(m_critSection);
m_groups.clear();
m_failedClientsForChannelGroups.clear();
}
bool CPVRChannelGroups::GetGroupsFromClients(void)
{
if (! CServiceBroker::GetSettings().GetBool(CSettings::SETTING_PVRMANAGER_SYNCCHANNELGROUPS))
return true;
return CServiceBroker::GetPVRManager().Clients()->GetChannelGroups(this, m_failedClientsForChannelGroups) == PVR_ERROR_NO_ERROR;
}
bool CPVRChannelGroups::Update(const CPVRChannelGroup &group, bool bUpdateFromClient /* = false */)
{
if (group.GroupName().empty() && group.GroupID() <= 0)
return true;
CPVRChannelGroupPtr updateGroup;
{
CSingleLock lock(m_critSection);
// There can be only one internal group! Make sure we never push a new one!
if (group.IsInternalGroup())
updateGroup = GetGroupAll();
// try to find the group by id
if (!updateGroup && group.GroupID() > 0)
updateGroup = GetById(group.GroupID());
// try to find the group by name if we didn't find it yet
if (!updateGroup)
updateGroup = GetByName(group.GroupName());
if (!updateGroup)
{
// create a new group if none was found. Copy the properties immediately
// so the group doesn't get flagged as "changed" further down.
updateGroup = CPVRChannelGroupPtr(new CPVRChannelGroup(group.IsRadio(), group.GroupID(), group.GroupName()));
m_groups.push_back(updateGroup);
}
updateGroup->SetRadio(group.IsRadio());
updateGroup->SetGroupID(group.GroupID());
updateGroup->SetGroupName(group.GroupName());
updateGroup->SetGroupType(group.GroupType());
updateGroup->SetPosition(group.GetPosition());
// don't override properties we only store locally in our PVR database
if (!bUpdateFromClient)
{
updateGroup->SetLastWatched(group.LastWatched());
updateGroup->SetHidden(group.IsHidden());
}
}
// sort groups
SortGroups();
// persist changes
if (bUpdateFromClient)
return updateGroup->Persist();
return true;
}
void CPVRChannelGroups::SortGroups()
{
CSingleLock lock(m_critSection);
// check if one of the group holds a valid sort position
std::vector<CPVRChannelGroupPtr>::iterator it = std::find_if(m_groups.begin(), m_groups.end(), [](const CPVRChannelGroupPtr &group) {
return (group->GetPosition() > 0);
});
// sort by position if we found a valid sort position
if (it != m_groups.end())
{
std::sort(m_groups.begin(), m_groups.end(), [](const CPVRChannelGroupPtr &group1, const CPVRChannelGroupPtr &group2) {
return group1->GetPosition() < group2->GetPosition();
});
}
}
CFileItemPtr CPVRChannelGroups::GetByPath(const std::string &strInPath) const
{
std::string strPath = strInPath;
URIUtils::RemoveSlashAtEnd(strPath);
std::string strCheckPath;
for (const auto& group: m_groups)
{
// check if the path matches
strCheckPath = group->GetPath();
if (URIUtils::PathHasParent(strPath, strCheckPath))
{
strPath.erase(0, strCheckPath.size());
std::vector<std::string> split(StringUtils::Split(strPath, '_', 2));
if (split.size() == 2)
{
const CPVRChannelPtr channel = group->GetByUniqueID(atoi(split[1].c_str()), CServiceBroker::GetPVRManager().Clients()->GetClientId(split[0]));
if (channel)
return CFileItemPtr(new CFileItem(channel));
}
}
}
// no match
return CFileItemPtr(new CFileItem());
}
CPVRChannelGroupPtr CPVRChannelGroups::GetById(int iGroupId) const
{
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::const_iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
if ((*it)->GroupID() == iGroupId)
return *it;
}
CPVRChannelGroupPtr empty;
return empty;
}
std::vector<CPVRChannelGroupPtr> CPVRChannelGroups::GetGroupsByChannel(const CPVRChannelPtr &channel, bool bExcludeHidden /* = false */) const
{
std::vector<CPVRChannelGroupPtr> groups;
for (CPVRChannelGroupPtr group : m_groups)
{
if ((!bExcludeHidden || !group->IsHidden()) && group->IsGroupMember(channel))
groups.push_back(group);
}
return groups;
}
CPVRChannelGroupPtr CPVRChannelGroups::GetByName(const std::string &strName) const
{
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::const_iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
if ((*it)->GroupName() == strName)
return *it;
}
CPVRChannelGroupPtr empty;
return empty;
}
void CPVRChannelGroups::RemoveFromAllGroups(const CPVRChannelPtr &channel)
{
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::const_iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
// only delete the channel from non-system groups
if (!(*it)->IsInternalGroup())
(*it)->RemoveFromGroup(channel);
}
}
bool CPVRChannelGroups::Update(bool bChannelsOnly /* = false */)
{
bool bUpdateAllGroups = !bChannelsOnly && CServiceBroker::GetSettings().GetBool(CSettings::SETTING_PVRMANAGER_SYNCCHANNELGROUPS);
bool bReturn(true);
// sync groups
if (bUpdateAllGroups)
GetGroupsFromClients();
// sync channels in groups
std::vector<CPVRChannelGroupPtr> groups;
{
CSingleLock lock(m_critSection);
groups = m_groups;
}
for (const auto &group : groups)
{
if (bUpdateAllGroups || group->IsInternalGroup())
bReturn = group->Update() && bReturn;
}
// persist changes
return PersistAll() && bReturn;
}
bool CPVRChannelGroups::LoadUserDefinedChannelGroups(void)
{
bool bSyncWithBackends = CServiceBroker::GetSettings().GetBool(CSettings::SETTING_PVRMANAGER_SYNCCHANNELGROUPS);
CSingleLock lock(m_critSection);
// load groups from the backends if the option is enabled
int iSize = m_groups.size();
if (bSyncWithBackends)
{
GetGroupsFromClients();
CLog::Log(LOGDEBUG, "CPVRChannelGroups - {0} - {1} new user defined {2} channel groups fetched from clients", __FUNCTION__, (m_groups.size() - iSize), m_bRadio ? "radio" : "TV");
}
else
CLog::Log(LOGDEBUG, "CPVRChannelGroups - %s - 'synchannelgroups' is disabled; skipping groups from clients", __FUNCTION__);
std::vector<CPVRChannelGroupPtr> emptyGroups;
// load group members
for (std::vector<CPVRChannelGroupPtr>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
// load only user defined groups, as internal group is already loaded
if (!(*it)->IsInternalGroup())
{
if (!(*it)->Load())
{
CLog::Log(LOGDEBUG, "CPVRChannelGroups - %s - failed to load channel group '%s'", __FUNCTION__, (*it)->GroupName().c_str());
return false;
}
// remove empty groups when sync with backend is enabled
if (bSyncWithBackends && (*it)->Size() == 0)
emptyGroups.push_back(*it);
}
}
for (std::vector<CPVRChannelGroupPtr>::iterator it = emptyGroups.begin(); it != emptyGroups.end(); ++it)
{
CLog::Log(LOGDEBUG, "CPVRChannelGroups - %s - deleting empty group '%s'", __FUNCTION__, (*it)->GroupName().c_str());
DeleteGroup(*(*it));
}
// persist changes if we fetched groups from the backends
return bSyncWithBackends ? PersistAll() : true;
}
bool CPVRChannelGroups::Load(void)
{
const CPVRDatabasePtr database(CServiceBroker::GetPVRManager().GetTVDatabase());
if (!database)
return false;
CSingleLock lock(m_critSection);
// remove previous contents
Clear();
CLog::Log(LOGDEBUG, "CPVRChannelGroups - %s - loading all %s channel groups", __FUNCTION__, m_bRadio ? "radio" : "TV");
// create the internal channel group
CPVRChannelGroupPtr internalGroup = CPVRChannelGroupPtr(new CPVRChannelGroupInternal(m_bRadio));
m_groups.push_back(internalGroup);
// load groups from the database
database->Get(*this);
CLog::Log(LOGDEBUG, "CPVRChannelGroups - {0} - {1} {2} groups fetched from the database", __FUNCTION__, m_groups.size(), m_bRadio ? "radio" : "TV");
// load channels of internal group
if (!internalGroup->Load())
{
CLog::Log(LOGERROR, "CPVRChannelGroups - %s - failed to load channels", __FUNCTION__);
return false;
}
// load the other groups from the database
if (!LoadUserDefinedChannelGroups())
{
CLog::Log(LOGERROR, "CPVRChannelGroups - %s - failed to load channel groups", __FUNCTION__);
return false;
}
// set the last played group as selected group at startup
CPVRChannelGroupPtr lastPlayedGroup = GetLastPlayedGroup();
SetSelectedGroup(lastPlayedGroup ? lastPlayedGroup : internalGroup);
CLog::Log(LOGDEBUG, "CPVRChannelGroups - {0} - {1} %s channel groups loaded", __FUNCTION__, m_groups.size(), m_bRadio ? "radio" : "TV");
// need at least 1 group
return m_groups.size() > 0;
}
bool CPVRChannelGroups::PersistAll(void)
{
bool bReturn(true);
CLog::Log(LOGDEBUG, "CPVRChannelGroups - %s - persisting all changes in channel groups", __FUNCTION__);
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
bReturn &= (*it)->Persist();
return bReturn;
}
CPVRChannelGroupPtr CPVRChannelGroups::GetGroupAll(void) const
{
CSingleLock lock(m_critSection);
if (!m_groups.empty())
return m_groups.at(0);
CPVRChannelGroupPtr empty;
return empty;
}
CPVRChannelGroupPtr CPVRChannelGroups::GetLastGroup(void) const
{
CSingleLock lock(m_critSection);
if (!m_groups.empty())
return m_groups.at(m_groups.size() - 1);
CPVRChannelGroupPtr empty;
return empty;
}
CPVRChannelGroupPtr CPVRChannelGroups::GetLastPlayedGroup(int iChannelID /* = -1 */) const
{
CSingleLock lock(m_critSection);
CPVRChannelGroupPtr group;
for (std::vector<CPVRChannelGroupPtr>::const_iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
if ((*it)->LastWatched() > 0 && (!group || (*it)->LastWatched() > group->LastWatched()) &&
(iChannelID == -1 || (iChannelID >= 0 && (*it)->IsGroupMember(iChannelID))) && !(*it)->IsHidden())
group = (*it);
}
return group;
}
std::vector<CPVRChannelGroupPtr> CPVRChannelGroups::GetMembers(bool bExcludeHidden /* = false */) const
{
CSingleLock lock(m_critSection);
std::vector<CPVRChannelGroupPtr> groups;
for (CPVRChannelGroupPtr group : m_groups)
{
if (!bExcludeHidden || !group->IsHidden())
groups.push_back(group);
}
return groups;
}
int CPVRChannelGroups::GetGroupList(CFileItemList* results, bool bExcludeHidden /* = false */) const
{
int iReturn(0);
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::const_iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
// exclude hidden groups if desired
if (bExcludeHidden && (*it)->IsHidden())
continue;
CFileItemPtr group(new CFileItem((*it)->GetPath(), true));
group->m_strTitle = (*it)->GroupName();
group->SetLabel((*it)->GroupName());
results->Add(group);
++iReturn;
}
return iReturn;
}
CPVRChannelGroupPtr CPVRChannelGroups::GetPreviousGroup(const CPVRChannelGroup &group) const
{
bool bReturnNext(false);
{
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::const_reverse_iterator it = m_groups.rbegin(); it != m_groups.rend(); ++it)
{
// return this entry
if (bReturnNext && !(*it)->IsHidden())
return *it;
// return the next entry
if ((*it)->GroupID() == group.GroupID())
bReturnNext = true;
}
// no match return last visible group
for (std::vector<CPVRChannelGroupPtr>::const_reverse_iterator it = m_groups.rbegin(); it != m_groups.rend(); ++it)
{
if (!(*it)->IsHidden())
return *it;
}
}
// no match
return GetLastGroup();
}
CPVRChannelGroupPtr CPVRChannelGroups::GetNextGroup(const CPVRChannelGroup &group) const
{
bool bReturnNext(false);
{
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::const_iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
// return this entry
if (bReturnNext && !(*it)->IsHidden())
return *it;
// return the next entry
if ((*it)->GroupID() == group.GroupID())
bReturnNext = true;
}
// no match return first visible group
for (std::vector<CPVRChannelGroupPtr>::const_iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
if (!(*it)->IsHidden())
return *it;
}
}
// no match
return GetFirstGroup();
}
CPVRChannelGroupPtr CPVRChannelGroups::GetSelectedGroup(void) const
{
CSingleLock lock(m_critSection);
return m_selectedGroup;
}
void CPVRChannelGroups::SetSelectedGroup(const CPVRChannelGroupPtr &group)
{
// update the selected group
{
CSingleLock lock(m_critSection);
if (m_selectedGroup)
m_selectedGroup->SetSelectedGroup(false);
m_selectedGroup = group;
group->SetSelectedGroup(true);
}
// update the channel number cache
group->Renumber();
}
bool CPVRChannelGroups::AddGroup(const std::string &strName)
{
bool bPersist(false);
CPVRChannelGroupPtr group;
{
CSingleLock lock(m_critSection);
// check if there's no group with the same name yet
group = GetByName(strName);
if (!group)
{
// create a new group
group = CPVRChannelGroupPtr(new CPVRChannelGroup());
group->SetRadio(m_bRadio);
group->SetGroupName(strName);
m_groups.push_back(group);
bPersist = true;
}
}
// persist in the db if a new group was added
return bPersist ? group->Persist() : true;
}
bool CPVRChannelGroups::DeleteGroup(const CPVRChannelGroup &group)
{
// don't delete internal groups
if (group.IsInternalGroup())
{
CLog::Log(LOGERROR, "CPVRChannelGroups - %s - cannot delete internal group '%s'", __FUNCTION__, group.GroupName().c_str());
return false;
}
bool bFound(false);
CPVRChannelGroupPtr playingGroup;
// delete the group in this container
{
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::iterator it = m_groups.begin(); !bFound && it != m_groups.end();)
{
if (*(*it) == group || (group.GroupID() > 0 && (*it)->GroupID() == group.GroupID()))
{
// update the selected group in the gui if it's deleted
CPVRChannelGroupPtr selectedGroup = GetSelectedGroup();
if (selectedGroup && *selectedGroup == group)
playingGroup = GetGroupAll();
it = m_groups.erase(it);
bFound = true;
}
else
{
++it;
}
}
}
if (playingGroup)
CServiceBroker::GetPVRManager().SetPlayingGroup(playingGroup);
if (group.GroupID() > 0)
{
// delete the group from the database
const CPVRDatabasePtr database(CServiceBroker::GetPVRManager().GetTVDatabase());
return database ? database->Delete(group) : false;
}
return bFound;
}
bool CPVRChannelGroups::CreateChannelEpgs(void)
{
bool bReturn(false);
CSingleLock lock(m_critSection);
for (std::vector<CPVRChannelGroupPtr>::iterator it = m_groups.begin(); it != m_groups.end(); ++it)
{
/* Only create EPGs for the internatl groups */
if ((*it)->IsInternalGroup())
bReturn = (*it)->CreateChannelEpgs();
}
return bReturn;
}