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