Fix system issues
[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         if(__pEvent != null)
97         {
98                 delete __pEvent;
99                 __pEvent = null;
100         }
101 }
102
103 result
104 _AlarmImpl::Construct(void)
105 {
106         return E_SUCCESS;
107 }
108
109 result
110 _AlarmImpl::Set(const DateTime& startTime, int period, const DateTime* pEndTime)
111 {
112         result r = E_SUCCESS;
113         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
114         SysTryReturnResult(NID_SYS, pAlarmManager != null, E_SYSTEM, "It is failed to get AlarmManager.");
115
116         __startTime = startTime;
117         __period = period;
118
119         if(pEndTime != null)
120         {
121                 DateTime* pRequiredEndTime = const_cast <DateTime*> (pEndTime);
122                 __pEndTime = pRequiredEndTime;
123                 __isSetEndTime = true;
124         }
125         else
126         {
127                 __isSetEndTime = false;
128         }
129
130         if(__alarmId.value == 0)
131         {
132                 r = pAlarmManager->RegisterAlarm(this);
133         }
134         else
135         {
136                 r = pAlarmManager->UpdateAlarm(this);
137         }
138
139         if(r != E_SUCCESS && r != E_INVALID_ARG)
140         {
141                 SysLog(NID_SYS, "It is failed to register alarm. [%s]", GetErrorMessage(r));
142                 r = E_SYSTEM;
143         }
144         return r;
145 }
146
147 result
148 _AlarmImpl::Cancel(void)
149 {
150         result r = E_SUCCESS;
151         _AlarmManager* pAlarmManager = _AlarmManager::GetInstance();
152         SysTryReturnResult(NID_SYS, pAlarmManager != null, E_SYSTEM, "It is failed to get AlarmManager.");
153
154         r = pAlarmManager->UnregisterAlarm(this);
155
156         if(r != E_SUCCESS && r != E_INVALID_ARG)
157         {
158                 SysLog(NID_SYS, "It is failed to cancel alarm. [%s]", GetErrorMessage(r));
159                 r = E_SYSTEM;
160         }
161
162         __alarmId.value = 0;
163
164         return r;
165 }
166
167 const DateTime
168 _AlarmImpl::GetStartTime(void) const
169 {
170         ClearLastResult();
171         return __startTime;
172 }
173
174 int
175 _AlarmImpl::GetPeriod(void) const
176 {
177         ClearLastResult();
178         return __period.ToInt();
179 }
180
181 const DateTime*
182 _AlarmImpl::GetEndTime(void) const
183 {
184         ClearLastResult();
185         return __pEndTime;
186 }
187
188 result
189 _AlarmImpl::SetAlarmEventListener(IAlarmEventListener* pListener)
190 {
191         if(pListener == null)
192         {
193                 SysLog(NID_SYS, "Listener null set doesn't mean disalarm");
194                 if(__pEvent != null)
195                 {
196                         delete __pEvent;
197                         __pEvent = null;
198                 }
199         }
200         else
201         {
202                 if(__pEvent == null)
203                 {
204                         _AlarmEvent* pAlarmEvent = new (std::nothrow) _AlarmEvent();
205                         SysTryReturnResult(NID_SYS, pAlarmEvent != null, E_OUT_OF_MEMORY, "It is failed to create Event instance");
206                         __pEvent = pAlarmEvent;
207                 }
208                 __pEvent->AddListener(*pListener);
209         }
210         return E_SUCCESS;
211 }
212
213 void 
214 _AlarmImpl::OnAlarmExpired(int alarmId)
215 {
216         if(__pEvent != null)
217         {
218                 _AlarmEventArg* pAlarmEventArg= new (std::nothrow) _AlarmEventArg();
219                 if(pAlarmEventArg == null)
220                 {
221                         SysLogException(NID_SYS, E_OUT_OF_MEMORY, "It is failed to create instance of _AlarmEventArg class.");
222                         return;
223                 }
224                 pAlarmEventArg->pAlarm = this->__pAlarm;
225                 if(pAlarmEventArg != null)
226                 {
227                         __pEvent->Fire(*pAlarmEventArg);
228                 }
229
230                 SysLog(NID_SYS, "Alarm Expired for alarm id: %d and listener called.", alarmId);
231         }
232         else
233         {
234                 SysLog(NID_SYS, "Reserved alarm is expired. However, Listener point is unavailable.");
235         }
236 }
237
238
239 _AlarmImpl*
240 _AlarmImpl::GetInstance(Alarm* pAlarm)
241 {
242         return (pAlarm) ? pAlarm->__pAlarmImpl : null;
243 }
244
245 const _AlarmImpl*
246 _AlarmImpl::GetInstance(const Alarm* pAlarm)
247 {
248         return (pAlarm) ? pAlarm->__pAlarmImpl : null;
249 }
250
251 } } // Tizen::System