Initialize Tizen 2.3
[framework/osp/messaging.git] / src / FMsg_PushMessageImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 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                 FMsg_PushMessageImpl.cpp
19 * @brief                This is the implementation file for the %_PushMessageImpl class.
20 *
21 * This file contains the implementation of the %_PushMessageImpl class.
22 */
23
24 #include <sys/time.h>
25 #include <time.h>
26 #include <FBaseSysLog.h>
27 #include <FBaseUtilStringTokenizer.h>
28 #include <FMsgPushMessage.h>
29 #include "FMsg_PushMessageImpl.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Base::Utility;
34 using namespace Tizen::Shell;
35
36 static const int _HASH_CODE_INITIAL_VALUE = 17;
37 static const int _HASH_CODE_COEFFICIENT_VALUE = 37;
38 static const wchar_t _PUSH_MESSAGE_TOKEN_DELIMITER[] = L"&";
39 static const wchar_t _PUSH_MESSAGE_KEY_VALUE_DELIMITER[] = L"=";
40 static const wchar_t _PUSH_MESSAGE_KEY_BADGE_OPTION[] = L"badgeOption";
41 static const wchar_t _PUSH_MESSAGE_KEY_BADGE_NUMBER[] = L"badgeNumber";
42 static const wchar_t _PUSH_MESSAGE_KEY_ACTION[] = L"action";
43 static const wchar_t _PUSH_MESSAGE_KEY_ALERT_MESSAGE[] = L"alertMessage";
44 static const wchar_t _PUSH_MESSAGE_VALUE_BADGE_SET[] = L"SET";
45 static const wchar_t _PUSH_MESSAGE_VALUE_BADGE_INCREASE[] = L"INCREASE";
46 static const wchar_t _PUSH_MESSAGE_VALUE_BADGE_DECREASE[] = L"DECREASE";
47 static const wchar_t _PUSH_MESSAGE_VALUE_ACTION_SILENT[] = L"SILENT";
48 static const wchar_t _PUSH_MESSAGE_VALUE_ACTION_DISCARD[] = L"DISCARD";
49 static const wchar_t _PUSH_MESSAGE_VALUE_ACTION_ALERT[] = L"ALERT";
50 static const wchar_t _PUSH_MESSAGE_VALUE_ACTION_LAUNCH[] = L"LAUNCH";
51
52
53 namespace Tizen { namespace Messaging
54 {
55
56 _PushMessageImpl::_PushMessageImpl(void)
57         : __dateTime()
58         , __text(null)
59         , __pushAction(PUSH_ACTION_SILENT)
60         , __notiMessage()
61 {
62 }
63
64 _PushMessageImpl::_PushMessageImpl(const DateTime& dateTime, const String& text)
65         : __pushAction(PUSH_ACTION_SILENT)
66         , __notiMessage()
67 {
68         __dateTime = dateTime;
69         __text = text;
70 }
71
72 _PushMessageImpl::~_PushMessageImpl(void)
73 {
74 }
75
76 _PushMessageImpl::_PushMessageImpl(const _PushMessageImpl& rhs)
77 {
78         __dateTime = rhs.__dateTime;
79         __text = rhs.__text;
80         __pushAction = rhs.__pushAction;
81         __notiMessage = rhs.__notiMessage;
82 }
83
84 _PushMessageImpl&
85 _PushMessageImpl::operator =(const _PushMessageImpl& rhs)
86 {
87         if (this == &rhs)
88         {
89                 return *this;
90         }
91
92         __dateTime = rhs.__dateTime;
93         __text = rhs.__text;
94         __pushAction = rhs.__pushAction;
95         __notiMessage = rhs.__notiMessage;
96
97         return *this;
98 }
99
100 bool
101 _PushMessageImpl::Equals(const Object& obj) const
102 {
103         const _PushMessageImpl* pRhs = dynamic_cast<const _PushMessageImpl*>(&obj);
104
105         if (pRhs == null)
106         {
107                 return false;
108         }
109
110         if (__dateTime != pRhs->__dateTime)
111         {
112                 return false;
113         }
114
115         if (__text != pRhs->__text)
116         {
117                 return false;
118         }
119
120         if (__pushAction != pRhs->__pushAction)
121         {
122                 return false;
123         }
124
125         if (__notiMessage.Equals(pRhs->__notiMessage) != true)
126         {
127                 return false;
128         }
129
130         return true;
131 }
132
133 int
134 _PushMessageImpl::GetHashCode(void) const
135 {
136         int hashCode = _HASH_CODE_INITIAL_VALUE;
137
138         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __dateTime.GetHashCode();
139         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + Integer(__pushAction).GetHashCode();
140         hashCode = _HASH_CODE_COEFFICIENT_VALUE * hashCode + __notiMessage.GetHashCode();
141
142         return hashCode;
143 }
144
145 DateTime
146 _PushMessageImpl::GetReceivedTime(void) const
147 {
148         return __dateTime;
149 }
150
151 String
152 _PushMessageImpl::GetText(void) const
153 {
154         return __text;
155 }
156
157 result
158 _PushMessageImpl::SetAction(PushAction action)
159 {
160         result r = E_SUCCESS;
161         SysTryReturn(NID_NET_SOCK, (action >= PUSH_ACTION_SILENT && action <= PUSH_ACTION_LAUNCH), r = E_INVALID_ARG, E_INVALID_ARG,
162                                 "[E_INVALID_ARG] Invalid PushAction [%d] argument.", (int)action);
163
164         __pushAction = action;
165
166         return r;
167 }
168
169 PushAction
170 _PushMessageImpl::GetAction(void) const
171 {
172         return __pushAction;
173 }
174
175 result
176 _PushMessageImpl::SetNotification(const NotificationRequest& notificationMessage)
177 {
178         result r = E_SUCCESS;
179
180         __notiMessage = notificationMessage;
181         __text = notificationMessage.GetAppMessage();
182
183         SysLog(NID_MSG, "Set NotificationRequest : action[%d]", __pushAction);
184         SysLog(NID_MSG, "Set NotificationRequest : badgeOffset[%d], badgeNumber[%d]", notificationMessage.GetBadgeOffset(), notificationMessage.GetBadgeNumber());
185         SysLog(NID_MSG, "Set NotificationRequest : alertText[%ls], appMessage[%ls]", (notificationMessage.GetAlertText()).GetPointer(), (notificationMessage.GetAppMessage()).GetPointer());
186
187         return r;
188 }
189
190 NotificationRequest
191 _PushMessageImpl::GetNotification(void) const
192 {
193  return __notiMessage;
194 }
195
196 PushMessage*
197 _PushMessageImpl::CreatePushMessageN(const DateTime& dateTime, const String& text)
198 {
199         return new (std::nothrow) PushMessage(dateTime, text);
200 }
201
202 DateTime
203 _PushMessageImpl::ConvertTime(long long int time)
204 {
205         DateTime dateTime;
206         struct tm gmTime;
207
208         gmtime_r((time_t*)&time, &gmTime);
209         dateTime.SetValue(gmTime.tm_year + 1900, gmTime.tm_mon + 1, gmTime.tm_mday, gmTime.tm_hour, gmTime.tm_min, gmTime.tm_sec);
210
211         return dateTime;
212 }
213
214 void
215 _PushMessageImpl::ConvertMessageToNotification(const String& message)
216 {
217         result r = E_SUCCESS;
218         int badgeNumber = 0;
219         String badgeValue;
220         String alertText;
221
222         SysLog(NID_MSG, "Set notification with message[%ls]", message.GetPointer());
223
224         r = __notiMessage.SetAppMessage(__text);
225
226         StringTokenizer tokens(message, _PUSH_MESSAGE_TOKEN_DELIMITER);
227
228         while (tokens.HasMoreTokens() == true)
229         {
230                 String token;
231                 String key;
232                 String value;
233                 int delimiterIndex;
234
235                 tokens.GetNextToken(token);
236
237                 r = token.IndexOf(_PUSH_MESSAGE_KEY_VALUE_DELIMITER, 0, delimiterIndex);
238                 if(IsFailed(r))
239                         continue;
240
241                 r = token.SubString(0, delimiterIndex, key);
242                 if (IsFailed(r))
243                         continue;
244
245                 r = token.SubString(delimiterIndex + 1, token.GetLength() - delimiterIndex - 1, value);
246                 if (IsFailed(r))
247                         continue;
248
249                 if ((key.IsEmpty() == true) || (value.IsEmpty() == true))
250                         continue;
251
252                 if (key.Equals(_PUSH_MESSAGE_KEY_BADGE_OPTION, false) == true)
253                 {
254                         badgeValue = value;
255                 }
256                 else if (key.Equals(_PUSH_MESSAGE_KEY_BADGE_NUMBER, false) == true)
257                 {
258                         r = Integer::Parse(value, badgeNumber);
259                         if (IsFailed(r))
260                                 continue;
261
262                         if (badgeNumber < 0)
263                         {
264                                 SysLog(NID_MSG, "badgeNumber[%d] is negative, so treat it as '0'", badgeNumber);
265                                 badgeNumber = 0;
266                         }
267
268                         if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
269                         {
270                                 SysLog(NID_MSG, "badgeNumber[%d] is out of range, so treat it as '%d'", badgeNumber, MAX_NOTIFICATION_BADGE_NUMBER);
271                                 badgeNumber = MAX_NOTIFICATION_BADGE_NUMBER;
272                         }
273                 }
274                 else if (key.Equals(_PUSH_MESSAGE_KEY_ACTION, false) == true)
275                 {
276                         if (value.Equals(_PUSH_MESSAGE_VALUE_ACTION_ALERT, false) == true)
277                         {
278                                 __pushAction = PUSH_ACTION_ALERT;
279                         }
280                         else if (value.Equals(_PUSH_MESSAGE_VALUE_ACTION_SILENT, false) == true)
281                         {
282                                 __pushAction = PUSH_ACTION_SILENT;
283                         }
284                         else if (value.Equals(_PUSH_MESSAGE_VALUE_ACTION_DISCARD, false) == true)
285                         {
286                                 __pushAction = PUSH_ACTION_DISCARD;
287                         }
288                         else if (value.Equals(_PUSH_MESSAGE_VALUE_ACTION_LAUNCH, false) == true)
289                         {
290                                 __pushAction = PUSH_ACTION_LAUNCH;
291                         }
292                         else
293                         {
294                                 __pushAction = PUSH_ACTION_SILENT;
295                         }
296                 }
297                 else if (key.Equals(_PUSH_MESSAGE_KEY_ALERT_MESSAGE, false) == true)
298                 {
299                         r = UrlDecoder::Decode(value, L"UTF-8", alertText);
300                         if (IsFailed(r))
301                                 continue;
302
303                         r = __notiMessage.SetAlertText(alertText);
304                         if (IsFailed(r))
305                                 continue;
306
307                         SysLog(NID_MSG, "AlertText - Original[%ls] Decoded[%ls] result[%s]", value.GetPointer(), alertText.GetPointer(), GetErrorMessage(r));
308                 }
309                 else
310                 {
311                         SysLog(NID_MSG, "Ignore UnKnown option[%ls]", key.GetPointer());
312                 }
313         }
314
315
316         if (badgeValue.Equals(_PUSH_MESSAGE_VALUE_BADGE_SET, false) == true)
317         {
318                 r = __notiMessage.SetBadgeNumber(badgeNumber);
319                 SysLog(NID_MSG, "Set badgeNumber[%d] result[%s]", badgeNumber, GetErrorMessage(r));
320         }
321         else if (badgeValue.Equals(_PUSH_MESSAGE_VALUE_BADGE_INCREASE, false) == true)
322         {
323                 r = __notiMessage.SetBadgeOffset(badgeNumber);
324                 SysLog(NID_MSG, "Set badgeOffset[%d] result[%s]", badgeNumber, GetErrorMessage(r));
325         }
326         else if (badgeValue.Equals(_PUSH_MESSAGE_VALUE_BADGE_DECREASE, false) == true)
327         {
328                 r = __notiMessage.SetBadgeOffset(-badgeNumber);
329                 SysLog(NID_MSG, "Set badgeOffset[%d] result[%s]", badgeNumber, GetErrorMessage(r));
330         }
331
332         SysLog(NID_MSG, "The converted value of action is [%d].", __pushAction);
333         SysLog(NID_MSG, "The converted value of badgeOffset is [%d], badgeNumber is [%d].", __notiMessage.GetBadgeOffset(), __notiMessage.GetBadgeNumber());
334         SysLog(NID_MSG, "The converted value of alertText is [%ls], appMessage is [%ls].", (__notiMessage.GetAlertText()).GetPointer(), (__notiMessage.GetAppMessage()).GetPointer());
335 }
336
337 _PushMessageImpl*
338 _PushMessageImpl::GetInstance(PushMessage& pushMessage)
339 {
340         return pushMessage.__pPushMessageImpl;
341 }
342
343 const _PushMessageImpl*
344 _PushMessageImpl::GetInstance(const PushMessage& pushMessage)
345 {
346         return pushMessage.__pPushMessageImpl;
347 }
348
349 } } // Tizen::Messaging