sync with tizen_2.0
[platform/framework/native/appfw.git] / src / system / FSysAlarm.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                FSysAlarm.cpp
20  * @brief               This is the implementation file for Alarm class.
21  */
22
23
24 #include <new>
25
26 #include <FSysAlarm.h>
27 #include <FBaseSysLog.h>
28 #include <FSec_AccessController.h>
29 #include <FSys_AlarmImpl.h>
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Security;
33
34 namespace Tizen { namespace System
35 {
36
37 Alarm::Alarm(void)
38         : __pAlarmImpl(null)
39 {
40 }
41
42 Alarm::~Alarm(void)
43 {
44         delete __pAlarmImpl;
45         __pAlarmImpl = null;
46 }
47
48 result
49 Alarm::Construct(IAlarmEventListener& listener)
50 {
51         result r = E_SUCCESS;
52
53         if (!__pAlarmImpl)
54         {
55                 __pAlarmImpl = new (std::nothrow) _AlarmImpl(this);
56                 SysTryReturnResult(NID_SYS, __pAlarmImpl, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
57
58                 __pAlarmImpl->Construct();
59         }
60
61         r = __pAlarmImpl->SetAlarmEventListener(&listener);
62         SysTryReturnResult(NID_SYS, !IsFailed(r), r, "Construction is failed");
63
64         return E_SUCCESS;
65 }
66
67 result
68 Alarm::Set(const DateTime& startTime)
69 {
70         return this->Set(startTime, 0, null);
71 }
72
73 result
74 Alarm::Set(const DateTime& startTime, int period, const DateTime* pEndTime)
75 {
76         result r = E_SUCCESS;
77         r = _AccessController::CheckUserPrivilege(_PRV_ALARM);
78         SysTryReturn(NID_SYS, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
79
80         SysAssertf(__pAlarmImpl != null, "Not yet constructed. Construct() should be called before use.");
81         if(pEndTime != null)
82         {
83                 SysTryReturn(NID_SYS, startTime <= *pEndTime, E_INVALID_ARG, E_INVALID_ARG,
84                                 "[E_INVALID_ARG] end time(%ls) is early than start time(%ls)",
85                                 pEndTime->ToString().GetPointer(), startTime.ToString().GetPointer());
86         }
87         SysTryReturn(NID_SYS, period >= 0, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The period(%d) must be greater than 0.", period);
88
89         return __pAlarmImpl->Set(startTime, period, pEndTime);
90 }
91
92 result
93 Alarm::Cancel(void)
94 {
95         result r = E_SUCCESS;
96         r = _AccessController::CheckUserPrivilege(_PRV_ALARM);
97         SysTryReturn(NID_SYS, r == E_SUCCESS, E_PRIVILEGE_DENIED, E_PRIVILEGE_DENIED, ("[E_PRIVILEGE_DENIED] The application does not have the privilege to call this method."));
98
99         SysAssertf(__pAlarmImpl != null, "Not yet constructed. Construct() should be called before use.");
100         r = __pAlarmImpl->Cancel();
101         return r;
102 }
103
104 const DateTime
105 Alarm::GetStartTime(void) const
106 {
107         return __pAlarmImpl->GetStartTime();
108 }
109
110 int
111 Alarm::GetPeriod(void) const
112 {
113         return __pAlarmImpl->GetPeriod();
114 }
115
116 const DateTime*
117 Alarm::GetEndTime(void) const
118 {
119         return __pAlarmImpl->GetEndTime();
120 }
121
122 } } // Tizen::System