summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/timers/PVRTimerRuleMatcher.cpp
blob: 9178e85ec206d7cdea4e7b7d56988b598c4bd529 (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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 *  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.
 */

#include "PVRTimerRuleMatcher.h"

#include "XBDateTime.h"
#include "addons/kodi-dev-kit/include/kodi/c-api/addon-instance/pvr/pvr_channels.h" // PVR_CHANNEL_INVALID_UID
#include "pvr/epg/EpgInfoTag.h"
#include "pvr/timers/PVRTimerInfoTag.h"
#include "utils/RegExp.h"

using namespace PVR;

CPVRTimerRuleMatcher::CPVRTimerRuleMatcher(const std::shared_ptr<CPVRTimerInfoTag>& timerRule,
                                           const CDateTime& start)
  : m_timerRule(timerRule), m_start(CPVRTimerInfoTag::ConvertUTCToLocalTime(start))
{
}

CPVRTimerRuleMatcher::~CPVRTimerRuleMatcher() = default;

std::shared_ptr<CPVRChannel> CPVRTimerRuleMatcher::GetChannel() const
{
  if (m_timerRule->GetTimerType()->SupportsChannels())
    return m_timerRule->Channel();

  return {};
}

CDateTime CPVRTimerRuleMatcher::GetNextTimerStart() const
{
  if (!m_timerRule->GetTimerType()->SupportsStartTime())
    return CDateTime(); // invalid datetime

  const CDateTime startDateLocal = m_timerRule->GetTimerType()->SupportsFirstDay()
                                       ? m_timerRule->FirstDayAsLocalTime()
                                       : m_start;
  const CDateTime startTimeLocal = m_timerRule->StartAsLocalTime();
  CDateTime nextStart(startDateLocal.GetYear(), startDateLocal.GetMonth(), startDateLocal.GetDay(),
                      startTimeLocal.GetHour(), startTimeLocal.GetMinute(), 0);

  const CDateTimeSpan oneDay(1, 0, 0, 0);
  while (nextStart < m_start)
  {
    nextStart += oneDay;
  }

  if (m_timerRule->GetTimerType()->SupportsWeekdays() &&
      m_timerRule->WeekDays() != PVR_WEEKDAY_ALLDAYS)
  {
    bool bMatch = false;
    while (!bMatch)
    {
      int startWeekday = nextStart.GetDayOfWeek();
      if (startWeekday == 0)
        startWeekday = 7;

      bMatch = ((1 << (startWeekday - 1)) & m_timerRule->WeekDays());
      if (!bMatch)
        nextStart += oneDay;
    }
  }

  return nextStart.GetAsUTCDateTime();
}

bool CPVRTimerRuleMatcher::Matches(const std::shared_ptr<CPVREpgInfoTag>& epgTag) const
{
  return epgTag && CPVRTimerInfoTag::ConvertUTCToLocalTime(epgTag->EndAsUTC()) > m_start &&
         MatchSeriesLink(epgTag) && MatchChannel(epgTag) && MatchStart(epgTag) &&
         MatchEnd(epgTag) && MatchDayOfWeek(epgTag) && MatchSearchText(epgTag);
}

bool CPVRTimerRuleMatcher::MatchSeriesLink(const std::shared_ptr<CPVREpgInfoTag>& epgTag) const
{
  if (m_timerRule->GetTimerType()->RequiresEpgSeriesLinkOnCreate())
    return epgTag->SeriesLink() == m_timerRule->SeriesLink();
  else
    return true;
}

bool CPVRTimerRuleMatcher::MatchChannel(const std::shared_ptr<CPVREpgInfoTag>& epgTag) const
{
  if (m_timerRule->GetTimerType()->SupportsAnyChannel() &&
      m_timerRule->ClientChannelUID() == PVR_CHANNEL_INVALID_UID)
    return true; // matches any channel

  if (m_timerRule->GetTimerType()->SupportsChannels())
    return m_timerRule->ClientChannelUID() != PVR_CHANNEL_INVALID_UID &&
           epgTag->ClientID() == m_timerRule->ClientID() &&
           epgTag->UniqueChannelID() == m_timerRule->ClientChannelUID();
  else
    return true;
}

bool CPVRTimerRuleMatcher::MatchStart(const std::shared_ptr<CPVREpgInfoTag>& epgTag) const
{
  if (m_timerRule->GetTimerType()->SupportsFirstDay())
  {
    // only year, month and day do matter here...
    const CDateTime startEpgLocal = CPVRTimerInfoTag::ConvertUTCToLocalTime(epgTag->StartAsUTC());
    const CDateTime startEpg(startEpgLocal.GetYear(), startEpgLocal.GetMonth(),
                             startEpgLocal.GetDay(), 0, 0, 0);
    const CDateTime firstDayLocal = m_timerRule->FirstDayAsLocalTime();
    const CDateTime startTimer(firstDayLocal.GetYear(), firstDayLocal.GetMonth(),
                               firstDayLocal.GetDay(), 0, 0, 0);
    if (startEpg < startTimer)
      return false;
  }

  if (m_timerRule->GetTimerType()->SupportsStartAnyTime() && m_timerRule->IsStartAnyTime())
    return true; // matches any start time

  if (m_timerRule->GetTimerType()->SupportsStartTime())
  {
    // only hours and minutes do matter here...
    const CDateTime startEpgLocal = CPVRTimerInfoTag::ConvertUTCToLocalTime(epgTag->StartAsUTC());
    const CDateTime startEpg(2000, 1, 1, startEpgLocal.GetHour(), startEpgLocal.GetMinute(), 0);
    const CDateTime startTimerLocal = m_timerRule->StartAsLocalTime();
    const CDateTime startTimer(2000, 1, 1, startTimerLocal.GetHour(), startTimerLocal.GetMinute(),
                               0);
    return startEpg >= startTimer;
  }
  else
    return true;
}

bool CPVRTimerRuleMatcher::MatchEnd(const std::shared_ptr<CPVREpgInfoTag>& epgTag) const
{
  if (m_timerRule->GetTimerType()->SupportsEndAnyTime() && m_timerRule->IsEndAnyTime())
    return true; // matches any end time

  if (m_timerRule->GetTimerType()->SupportsEndTime())
  {
    // only hours and minutes do matter here...
    const CDateTime endEpgLocal = CPVRTimerInfoTag::ConvertUTCToLocalTime(epgTag->EndAsUTC());
    const CDateTime endEpg(2000, 1, 1, endEpgLocal.GetHour(), endEpgLocal.GetMinute(), 0);
    const CDateTime endTimerLocal = m_timerRule->EndAsLocalTime();
    const CDateTime endTimer(2000, 1, 1, endTimerLocal.GetHour(), endTimerLocal.GetMinute(), 0);
    return endEpg <= endTimer;
  }
  else
    return true;
}

bool CPVRTimerRuleMatcher::MatchDayOfWeek(const std::shared_ptr<CPVREpgInfoTag>& epgTag) const
{
  if (m_timerRule->GetTimerType()->SupportsWeekdays())
  {
    if (m_timerRule->WeekDays() != PVR_WEEKDAY_ALLDAYS)
    {
      const CDateTime startEpgLocal = CPVRTimerInfoTag::ConvertUTCToLocalTime(epgTag->StartAsUTC());
      int startWeekday = startEpgLocal.GetDayOfWeek();
      if (startWeekday == 0)
        startWeekday = 7;

      return ((1 << (startWeekday - 1)) & m_timerRule->WeekDays());
    }
  }
  return true;
}

bool CPVRTimerRuleMatcher::MatchSearchText(const std::shared_ptr<CPVREpgInfoTag>& epgTag) const
{
  if (m_timerRule->GetTimerType()->SupportsEpgFulltextMatch() && m_timerRule->IsFullTextEpgSearch())
  {
    if (!m_textSearch)
    {
      m_textSearch.reset(new CRegExp(true /* case insensitive */));
      m_textSearch->RegComp(m_timerRule->EpgSearchString());
    }
    return m_textSearch->RegFind(epgTag->Title()) >= 0 ||
           m_textSearch->RegFind(epgTag->EpisodeName()) >= 0 ||
           m_textSearch->RegFind(epgTag->PlotOutline()) >= 0 ||
           m_textSearch->RegFind(epgTag->Plot()) >= 0;
  }
  else if (m_timerRule->GetTimerType()->SupportsEpgTitleMatch())
  {
    if (!m_textSearch)
    {
      m_textSearch.reset(new CRegExp(true /* case insensitive */));
      m_textSearch->RegComp(m_timerRule->EpgSearchString());
    }
    return m_textSearch->RegFind(epgTag->Title()) >= 0;
  }
  else
    return true;
}