Merge pull request #26148 from ksooo/fix-secondstotimestring-warning
[xbmc.git] / xbmc / utils / AlarmClock.cpp
blobf37f828aa931769d2eea8a12718be24e767ea217
1 /*
2 * Copyright (C) 2005-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.
7 */
9 #include "AlarmClock.h"
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 "utils/StringUtils.h"
20 #include <mutex>
21 #include <utility>
23 using namespace std::chrono_literals;
25 CAlarmClock::CAlarmClock() : CThread("AlarmClock")
29 CAlarmClock::~CAlarmClock() = default;
31 void CAlarmClock::Start(const std::string& strName, float n_secs, const std::string& strCommand, bool bSilent /* false */, bool bLoop /* false */)
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 = static_cast<double>(n_secs);
39 event.m_strCommand = strCommand;
40 event.m_loop = bLoop;
41 if (!m_bIsRunning)
43 StopThread();
44 Create();
45 m_bIsRunning = true;
48 uint32_t labelAlarmClock;
49 uint32_t labelStarted;
50 if (StringUtils::EqualsNoCase(strName, "shutdowntimer"))
52 labelAlarmClock = 20144;
53 labelStarted = 20146;
55 else
57 labelAlarmClock = 13208;
58 labelStarted = 13210;
61 EventPtr alarmClockActivity(new CNotificationEvent(
62 labelAlarmClock,
63 StringUtils::Format(g_localizeStrings.Get(labelStarted), static_cast<int>(event.m_fSecs) / 60,
64 static_cast<int>(event.m_fSecs) % 60)));
66 auto eventLog = CServiceBroker::GetEventLog();
67 if (eventLog)
69 if (bSilent)
70 eventLog->Add(alarmClockActivity);
71 else
72 eventLog->AddWithNotification(alarmClockActivity);
75 event.watch.StartZero();
76 std::unique_lock<CCriticalSection> lock(m_events);
77 m_event.insert(make_pair(lowerName,event));
78 CLog::Log(LOGDEBUG, "started alarm with name: {}", lowerName);
81 void CAlarmClock::Stop(const std::string& strName, bool bSilent /* false */)
83 std::unique_lock<CCriticalSection> lock(m_events);
85 std::string lowerName(strName);
86 StringUtils::ToLower(lowerName); // lookup as lowercase only
87 std::map<std::string,SAlarmClockEvent>::iterator iter = m_event.find(lowerName);
89 if (iter == m_event.end())
90 return;
92 uint32_t labelAlarmClock;
93 if (StringUtils::EqualsNoCase(strName, "shutdowntimer"))
94 labelAlarmClock = 20144;
95 else
96 labelAlarmClock = 13208;
98 std::string strMessage;
99 float elapsed = 0.f;
101 if (iter->second.watch.IsRunning())
102 elapsed = iter->second.watch.GetElapsedSeconds();
104 if (elapsed > static_cast<float>(iter->second.m_fSecs))
105 strMessage = g_localizeStrings.Get(13211);
106 else
108 float remaining = static_cast<float>(iter->second.m_fSecs) - elapsed;
109 strMessage = StringUtils::Format(g_localizeStrings.Get(13212), static_cast<int>(remaining) / 60,
110 static_cast<int>(remaining) % 60);
113 if (iter->second.m_strCommand.empty() || static_cast<float>(iter->second.m_fSecs) > elapsed)
115 EventPtr alarmClockActivity(new CNotificationEvent(labelAlarmClock, strMessage));
116 auto eventLog = CServiceBroker::GetEventLog();
117 if (eventLog)
119 if (bSilent)
120 eventLog->Add(alarmClockActivity);
121 else
122 eventLog->AddWithNotification(alarmClockActivity);
125 else
127 CServiceBroker::GetAppMessenger()->PostMsg(TMSG_EXECUTE_BUILT_IN, -1, -1, nullptr,
128 iter->second.m_strCommand);
129 if (iter->second.m_loop)
131 iter->second.watch.Reset();
132 return;
136 iter->second.watch.Stop();
137 m_event.erase(iter);
140 void CAlarmClock::Process()
142 while( !m_bStop)
144 std::string strLast;
146 std::unique_lock<CCriticalSection> lock(m_events);
147 for (std::map<std::string,SAlarmClockEvent>::iterator iter=m_event.begin();iter != m_event.end(); ++iter)
148 if (iter->second.watch.IsRunning() &&
149 iter->second.watch.GetElapsedSeconds() >= static_cast<float>(iter->second.m_fSecs))
151 Stop(iter->first);
152 if ((iter = m_event.find(strLast)) == m_event.end())
153 break;
155 else
156 strLast = iter->first;
158 CThread::Sleep(100ms);