merge with master
[framework/osp/messaging.git] / src / FMsg_SmsMmsCallbackHandler.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_SmsMmsCallbackHandler.cpp
19  * @brief               This is the implementation file for the %_SmsMmsCallbackHandler class.
20  *
21  * This file contains the implementation of the %_SmsMmsCallbackHandler class.
22  */
23
24 #include <msg.h>
25 #include <msg_transport.h>
26 #include <msg_storage.h>
27 #include <dbus/dbus.h>
28 #include <email-api.h>
29 #include <FMsgSmsManager.h>
30 #include <FBaseInteger.h>
31 #include <FBaseSysLog.h>
32 #include <FMsgEtwsPrimaryNotification.h>
33 #include <FBase_StringConverter.h>
34 #include "FMsg_SmsEvent.h"
35 #include "FMsg_SmsEventArg.h"
36 #include "FMsg_MmsEvent.h"
37 #include "FMsg_MmsEventArg.h"
38 #include "FMsg_MsgUtil.h"
39 #include "FMsg_SmsMmsCallbackHandler.h"
40 #include "FMsg_CbsChannelImpl.h"
41
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44 using namespace Tizen::Base::Runtime;
45
46 namespace Tizen { namespace Messaging
47 {
48
49 _SmsMmsCallbackHandler* _SmsMmsCallbackHandler::__pMessageCallbackHandler = 0;
50
51 _SmsMmsCallbackHandler::_SmsMmsCallbackHandler(void)
52         : __msgHandle(0)
53         , __cbsSettingsHandle(0)
54         , __pCbsChannelList(0)
55         , __pTempCbsChannelList(0)
56         , __callbackRegistered(false)
57         , __incomingSmsCallbackRegistered(true)
58         , __pCbsUserEvent(null)
59         , __pEtwsUserEvent(null)
60 {
61 }
62
63 result
64 _SmsMmsCallbackHandler::Construct()
65 {
66         result r = E_SUCCESS;
67
68         r = __messageMap.Construct();
69
70         if (r == E_SUCCESS)
71         {
72                 r = __msgMutex.Create();
73         }
74
75         return r;
76 }
77
78 _SmsMmsCallbackHandler::~_SmsMmsCallbackHandler(void)
79 {
80         if (__msgHandle)
81         {
82                 msg_close_msg_handle(&__msgHandle);
83                 __msgHandle = 0;
84         }
85
86         if (__messageMap.GetCount())
87         {
88                 __messageMap.RemoveAll(true);
89         }
90
91         if (__cbsSettingsHandle)
92         {
93                 msg_release_struct(&__cbsSettingsHandle);
94                 __cbsSettingsHandle = 0;
95         }
96
97         if (__pTempCbsChannelList)
98         {
99                 if (__pTempCbsChannelList->GetCount() > 0)
100                 {
101                         __pTempCbsChannelList->RemoveAll(true);
102                 }
103                 delete __pTempCbsChannelList;
104                 __pTempCbsChannelList = null;
105         }
106 }
107
108 _SmsMmsCallbackHandler*
109 _SmsMmsCallbackHandler::GetInstance(void)
110 {
111         result r = E_SUCCESS;
112
113         if (!__pMessageCallbackHandler)
114         {
115                 __pMessageCallbackHandler = new (std::nothrow) _SmsMmsCallbackHandler();
116                 SysTryReturn(NID_MSG, __pMessageCallbackHandler != NULL, null, E_SYSTEM, "failed to create instance");
117
118                 if (__pMessageCallbackHandler)
119                 {
120                         r = __pMessageCallbackHandler->Construct();
121
122                         if (r != E_SUCCESS)
123                         {
124                                 delete __pMessageCallbackHandler;
125                                  __pMessageCallbackHandler = 0;
126                         }
127                 }
128         }
129
130         return __pMessageCallbackHandler;
131 }
132
133 int
134 _SmsMmsCallbackHandler::OpenMessageHandle(msg_handle_t& messageHandle)
135 {
136         int err = MSG_SUCCESS;
137
138         if (!__msgHandle)
139         {
140                 err = msg_open_msg_handle(&__msgHandle);
141         }
142
143         messageHandle = __msgHandle;
144         return err;
145 }
146
147 int
148 _SmsMmsCallbackHandler::OpenCbsSettingsHandle(msg_struct_t& cbsSettingsHandle)
149 {
150         int err = MSG_SUCCESS;
151
152         if (!__cbsSettingsHandle)
153         {
154                 __cbsSettingsHandle = msg_create_struct(MSG_STRUCT_SETTING_CB_OPT);
155                 if (__cbsSettingsHandle && __msgHandle)
156                 {
157                         err = msg_get_cb_opt(__msgHandle, __cbsSettingsHandle);
158                 }
159         }
160
161         cbsSettingsHandle = __cbsSettingsHandle;
162         return err;
163 }
164
165 result
166 _SmsMmsCallbackHandler::AddCbsChannel(int from, int to, Tizen::Base::String& name, bool activate)
167 {
168         int err = MSG_SUCCESS;
169         result r = E_SUCCESS;
170         CbsChannel* pCbsChannel = NULL;
171
172         pCbsChannel = _CbsChannelImpl::GetCbsChannelN(from, to, name);
173         SysTryReturn(NID_MSG, pCbsChannel != null, r = E_SYSTEM, E_SYSTEM, "Failed to get the channel.");
174
175         if (activate)
176         {
177                 _CbsChannelImpl::GetInstance(*pCbsChannel)->SetCbsChannelState(activate);
178         }
179
180         r =__pTempCbsChannelList->Add(*pCbsChannel);
181
182         err = SaveCbsSettings();
183         r = ConvertException(err);
184
185         return r;
186 }
187
188 result
189 _SmsMmsCallbackHandler::RemoveCbsChannel(int from, int to)
190 {
191         int err = MSG_SUCCESS;
192         result r = E_SUCCESS;
193         int fromId = 0;
194         int toId = 0;
195         bool channelFound = false;
196         CbsChannel* pCbsChannel = NULL;
197
198         for (int index = 0; index < __pTempCbsChannelList->GetCount(); index++)
199         {
200                 pCbsChannel = dynamic_cast< Tizen::Messaging::CbsChannel* >(__pTempCbsChannelList->GetAt(index));
201                 SysTryReturn(NID_MSG, pCbsChannel != null, r = E_SYSTEM, E_SYSTEM, "Failed to get the channel.");
202
203                 pCbsChannel->GetRange(fromId, toId);
204                 if (from == fromId && to == toId)
205                 {
206                         channelFound = true;  // found channel to be removed.
207                         __pTempCbsChannelList->RemoveAt(index);
208
209                         err = SaveCbsSettings();
210                         r = ConvertException(err);
211                         delete pCbsChannel;
212                         break;
213                 }
214         }
215
216         if (!channelFound)
217         {
218                 return E_OBJ_NOT_FOUND;
219         }
220
221         return r;
222 }
223
224 result
225 _SmsMmsCallbackHandler::SearchCbsChannel(int from, int to, int& index)
226 {
227         result r = E_SUCCESS;
228         int fromId = 0;
229         int toId = 0;
230         bool channelFound = false;
231         CbsChannel* pCbsChannel = NULL;
232
233         for (int tempIndex = 0; tempIndex < __pTempCbsChannelList->GetCount(); tempIndex++)
234         {
235                 pCbsChannel = dynamic_cast< Tizen::Messaging::CbsChannel* >(__pTempCbsChannelList->GetAt(tempIndex));
236                 SysTryReturn(NID_MSG, pCbsChannel != null, r = E_SYSTEM, E_SYSTEM, "Failed to get the channel.");
237
238                 pCbsChannel->GetRange(fromId, toId);
239                 if (from == fromId && to == toId)
240                 {
241                         channelFound = true;
242                         index = tempIndex;
243                         break;  // found channel to be searched.
244                 }
245         }
246
247         if (!channelFound)
248         {
249                 return E_OBJ_NOT_FOUND;
250         }
251
252         return r;
253 }
254
255 result
256 _SmsMmsCallbackHandler::UpdateCbsChannel(int from, int to, Tizen::Base::String name, bool activate)
257 {
258         int err = MSG_SUCCESS;
259         result r = E_SUCCESS;
260         int fromId = 0;
261         int toId = 0;
262         bool channelFound = false;
263         CbsChannel* pCbsChannel = NULL;
264
265         for (int index = 0; index < __pTempCbsChannelList->GetCount(); index++)
266         {
267                 pCbsChannel = dynamic_cast< Tizen::Messaging::CbsChannel* >(__pTempCbsChannelList->GetAt(index));
268                 SysTryReturn(NID_MSG, pCbsChannel != null, r = E_SYSTEM, E_SYSTEM, "Failed to get the channel.");
269
270                 pCbsChannel->GetRange(fromId, toId);
271                 if (from == fromId && to == toId)
272                 {
273                         channelFound = true;
274                         break;  // found channel to be updated.
275                 }
276         }
277
278         if (!channelFound)
279         {
280                 return E_OBJ_NOT_FOUND;
281         }
282
283         // set cbs channel name
284         _CbsChannelImpl::GetInstance(*pCbsChannel)->SetCbsChannelName(name);
285
286         if (activate)
287         {
288                 _CbsChannelImpl::GetInstance(*pCbsChannel)->SetCbsChannelState(activate);
289         }
290         else
291         {
292                 _CbsChannelImpl::GetInstance(*pCbsChannel)->SetCbsChannelState(false);
293         }
294
295         err = SaveCbsSettings();
296         r = ConvertException(err);
297         return r;
298 }
299
300 result
301 _SmsMmsCallbackHandler::GetCbsChannel(int index, int& from, int& to, Tizen::Base::String& name, bool& activate)
302 {
303         result r = E_SUCCESS;
304         CbsChannel* pCbsChannel = NULL;
305
306         pCbsChannel = dynamic_cast< Tizen::Messaging::CbsChannel* >(__pTempCbsChannelList->GetAt(index));
307         SysTryReturn(NID_MSG, pCbsChannel != null, r = E_SYSTEM, E_SYSTEM, "Failed to get the channel.");
308
309         pCbsChannel->GetRange(from, to);
310         name = pCbsChannel->GetName();
311         if (pCbsChannel->IsActivated())
312         {
313                 activate = true;
314         }
315         else
316         {
317                 activate = false;
318         }
319         return r;
320 }
321
322 result
323 _SmsMmsCallbackHandler::GetCbsChannelsCount(int& count)
324 {
325         result r = E_SUCCESS;
326
327         if (__pTempCbsChannelList)
328         {
329                 count = __pTempCbsChannelList->GetCount();
330         }
331
332         return r;
333 }
334
335 result
336 _SmsMmsCallbackHandler::SaveCbsSettings()
337 {
338         int err = MSG_SUCCESS;
339         result r = E_SUCCESS;
340         int fromId = 0;
341         int toId = 0;
342         bool isChannelActive = false;
343         String channelName;
344         char* pTempChannelName = null;
345
346         SysTryReturn(NID_MSG, __pTempCbsChannelList != null, r = E_SYSTEM, E_SYSTEM, "Failed to save the channel.");
347         err = msg_get_list_handle(__cbsSettingsHandle, MSG_CB_CHANNEL_LIST_STRUCT, (void **)&__pCbsChannelList);
348
349         // save Channel List
350         for (int index = 0; index < __pTempCbsChannelList->GetCount(); index++)
351         {
352                 Tizen::Messaging::CbsChannel* pCbsChannel = dynamic_cast< Tizen::Messaging::CbsChannel* >(__pTempCbsChannelList->GetAt(index));
353                 SysTryReturn(NID_MSG, pCbsChannel != null, r = E_SYSTEM, E_SYSTEM, "Failed to get the channel.");
354                 pCbsChannel->GetRange(fromId, toId);
355                 channelName = pCbsChannel->GetName();
356                 if (pCbsChannel->IsActivated())
357                 {
358                         isChannelActive = true;
359                 }
360
361                 pTempChannelName = _StringConverter::CopyToCharArrayN(channelName);
362                 SysTryReturn(NID_MSG, pTempChannelName != null, r = E_OUT_OF_MEMORY, r, "Memory allocation failed.");
363
364                 msg_set_bool_value(__pCbsChannelList->msg_struct_info[index], MSG_CB_CHANNEL_ACTIVATE_BOOL, isChannelActive);
365                 msg_set_int_value(__pCbsChannelList->msg_struct_info[index], MSG_CB_CHANNEL_ID_FROM_INT, fromId);
366                 msg_set_int_value(__pCbsChannelList->msg_struct_info[index], MSG_CB_CHANNEL_ID_TO_INT, toId);
367                 msg_set_str_value(__pCbsChannelList->msg_struct_info[index], MSG_CB_CHANNEL_NAME_STR,
368                                          pTempChannelName,CB_CHANNEL_NAME_MAX);
369
370                 if (pTempChannelName)
371                 {
372                         delete[] pTempChannelName;
373                         pTempChannelName = null;
374                 }
375         }
376
377         err = msg_set_cb_opt(__msgHandle, __cbsSettingsHandle);
378         r = ConvertException(err);
379         return r;
380 }
381
382 result
383 _SmsMmsCallbackHandler::LoadCbsSettings()
384 {
385         int err = MSG_SUCCESS;
386         result r = E_SUCCESS;
387         CbsChannel* pCbsChannel = NULL;
388         int fromId = 0;
389         int toId = 0;
390         bool isChannelActive = false;
391         char channelName[CB_CHANNEL_NAME_MAX + 1] = {0,};
392
393         __pTempCbsChannelList = new (std::nothrow) ArrayList();
394         SysTryCatch(NID_MSG, __pTempCbsChannelList != null, r = E_OUT_OF_MEMORY, r, "memory allocation failed");
395
396         // make a list
397         r = __pTempCbsChannelList->Construct();
398         SysTryCatch(NID_MSG, r == E_SUCCESS, , r, "Failed to construct an ArrayList");
399
400         if (__cbsSettingsHandle)
401         {
402                 // Load Channel List
403                 err = msg_get_list_handle(__cbsSettingsHandle, MSG_CB_CHANNEL_LIST_STRUCT, (void **)&__pCbsChannelList);
404                 for (int index = 0; index < __pCbsChannelList->nCount; index++)
405                 {
406                         msg_get_bool_value(__pCbsChannelList->msg_struct_info[index], MSG_CB_CHANNEL_ACTIVATE_BOOL, &isChannelActive);
407                         msg_get_int_value(__pCbsChannelList->msg_struct_info[index], MSG_CB_CHANNEL_ID_FROM_INT, &fromId);
408                         msg_get_int_value(__pCbsChannelList->msg_struct_info[index], MSG_CB_CHANNEL_ID_TO_INT, &toId);
409                         msg_get_str_value(__pCbsChannelList->msg_struct_info[index], MSG_CB_CHANNEL_NAME_STR,
410                                                  channelName,CB_CHANNEL_NAME_MAX);
411
412                         pCbsChannel = _CbsChannelImpl::GetCbsChannelN(fromId, toId, String(channelName));
413                         SysTryCatch(NID_MSG, pCbsChannel != null, r = E_SYSTEM, r, "Failed to get the channel.");
414                         if (isChannelActive)
415                         {
416                                 _CbsChannelImpl::GetInstance(*pCbsChannel)->SetCbsChannelState(isChannelActive);
417                         }
418                         __pTempCbsChannelList->Add(*pCbsChannel);
419                 }
420         }
421
422         r = ConvertException(err);
423         return r;
424
425 CATCH:
426         if (__pTempCbsChannelList)
427         {
428                 if (__pTempCbsChannelList->GetCount() > 0)
429                 {
430                         __pTempCbsChannelList->RemoveAll(true);
431                 }
432                 delete __pTempCbsChannelList;
433                 __pTempCbsChannelList = null;
434         }
435
436         return r;
437 }
438
439 int
440 _SmsMmsCallbackHandler::RegisterMessageCallback()
441 {
442         int err = MSG_SUCCESS;
443
444         if (__msgHandle && !__callbackRegistered)
445         {
446                 err = msg_reg_sent_status_callback(__msgHandle, &_SmsMmsCallbackHandler::SentMessageStatusCallback, null);
447                 __callbackRegistered = true;
448         }
449
450         return err;
451 }
452
453 int
454 _SmsMmsCallbackHandler::CloseMessageHandle()
455 {
456         int err = MSG_SUCCESS;
457
458         if (__msgHandle)
459         {
460                 err = msg_close_msg_handle(&__msgHandle);
461                 __msgHandle = 0;
462         }
463
464         return err;
465 }
466
467 int
468 _SmsMmsCallbackHandler::CloseCbsSettingsHandle()
469 {
470         int err = MSG_SUCCESS;
471
472         if (__cbsSettingsHandle)
473         {
474                 err = msg_release_struct(&__cbsSettingsHandle);
475                 __cbsSettingsHandle = 0;
476         }
477
478         if (__pTempCbsChannelList)
479         {
480                 if (__pTempCbsChannelList->GetCount() > 0)
481                 {
482                         __pTempCbsChannelList->RemoveAll(true);
483                 }
484                 delete __pTempCbsChannelList;
485                 __pTempCbsChannelList = null;
486         }
487
488         return err;
489 }
490
491 void
492 _SmsMmsCallbackHandler::SetInComingSmsCallback(bool incomingSmsCallbackRegistered)
493 {
494         __incomingSmsCallbackRegistered = incomingSmsCallbackRegistered;
495 }
496
497 void
498 _SmsMmsCallbackHandler::SetCbsAndEtwsUserEvent(_MessageType msgType, Tizen::Base::Object* pEvent)
499 {
500         if (msgType == _MSG_CBS)
501         {
502                 __pCbsUserEvent = pEvent;
503         }
504         else
505         {
506                 __pEtwsUserEvent = pEvent;
507         }
508 }
509
510 result
511 _SmsMmsCallbackHandler::AddToMessageMap(int reqId, _MessageType msgType, Tizen::Base::Object* pEvent)
512 {
513         result r = E_SUCCESS;
514
515         Integer* pReqId = new (std::nothrow) Integer(reqId);
516         SysTryReturn(NID_MSG, pReqId != null, E_OUT_OF_MEMORY, r = E_OUT_OF_MEMORY, "memory allocation failed.");
517         Integer* pMsgType = new (std::nothrow) Integer(msgType);
518         SysTryReturn(NID_MSG, pMsgType != null, E_OUT_OF_MEMORY, r = E_OUT_OF_MEMORY, "memory allocation failed.");
519
520         __msgMutex.Acquire();
521
522         r = __messageMap.Add(*pReqId, *pMsgType);
523         r = __messageMap.Add(*pReqId, *pEvent);
524
525         __msgMutex.Release();
526
527         return r;
528 }
529
530 result
531 _SmsMmsCallbackHandler::GetAndRemoveFromMessageMap(int reqId, _MessageType& msgType, Tizen::Base::Object** pUserEvent)
532 {
533         result r = E_SUCCESS;
534
535         Integer* pReqId = new (std::nothrow) Integer(reqId);
536         SysTryReturn(NID_MSG, pReqId != null, E_OUT_OF_MEMORY, r = E_OUT_OF_MEMORY, "memory allocation failed.");
537         Integer* pKey = null;
538         Integer* pMsgType = null;
539         Tizen::Base::Object* pEvent = null;
540         IEnumerator* pValueEnum = null;
541         IMapEnumerator* pMapEnum = null;
542
543         __msgMutex.Acquire();
544
545         pValueEnum = __messageMap.GetValuesN(*pReqId);
546
547         if (pValueEnum)
548         {
549                 if (pValueEnum->MoveNext() == E_SUCCESS)
550                 {
551                         pMsgType = static_cast <Integer*>(pValueEnum->GetCurrent());
552
553                         if (pValueEnum->MoveNext() == E_SUCCESS)
554                         {
555                                 pEvent = static_cast <Tizen::Base::Object*>(pValueEnum->GetCurrent());
556                         }
557                 }
558         }
559
560         *pUserEvent = pEvent;
561
562         if (pMsgType)
563         {
564                 if (pMsgType->ToInt() == _MSG_SMS)
565                 {
566                         msgType = _MSG_SMS;
567                 }
568                 else
569                 {
570                         msgType = _MSG_MMS;
571                 }
572         }
573
574         //Manually getting and deleting the key value
575         pMapEnum = __messageMap.GetMapEnumeratorN();
576
577         if (pMapEnum)
578         {
579                 while (pMapEnum->MoveNext() == E_SUCCESS)
580                 {
581                         pKey = static_cast <Integer*>(pMapEnum->GetKey());
582                         if (pKey)
583                         {
584                                 if (!(pKey->CompareTo(*pReqId)))
585                                 {
586                                         break;
587                                 }
588                         }
589                 }
590         }
591
592         r = __messageMap.Remove(*pReqId, false);
593
594         delete pMapEnum;
595         delete pValueEnum;
596         delete pMsgType;
597         delete pReqId;
598         delete pKey;
599
600         __msgMutex.Release();
601
602         return r;
603 }
604
605 void
606 _SmsMmsCallbackHandler::SentMessageStatusCallback(msg_handle_t hMsgHandle, msg_struct_t msgStatus, void* pUserParam)
607 {
608         result r = E_SUCCESS;
609         _MessageType msgType = (_MessageType) -1;
610         Tizen::Base::Object* pUserEvent = null;
611         int status = MSG_NETWORK_SEND_FAIL;
612         int reqId = 0;
613
614         msg_get_int_value(msgStatus, MSG_SENT_STATUS_REQUESTID_INT, &reqId);
615         msg_get_int_value(msgStatus, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
616
617         _SmsMmsCallbackHandler::GetInstance()->GetAndRemoveFromMessageMap(reqId, msgType, &pUserEvent);
618
619         SysLog(NID_MSG, "hMsgHandle[%d]", hMsgHandle);
620         SysLog(NID_MSG, "req[%d]", reqId);
621         SysLog(NID_MSG, "msgType[%d]", msgType);
622         SysLog(NID_MSG, "pUserEvent[%d]", pUserEvent);
623
624         if (status == MSG_NETWORK_SEND_SUCCESS)
625         {
626                 r = E_SUCCESS;
627                 SysLog(NID_MSG, "Message is sent successfully");
628         }
629         else
630         {
631                 r = E_FAILURE;
632                 SysLog(NID_MSG, "Failed to send the message. error cause: Message is not sending");
633         }
634
635         if (msgType == _MSG_SMS)
636         {
637                 _SmsEvent* pSmsEvent = dynamic_cast <_SmsEvent*>(pUserEvent);
638                 _SmsEventArg* pSmsEventArg = null;
639
640                 if (pSmsEvent)
641                 {
642                         pSmsEventArg = new (std::nothrow) _SmsEventArg(r);
643                         if (pSmsEventArg)
644                         {
645                                 pSmsEvent->Fire(*pSmsEventArg);
646                         }
647                 }
648         }
649         else
650         {
651                 if (msgType == _MSG_MMS)
652                 {
653                         _MmsEvent* pMmsEvent = dynamic_cast <_MmsEvent*>(pUserEvent);
654                         _MmsEventArg* pMmsEventArg = null;
655
656                                 if (pMmsEvent)
657                                 {
658                                         pMmsEventArg = new (std::nothrow) _MmsEventArg(r);
659                                                 if (pMmsEventArg)
660                                                 {
661                                                         pMmsEvent->Fire(*pMmsEventArg);
662                                                 }
663                                 }
664                 }
665         }
666
667 }
668
669 void
670 _SmsMmsCallbackHandler::IncomingSmsMessageCallback(msg_handle_t hMsgHandle, msg_struct_t newMsg, void* pUserParam)
671 {
672         _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(pUserParam);
673         _SmsEventArg* pSmsEventArg = null;
674
675         int msgSize = 0;
676         msg_get_int_value(newMsg, MSG_MESSAGE_DATA_SIZE_INT, &msgSize);
677         char msgText[msgSize + 1];
678         memset(msgText, 0, msgSize + 1);
679         msg_get_str_value(newMsg, MSG_MESSAGE_SMS_DATA_STR, msgText, msgSize);
680         String message(msgText);
681
682         int port = -1;
683         msg_get_int_value(newMsg, MSG_MESSAGE_DEST_PORT_INT, &port);
684
685         int displayTime = -1;
686         msg_get_int_value(newMsg, MSG_MESSAGE_DISPLAY_TIME_INT, &displayTime);
687         DateTime receivedTime = _MsgUtil::ConvertTime((time_t *) &displayTime);
688
689         char address[MAX_ADDRESS_VAL_LEN] = {0, };
690         msg_struct_list_s* pAddrList = NULL;
691         msg_struct_t addrInfo = NULL;
692         msg_get_list_handle(newMsg, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&pAddrList);
693         addrInfo = pAddrList->msg_struct_info[0];
694         msg_get_str_value(addrInfo, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, address, MAX_ADDRESS_VAL_LEN);
695         String senderAddress(address);
696
697         SysLog(NID_MSG, "hMsgHandle: [%d]", hMsgHandle);
698         SysLog(NID_MSG, "msg_body: [%s]", msgText);
699         SysLog(NID_MSG, "port: [%d]", port);
700         //SysLog(NID_MSG, "received time: [%s]", ctime(pReceivedTime));
701
702         if (_SmsMmsCallbackHandler::GetInstance()->__incomingSmsCallbackRegistered)
703         {
704                 if (pSmsEvent)
705                 {
706                         if (port == 0)  // normal incoming SMS message case
707                         {
708                                 pSmsEventArg = new (std::nothrow) _SmsEventArg(message, senderAddress, receivedTime);
709                                 SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
710                         }
711                         else            // SMS trigger message case
712                         {
713                                 pSmsEventArg = new (std::nothrow) _SmsEventArg(port, receivedTime, message, senderAddress);
714                                 SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
715                         }
716                         pSmsEvent->Fire(*pSmsEventArg);
717                 }
718         }
719 }
720
721 void
722 _SmsMmsCallbackHandler::IncomingCbsMessageCallback(msg_handle_t hMsgHandle, msg_struct_t newMsg, void* pUserParam)
723 {
724         _SmsEventArg* pSmsEventArg = null;
725         int tempReceivedTime = 0;
726         DateTime receivedTime;
727         bool etwsNotifiation = false;
728         int messgeId = 0;
729         int serialNumber = 0;
730         int length = 0;
731         int cbsType = -1;
732         Tizen::Base::ByteBuffer msgSerialNumber;
733
734         msg_get_int_value(newMsg, MSG_CB_MSG_RECV_TIME_INT, &tempReceivedTime);
735         receivedTime = _MsgUtil::ConvertTime((time_t *) &tempReceivedTime);
736
737         msg_get_int_value(newMsg, MSG_CB_MSG_MSG_ID_INT, &messgeId);
738         msg_get_int_value(newMsg, MSG_CB_MSG_SERIAL_NUM_INT, &serialNumber);
739         msg_get_int_value(newMsg, MSG_CB_MSG_TYPE_INT, &cbsType);
740
741         Tizen::Base::Integer serialno(serialNumber);
742         length = serialno.ToString().GetLength();
743
744         msgSerialNumber.Construct(length);
745         msgSerialNumber.SetInt(0, serialNumber);
746         msgSerialNumber.SetPosition(0);
747
748         if (cbsType == MSG_TYPE_SMS_ETWS_PRIMARY || cbsType == MSG_TYPE_SMS_ETWS_SECONDARY)
749         {
750                 int etwsWarningType;
751                 char etwsWarningInfo[MESSAGES_ETWS_WARNING_INFO_LEN];
752                 Tizen::Base::ByteBuffer msgSecurityInfo;
753                 Tizen::Base::ByteBuffer etwsMsgWarningType;
754                 msg_get_int_value(newMsg, MSG_CB_MSG_ETWS_WARNING_TYPE_INT, &etwsWarningType);
755                 msg_get_str_value(newMsg, MSG_CB_MSG_ETWS_WARNING_SECU_INFO_STR, etwsWarningInfo, MESSAGES_ETWS_WARNING_INFO_LEN);
756
757                 String etwsSecurityInfo(etwsWarningInfo);
758                 length = etwsSecurityInfo.GetLength();
759                 msgSecurityInfo.Construct(length);
760                 msgSecurityInfo.SetArray((byte*) etwsWarningInfo, 0, length);
761                 msgSecurityInfo.SetPosition(0);
762
763                 Tizen::Base::Integer warningtype(etwsWarningType);
764                 length = warningtype.ToString().GetLength();
765
766                 etwsMsgWarningType.Construct(length);
767                 etwsMsgWarningType.SetInt(0, etwsWarningType);
768                 etwsMsgWarningType.SetPosition(0);
769
770                 etwsNotifiation = true;
771                 _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(_SmsMmsCallbackHandler::GetInstance()->__pEtwsUserEvent);
772
773                 SysLog(NID_MSG, "cbs etwsWarningType: [%d]", etwsWarningType);
774
775                 if (pSmsEvent)
776                 {
777                         pSmsEventArg = new (std::nothrow) _SmsEventArg(receivedTime, msgSerialNumber, msgSecurityInfo, messgeId,
778                                                                          etwsMsgWarningType, _SMS_EVENT_TYPE_ETWS_RECEIVE_RESULT);
779                         SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
780                         pSmsEvent->Fire(*pSmsEventArg);
781                 }
782         }
783         else
784         {
785                 _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(_SmsMmsCallbackHandler::GetInstance()->__pCbsUserEvent);
786                 int dataCodingScheme = 0;
787                 int msgSize = 0;
788
789                 msg_get_int_value(newMsg, MSG_CB_MSG_CB_TEXT_LEN_INT, &msgSize);
790                 char msgBody[msgSize + 1];
791                 memset(msgBody, 0, msgSize + 1);
792                 msg_get_str_value(newMsg, MSG_CB_MSG_CB_TEXT_STR, msgBody, msgSize + 1);
793                 String message(msgBody);       // porting char* to String
794
795                 msg_get_int_value(newMsg, MSG_CB_MSG_DCS_INT, &dataCodingScheme);
796                 Tizen::Base::Integer dataCodingSchemeTemp(dataCodingScheme);
797
798                 //language type of cbs message
799                 String messageLanguage(L"");
800                 byte higherNibble = dataCodingScheme & 0xf0;
801                 byte lowerNibble = dataCodingScheme & 0x0f;
802
803                 //check higher nibble
804                 // if higher nibble 0000
805                 if (higherNibble == 0x00)
806                 {               
807                         messageLanguage = GetCbsLanguageType(lowerNibble);
808                 }
809                 else if (higherNibble == 0x01)
810                 {
811                         // if higher nibble is 0001, get language type from framework
812                         int len = 15;
813                         char type[len + 1];
814                         memset(type, 0, len + 1);
815                         msg_get_str_value(newMsg, MSG_CB_MSG_LANGUAGE_TYPE_STR, type, len);
816                         messageLanguage = String(type);
817                 }
818                 else if (higherNibble == 0x20 && lowerNibble == 0x00)
819                 {
820                         // if 0010 0000         
821                         messageLanguage = String(L"CS");                
822                 }
823                 else
824                 {
825                         // use empty string
826                 }
827
828                 if (pSmsEvent)
829                 {
830                         pSmsEventArg = new (std::nothrow) _SmsEventArg(message, msgSerialNumber, receivedTime, messgeId, etwsNotifiation,
831                                          ((byte) dataCodingSchemeTemp.ToChar()),messageLanguage, _SMS_EVENT_TYPE_CBS_RECEIVE_RESULT);
832                         SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
833                         pSmsEvent->Fire(*pSmsEventArg);
834                 }
835
836                 SysLog(NID_MSG, "cbs msg_body: [%s]", msgBody);
837                 SysLog(NID_MSG, "cbs dataCodingScheme: [%d]", dataCodingScheme);
838                 SysLog(NID_MSG, "cbs languageType: [%s]", messageLanguage.GetPointer());
839         }
840
841         SysLog(NID_MSG, "cbs msg_id: [%d]", messgeId);
842         SysLog(NID_MSG, "cbs serialNumber: [%d]", serialNumber);
843         SysLog(NID_MSG, "cbs etwsNotifiation: [%d]", etwsNotifiation);
844 }
845
846 void
847 _SmsMmsCallbackHandler::IncomingWapPushMessageCallback(msg_handle_t hMsgHandle, const char* pHeader, const char* pBody, int bodyLength, void* pUserParam)
848 {
849         _SmsEvent* pSmsEvent = static_cast <_SmsEvent*>(pUserParam);
850         _SmsEventArg* pSmsEventArg = null;
851         String msgHeader(pHeader);
852         Tizen::Base::ByteBuffer msgBody;
853         msgBody.Construct(bodyLength);
854         msgBody.SetArray((byte*) pBody, 0, bodyLength);
855         msgBody.SetPosition(0);
856
857         SysLog(NID_MSG, "push msg body: [%s]", pBody);
858         SysLog(NID_MSG, "push msg header: [%s]", pHeader);
859         SysLog(NID_MSG, "push body length: [%d]", bodyLength);
860
861         if (pSmsEvent)
862         {
863                 pSmsEventArg = new (std::nothrow) _SmsEventArg(msgHeader, msgBody, _SMS_EVENT_TYPE_WAP_PUSH_RECEIVE_RESULT);
864                 SysTryReturnVoidResult(NID_MSG, pSmsEventArg != null, E_OUT_OF_MEMORY, "memory allocation failed.");
865                 pSmsEvent->Fire(*pSmsEventArg);
866         }
867 }
868
869 Tizen::Base::String
870 _SmsMmsCallbackHandler::GetCbsLanguageType(byte codingScheme)
871 {
872         String languageType(L"");
873
874         switch(codingScheme)
875         {
876         case 0x00:
877                 languageType = String(L"DE");
878                 break;
879         case 0x01:
880                 languageType = String(L"EN");
881                 break;
882         case 0x02:
883                 languageType = String(L"IT");
884                 break;
885         case 0x03:
886                 languageType = String(L"FR");
887                 break;
888         case 0x04:
889                 languageType = String(L"ES");
890                 break;
891         case 0x05:
892                 languageType = String(L"NL");
893                 break;
894         case 0x06:
895                 languageType = String(L"SV");
896                 break;
897         case 0x07:
898                 languageType = String(L"DA");
899                 break;
900         case 0x08:
901                 languageType = String(L"PT");
902                 break;
903         case 0x09:
904                 languageType = String(L"FI");
905                 break;
906         case 0x0a:
907                 languageType = String(L"NO");
908                 break;
909         case 0x0b:
910                 languageType = String(L"EL");
911                 break;
912         case 0x0c:
913                 languageType = String(L"TR");
914                 break;
915         case 0x0d:
916                 languageType = String(L"HU");
917                 break;
918         case 0x0e:
919                 languageType = String(L"PL");
920                 break;
921         case 0x0f:
922                 languageType = String(L"");
923                 SysLog(NID_MSG, "Language unspecified.");
924                 break;
925         default:
926                 break;
927         };
928         
929         return languageType;
930 }
931
932 result
933 _SmsMmsCallbackHandler::ConvertException(int err) const
934 {
935         result r = E_SUCCESS;
936
937         switch (err)
938         {
939         case MSG_SUCCESS:
940                 r = E_SUCCESS;
941                 break;
942
943         case MSG_ERR_INVALID_PARAMETER:
944         case MSG_ERR_INVALID_MESSAGE:
945         case MSG_ERR_NULL_POINTER:
946         case MSG_ERR_NULL_MESSAGE:
947                 r = E_INVALID_ARG;
948                 break;
949
950         case MSG_ERR_MEMORY_ERROR:
951                 r = E_OUT_OF_MEMORY;
952                 break;
953
954         case MSG_ERR_SERVER_NOT_READY:
955         case MSG_ERR_TRANSPORT_ERROR:
956         case MSG_ERR_COMMUNICATION_ERROR:
957                 r = E_NETWORK_UNAVAILABLE;
958                 break;
959
960         case MSG_ERR_NO_SIM:
961                 r = E_DEVICE_UNAVAILABLE;
962                 break;
963
964         case MSG_ERR_PLUGIN_TAPI_FAILED:
965                 r = E_FAILURE;
966                 break;
967
968         default:
969                 r = E_SYSTEM;
970                 break;
971         }
972
973         return r;
974 }
975
976 } } // Tizen::Messaging