02a2664f9725fe73e6d89e17c804b759522b72df
[framework/osp/social.git] / src / FSclReminder.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  * @file                FSclReminder.cpp
18  * @brief               This is the implementation for Reminder class.
19  *
20  * This file contains definitions of @e Reminder class.
21  */
22
23 #include <FBaseSysLog.h>
24 #include <FBaseDateTime.h>
25 #include <FIoFile.h>
26 #include <FSclReminder.h>
27 #include <FApp_AppInfo.h>
28 #include "FScl_ReminderImpl.h"
29
30 using namespace Tizen::Base;
31 using namespace Tizen::App;
32 using namespace Tizen::Io;
33
34 namespace Tizen { namespace Social
35 {
36
37 static const int _MINUTES_OF_HOUR = 60;
38 static const int _MINUTES_OF_DAY = 1440;
39 static const int _MINUTES_OF_WEEK = 10080;
40
41 Reminder::Reminder(void)
42         : __timeUnit(REMINDER_TIME_UNIT_MINUTE)
43         , __timeOffset(0)
44         , __pReminderImpl(null)
45 {
46         __pReminderImpl = new (std::nothrow) _ReminderImpl();
47         SysTryReturnVoidResult(NID_SCL, __pReminderImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
48 }
49
50 Reminder::Reminder(const Reminder& rhs)
51         : __timeUnit(rhs.__timeUnit)
52         , __timeOffset(rhs.__timeOffset)
53         , __soundFilePath(rhs.__soundFilePath)
54         , __pReminderImpl(null)
55 {
56         __pReminderImpl = new (std::nothrow) _ReminderImpl(*rhs.__pReminderImpl);
57         SysTryReturnVoidResult(NID_SCL, __pReminderImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
58 }
59
60 Reminder::~Reminder(void)
61 {
62         delete __pReminderImpl;
63 }
64
65 Reminder&
66 Reminder::operator =(const Reminder& rhs)
67 {
68         if (this == &rhs)
69         {
70                 return *this;
71         }
72
73         __timeUnit = rhs.__timeUnit;
74         __timeOffset = rhs.__timeOffset;
75         __soundFilePath = rhs.__soundFilePath;
76         *__pReminderImpl = *rhs.__pReminderImpl;
77
78         return *this;
79 }
80
81 bool
82 Reminder::Equals(const Object& rhs) const
83 {
84         const Reminder* pReminder = dynamic_cast<const Reminder*>(&rhs);
85
86         if (pReminder == null)
87         {
88                 return false;
89         }
90
91         return (__timeUnit == pReminder->__timeUnit && __timeOffset == pReminder->__timeOffset
92                         && __soundFilePath == pReminder->__soundFilePath && __pReminderImpl->Equals(*pReminder->__pReminderImpl));
93 }
94
95 int
96 Reminder::GetHashCode(void) const
97 {
98         int hashCode = 17;
99
100         hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __timeUnit;
101         hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __timeOffset;
102         hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __soundFilePath.GetHashCode();
103         hashCode = ( hashCode << 4 ) ^ ( hashCode >> 28 ) ^ __pReminderImpl->GetHashCode();
104
105         return hashCode;
106 }
107
108 int
109 Reminder::GetMinutesBefore(void) const
110 {
111         int minutes = 0;
112
113         switch (__timeUnit)
114         {
115         case REMINDER_TIME_UNIT_MINUTE:
116                 minutes = __timeOffset;
117                 break;
118         case REMINDER_TIME_UNIT_HOUR:
119                 minutes = __timeOffset * _MINUTES_OF_HOUR;
120                 break;
121         case REMINDER_TIME_UNIT_DAY:
122                 minutes = __timeOffset * _MINUTES_OF_DAY;
123                 break;
124         case REMINDER_TIME_UNIT_WEEK:
125                 minutes = __timeOffset * _MINUTES_OF_WEEK;
126                 break;
127         default:
128                 break;
129         }
130
131         return minutes;
132 }
133
134 String
135 Reminder::GetSoundFile(void) const
136 {
137         return __soundFilePath;
138 }
139
140 result
141 Reminder::SetMinutesBefore(int minutes)
142 {
143         SysTryReturnResult(NID_SCL, minutes >= 0, E_INVALID_ARG, "Invalid argument is used. The minutes is less than 0.");
144         SysTryReturnResult(NID_SCL, minutes <= MAX_REMINDER_OFFSET_VALUE, E_INVALID_ARG, "Invalid argument is used. The minutes is greater than 40320.");
145
146         __timeUnit = REMINDER_TIME_UNIT_MINUTE;
147         __timeOffset = minutes;
148
149         __pReminderImpl->__absoluteTime = DateTime::GetMinValue();
150         __pReminderImpl->__isAbsolute = false;
151
152         return E_SUCCESS;
153 }
154
155 result
156 Reminder::SetSoundFile(const String& filePath)
157 {
158         if (!filePath.IsEmpty())
159         {
160                 SysTryReturnResult(NID_SCL, File::IsFileExist(filePath), E_INVALID_ARG
161                                 , "Invalid argument is used. The length of the filePath exceeds the maximum length or the file path is invalid or the file does not exist.");
162         }
163
164         __soundFilePath = filePath;
165
166         return E_SUCCESS;
167 }
168
169 result
170 Reminder::SetTimeOffset(ReminderTimeUnit timeUnit, int timeOffset)
171 {
172         SysTryReturnResult(NID_SCL, timeUnit != REMINDER_TIME_UNIT_NONE, E_INVALID_ARG, "Invalid argument is used. The timeUnit is REMINDER_TIME_UNIT_NONE.");
173         SysTryReturnResult(NID_SCL, timeOffset >= 0, E_INVALID_ARG,     "Invalid argument is used. The timeOffset is less than 0.");
174
175         __timeUnit = timeUnit;
176         __timeOffset = timeOffset;
177
178         __pReminderImpl->__absoluteTime = DateTime::GetMinValue();
179         __pReminderImpl->__isAbsolute = false;
180
181         return E_SUCCESS;
182 }
183
184 ReminderTimeUnit
185 Reminder::GetTimeUnit(void) const
186 {
187         return __timeUnit;
188 }
189
190 int
191 Reminder::GetTimeOffset(void) const
192 {
193         return __timeOffset;
194 }
195
196 result
197 Reminder::SetAbsoluteTime(const DateTime& time)
198 {
199         result r = __pReminderImpl->SetAbsoluteTime(time);
200         SysTryReturn(NID_SCL, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
201
202         __timeUnit = REMINDER_TIME_UNIT_NONE;
203         __timeOffset = 0;
204
205         return E_SUCCESS;
206 }
207
208 DateTime
209 Reminder::GetAbsoluteTime(void) const
210 {
211         return __pReminderImpl->GetAbsoluteTime();
212 }
213
214 bool
215 Reminder::IsAbsolute(void) const
216 {
217         return __pReminderImpl->IsAbsolute();
218 }
219
220 }}  // Tizen::Social