Update doxygen
[platform/framework/native/appfw.git] / src / system / FSys_AlarmImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FSys_AlarmImpl.cpp
20  * @brief               This is the implementation file for _AlarmImpl class.
21  */
22
23 #include <unique_ptr.h>
24 #include <stdlib.h>
25 #include <time.h>
26 #include <unistd.h>
27
28 #include <FBaseRtIEventArg.h>
29 #include <FBaseSysLog.h>
30
31 #include <FBase_NativeError.h>
32 #include "FSys_AlarmImpl.h"
33 #include "FSys_AlarmManager.h"
34 #include "FSys_SystemTimeImpl.h"
35
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Runtime;
38 using namespace Tizen::Base::Collection;
39 using namespace Tizen::Io;
40
41 namespace Tizen { namespace System
42 {
43
44 class _AlarmEventArg : public IEventArg
45 {
46 public:
47         _AlarmEventArg()
48         : pAlarm(null)
49         {
50         }
51         Alarm* pAlarm;
52 };
53
54 class _AlarmEvent : public Event
55 {
56 protected:
57         virtual void FireImpl(IEventListener& listener, const IEventArg& arg)
58         {
59                 IAlarmEventListener* pListener = dynamic_cast<IAlarmEventListener*> (&listener);
60                 const _AlarmEventArg* pArg = dynamic_cast<const _AlarmEventArg*>(&arg);
61
62                 if(pListener == null || pArg == null)
63                 {
64                         SysLogException(NID_SYS, E_SYSTEM, "listener or arg is null.");
65                         return;
66                 }
67
68                 if(pArg->pAlarm == null)
69                 {
70                         SysLogException(NID_SYS, E_SYSTEM, "Alarm data is invalid.");
71                         return;
72                 }
73
74                 pListener->OnAlarmExpired(*(pArg->pAlarm));
75         }
76 };
77
78 _AlarmImpl::_AlarmImpl(Alarm* pAlarm)
79         : __alarmId(0)
80         , __period(0)
81         , __pEndTime(null)
82         , __isSetEndTime(false)
83         , __pEvent(null)
84         , __pAlarm(pAlarm)
85 {
86
87 }
88
89 _AlarmImpl::~_AlarmImpl(void)
90 {
91         if(__alarmId.value != 0)
92         {
93                 Cancel();
94         }
95 }
96
97 result
98 _AlarmImpl::Construct(void)
99 {
100         return E_SUCCESS;
101 }
102
103 result
104 _AlarmImpl::Set(const DateTime& startTime, int period, const DateTime* pEndTime)
105 {
106         result r = E_SUCCESS;
107         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
108         SysTryReturnResult(NID_SYS, pAlarmManager != null, E_SYSTEM, "It is failed to get AlarmManager.");
109
110         __startTime = startTime;
111         __period = period;
112
113         if(pEndTime != null)
114         {
115                 DateTime* pRequiredEndTime = const_cast <DateTime*> (pEndTime);
116                 __pEndTime = pRequiredEndTime;
117                 __isSetEndTime = true;
118         }
119         else
120         {
121                 __isSetEndTime = false;
122         }
123
124         if(__alarmId.value == 0)
125         {
126                 r = pAlarmManager->RegisterAlarm(this);
127         }
128         else
129         {
130                 r = pAlarmManager->UpdateAlarm(this);
131         }
132
133         if(r != E_SUCCESS && r != E_INVALID_ARG)
134         {
135                 SysLog(NID_SYS, "It is failed to register alarm. [%s]", GetErrorMessage(r));
136                 r = E_SYSTEM;
137         }
138         return r;
139 }
140
141 result
142 _AlarmImpl::Cancel(void)
143 {
144         result r = E_SUCCESS;
145         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
146         SysTryReturnResult(NID_SYS, pAlarmManager != null, E_SYSTEM, "It is failed to get AlarmManager.");
147
148         r = pAlarmManager->UnregisterAlarm(this);
149
150         if(r != E_SUCCESS && r != E_INVALID_ARG)
151         {
152                 SysLog(NID_SYS, "It is failed to cancel alarm. [%s]", GetErrorMessage(r));
153                 r = E_SYSTEM;
154         }
155
156         __alarmId.value = 0;
157
158         return r;
159 }
160
161 const DateTime
162 _AlarmImpl::GetStartTime(void) const
163 {
164         ClearLastResult();
165         return __startTime;
166 }
167
168 int
169 _AlarmImpl::GetPeriod(void) const
170 {
171         ClearLastResult();
172         return __period.ToInt();
173 }
174
175 const DateTime*
176 _AlarmImpl::GetEndTime(void) const
177 {
178         ClearLastResult();
179         return __pEndTime;
180 }
181
182 result
183 _AlarmImpl::SetAlarmEventListener(IAlarmEventListener* pListener)
184 {
185         if(pListener == null)
186         {
187                 SysLog(NID_SYS, "Listener null set doesn't mean disalarm");
188                 if(__pEvent != null)
189                 {
190                         delete __pEvent;
191                         __pEvent = null;
192                 }
193         }
194         else
195         {
196                 if(__pEvent == null)
197                 {
198                         _AlarmEvent* pAlarmEvent = new (std::nothrow) _AlarmEvent();
199                         SysTryReturnResult(NID_SYS, pAlarmEvent != null, E_OUT_OF_MEMORY, "It is failed to create Event instance");
200                         __pEvent = pAlarmEvent;
201                 }
202                 __pEvent->AddListener(*pListener);
203         }
204         return E_SUCCESS;
205 }
206
207 void 
208 _AlarmImpl::OnAlarmExpired(int alarmId)
209 {
210         if(__pEvent != null)
211         {
212                 _AlarmEventArg* pAlarmEventArg= new (std::nothrow) _AlarmEventArg();
213                 if(pAlarmEventArg == null)
214                 {
215                         SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of _AlarmEventArg class.");
216                         return;
217                 }
218                 pAlarmEventArg->pAlarm = this->__pAlarm;
219                 if(pAlarmEventArg != null)
220                 {
221                         __pEvent->Fire(*pAlarmEventArg);
222                 }
223
224                 SysLog(NID_SYS, "Alarm Expired for alarm id: %d and listener called.", alarmId);
225         }
226         else
227         {
228                 SysLog(NID_SYS, "Reserved alarm is expired. However, Listener point is unavailable.");
229         }
230 }
231
232
233 _AlarmImpl*
234 _AlarmImpl::GetInstance(Alarm* pAlarm)
235 {
236         return (pAlarm) ? pAlarm->__pAlarmImpl : null;
237 }
238
239 const _AlarmImpl*
240 _AlarmImpl::GetInstance(const Alarm* pAlarm)
241 {
242         return (pAlarm) ? pAlarm->__pAlarmImpl : null;
243 }
244
245 } } // Tizen::System