summaryrefslogtreecommitdiffstats
path: root/xbmc/pvr/guilib/PVRGUIActionsPowerManagement.cpp
blob: 1a0a99a7a295d8dc13d6ecf40f48b43cc390f836 (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
/*
 *  Copyright (C) 2016-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 "PVRGUIActionsPowerManagement.h"

#include "FileItem.h"
#include "ServiceBroker.h"
#include "guilib/LocalizeStrings.h"
#include "messaging/helpers/DialogHelper.h"
#include "network/Network.h"
#include "pvr/PVRManager.h"
#include "pvr/addons/PVRClient.h"
#include "pvr/timers/PVRTimerInfoTag.h"
#include "pvr/timers/PVRTimers.h"
#include "settings/Settings.h"
#include "utils/StringUtils.h"
#include "utils/Variant.h"

#include <memory>
#include <string>
#include <vector>

using namespace PVR;
using namespace KODI::MESSAGING;

CPVRGUIActionsPowerManagement::CPVRGUIActionsPowerManagement()
  : m_settings({CSettings::SETTING_PVRPOWERMANAGEMENT_DAILYWAKEUPTIME,
                CSettings::SETTING_PVRPOWERMANAGEMENT_BACKENDIDLETIME})
{
}

bool CPVRGUIActionsPowerManagement::CanSystemPowerdown(bool bAskUser /*= true*/) const
{
  bool bReturn(true);
  if (CServiceBroker::GetPVRManager().IsStarted())
  {
    std::shared_ptr<CPVRTimerInfoTag> cause;
    if (!AllLocalBackendsIdle(cause))
    {
      if (bAskUser)
      {
        std::string text;

        if (cause)
        {
          if (cause->IsRecording())
          {
            text = StringUtils::Format(
                g_localizeStrings.Get(19691), // "PVR is currently recording...."
                cause->Title(), cause->ChannelName());
          }
          else
          {
            // Next event is due to a local recording or reminder.
            const CDateTime now(CDateTime::GetUTCDateTime());
            const CDateTime start(cause->StartAsUTC());
            const CDateTimeSpan prestart(0, 0, cause->MarginStart(), 0);

            CDateTimeSpan diff(start - now);
            diff -= prestart;
            int mins = diff.GetSecondsTotal() / 60;

            std::string dueStr;
            if (mins > 1)
            {
              // "%d minutes"
              dueStr = StringUtils::Format(g_localizeStrings.Get(19694), mins);
            }
            else
            {
              // "about a minute"
              dueStr = g_localizeStrings.Get(19695);
            }

            text = StringUtils::Format(
                cause->IsReminder()
                    ? g_localizeStrings.Get(19690) // "PVR has scheduled a reminder...."
                    : g_localizeStrings.Get(19692), // "PVR will start recording...."
                cause->Title(), cause->ChannelName(), dueStr);
          }
        }
        else
        {
          // Next event is due to automatic daily wakeup of PVR.
          const CDateTime now(CDateTime::GetUTCDateTime());

          CDateTime dailywakeuptime;
          dailywakeuptime.SetFromDBTime(
              m_settings.GetStringValue(CSettings::SETTING_PVRPOWERMANAGEMENT_DAILYWAKEUPTIME));
          dailywakeuptime = dailywakeuptime.GetAsUTCDateTime();

          const CDateTimeSpan diff(dailywakeuptime - now);
          int mins = diff.GetSecondsTotal() / 60;

          std::string dueStr;
          if (mins > 1)
          {
            // "%d minutes"
            dueStr = StringUtils::Format(g_localizeStrings.Get(19694), mins);
          }
          else
          {
            // "about a minute"
            dueStr = g_localizeStrings.Get(19695);
          }

          text = StringUtils::Format(g_localizeStrings.Get(19693), // "Daily wakeup is due in...."
                                     dueStr);
        }

        // Inform user about PVR being busy. Ask if user wants to powerdown anyway.
        bReturn = HELPERS::ShowYesNoDialogText(CVariant{19685}, // "Confirm shutdown"
                                               CVariant{text}, CVariant{222}, // "Shutdown anyway",
                                               CVariant{19696}, // "Cancel"
                                               10000) // timeout value before closing
                  == HELPERS::DialogResponse::CHOICE_YES;
      }
      else
        bReturn = false; // do not powerdown (busy, but no user interaction requested).
    }
  }
  return bReturn;
}

bool CPVRGUIActionsPowerManagement::AllLocalBackendsIdle(
    std::shared_ptr<CPVRTimerInfoTag>& causingEvent) const
{
  // active recording on local backend?
  const std::vector<std::shared_ptr<CPVRTimerInfoTag>> activeRecordings =
      CServiceBroker::GetPVRManager().Timers()->GetActiveRecordings();
  for (const auto& timer : activeRecordings)
  {
    if (EventOccursOnLocalBackend(timer))
    {
      causingEvent = timer;
      return false;
    }
  }

  // soon recording on local backend?
  if (IsNextEventWithinBackendIdleTime())
  {
    const std::shared_ptr<CPVRTimerInfoTag> timer =
        CServiceBroker::GetPVRManager().Timers()->GetNextActiveTimer(false);
    if (!timer)
    {
      // Next event is due to automatic daily wakeup of PVR!
      causingEvent.reset();
      return false;
    }

    if (EventOccursOnLocalBackend(timer))
    {
      causingEvent = timer;
      return false;
    }
  }
  return true;
}

bool CPVRGUIActionsPowerManagement::EventOccursOnLocalBackend(
    const std::shared_ptr<CPVRTimerInfoTag>& event) const
{
  const std::shared_ptr<CPVRClient> client =
      CServiceBroker::GetPVRManager().GetClient(CFileItem(event));
  if (client)
  {
    const std::string hostname = client->GetBackendHostname();
    if (!hostname.empty() && CServiceBroker::GetNetwork().IsLocalHost(hostname))
      return true;
  }
  return false;
}

bool CPVRGUIActionsPowerManagement::IsNextEventWithinBackendIdleTime() const
{
  // timers going off soon?
  const CDateTime now(CDateTime::GetUTCDateTime());
  const CDateTimeSpan idle(
      0, 0, m_settings.GetIntValue(CSettings::SETTING_PVRPOWERMANAGEMENT_BACKENDIDLETIME), 0);
  const CDateTime next(CServiceBroker::GetPVRManager().Timers()->GetNextEventTime());
  const CDateTimeSpan delta(next - now);

  return (delta <= idle);
}