2 * Copyright (C) 2005-2008 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #include "AlarmClock.h"
23 #include "Application.h"
24 #include "LocalizeStrings.h"
25 #include "SingleLock.h"
28 CAlarmClock g_alarmClock
;
32 CAlarmClock::CAlarmClock() : m_bIsRunning(false)
36 CAlarmClock::~CAlarmClock()
40 void CAlarmClock::Start(const CStdString
& strName
, float n_secs
, const CStdString
& strCommand
, bool bSilent
/* false */)
42 // make lower case so that lookups are case-insensitive
43 CStdString
lowerName(strName
);
46 SAlarmClockEvent event
;
47 event
.m_fSecs
= n_secs
;
48 event
.m_strCommand
= strCommand
;
56 CStdString strAlarmClock
;
57 CStdString strStarted
;
58 if (event
.m_strCommand
.Equals("xbmc.shutdown") || event
.m_strCommand
.Equals("xbmc.shutdown()"))
60 strAlarmClock
= g_localizeStrings
.Get(20144);
61 strStarted
= g_localizeStrings
.Get(20146);
65 strAlarmClock
= g_localizeStrings
.Get(13208);
66 strStarted
= g_localizeStrings
.Get(13210);
69 CStdString strMessage
;
71 strMessage
.Format(strStarted
.c_str(),static_cast<int>(event
.m_fSecs
)/60);
74 g_application
.m_guiDialogKaiToast
.QueueNotification(CGUIDialogKaiToast::Info
, strAlarmClock
, strMessage
);
76 event
.watch
.StartZero();
77 CSingleLock
lock(m_events
);
78 m_event
.insert(make_pair(lowerName
,event
));
79 CLog::Log(LOGDEBUG
,"started alarm with name: %s",lowerName
.c_str());
82 void CAlarmClock::Stop(const CStdString
& strName
, bool bSilent
/* false */)
84 CSingleLock
lock(m_events
);
86 CStdString
lowerName(strName
);
87 lowerName
.ToLower(); // lookup as lowercase only
88 map
<CStdString
,SAlarmClockEvent
>::iterator iter
= m_event
.find(lowerName
);
90 if (iter
== m_event
.end())
93 SAlarmClockEvent
& event
= iter
->second
;
95 CStdString strAlarmClock
;
96 if (event
.m_strCommand
.Equals("xbmc.shutdown") || event
.m_strCommand
.Equals("xbmc.shutdown()"))
97 strAlarmClock
= g_localizeStrings
.Get(20144);
99 strAlarmClock
= g_localizeStrings
.Get(13208);
101 CStdString strMessage
;
102 if( iter
->second
.watch
.GetElapsedSeconds() > iter
->second
.m_fSecs
)
103 strMessage
= g_localizeStrings
.Get(13211);
106 float remaining
= static_cast<float>(iter
->second
.m_fSecs
-iter
->second
.watch
.GetElapsedSeconds());
107 CStdString strStarted
= g_localizeStrings
.Get(13212);
108 strMessage
.Format(strStarted
.c_str(),static_cast<int>(remaining
)/60,static_cast<int>(remaining
)%60);
110 if (iter
->second
.m_strCommand
.IsEmpty() || iter
->second
.m_fSecs
> iter
->second
.watch
.GetElapsedSeconds())
113 g_application
.m_guiDialogKaiToast
.QueueNotification(CGUIDialogKaiToast::Info
, strAlarmClock
, strMessage
);
116 g_application
.getApplicationMessenger().ExecBuiltIn(iter
->second
.m_strCommand
);
118 iter
->second
.watch
.Stop();
122 void CAlarmClock::Process()
126 CStdString strLast
= "";
128 CSingleLock
lock(m_events
);
129 for (map
<CStdString
,SAlarmClockEvent
>::iterator iter
=m_event
.begin();iter
!= m_event
.end(); ++iter
)
130 if (iter
->second
.watch
.GetElapsedSeconds() >= iter
->second
.m_fSecs
)
133 if ((iter
= m_event
.find(strLast
)) == m_event
.end())
137 strLast
= iter
->first
;