2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FMsg_PushMessageImpl.cpp
19 * @brief This is the implementation file for the %_PushMessageImpl class.
21 * This file contains the implementation of the %_PushMessageImpl class.
26 #include <FBaseSysLog.h>
27 #include <FBaseUtilStringTokenizer.h>
28 #include <FMsgPushMessage.h>
29 #include "FMsg_PushMessageImpl.h"
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33 using namespace Tizen::Base::Utility;
34 using namespace Tizen::Shell;
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";
53 namespace Tizen { namespace Messaging
56 _PushMessageImpl::_PushMessageImpl(void)
59 , __pushAction(PUSH_ACTION_SILENT)
64 _PushMessageImpl::_PushMessageImpl(const DateTime& dateTime, const String& text)
65 : __pushAction(PUSH_ACTION_SILENT)
68 __dateTime = dateTime;
72 _PushMessageImpl::~_PushMessageImpl(void)
76 _PushMessageImpl::_PushMessageImpl(const _PushMessageImpl& rhs)
78 __dateTime = rhs.__dateTime;
80 __pushAction = rhs.__pushAction;
81 __notiMessage = rhs.__notiMessage;
85 _PushMessageImpl::operator =(const _PushMessageImpl& rhs)
92 __dateTime = rhs.__dateTime;
94 __pushAction = rhs.__pushAction;
95 __notiMessage = rhs.__notiMessage;
101 _PushMessageImpl::Equals(const Object& obj) const
103 const _PushMessageImpl* pRhs = dynamic_cast<const _PushMessageImpl*>(&obj);
110 if (__dateTime != pRhs->__dateTime)
115 if (__text != pRhs->__text)
120 if (__pushAction != pRhs->__pushAction)
125 if (__notiMessage.Equals(pRhs->__notiMessage) != true)
134 _PushMessageImpl::GetHashCode(void) const
136 int hashCode = _HASH_CODE_INITIAL_VALUE;
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();
146 _PushMessageImpl::GetReceivedTime(void) const
152 _PushMessageImpl::GetText(void) const
158 _PushMessageImpl::SetAction(PushAction action)
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);
164 __pushAction = action;
170 _PushMessageImpl::GetAction(void) const
176 _PushMessageImpl::SetNotification(const NotificationRequest& notificationMessage)
178 result r = E_SUCCESS;
180 __notiMessage = notificationMessage;
181 __text = notificationMessage.GetAppMessage();
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());
191 _PushMessageImpl::GetNotification(void) const
193 return __notiMessage;
197 _PushMessageImpl::CreatePushMessageN(const DateTime& dateTime, const String& text)
199 return new (std::nothrow) PushMessage(dateTime, text);
203 _PushMessageImpl::ConvertTime(long long int time)
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);
215 _PushMessageImpl::ConvertMessageToNotification(const String& message)
217 result r = E_SUCCESS;
222 SysLog(NID_MSG, "Set notification with message[%ls]", message.GetPointer());
224 r = __notiMessage.SetAppMessage(__text);
226 StringTokenizer tokens(message, _PUSH_MESSAGE_TOKEN_DELIMITER);
228 while (tokens.HasMoreTokens() == true)
235 tokens.GetNextToken(token);
237 r = token.IndexOf(_PUSH_MESSAGE_KEY_VALUE_DELIMITER, 0, delimiterIndex);
241 r = token.SubString(0, delimiterIndex, key);
245 r = token.SubString(delimiterIndex + 1, token.GetLength() - delimiterIndex - 1, value);
249 if ((key.IsEmpty() == true) || (value.IsEmpty() == true))
252 if (key.Equals(_PUSH_MESSAGE_KEY_BADGE_OPTION, false) == true)
256 else if (key.Equals(_PUSH_MESSAGE_KEY_BADGE_NUMBER, false) == true)
258 r = Integer::Parse(value, badgeNumber);
264 SysLog(NID_MSG, "badgeNumber[%d] is negative, so treat it as '0'", badgeNumber);
268 if (badgeNumber > MAX_NOTIFICATION_BADGE_NUMBER)
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;
274 else if (key.Equals(_PUSH_MESSAGE_KEY_ACTION, false) == true)
276 if (value.Equals(_PUSH_MESSAGE_VALUE_ACTION_ALERT, false) == true)
278 __pushAction = PUSH_ACTION_ALERT;
280 else if (value.Equals(_PUSH_MESSAGE_VALUE_ACTION_SILENT, false) == true)
282 __pushAction = PUSH_ACTION_SILENT;
284 else if (value.Equals(_PUSH_MESSAGE_VALUE_ACTION_DISCARD, false) == true)
286 __pushAction = PUSH_ACTION_DISCARD;
288 else if (value.Equals(_PUSH_MESSAGE_VALUE_ACTION_LAUNCH, false) == true)
290 __pushAction = PUSH_ACTION_LAUNCH;
294 __pushAction = PUSH_ACTION_SILENT;
297 else if (key.Equals(_PUSH_MESSAGE_KEY_ALERT_MESSAGE, false) == true)
299 r = UrlDecoder::Decode(value, L"UTF-8", alertText);
303 r = __notiMessage.SetAlertText(alertText);
307 SysLog(NID_MSG, "AlertText - Original[%ls] Decoded[%ls] result[%s]", value.GetPointer(), alertText.GetPointer(), GetErrorMessage(r));
311 SysLog(NID_MSG, "Ignore UnKnown option[%ls]", key.GetPointer());
316 if (badgeValue.Equals(_PUSH_MESSAGE_VALUE_BADGE_SET, false) == true)
318 r = __notiMessage.SetBadgeNumber(badgeNumber);
319 SysLog(NID_MSG, "Set badgeNumber[%d] result[%s]", badgeNumber, GetErrorMessage(r));
321 else if (badgeValue.Equals(_PUSH_MESSAGE_VALUE_BADGE_INCREASE, false) == true)
323 r = __notiMessage.SetBadgeOffset(badgeNumber);
324 SysLog(NID_MSG, "Set badgeOffset[%d] result[%s]", badgeNumber, GetErrorMessage(r));
326 else if (badgeValue.Equals(_PUSH_MESSAGE_VALUE_BADGE_DECREASE, false) == true)
328 r = __notiMessage.SetBadgeOffset(-badgeNumber);
329 SysLog(NID_MSG, "Set badgeOffset[%d] result[%s]", badgeNumber, GetErrorMessage(r));
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());
338 _PushMessageImpl::GetInstance(PushMessage& pushMessage)
340 return pushMessage.__pPushMessageImpl;
343 const _PushMessageImpl*
344 _PushMessageImpl::GetInstance(const PushMessage& pushMessage)
346 return pushMessage.__pPushMessageImpl;
349 } } // Tizen::Messaging