diff options
Diffstat (limited to 'xbmc/utils/AlarmClock.cpp')
| -rw-r--r-- | xbmc/utils/AlarmClock.cpp | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/xbmc/utils/AlarmClock.cpp b/xbmc/utils/AlarmClock.cpp new file mode 100644 index 0000000..5e01243 --- /dev/null +++ b/xbmc/utils/AlarmClock.cpp | |||
| @@ -0,0 +1,148 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2005-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 | #include "AlarmClock.h" | ||
| 10 | |||
| 11 | #include "ServiceBroker.h" | ||
| 12 | #include "dialogs/GUIDialogKaiToast.h" | ||
| 13 | #include "events/EventLog.h" | ||
| 14 | #include "events/NotificationEvent.h" | ||
| 15 | #include "guilib/LocalizeStrings.h" | ||
| 16 | #include "log.h" | ||
| 17 | #include "messaging/ApplicationMessenger.h" | ||
| 18 | #include "threads/SingleLock.h" | ||
| 19 | #include "utils/StringUtils.h" | ||
| 20 | |||
| 21 | #include <utility> | ||
| 22 | |||
| 23 | using namespace KODI::MESSAGING; | ||
| 24 | |||
| 25 | CAlarmClock::CAlarmClock() : CThread("AlarmClock") | ||
| 26 | { | ||
| 27 | } | ||
| 28 | |||
| 29 | CAlarmClock::~CAlarmClock() = default; | ||
| 30 | |||
| 31 | void CAlarmClock::Start(const std::string& strName, float n_secs, const std::string& strCommand, bool bSilent /* false */, bool bLoop /* false */) | ||
| 32 | { | ||
| 33 | // make lower case so that lookups are case-insensitive | ||
| 34 | std::string lowerName(strName); | ||
| 35 | StringUtils::ToLower(lowerName); | ||
| 36 | Stop(lowerName); | ||
| 37 | SAlarmClockEvent event; | ||
| 38 | event.m_fSecs = n_secs; | ||
| 39 | event.m_strCommand = strCommand; | ||
| 40 | event.m_loop = bLoop; | ||
| 41 | if (!m_bIsRunning) | ||
| 42 | { | ||
| 43 | StopThread(); | ||
| 44 | Create(); | ||
| 45 | m_bIsRunning = true; | ||
| 46 | } | ||
| 47 | |||
| 48 | uint32_t labelAlarmClock; | ||
| 49 | uint32_t labelStarted; | ||
| 50 | if (StringUtils::EqualsNoCase(strName, "shutdowntimer")) | ||
| 51 | { | ||
| 52 | labelAlarmClock = 20144; | ||
| 53 | labelStarted = 20146; | ||
| 54 | } | ||
| 55 | else | ||
| 56 | { | ||
| 57 | labelAlarmClock = 13208; | ||
| 58 | labelStarted = 13210; | ||
| 59 | } | ||
| 60 | |||
| 61 | EventPtr alarmClockActivity(new CNotificationEvent(labelAlarmClock, | ||
| 62 | StringUtils::Format(g_localizeStrings.Get(labelStarted).c_str(), static_cast<int>(event.m_fSecs) / 60, static_cast<int>(event.m_fSecs) % 60))); | ||
| 63 | if (bSilent) | ||
| 64 | CServiceBroker::GetEventLog().Add(alarmClockActivity); | ||
| 65 | else | ||
| 66 | CServiceBroker::GetEventLog().AddWithNotification(alarmClockActivity); | ||
| 67 | |||
| 68 | event.watch.StartZero(); | ||
| 69 | CSingleLock lock(m_events); | ||
| 70 | m_event.insert(make_pair(lowerName,event)); | ||
| 71 | CLog::Log(LOGDEBUG,"started alarm with name: %s",lowerName.c_str()); | ||
| 72 | } | ||
| 73 | |||
| 74 | void CAlarmClock::Stop(const std::string& strName, bool bSilent /* false */) | ||
| 75 | { | ||
| 76 | CSingleLock lock(m_events); | ||
| 77 | |||
| 78 | std::string lowerName(strName); | ||
| 79 | StringUtils::ToLower(lowerName); // lookup as lowercase only | ||
| 80 | std::map<std::string,SAlarmClockEvent>::iterator iter = m_event.find(lowerName); | ||
| 81 | |||
| 82 | if (iter == m_event.end()) | ||
| 83 | return; | ||
| 84 | |||
| 85 | uint32_t labelAlarmClock; | ||
| 86 | if (StringUtils::EqualsNoCase(strName, "shutdowntimer")) | ||
| 87 | labelAlarmClock = 20144; | ||
| 88 | else | ||
| 89 | labelAlarmClock = 13208; | ||
| 90 | |||
| 91 | std::string strMessage; | ||
| 92 | float elapsed = 0.f; | ||
| 93 | |||
| 94 | if (iter->second.watch.IsRunning()) | ||
| 95 | elapsed = iter->second.watch.GetElapsedSeconds(); | ||
| 96 | |||
| 97 | if (elapsed > iter->second.m_fSecs) | ||
| 98 | strMessage = g_localizeStrings.Get(13211); | ||
| 99 | else | ||
| 100 | { | ||
| 101 | float remaining = static_cast<float>(iter->second.m_fSecs - elapsed); | ||
| 102 | strMessage = StringUtils::Format(g_localizeStrings.Get(13212).c_str(), static_cast<int>(remaining) / 60, static_cast<int>(remaining) % 60); | ||
| 103 | } | ||
| 104 | |||
| 105 | if (iter->second.m_strCommand.empty() || iter->second.m_fSecs > elapsed) | ||
| 106 | { | ||
| 107 | EventPtr alarmClockActivity(new CNotificationEvent(labelAlarmClock, strMessage)); | ||
| 108 | if (bSilent) | ||
| 109 | CServiceBroker::GetEventLog().Add(alarmClockActivity); | ||
| 110 | else | ||
| 111 | CServiceBroker::GetEventLog().AddWithNotification(alarmClockActivity); | ||
| 112 | } | ||
| 113 | else | ||
| 114 | { | ||
| 115 | CApplicationMessenger::GetInstance().PostMsg(TMSG_EXECUTE_BUILT_IN, -1, -1, nullptr, iter->second.m_strCommand); | ||
| 116 | if (iter->second.m_loop) | ||
| 117 | { | ||
| 118 | iter->second.watch.Reset(); | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | |||
| 123 | iter->second.watch.Stop(); | ||
| 124 | m_event.erase(iter); | ||
| 125 | } | ||
| 126 | |||
| 127 | void CAlarmClock::Process() | ||
| 128 | { | ||
| 129 | while( !m_bStop) | ||
| 130 | { | ||
| 131 | std::string strLast; | ||
| 132 | { | ||
| 133 | CSingleLock lock(m_events); | ||
| 134 | for (std::map<std::string,SAlarmClockEvent>::iterator iter=m_event.begin();iter != m_event.end(); ++iter) | ||
| 135 | if ( iter->second.watch.IsRunning() | ||
| 136 | && iter->second.watch.GetElapsedSeconds() >= iter->second.m_fSecs) | ||
| 137 | { | ||
| 138 | Stop(iter->first); | ||
| 139 | if ((iter = m_event.find(strLast)) == m_event.end()) | ||
| 140 | break; | ||
| 141 | } | ||
| 142 | else | ||
| 143 | strLast = iter->first; | ||
| 144 | } | ||
| 145 | CThread::Sleep(100); | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
