xbmc
PVRChannelGroup.h
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 #pragma once
10 
11 #include "pvr/channels/PVRChannelGroupSettings.h"
12 #include "pvr/channels/PVRChannelNumber.h"
13 #include "pvr/channels/PVRChannelsPath.h"
14 #include "utils/EventStream.h"
15 
16 #include <map>
17 #include <memory>
18 #include <optional>
19 #include <string>
20 #include <utility>
21 #include <vector>
22 
23 struct PVR_CHANNEL_GROUP;
24 
25 namespace PVR
26 {
27 static constexpr int PVR_GROUP_TYPE_REMOTE = 0;
28 static constexpr int PVR_GROUP_TYPE_ALL_CHANNELS = 1;
29 static constexpr int PVR_GROUP_TYPE_LOCAL = 2;
30 
31 static constexpr int PVR_GROUP_CLIENT_ID_UNKNOWN = -2;
32 static constexpr int PVR_GROUP_CLIENT_ID_LOCAL = -1;
33 
34 static constexpr int PVR_GROUP_ID_UNNKOWN{-1};
35 
36 enum class PVREvent;
37 
38 class CPVRChannel;
39 class CPVRChannelGroupMember;
40 class CPVRClient;
41 class CPVREpgInfoTag;
42 
43 enum RenumberMode
44 {
45  NORMAL = 0,
46  IGNORE_NUMBERING_FROM_ONE = 1
47 };
48 
49 using GroupMemberPair =
50  std::pair<std::shared_ptr<CPVRChannelGroupMember>, std::shared_ptr<CPVRChannelGroupMember>>;
51 
53 {
54  friend class CPVRChannelGroups; // for GroupType()
55  friend class CPVRDatabase; // for GroupType()
56 
57 public:
58  static const int INVALID_GROUP_ID = -1;
59 
66  const std::shared_ptr<CPVRChannelGroup>& allChannelsGroup);
67 
68  ~CPVRChannelGroup() override;
69 
70  bool operator==(const CPVRChannelGroup& right) const;
71  bool operator!=(const CPVRChannelGroup& right) const;
72 
78 
82  CEventStream<PVREvent>& Events() { return m_events; }
83 
90  bool LoadFromDatabase(const std::map<std::pair<int, int>, std::shared_ptr<CPVRChannel>>& channels,
91  const std::vector<std::shared_ptr<CPVRClient>>& clients);
92 
96  void Unload();
97 
101  size_t Size() const;
102 
108  virtual bool UpdateFromClients(const std::vector<std::shared_ptr<CPVRClient>>& clients) = 0;
109 
114  int GetClientID() const;
115 
120  void SetClientID(int clientID);
121 
126  const CPVRChannelsPath& GetPath() const;
127 
132  void SetPath(const CPVRChannelsPath& path);
133 
139  virtual bool RemoveFromGroup(const std::shared_ptr<CPVRChannelGroupMember>& groupMember);
140 
146  virtual bool AppendToGroup(const std::shared_ptr<CPVRChannelGroupMember>& groupMember);
147 
153  virtual bool IsGroupMember(const std::shared_ptr<CPVRChannelGroupMember>& groupMember) const;
154 
160  void SetGroupName(const std::string& strGroupName, bool isUserSetName = false);
161 
166  void SetClientGroupName(const std::string& groupName);
167 
172  bool IsUserSetName() const { return m_isUserSetName; }
173 
178  bool Persist();
179 
184  bool IsRadio() const;
185 
190  int GroupID() const;
191 
196  void SetGroupID(int iGroupId);
197 
201  time_t LastWatched() const;
202 
207  void SetLastWatched(time_t iLastWatched);
208 
212  uint64_t LastOpened() const;
213 
218  void SetLastOpened(uint64_t iLastOpened);
219 
224  std::string GroupName() const;
225 
230  std::string ClientGroupName() const;
231 
235 
239  void Sort();
240 
245  bool SortAndRenumber();
246 
252  bool Renumber(RenumberMode mode = NORMAL);
253 
255 
256  // IChannelGroupSettingsCallback implementation
257  void UseBackendChannelOrderChanged() override;
258  void UseBackendChannelNumbersChanged() override;
259  void StartGroupChannelNumbersFromOneChanged() override;
260 
266  std::shared_ptr<CPVRChannelGroupMember> GetLastPlayedChannelGroupMember(
267  int iCurrentChannel = -1) const;
268 
273  GroupMemberPair GetLastAndPreviousToLastPlayedChannelGroupMember() const;
274 
280  std::shared_ptr<CPVRChannelGroupMember> GetByChannelNumber(
281  const CPVRChannelNumber& channelNumber) const;
282 
288  CPVRChannelNumber GetChannelNumber(const std::shared_ptr<CPVRChannel>& channel) const;
289 
295  CPVRChannelNumber GetClientChannelNumber(const std::shared_ptr<CPVRChannel>& channel) const;
296 
302  std::shared_ptr<CPVRChannelGroupMember> GetNextChannelGroupMember(
303  const std::shared_ptr<CPVRChannelGroupMember>& groupMember) const;
304 
310  std::shared_ptr<CPVRChannelGroupMember> GetPreviousChannelGroupMember(
311  const std::shared_ptr<CPVRChannelGroupMember>& groupMember) const;
312 
318  std::shared_ptr<CPVRChannel> GetByChannelID(int iChannelID) const;
319 
320  enum class Include
321  {
322  ALL,
323  ONLY_HIDDEN,
324  ONLY_VISIBLE
325  };
326 
332  std::vector<std::shared_ptr<CPVRChannelGroupMember>> GetMembers(
333  Include eFilter = Include::ALL) const;
334 
339  void GetChannelNumbers(std::vector<std::string>& channelNumbers) const;
340 
345  bool HasChannels() const;
346 
351  bool HasNewChannels() const;
352 
357  bool HasHiddenChannels() const;
358 
362  bool HasChanges() const;
363 
367  bool IsNew() const;
368 
375  std::shared_ptr<CPVRChannel> GetByUniqueID(int iUniqueChannelId, int iClientID) const;
376 
382  std::shared_ptr<CPVRChannelGroupMember> GetByUniqueID(const std::pair<int, int>& id) const;
383 
384  bool SetHidden(bool bHidden);
385  bool IsHidden() const;
386 
391  int GetPosition() const;
392 
397  void SetPosition(int iPosition);
398 
403  int GetClientPosition() const;
404 
409  void SetClientPosition(int iPosition);
410 
417  bool HasValidDataForClient(int iClientId) const;
418 
425  bool HasValidDataForClients(const std::vector<std::shared_ptr<CPVRClient>>& clients) const;
426 
432 
436  void Delete();
437 
442  bool IsDeleted() const { return m_bDeleted; }
443 
448  int CleanupCachedImages();
449 
454  int GetClientPriority() const;
455 
459  void UpdateClientPriorities();
460 
465  virtual bool SupportsDelete() const = 0;
466 
471  virtual bool SupportsMemberAdd() const = 0;
472 
477  virtual bool SupportsMemberRemove() const = 0;
478 
483  virtual bool IsChannelsOwner() const = 0;
484 
485 protected:
491  virtual std::vector<std::shared_ptr<CPVRChannelGroupMember>> RemoveDeletedGroupMembers(
492  const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers);
493 
499  bool UpdateGroupEntries(const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers);
500 
505 
509  void SortByChannelNumber();
510 
515 
516  std::shared_ptr<CPVRChannelGroupSettings> GetSettings() const;
517 
518  int m_iGroupId = INVALID_GROUP_ID;
519  bool m_bLoaded = false;
520  bool m_bChanged =
521  false;
522  time_t m_iLastWatched = 0;
523  uint64_t m_iLastOpened = 0;
524  bool m_bHidden = false;
525  int m_iPosition = 0;
526  std::vector<std::shared_ptr<CPVRChannelGroupMember>>
528  std::map<std::pair<int, int>, std::shared_ptr<CPVRChannelGroupMember>>
530  mutable CCriticalSection m_critSection;
531  std::vector<int> m_failedClients;
532  CEventSource<PVREvent> m_events;
533  mutable std::shared_ptr<CPVRChannelGroupSettings> m_settings;
534 
535  // the settings singleton shared between all group instances
536  static CCriticalSection m_settingsSingletonCritSection;
537  static std::weak_ptr<CPVRChannelGroupSettings> m_settingsSingleton;
538 
539 private:
543  virtual int GroupType() const = 0;
544 
550  int LoadFromDatabase(const std::vector<std::shared_ptr<CPVRClient>>& clients);
551 
556  void DeleteGroupMembersFromDb(
557  const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& membersToDelete);
558 
564  bool UpdateFromClient(const std::shared_ptr<CPVRChannelGroupMember>& groupMember);
565 
571  bool AddAndUpdateGroupMembers(
572  const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers);
573 
574  void OnSettingChanged();
575 
576  std::shared_ptr<CPVRChannelGroup> m_allChannelsGroup;
577  CPVRChannelsPath m_path;
578  bool m_bDeleted = false;
579  mutable std::optional<int> m_clientPriority;
580  bool m_isUserSetName{false};
581  std::string m_clientGroupName;
582  int m_iClientPosition{0};
583 };
584 } // namespace PVR
void SetLastWatched(time_t iLastWatched)
Last time group has been watched.
Definition: PVRChannelGroup.cpp:1064
bool LoadFromDatabase(const std::map< std::pair< int, int >, std::shared_ptr< CPVRChannel >> &channels, const std::vector< std::shared_ptr< CPVRClient >> &clients)
Load the channels from the database.
Definition: PVRChannelGroup.cpp:97
virtual bool RemoveFromGroup(const std::shared_ptr< CPVRChannelGroupMember > &groupMember)
Remove a channel group member from this container.
Definition: PVRChannelGroup.cpp:759
uint64_t m_iLastOpened
Definition: PVRChannelGroup.h:523
std::shared_ptr< CPVRChannelGroupMember > GetByChannelNumber(const CPVRChannelNumber &channelNumber) const
Get a channel group member given it&#39;s active channel number.
Definition: PVRChannelGroup.cpp:371
void Sort()
Sort the group.
Definition: PVRChannelGroup.cpp:208
int GetClientID() const
Get the identifier of the client that serves this channel group.
Definition: PVRChannelGroup.cpp:140
std::shared_ptr< CPVRChannelGroupMember > GetNextChannelGroupMember(const std::shared_ptr< CPVRChannelGroupMember > &groupMember) const
Get the next channel group member in this group.
Definition: PVRChannelGroup.cpp:387
Definition: ContextMenuManager.h:24
int CleanupCachedImages()
Erase stale texture db entries and image files.
Definition: PVRChannelGroup.cpp:1173
std::string ClientGroupName() const
Get the name of this group on the client.
Definition: PVRChannelGroup.cpp:1031
std::shared_ptr< CPVRChannelGroupMember > GetLastPlayedChannelGroupMember(int iCurrentChannel=-1) const
Get the channel group member that was played last.
Definition: PVRChannelGroup.cpp:309
virtual bool SupportsDelete() const =0
Check whether this group could be deleted by the user.
time_t m_iLastWatched
Definition: PVRChannelGroup.h:522
CPVRChannelNumber GetClientChannelNumber(const std::shared_ptr< CPVRChannel > &channel) const
Get the client channel number in this group of the given channel.
Definition: PVRChannelGroup.cpp:363
virtual bool IsChannelsOwner() const =0
Check whether this group is owner of the channel instances it contains.
std::shared_ptr< CPVRChannel > GetByChannelID(int iChannelID) const
Get a channel given it&#39;s channel ID.
Definition: PVRChannelGroup.cpp:299
int GetPosition() const
Get the local position of this group.
Definition: PVRChannelGroup.cpp:1137
A container class for channel groups.
Definition: PVRChannelGroups.h:30
int GetClientPriority() const
Get the priority of the client that provides this group.
Definition: PVRChannelGroup.cpp:1188
void UpdateClientPriorities()
Update the client priority for this group and all members of this group.
Definition: PVRChannelGroup.cpp:235
Definition: PVRChannelsPath.h:17
virtual std::vector< std::shared_ptr< CPVRChannelGroupMember > > RemoveDeletedGroupMembers(const std::vector< std::shared_ptr< CPVRChannelGroupMember >> &groupMembers)
Remove deleted group members from this group.
Definition: PVRChannelGroup.cpp:670
void FillAddonData(PVR_CHANNEL_GROUP &group) const
Copy over data to the given PVR_CHANNEL_GROUP instance.
Definition: PVRChannelGroup.cpp:66
std::shared_ptr< CPVRChannel > GetByUniqueID(int iUniqueChannelId, int iClientID) const
Get a channel given the channel number on the client.
Definition: PVRChannelGroup.cpp:291
void GetChannelNumbers(std::vector< std::string > &channelNumbers) const
Get the list of active channel numbers in a group.
Definition: PVRChannelGroup.cpp:473
std::string GroupName() const
The name of this group.
Definition: PVRChannelGroup.cpp:1002
bool UpdateGroupEntries(const std::vector< std::shared_ptr< CPVRChannelGroupMember >> &groupMembers)
Update the current channel group members with the given list.
Definition: PVRChannelGroup.cpp:729
CPVRChannelNumber GetChannelNumber(const std::shared_ptr< CPVRChannel > &channel) const
Get the channel number in this group of the given channel.
Definition: PVRChannelGroup.cpp:355
void SetLastOpened(uint64_t iLastOpened)
Set the time in milliseconds from epoch this group was last opened.
Definition: PVRChannelGroup.cpp:1084
bool Persist()
Persist changed or new data.
Definition: PVRChannelGroup.cpp:823
bool HasChannels() const
Check whether this container has any channels.
Definition: PVRChannelGroup.cpp:1104
bool m_bChanged
Definition: PVRChannelGroup.h:520
size_t Size() const
Definition: PVRChannelGroup.cpp:1098
void SetClientPosition(int iPosition)
Set the client-supplied position of this group.
Definition: PVRChannelGroup.cpp:1161
void SetPosition(int iPosition)
Set the local position of this group.
Definition: PVRChannelGroup.cpp:1143
bool HasValidDataForClients(const std::vector< std::shared_ptr< CPVRClient >> &clients) const
Check, whether data for given pvr clients are currently valid. For instance, data can be invalid beca...
Definition: PVRChannelGroup.cpp:641
CPVRChannelGroup(const CPVRChannelsPath &path, const std::shared_ptr< CPVRChannelGroup > &allChannelsGroup)
Create a new channel group instance.
Definition: PVRChannelGroup.cpp:40
void SetGroupName(const std::string &strGroupName, bool isUserSetName=false)
Change the name of this group.
Definition: PVRChannelGroup.cpp:1008
virtual bool UpdateFromClients(const std::vector< std::shared_ptr< CPVRClient >> &clients)=0
Update data with channel group members from the given clients, sync with local data.
bool IsUserSetName() const
Check whether the group name was set by the user.
Definition: PVRChannelGroup.h:172
bool UpdateChannelNumbersFromAllChannelsGroup()
Update the channel numbers according to the all channels group and publish event. ...
Definition: PVRChannelGroup.cpp:650
std::shared_ptr< CPVRChannelGroupMember > GetPreviousChannelGroupMember(const std::shared_ptr< CPVRChannelGroupMember > &groupMember) const
Get the previous channel group member in this group.
Definition: PVRChannelGroup.cpp:415
const CPVRChannelsPath & GetPath() const
Get the path of this group.
Definition: PVRChannelGroup.cpp:160
bool Renumber(RenumberMode mode=NORMAL)
Remove invalid channels and updates the channel numbers.
Definition: PVRChannelGroup.cpp:872
bool m_bLoaded
Definition: PVRChannelGroup.h:519
virtual bool SupportsMemberRemove() const =0
Check whether members could be removed from this group by the user.
bool HasChanges() const
Definition: PVRChannelGroup.cpp:930
int m_iPosition
Definition: PVRChannelGroup.h:525
bool HasHiddenChannels() const
Check whether this container has any hidden channels.
Definition: PVRChannelGroup.cpp:1110
int GetClientPosition() const
Get the position of this group as supplied by the PVR client.
Definition: PVRChannelGroup.cpp:1155
Definition: SmartPlayList.cpp:137
void SortByClientChannelNumber()
Sort the current channel list by client channel number.
Definition: PVRChannelGroup.cpp:223
"C" PVR add-on channel group.
Definition: pvr_channel_groups.h:30
int GroupID() const
The database ID of this group.
Definition: PVRChannelGroup.cpp:984
bool HasValidDataForClient(int iClientId) const
Check, whether data for a given pvr client are currently valid. For instance, data can be invalid bec...
Definition: PVRChannelGroup.cpp:635
std::map< std::pair< int, int >, std::shared_ptr< CPVRChannelGroupMember > > m_members
Definition: PVRChannelGroup.h:529
bool SortAndRenumber()
Sort the group and fix up channel numbers.
Definition: PVRChannelGroup.cpp:216
void SetPath(const CPVRChannelsPath &path)
Set the path of this group.
Definition: PVRChannelGroup.cpp:166
std::vector< std::shared_ptr< CPVRChannelGroupMember > > GetMembers(Include eFilter=Include::ALL) const
Get the current members of this group.
Definition: PVRChannelGroup.cpp:443
void SetClientID(int clientID)
Set the identifier of the client that serves this channel group.
Definition: PVRChannelGroup.cpp:146
Definition: PVRDatabase.h:33
bool HasNewChannels() const
Check whether this container has any new channels.
Definition: PVRChannelGroup.cpp:923
time_t LastWatched() const
Definition: PVRChannelGroup.cpp:1058
virtual bool AppendToGroup(const std::shared_ptr< CPVRChannelGroupMember > &groupMember)
Append a channel group member to this container.
Definition: PVRChannelGroup.cpp:785
Definition: PVRChannelNumber.h:15
bool m_bHidden
Definition: PVRChannelGroup.h:524
Definition: PVRChannelGroupSettings.h:20
bool IsDeleted() const
Whether this group is deleted.
Definition: PVRChannelGroup.h:442
void Unload()
Clear all data.
Definition: PVRChannelGroup.cpp:132
uint64_t LastOpened() const
Definition: PVRChannelGroup.cpp:1078
int m_iGroupId
Definition: PVRChannelGroup.h:518
virtual bool IsGroupMember(const std::shared_ptr< CPVRChannelGroupMember > &groupMember) const
Check whether a channel group member is in this container.
Definition: PVRChannelGroup.cpp:816
void SetGroupID(int iGroupId)
Set the database ID of this group.
Definition: PVRChannelGroup.cpp:989
Definition: PVRChannelGroup.h:52
GroupMemberPair GetLastAndPreviousToLastPlayedChannelGroupMember() const
Get the last and previous to last played channel group members.
Definition: PVRChannelGroup.cpp:330
bool IsRadio() const
True if this group holds radio channels, false if it holds TV channels.
Definition: PVRChannelGroup.cpp:1052
bool IsNew() const
Definition: PVRChannelGroup.cpp:936
std::vector< std::shared_ptr< CPVRChannelGroupMember > > m_sortedMembers
Definition: PVRChannelGroup.h:527
void SortByChannelNumber()
Sort the current channel list by channel number.
Definition: PVRChannelGroup.cpp:229
void Delete()
Remove this group from database.
Definition: PVRChannelGroup.cpp:854
CEventStream< PVREvent > & Events()
Query the events available for CEventStream.
Definition: PVRChannelGroup.h:82
virtual bool SupportsMemberAdd() const =0
Check whether members could be added to this group by the user.
void SetClientGroupName(const std::string &groupName)
Set the name this group has on the client.
Definition: PVRChannelGroup.cpp:1037
bool UpdateMembersClientPriority()
Update the client priority for all members of this group.
Definition: PVRChannelGroup.cpp:248