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