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