2 * Copyright (C) 2016-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
9 #include "PVRGUIActionsPowerManagement.h"
12 #include "ServiceBroker.h"
13 #include "guilib/LocalizeStrings.h"
14 #include "messaging/helpers/DialogHelper.h"
15 #include "network/Network.h"
16 #include "pvr/PVRManager.h"
17 #include "pvr/addons/PVRClient.h"
18 #include "pvr/timers/PVRTimerInfoTag.h"
19 #include "pvr/timers/PVRTimers.h"
20 #include "settings/Settings.h"
21 #include "utils/StringUtils.h"
22 #include "utils/Variant.h"
29 using namespace KODI::MESSAGING
;
31 CPVRGUIActionsPowerManagement::CPVRGUIActionsPowerManagement()
32 : m_settings({CSettings::SETTING_PVRPOWERMANAGEMENT_DAILYWAKEUPTIME
,
33 CSettings::SETTING_PVRPOWERMANAGEMENT_BACKENDIDLETIME
})
37 bool CPVRGUIActionsPowerManagement::CanSystemPowerdown(bool bAskUser
/*= true*/) const
40 if (CServiceBroker::GetPVRManager().IsStarted())
42 std::shared_ptr
<CPVRTimerInfoTag
> cause
;
43 if (!AllLocalBackendsIdle(cause
))
51 if (cause
->IsRecording())
53 text
= StringUtils::Format(
54 g_localizeStrings
.Get(19691), // "PVR is currently recording...."
55 cause
->Title(), cause
->ChannelName());
59 // Next event is due to a local recording or reminder.
60 const CDateTime
now(CDateTime::GetUTCDateTime());
61 const CDateTime
start(cause
->StartAsUTC());
62 const CDateTimeSpan
prestart(0, 0, cause
->MarginStart(), 0);
64 CDateTimeSpan
diff(start
- now
);
66 int mins
= diff
.GetSecondsTotal() / 60;
72 dueStr
= StringUtils::Format(g_localizeStrings
.Get(19694), mins
);
77 dueStr
= g_localizeStrings
.Get(19695);
80 text
= StringUtils::Format(
82 ? g_localizeStrings
.Get(19690) // "PVR has scheduled a reminder...."
83 : g_localizeStrings
.Get(19692), // "PVR will start recording...."
84 cause
->Title(), cause
->ChannelName(), dueStr
);
89 // Next event is due to automatic daily wakeup of PVR.
90 const CDateTime
now(CDateTime::GetUTCDateTime());
92 CDateTime dailywakeuptime
;
93 dailywakeuptime
.SetFromDBTime(
94 m_settings
.GetStringValue(CSettings::SETTING_PVRPOWERMANAGEMENT_DAILYWAKEUPTIME
));
95 dailywakeuptime
= dailywakeuptime
.GetAsUTCDateTime();
97 const CDateTimeSpan
diff(dailywakeuptime
- now
);
98 int mins
= diff
.GetSecondsTotal() / 60;
104 dueStr
= StringUtils::Format(g_localizeStrings
.Get(19694), mins
);
109 dueStr
= g_localizeStrings
.Get(19695);
112 text
= StringUtils::Format(g_localizeStrings
.Get(19693), // "Daily wakeup is due in...."
116 // Inform user about PVR being busy. Ask if user wants to powerdown anyway.
117 bReturn
= HELPERS::ShowYesNoDialogText(CVariant
{19685}, // "Confirm shutdown"
118 CVariant
{text
}, CVariant
{222}, // "Shutdown anyway",
119 CVariant
{19696}, // "Cancel"
120 10000) // timeout value before closing
121 == HELPERS::DialogResponse::CHOICE_YES
;
124 bReturn
= false; // do not powerdown (busy, but no user interaction requested).
130 bool CPVRGUIActionsPowerManagement::AllLocalBackendsIdle(
131 std::shared_ptr
<CPVRTimerInfoTag
>& causingEvent
) const
133 // active recording on local backend?
134 const std::vector
<std::shared_ptr
<CPVRTimerInfoTag
>> activeRecordings
=
135 CServiceBroker::GetPVRManager().Timers()->GetActiveRecordings();
136 for (const auto& timer
: activeRecordings
)
138 if (EventOccursOnLocalBackend(timer
))
140 causingEvent
= timer
;
145 // soon recording on local backend?
146 if (IsNextEventWithinBackendIdleTime())
148 const std::shared_ptr
<CPVRTimerInfoTag
> timer
=
149 CServiceBroker::GetPVRManager().Timers()->GetNextActiveTimer(false);
152 // Next event is due to automatic daily wakeup of PVR!
153 causingEvent
.reset();
157 if (EventOccursOnLocalBackend(timer
))
159 causingEvent
= timer
;
166 bool CPVRGUIActionsPowerManagement::EventOccursOnLocalBackend(
167 const std::shared_ptr
<CPVRTimerInfoTag
>& event
) const
169 const std::shared_ptr
<const CPVRClient
> client
=
170 CServiceBroker::GetPVRManager().GetClient(CFileItem(event
));
173 const std::string hostname
= client
->GetBackendHostname();
174 if (!hostname
.empty() && CServiceBroker::GetNetwork().IsLocalHost(hostname
))
180 bool CPVRGUIActionsPowerManagement::IsNextEventWithinBackendIdleTime() const
182 // timers going off soon?
183 const CDateTime
now(CDateTime::GetUTCDateTime());
184 const CDateTimeSpan
idle(
185 0, 0, m_settings
.GetIntValue(CSettings::SETTING_PVRPOWERMANAGEMENT_BACKENDIDLETIME
), 0);
186 const CDateTime
next(CServiceBroker::GetPVRManager().Timers()->GetNextEventTime());
187 const CDateTimeSpan
delta(next
- now
);
189 return (delta
<= idle
);