kodi
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<const 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<const CPVRChannelGroupMember>& groupMember);
140 
146  virtual bool AppendToGroup(const std::shared_ptr<const CPVRChannelGroupMember>& groupMember);
147 
153  virtual bool IsGroupMember(
154  const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) const;
155 
161  void SetGroupName(const std::string& strGroupName, bool isUserSetName = false);
162 
167  void SetClientGroupName(const std::string& groupName);
168 
173  bool IsUserSetName() const { return m_isUserSetName; }
174 
179  bool Persist();
180 
185  bool IsRadio() const;
186 
191  int GroupID() const;
192 
197  void SetGroupID(int iGroupId);
198 
202  time_t LastWatched() const;
203 
208  void SetLastWatched(time_t iLastWatched);
209 
213  uint64_t LastOpened() const;
214 
219  void SetLastOpened(uint64_t iLastOpened);
220 
225  std::string GroupName() const;
226 
231  std::string ClientGroupName() const;
232 
236 
240  void Sort();
241 
246  bool SortAndRenumber();
247 
253  bool Renumber(RenumberMode mode = NORMAL);
254 
256 
257  // IChannelGroupSettingsCallback implementation
258  void UseBackendChannelOrderChanged() override;
259  void UseBackendChannelNumbersChanged() override;
260  void StartGroupChannelNumbersFromOneChanged() override;
261 
267  std::shared_ptr<CPVRChannelGroupMember> GetLastPlayedChannelGroupMember(
268  int iCurrentChannel = -1) const;
269 
274  GroupMemberPair GetLastAndPreviousToLastPlayedChannelGroupMember() const;
275 
281  std::shared_ptr<CPVRChannelGroupMember> GetByChannelNumber(
282  const CPVRChannelNumber& channelNumber) const;
283 
289  CPVRChannelNumber GetChannelNumber(const std::shared_ptr<const CPVRChannel>& channel) const;
290 
296  CPVRChannelNumber GetClientChannelNumber(const std::shared_ptr<const CPVRChannel>& channel) const;
297 
303  std::shared_ptr<CPVRChannelGroupMember> GetNextChannelGroupMember(
304  const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) const;
305 
311  std::shared_ptr<CPVRChannelGroupMember> GetPreviousChannelGroupMember(
312  const std::shared_ptr<const CPVRChannelGroupMember>& groupMember) const;
313 
319  std::shared_ptr<CPVRChannel> GetByChannelID(int iChannelID) const;
320 
321  enum class Include
322  {
323  ALL,
324  ONLY_HIDDEN,
325  ONLY_VISIBLE
326  };
327 
333  std::vector<std::shared_ptr<CPVRChannelGroupMember>> GetMembers(
334  Include eFilter = Include::ALL) const;
335 
340  void GetChannelNumbers(std::vector<std::string>& channelNumbers) const;
341 
346  bool HasChannels() const;
347 
352  bool HasNewChannels() const;
353 
358  bool HasHiddenChannels() const;
359 
363  bool HasChanges() const;
364 
368  bool IsNew() const;
369 
376  std::shared_ptr<CPVRChannel> GetByUniqueID(int iUniqueChannelId, int iClientID) const;
377 
383  std::shared_ptr<CPVRChannelGroupMember> GetByUniqueID(const std::pair<int, int>& id) const;
384 
385  bool SetHidden(bool bHidden);
386  bool IsHidden() const;
387 
392  int GetPosition() const;
393 
398  void SetPosition(int iPosition);
399 
404  int GetClientPosition() const;
405 
410  void SetClientPosition(int iPosition);
411 
418  bool HasValidDataForClient(int iClientId) const;
419 
426  bool HasValidDataForClients(const std::vector<std::shared_ptr<CPVRClient>>& clients) const;
427 
433 
437  void Delete();
438 
443  bool IsDeleted() const { return m_bDeleted; }
444 
449  int CleanupCachedImages();
450 
455  int GetClientPriority() const;
456 
460  void UpdateClientPriorities();
461 
466  virtual bool SupportsDelete() const = 0;
467 
472  virtual bool SupportsMemberAdd() const = 0;
473 
478  virtual bool SupportsMemberRemove() const = 0;
479 
484  virtual bool IsChannelsOwner() const = 0;
485 
486 protected:
492  virtual std::vector<std::shared_ptr<CPVRChannelGroupMember>> RemoveDeletedGroupMembers(
493  const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers);
494 
500  bool UpdateGroupEntries(const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers);
501 
506 
510  void SortByChannelNumber();
511 
516 
517  std::shared_ptr<CPVRChannelGroupSettings> GetSettings() const;
518 
519  int m_iGroupId = INVALID_GROUP_ID;
520  bool m_bLoaded = false;
521  bool m_bChanged =
522  false;
523  time_t m_iLastWatched = 0;
524  uint64_t m_iLastOpened = 0;
525  bool m_bHidden = false;
526  int m_iPosition = 0;
527  std::vector<std::shared_ptr<CPVRChannelGroupMember>>
529  std::map<std::pair<int, int>, std::shared_ptr<CPVRChannelGroupMember>>
531  mutable CCriticalSection m_critSection;
532  std::vector<int> m_failedClients;
533  CEventSource<PVREvent> m_events;
534  mutable std::shared_ptr<CPVRChannelGroupSettings> m_settings;
535 
536  // the settings singleton shared between all group instances
537  static CCriticalSection m_settingsSingletonCritSection;
538  static std::weak_ptr<CPVRChannelGroupSettings> m_settingsSingleton;
539 
540 private:
544  virtual int GroupType() const = 0;
545 
551  int LoadFromDatabase(const std::vector<std::shared_ptr<CPVRClient>>& clients);
552 
557  void DeleteGroupMembersFromDb(
558  const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& membersToDelete);
559 
565  bool UpdateFromClient(const std::shared_ptr<CPVRChannelGroupMember>& groupMember);
566 
572  bool AddAndUpdateGroupMembers(
573  const std::vector<std::shared_ptr<CPVRChannelGroupMember>>& groupMembers);
574 
575  void OnSettingChanged();
576 
577  std::shared_ptr<const CPVRChannelGroup> m_allChannelsGroup;
578  CPVRChannelsPath m_path;
579  bool m_bDeleted = false;
580  mutable std::optional<int> m_clientPriority;
581  bool m_isUserSetName{false};
582  std::string m_clientGroupName;
583  int m_iClientPosition{0};
584 };
585 } // namespace PVR
void SetLastWatched(time_t iLastWatched)
Last time group has been watched.
Definition: PVRChannelGroup.cpp:1068
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
uint64_t m_iLastOpened
Definition: PVRChannelGroup.h:524
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
CPVRChannelNumber GetChannelNumber(const std::shared_ptr< const CPVRChannel > &channel) const
Get the channel number in this group of the given channel.
Definition: PVRChannelGroup.cpp:355
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
Definition: ContextMenuManager.h:24
virtual bool AppendToGroup(const std::shared_ptr< const CPVRChannelGroupMember > &groupMember)
Append a channel group member to this container.
Definition: PVRChannelGroup.cpp:788
int CleanupCachedImages()
Erase stale texture db entries and image files.
Definition: PVRChannelGroup.cpp:1177
std::string ClientGroupName() const
Get the name of this group on the client.
Definition: PVRChannelGroup.cpp:1035
CPVRChannelGroup(const CPVRChannelsPath &path, const std::shared_ptr< const CPVRChannelGroup > &allChannelsGroup)
Create a new channel group instance.
Definition: PVRChannelGroup.cpp:40
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:523
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:1141
std::shared_ptr< CPVRChannelGroupMember > GetNextChannelGroupMember(const std::shared_ptr< const CPVRChannelGroupMember > &groupMember) const
Get the next channel group member in this group.
Definition: PVRChannelGroup.cpp:387
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:1192
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:672
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:1006
bool UpdateGroupEntries(const std::vector< std::shared_ptr< CPVRChannelGroupMember >> &groupMembers)
Update the current channel group members with the given list.
Definition: PVRChannelGroup.cpp:731
void SetLastOpened(uint64_t iLastOpened)
Set the time in milliseconds from epoch this group was last opened.
Definition: PVRChannelGroup.cpp:1088
bool Persist()
Persist changed or new data.
Definition: PVRChannelGroup.cpp:827
bool HasChannels() const
Check whether this container has any channels.
Definition: PVRChannelGroup.cpp:1108
bool m_bChanged
Definition: PVRChannelGroup.h:521
size_t Size() const
Definition: PVRChannelGroup.cpp:1102
void SetClientPosition(int iPosition)
Set the client-supplied position of this group.
Definition: PVRChannelGroup.cpp:1165
void SetPosition(int iPosition)
Set the local position of this group.
Definition: PVRChannelGroup.cpp:1147
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:642
virtual bool RemoveFromGroup(const std::shared_ptr< const CPVRChannelGroupMember > &groupMember)
Remove a channel group member from this container.
Definition: PVRChannelGroup.cpp:761
void SetGroupName(const std::string &strGroupName, bool isUserSetName=false)
Change the name of this group.
Definition: PVRChannelGroup.cpp:1012
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:173
bool UpdateChannelNumbersFromAllChannelsGroup()
Update the channel numbers according to the all channels group and publish event. ...
Definition: PVRChannelGroup.cpp:652
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:876
bool m_bLoaded
Definition: PVRChannelGroup.h:520
virtual bool SupportsMemberRemove() const =0
Check whether members could be removed from this group by the user.
bool HasChanges() const
Definition: PVRChannelGroup.cpp:934
int m_iPosition
Definition: PVRChannelGroup.h:526
bool HasHiddenChannels() const
Check whether this container has any hidden channels.
Definition: PVRChannelGroup.cpp:1114
int GetClientPosition() const
Get the position of this group as supplied by the PVR client.
Definition: PVRChannelGroup.cpp:1159
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:988
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:636
std::map< std::pair< int, int >, std::shared_ptr< CPVRChannelGroupMember > > m_members
Definition: PVRChannelGroup.h:530
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:927
time_t LastWatched() const
Definition: PVRChannelGroup.cpp:1062
Definition: PVRChannelNumber.h:15
bool m_bHidden
Definition: PVRChannelGroup.h:525
Definition: PVRChannelGroupSettings.h:20
virtual bool IsGroupMember(const std::shared_ptr< const CPVRChannelGroupMember > &groupMember) const
Check whether a channel group member is in this container.
Definition: PVRChannelGroup.cpp:820
bool IsDeleted() const
Whether this group is deleted.
Definition: PVRChannelGroup.h:443
void Unload()
Clear all data.
Definition: PVRChannelGroup.cpp:132
CPVRChannelNumber GetClientChannelNumber(const std::shared_ptr< const CPVRChannel > &channel) const
Get the client channel number in this group of the given channel.
Definition: PVRChannelGroup.cpp:363
uint64_t LastOpened() const
Definition: PVRChannelGroup.cpp:1082
int m_iGroupId
Definition: PVRChannelGroup.h:519
void SetGroupID(int iGroupId)
Set the database ID of this group.
Definition: PVRChannelGroup.cpp:993
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:1056
bool IsNew() const
Definition: PVRChannelGroup.cpp:940
std::vector< std::shared_ptr< CPVRChannelGroupMember > > m_sortedMembers
Definition: PVRChannelGroup.h:528
void SortByChannelNumber()
Sort the current channel list by channel number.
Definition: PVRChannelGroup.cpp:229
void Delete()
Remove this group from database.
Definition: PVRChannelGroup.cpp:858
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:1041
bool UpdateMembersClientPriority()
Update the client priority for all members of this group.
Definition: PVRChannelGroup.cpp:248
std::shared_ptr< CPVRChannelGroupMember > GetPreviousChannelGroupMember(const std::shared_ptr< const CPVRChannelGroupMember > &groupMember) const
Get the previous channel group member in this group.
Definition: PVRChannelGroup.cpp:415