summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/guilib/GUIEPGGridContainerModel.h
blob: 2e0a6d6ffa710607696e0e74fe380d0830f2f3e8 (plain)
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
/*
 *  Copyright (C) 2012-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#pragma once

#include "XBDateTime.h"

#include <functional>
#include <map>
#include <memory>
#include <unordered_map>
#include <utility>
#include <vector>

class CFileItem;
class CFileItemList;

namespace PVR
{
struct GridItem
{
  GridItem(const std::shared_ptr<CFileItem>& _item, float _width, int _startBlock, int _endBlock)
    : item(_item), originWidth(_width), width(_width), startBlock(_startBlock), endBlock(_endBlock)
  {
  }

  bool operator==(const GridItem& other) const
  {
    return (startBlock == other.startBlock && endBlock == other.endBlock);
  }

  std::shared_ptr<CFileItem> item;
  float originWidth = 0.0f;
  float width = 0.0f;
  int startBlock = 0;
  int endBlock = 0;
};

class CPVREpgInfoTag;

class CGUIEPGGridContainerModel
{
public:
  static constexpr int MINSPERBLOCK = 5; // minutes

  CGUIEPGGridContainerModel() = default;
  virtual ~CGUIEPGGridContainerModel() = default;

  void Initialize(const std::unique_ptr<CFileItemList>& items,
                  const CDateTime& gridStart,
                  const CDateTime& gridEnd,
                  int iFirstChannel,
                  int iChannelsPerPage,
                  int iFirstBlock,
                  int iBlocksPerPage,
                  int iRulerUnit,
                  float fBlockSize);
  void SetInvalid();

  static const int INVALID_INDEX = -1;
  void FindChannelAndBlockIndex(int channelUid,
                                unsigned int broadcastUid,
                                int eventOffset,
                                int& newChannelIndex,
                                int& newBlockIndex) const;

  void FreeChannelMemory(int keepStart, int keepEnd);
  bool FreeProgrammeMemory(int firstChannel, int lastChannel, int firstBlock, int lastBlock);
  void FreeRulerMemory(int keepStart, int keepEnd);

  std::shared_ptr<CFileItem> GetChannelItem(int iIndex) const { return m_channelItems[iIndex]; }
  bool HasChannelItems() const { return !m_channelItems.empty(); }
  int ChannelItemsSize() const { return static_cast<int>(m_channelItems.size()); }
  int GetLastChannel() const
  {
    return m_channelItems.empty() ? -1 : static_cast<int>(m_channelItems.size()) - 1;
  }

  std::shared_ptr<CFileItem> GetRulerItem(int iIndex) const { return m_rulerItems[iIndex]; }
  int RulerItemsSize() const { return static_cast<int>(m_rulerItems.size()); }

  int GridItemsSize() const { return m_blocks; }
  bool IsSameGridItem(int iChannel, int iBlock1, int iBlock2) const;
  std::shared_ptr<CFileItem> GetGridItem(int iChannel, int iBlock) const;
  int GetGridItemStartBlock(int iChannel, int iBlock) const;
  int GetGridItemEndBlock(int iChannel, int iBlock) const;
  CDateTime GetGridItemEndTime(int iChannel, int iBlock) const;
  float GetGridItemWidth(int iChannel, int iBlock) const;
  float GetGridItemOriginWidth(int iChannel, int iBlock) const;
  void DecreaseGridItemWidth(int iChannel, int iBlock, float fSize);

  bool IsZeroGridDuration() const { return (m_gridEnd - m_gridStart) == CDateTimeSpan(0, 0, 0, 0); }
  const CDateTime& GetGridStart() const { return m_gridStart; }
  const CDateTime& GetGridEnd() const { return m_gridEnd; }
  unsigned int GetGridStartPadding() const;

  unsigned int GetPageNowOffset() const;
  int GetNowBlock() const;
  int GetLastBlock() const { return m_blocks - 1; }

  CDateTime GetStartTimeForBlock(int block) const;
  int GetBlock(const CDateTime& datetime) const;
  int GetFirstEventBlock(const std::shared_ptr<CPVREpgInfoTag>& event) const;
  int GetLastEventBlock(const std::shared_ptr<CPVREpgInfoTag>& event) const;
  bool IsEventMemberOfBlock(const std::shared_ptr<CPVREpgInfoTag>& event, int iBlock) const;

  std::unique_ptr<CFileItemList> GetCurrentTimeLineItems(int firstChannel, int numChannels) const;

private:
  GridItem* GetGridItemPtr(int iChannel, int iBlock) const;
  std::shared_ptr<CFileItem> CreateGapItem(int iChannel) const;
  std::shared_ptr<CFileItem> GetItem(int iChannel, int iBlock) const;

  std::vector<std::shared_ptr<CPVREpgInfoTag>> GetEPGTimeline(int iChannel,
                                                              const CDateTime& minEventEnd,
                                                              const CDateTime& maxEventStart) const;

  struct EpgTags
  {
    std::vector<std::shared_ptr<CFileItem>> tags;
    int firstBlock = -1;
    int lastBlock = -1;
  };

  using EpgTagsMap = std::unordered_map<int, EpgTags>;

  std::shared_ptr<CFileItem> CreateEpgTags(int iChannel, int iBlock) const;
  std::shared_ptr<CFileItem> GetEpgTags(EpgTagsMap::iterator& itEpg,
                                        int iChannel,
                                        int iBlock) const;
  std::shared_ptr<CFileItem> GetEpgTagsBefore(EpgTags& epgTags, int iChannel, int iBlock) const;
  std::shared_ptr<CFileItem> GetEpgTagsAfter(EpgTags& epgTags, int iChannel, int iBlock) const;

  mutable EpgTagsMap m_epgItems;

  CDateTime m_gridStart;
  CDateTime m_gridEnd;

  std::vector<std::shared_ptr<CFileItem>> m_channelItems;
  std::vector<std::shared_ptr<CFileItem>> m_rulerItems;

  struct GridCoordinates
  {
    GridCoordinates(int _channel, int _block) : channel(_channel), block(_block) {}

    bool operator==(const GridCoordinates& other) const
    {
      return (channel == other.channel && block == other.block);
    }

    int channel = 0;
    int block = 0;
  };

  struct GridCoordinatesHash
  {
    std::size_t operator()(const GridCoordinates& coordinates) const
    {
      return std::hash<int>()(coordinates.channel) ^ std::hash<int>()(coordinates.block);
    }
  };

  mutable std::unordered_map<GridCoordinates, GridItem, GridCoordinatesHash> m_gridIndex;

  int m_blocks = 0;
  float m_fBlockSize = 0.0f;

  int m_firstActiveChannel = 0;
  int m_lastActiveChannel = 0;
  int m_firstActiveBlock = 0;
  int m_lastActiveBlock = 0;
};
} // namespace PVR