Update change log and spec for wrt-plugins-tizen_0.2.84
[framework/web/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / Sms.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18 /**
19  *
20  *
21  * @file       Sms.cpp
22  * @author     Pawel Misiak (p.misiak@samsung.com)
23  * @version    0.1
24  * @brief
25  */
26 #include "Sms.h"
27 #include <dpl/log/log.h>
28 #include <Commons/Exception.h>
29 #include <API/Messaging/ReqReceiverMessage.h>
30 #include "Messaging.h"
31 #include "CallbackMgr.h"
32 #include "MsgServiceHandleMgr.h"
33
34 extern "C" {
35 #include <msg_storage.h>
36 }
37
38 using namespace std;
39 using namespace TizenApis::Api::Messaging;
40
41 namespace {
42 const char* EMPTY_ID = "0";
43 }
44
45 namespace TizenApis {
46 namespace Platform {
47 namespace Messaging {
48
49 Sms::Sms(const string& id) :
50     IMessage(SMS, id),
51     m_messageData(NULL)
52 {
53     LogDebug("enter");
54     LogDebug("m_id=" << getIdRef());
55     LogDebug("m_msgType=" << getMessageType());
56
57     if (getIdRef().empty()) {
58         createNewMessage();
59     } else {
60         readExistingMessage();
61     }
62
63     Touch();
64 }
65
66 Sms::Sms(const std::string& id, const msg_struct_t msg_data) :
67         IMessage(SMS, id),
68         m_messageData(msg_data)
69 {
70         LogDebug("enter");
71         LogDebug("m_id=" << getIdRef());
72         LogDebug("m_msgType=" << getMessageType());
73         readRecipientList(m_messageData);
74         readBody(m_messageData);
75         readFolder(m_messageData);
76         readDateTime(m_messageData);
77         readReadStatus(m_messageData);
78         readSize(m_messageData);
79         setMessageStatus(Api::Messaging::MESSAGE_STATUS_LOADED);
80
81         Touch();
82 }
83
84 Sms::~Sms()
85 {
86     LogDebug("enter");
87
88     if (m_messageData) {
89         // release platform message structure
90         msg_release_struct(&m_messageData);
91     }
92 }
93
94 void Sms::update(bool draftsOnly)
95 {
96     LogDebug("updating m_id=" << getIdRef());
97
98     if (!m_messageData) {
99         //error if platform message not exists
100         LogError("message can not be updated");
101         Throw(WrtDeviceApis::Commons::PlatformException);
102     }
103
104     //update all sms data
105     if (!draftsOnly || getCurrentFolder() == Api::Messaging::DRAFTBOX) {
106         updateBody();
107         updateFrom();
108         updateTo();
109         updateSourceAddress();
110     }
111     updateReadStatus();
112 }
113
114 void Sms::readAllData()
115 {
116     readExistingMessage();
117 }
118
119 void Sms::moveToFolder(const FolderType newFolder)
120 {
121 /*
122     update();
123
124     Try
125     {
126         const msg_folder_id_t platfromFolderId =
127             Messaging::convertFolderToPlatform(newFolder);
128
129         //call platform api to move message to other folder
130         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
131                                    platfromFolderId) != MSG_SUCCESS) {
132             Throw(WrtDeviceApis::Commons::PlatformException);
133         }
134     }
135
136     Catch(WrtDeviceApis::Commons::PlatformException) {
137         LogError("remove message error");
138         throw;
139     }
140 */
141 }
142
143 void Sms::moveToFolder(const std::string& newFolder)
144 {
145 //    update();
146     //TODO
147 }
148
149 void Sms::copyToFolder(const FolderType newFolder)
150 {
151 /*
152     update();
153
154     msg_message_t msg = msg_new_message();
155
156     Try
157     {
158         MSG_SENDINGOPT_S option = { false, false, false };
159         option.option.smsSendOpt.bReplyPath = true;
160         // trying to get message from platform
161         if (MSG_SUCCESS ==
162             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
163                             &option)) {
164             const msg_folder_id_t platfromFolderId =
165                 Messaging::convertFolderToPlatform(newFolder);
166             // new id will me set (initialize to 0)
167             msg_set_message_id(msg, 0);
168             msg_set_folder_id(msg, platfromFolderId);
169
170             // trying to add message
171             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
172             if (error != MSG_SUCCESS) {
173                 LogError("msg_add_message failed, error code=" << error);
174                 Throw(WrtDeviceApis::Commons::PlatformException);
175             }
176
177             //releasing platform message structure
178             msg_release_message(&msg);
179         }
180     }
181
182     Catch(WrtDeviceApis::Commons::PlatformException) {
183         LogError("remove message error");
184         if (msg) {
185             //releasing platform message structure
186             msg_release_message(&msg);
187         }
188         throw;
189     }
190 */
191 }
192
193 void Sms::copyToFolder(const std::string& newFolder)
194 {
195 //    update();
196     //TODO
197 }
198
199 int Sms::send()
200 {
201         LogDebug("sending message, id=" << getIdRef());
202         Try{
203                 if(m_messageData == NULL)
204                 {
205                         //prepare for sending sms
206                         update();
207                 }
208                 MsgGetCommonHandle();
209         }
210         Catch(WrtDeviceApis::Commons::PlatformException) 
211         {
212                 LogDebug("getHandle failed");
213                 Throw(WrtDeviceApis::Commons::PlatformException);
214         }
215
216     // split the message
217         SendRequest req;
218
219         msg_struct_list_s *addr_list = NULL;
220         msg_struct_list_s *new_addr_list = NULL;
221         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
222         int nToCnt;
223         int tempInt;
224         int tempIdInt;
225
226         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);  
227         msg_get_int_value(m_messageData, MSG_MESSAGE_ID_INT, &tempIdInt);
228
229         nToCnt = addr_list->nCount;
230         LogDebug("nToCnt : " << nToCnt);
231
232         for (int i = 0; i < nToCnt; ++i) 
233         {
234                 msg_struct_t new_msg = createNewCopyOfPLatformMsg(m_messageData);
235                 msg_set_int_value(new_msg, MSG_MESSAGE_ID_INT, tempIdInt);
236
237                 msg_get_list_handle(new_msg, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&new_addr_list);
238
239                 msg_get_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
240                 LogDebug("RECIPIENT_TYPE : " << tempInt);
241                 if(MSG_RECIPIENTS_TYPE_TO != tempInt) 
242                 {
243                         LogDebug("skip");
244                         continue;
245                 }
246
247                 msg_get_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
248                 msg_set_str_value(new_addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
249                 msg_set_int_value(new_addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_TO);              
250                 new_addr_list->nCount = 1;
251
252                 setMessageStatus(MESSAGE_STATUS_SENDING);
253
254                 req.queue.push(new_msg);                
255         }
256
257
258         DPL::Mutex::ScopedLock lock(&m_mutex);
259         m_sendRequests.push(req);
260
261     // schedule sending
262         PostEvent(0);
263
264         LogDebug("sending method finished");
265
266         return 0;               //sms handle is -1;
267 }
268
269 void Sms::sendingCallback(msg_struct_t sent_status)
270 {
271 //    LogInfo(
272 //        "sendingCallback callback received. Req id = " <<
273 //        sent_status->reqId << " Status = " << (int)sent_status->status <<
274 //        ", Recipient=" << getRecipient());
275     int status = MSG_NETWORK_SEND_FAIL;
276
277     msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
278
279     DPL::Mutex::ScopedLock lock(&m_mutex);
280
281     if (status != MSG_NETWORK_SEND_SUCCESS) {
282         m_sendRequests.front().failed = true; // TODO mutex
283         //send callback, error for recipient
284         setSendingStatusFailed(getRecipient());
285         setMessageStatus(MESSAGE_STATUS_FAILED);
286     } else {
287         setSendingStatusOk(getRecipient());
288         setMessageStatus(MESSAGE_STATUS_SENT);
289     }
290         
291     msg_release_struct(&currentQueue().front());
292     currentQueue().pop();
293
294     //schedule another sub message send
295     PostEvent(0);
296 }
297
298 void Sms::OnEventReceived(const int&)
299 {
300     LogDebug("enter");
301     // send another one
302     sendSubMessage();
303 }
304
305 Api::Messaging::FolderType Sms::toFolder(const std::string &folder)
306 {
307     if (folder == "INBOX") {
308         return INBOX;
309     } else if (folder == "OUTBOX") {
310         return OUTBOX;
311     } else if (folder == "SENTBOX") {
312         return SENTBOX;
313     } else if (folder == "DRAFTBOX") {
314         return DRAFTBOX;
315     }
316     ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Invalid folder");
317 }
318
319 void Sms::sendSubMessage()
320 {
321     LogDebug("enter");
322
323     DPL::Mutex::ScopedLock lock(&m_mutex);
324
325     // if current request queue gets empty, try next one
326     while (!m_sendRequests.empty()) {
327         // send as long as the sending fails until there are no more messages
328         for (msg_error_t err = MSG_ERR_UNKNOWN;
329              err != MSG_SUCCESS && !currentQueue().empty(); ) 
330         {
331             err = CallbackMgrSingleton::Instance().registerAndSend(
332                     msg_sms_send_message,
333                     currentQueue().front(),
334                     this);
335             LogInfo("Sending Message (submit request) result:" << err);
336             if (err != MSG_SUCCESS) {
337                 LogError("Sending Message (submit request) failed!!! err=" << err);
338                 msg_release_struct(&currentQueue().front());
339                 currentQueue().pop();
340                 m_sendRequests.front().failed = true;
341                 //send callback, error for recipient
342                 setSendingStatusFailed(getRecipient());
343             }
344         }
345
346         // empty queue -> notify about results
347         if (currentQueue().empty()) {
348                 /*
349             if (m_sendRequests.front().failed) {
350                 setSendingStatusFailed();
351             } else {
352                 setSendingStatusOk();
353             }
354             */
355             // this request is finished, try next one
356             m_sendRequests.pop();
357             continue;
358         }
359         break;
360     }
361 }
362
363 void Sms::setSendingStatusOk(const string &recipient)
364 {
365     LogDebug("enter, success number is : " << recipient);
366     //success callback should be executed here
367     ReqReceiverMessage *requestReceiver = getRequestReceiver();
368     if (requestReceiver) {
369          EventMessagingServicePtr event = getMessagingServiceEvent();
370          event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
371          event->m_successRecipients.push_back(recipient);
372
373          msg_struct_list_s *addr_list = NULL;    
374          msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
375          
376           if ( event->m_successRecipients.size() + event->m_failRecipients.size() == (unsigned int)addr_list->nCount)
377         {
378                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
379         }
380          
381 #if 0
382         EventSendMessagePtr event = getSendMessageEvent();
383         event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
384         event->m_successRecipients.push_back(recipient);
385           //LogDebug("total address size : " << msg_get_address_count(m_messageData) << " Status size : " << event->m_successRecipients.size() + event->m_failRecipients.size());
386         if ( event->m_successRecipients.size() + event->m_failRecipients.size() == (unsigned int)msg_get_address_count(m_messageData))
387         {
388                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventSendMessage >::ManualAnswer(event);
389         }
390 #endif
391                 
392     }
393 }
394
395 void Sms::setSendingStatusFailed(const string &recipient)
396 {
397     LogDebug("enter, fail number is : " << recipient);
398     //error callback should be executed here
399     EventOnSendingFailedEmitterPtr emitter = getEmitter();
400     if (emitter) {
401         EventOnSendingFailedPtr event(new EventOnSendingFailed);
402         event->setError(EventOnSendingFailed::UNKNOWN); // TODO error codes
403         emitter->emit(event);
404     } else {
405         ReqReceiverMessage *requestReceiver = getRequestReceiver();
406         if (requestReceiver) {
407             LogError("calling JS error callback");
408             EventMessagingServicePtr event = getMessagingServiceEvent();
409             event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
410                 event->m_successRecipients.push_back(recipient);
411                 msg_struct_list_s *addr_list = NULL;    
412                 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
413                 
414                  if ( event->m_successRecipients.size() + event->m_failRecipients.size() == (unsigned int)addr_list->nCount)
415                  {
416                    requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
417                  }
418
419         }
420     }
421 }
422
423 void Sms::remove()
424 {
425     Try
426     {
427         // delete message from platform
428         if (msg_delete_message(MsgGetCommonHandle(),
429                                convertId(getId())) != MSG_SUCCESS) {
430             LogError("delete message failed");
431             Throw(WrtDeviceApis::Commons::PlatformException);
432         } else {
433             LogDebug("message deleted succesfully");
434         }
435     }
436
437     Catch(WrtDeviceApis::Commons::PlatformException) {
438         LogError("remove message error");
439         throw;
440     }
441 }
442
443 void Sms::createNewMessage()
444 {
445     LogDebug("enter");
446     if (m_messageData) {
447         // release platform message if it was created previously
448         msg_release_struct(&m_messageData);
449         m_messageData = NULL;
450     }
451     // create new platform message
452     LogDebug("create messeage instance");
453     m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
454     LogDebug("created message : " << m_messageData);
455         
456     Try
457     {
458         //default message initialization
459         // update sms data
460         updateBody();
461         updateTo();
462
463         setMessageStatus(Api::Messaging::MESSAGE_STATUS_DRAFT);
464
465         tm dateT = getDateTime();
466         msg_set_int_value(m_messageData, MSG_MESSAGE_DISPLAY_TIME_INT, (int)mktime(&dateT));
467
468           setMessageStatus(Api::Messaging::MESSAGE_STATUS_CREATED);
469         LogDebug("Message created successfully, msgId=" << getId());
470     }
471     Catch(WrtDeviceApis::Commons::PlatformException) {
472         msg_release_struct(&m_messageData);
473         LogError("Problem with message creation, cleaning");
474     }
475 }
476
477 void Sms::readExistingMessage()
478 {
479         if (getIdRef().empty() || (EMPTY_ID == getIdRef())) {
480                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
481         }
482
483         // release old data
484         msg_release_struct(&m_messageData);
485         m_messageData = NULL;
486         msg_struct_t sendOpt = NULL;
487
488         Try {
489                 // create new platform structure
490                 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
491                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
492
493                 msg_message_id_t l_id = convertId(getIdRef());
494                 LogDebug("reading message id=" << l_id);
495                 // trying to get message from platform
496                 if (MSG_SUCCESS == msg_get_message(MsgGetCommonHandle(), l_id, m_messageData, sendOpt)) 
497                 {
498                         LogDebug("message found with msgId=" << getIdRef());
499
500                         // read all mms dat to abstraction layer
501                         readRecipientList(m_messageData);
502                         readBody(m_messageData);
503                         readFolder(m_messageData);
504                         readDateTime(m_messageData);
505                         readReadStatus(m_messageData);
506                         readSize(m_messageData);
507                         setMessageStatus(Api::Messaging::MESSAGE_STATUS_LOADED);
508
509                 } else {
510                         LogError("message not found with msgId=" << getIdRef());
511                         setId("");
512                         Throw(WrtDeviceApis::Commons::PlatformException);
513                 }
514
515                 msg_release_struct(&sendOpt);
516         }
517         Catch(WrtDeviceApis::Commons::PlatformException) {
518                 LogError("exception");
519                 // nothing to do
520                 msg_release_struct(&sendOpt);
521         }
522 }
523
524 void Sms::updateBody()
525 {
526     // check if abstraction body value has been changed
527     if (isBodyValid()) {
528         return;
529     }
530     LogInfo("updating platfoprm body");
531
532     // change body value in platform structure
533     if (  getBodyRef().length() >  0)
534     {
535                 if (msg_set_str_value(m_messageData, MSG_MESSAGE_SMS_DATA_STR, 
536                         const_cast<char*>(getBodyRef().c_str()), getBodyRef().length()) != MSG_SUCCESS)
537                 {
538         LogError("problem with update Body");
539         Throw(WrtDeviceApis::Commons::PlatformException);
540         }
541     // set flag as true - platform synchronized with abstraction
542     setBodyValidity(true);
543 }
544     // set flag as true - platform synchronized with abstraction
545 }
546
547 void Sms::updateTo()
548 {
549         // check if abstraction recipient value has been changed
550         if (getToRecipients().isValid()) {
551                 return;
552         }
553
554         LogInfo("SMS updating platform recipients");
555
556         msg_struct_list_s *addr_list = NULL;
557         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
558         int nToCnt = addr_list->nCount;
559
560         LogInfo("nToCnt size  " << nToCnt);
561
562         if (nToCnt > 0)
563         {
564                 for (int i = 0; i < nToCnt; i++) {
565                         msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, "", 0);
566                         msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_DISPLAYNAME_STR, "", 0);
567                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
568                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_UNKNOWN);
569                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_CONTACT_ID_INT, 0);
570                 }
571                 nToCnt = 0;
572                 addr_list->nCount = nToCnt;
573         }
574
575         vector<string> to = getToRecipients().getRecipients();
576         // update addresses in platform structure
577         if (to.size())
578         {
579                 LogInfo("updating to");
580                 for (size_t i = 0; i < to.size(); i++)
581                 {
582                         string tmpAddr = to[i];
583                         if(validatePhoneNumber(tmpAddr))
584                         {
585                                 if (i >= MAX_TO_ADDRESS_CNT)
586                                 {
587                                         LogError("max number of recipient exceeded");
588                                         break;
589                                 }
590                                 LogDebug("adding to[" << i << "]=" << to[i]);
591
592                                 msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
593
594                                 if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT,
595                                 MSG_RECIPIENTS_TYPE_TO) != MSG_SUCCESS)
596                                 {
597                                         LogError("problem with adding to address");
598                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
599                                 }
600
601                                 if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR,
602                                 const_cast<char*>(to[i].c_str()), to[i].size()) != MSG_SUCCESS)
603                                 {
604                                         LogError("problem with adding to address");
605                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
606                                 }
607
608                                 addr_list->nCount ++;
609                                 LogDebug("addr_list->nCount =" << addr_list->nCount);
610                         }
611                         else
612                         {
613                                 LogError("wrong phone number format");
614                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "wrong phone number format");                               
615                         }
616                 }
617         }
618
619         setToValidity(true);
620
621 }
622
623 void Sms::updateFrom()
624 {
625     // check if abstraction from field value has been changed
626     if (getFromValidity()) {
627         // do not update if not changed
628         return;
629     }
630     LogInfo("updating platform from");
631
632     //TODO
633
634     // set flag as true - platform synchronized with abstraction
635     setFromValidity(true);
636 }
637
638 void Sms::updateSourceAddress()
639 {
640     // check if abstraction source address value has been changed
641     if (getSourceAddressValidity()) {
642         // do not update if not changed
643         return;
644     }
645     LogInfo("updating platform source address");
646
647     //TODO
648
649     // set flag as true - platform synchronized with abstraction
650     setSourceAddressValidity(true);
651 }
652
653 void Sms::updateReadStatus()
654 {
655     LogInfo("updating platform read status: " << isRead());
656     if (isReadStatusValid()) {
657         // do not update if not changed
658         return;
659     }
660     if (MSG_SUCCESS != msg_set_bool_value(m_messageData, MSG_MESSAGE_READ_BOOL, isRead())) {
661         LogError("problem with setting subject");
662         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
663     }
664     setReadStatusValidity(true);
665 }
666
667 void Sms::updateIsRead()
668 {
669     LogDebug("updating m_id=" << getIdRef());
670
671     if (!m_messageData) {
672         //error if platform message not exists
673         LogError("message can not be updated");
674         Throw(WrtDeviceApis::Commons::PlatformException);
675     }
676         
677     // check if abstraction from m_isReadChange value has been changed
678     if (isReadChangeStatusValid()) {
679         // do not update if not changed
680         return;
681     }
682         
683     Try
684     {
685         if (this->getIdRef().empty()) {
686             LogError("existing msgId is zero, remove msg not done");
687             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
688                     "existing msgId is zero, remove msg not done");
689         }
690
691         if (MSG_SUCCESS !=
692             msg_update_read_status(MsgGetCommonHandle(), convertId(getIdRef()), isReadChangeStatus()))
693         {
694             LogError("Failed to update isRead");
695             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update isRead");
696         }
697     }
698     Catch(WrtDeviceApis::Commons::PlatformException) {
699         LogError("platform error occurs");
700     }
701     setisReadChangeStatusValidity(true);
702         
703 }
704
705 void Sms::updateMessage()
706 {
707         LogDebug("updating m_id=" << getIdRef());
708         msg_error_t err = MSG_SUCCESS;
709         
710         msg_struct_t msg = NULL;
711         msg_struct_t sendOpt = NULL;
712         
713         if (!m_messageData) {
714                 //error if platform message not exists
715                 LogError("message can not be updated");
716                 Throw(WrtDeviceApis::Commons::PlatformException);
717         }
718         
719         Try
720         {
721
722                 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
723                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
724                 
725                 err = msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg, sendOpt);
726                 
727                 if (err != MSG_SUCCESS)
728                 {
729                         LogError("Get Message Failed!");
730                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
731                 }
732         
733                 if (this->getIdRef().empty()) {
734                         LogError("existing msgId is zero, update msg not done");
735                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
736                                         "existing msgId is zero, update msg not done");
737                 }
738                 
739                 update(TRUE);
740                 
741                 if (MSG_SUCCESS != msg_update_message(MsgGetCommonHandle(), m_messageData, sendOpt))
742                 {
743                         LogError("Failed to update message");
744                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
745                 }
746                 msg_release_struct(&msg);
747                 msg_release_struct(&sendOpt);
748         }
749         Catch(WrtDeviceApis::Commons::PlatformException) {
750                 msg_release_struct(&msg);
751                 msg_release_struct(&sendOpt);
752                 LogError("platform error occurs");
753         }
754 }
755
756 void Sms::createSendMessage()
757 {
758         LogDebug("convert m_id= " << convertId(getIdRef()));    
759
760         //prepare for add sms to draft folder
761         if (!m_messageData) {
762                 //error if platform message not exists
763                 LogError("message can not be updated");
764                 Throw(WrtDeviceApis::Commons::PlatformException);
765         }
766
767         //update all sms data
768         if (getCurrentFolder() == Api::Messaging::DRAFTBOX) {
769                 LogDebug("update all sms data");
770                 updateBody();
771                 updateFrom();
772                 updateTo();
773                 updateSourceAddress();
774         }
775
776         Try
777         {
778                 int msgId = 0;
779 //              MSG_SENDINGOPT_S option = { false, false, false };
780 //              option.option.smsSendOpt.bReplyPath = true;
781                 // trying to get message from platform
782
783 //              const msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(DRAFTBOX);
784
785 //              msg_set_message_id(m_messageData, msgId);               
786 //              msg_set_folder_id(m_messageData, platfromFolderId);
787 //              msg_set_network_status(m_messageData, MSG_NETWORK_NOT_SEND);
788
789                 setId(convertId(msgId));
790                 setFolderType(Api::Messaging::DRAFTBOX);
791                 setMessageStatus(Api::Messaging::MESSAGE_STATUS_DRAFT);
792         }
793         Catch(WrtDeviceApis::Commons::PlatformException) {
794                 LogError("remove message error");
795                 if (m_messageData) {
796                         //releasing platform message structure
797                         msg_release_struct(&m_messageData);
798                 }
799                 throw;
800         }
801
802 }
803
804
805
806 void Sms::addMessageToDraft()
807 {
808     LogDebug("convert m_id= " << convertId(getIdRef()));        
809     msg_struct_t sendOpt = NULL;
810
811     //prepare for add sms to draft folder
812     if (!m_messageData) {
813         //error if platform message not exists
814         LogError("message can not be updated");
815         Throw(WrtDeviceApis::Commons::PlatformException);
816     }
817
818     //update all sms data
819     if (getCurrentFolder() == Api::Messaging::DRAFTBOX) {
820         LogDebug("update all sms data");
821         updateBody();
822         updateFrom();
823         updateTo();
824         updateSourceAddress();
825     }
826
827     Try
828     {
829         sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
830         msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
831         int msgId = -1;
832 //        MSG_SENDINGOPT_S option = { false, false, false };
833 //        option.option.smsSendOpt.bReplyPath = true;
834         // trying to get message from platform
835
836 //        const msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(DRAFTBOX);
837
838 //        msg_set_message_id(m_messageData, 0);         
839 //        msg_set_folder_id(m_messageData, platfromFolderId);
840 //        msg_set_network_status(m_messageData, MSG_NETWORK_NOT_SEND);
841
842         // trying to add message
843         int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
844         if (ret < MSG_SUCCESS) {
845             LogError("msg_add_message failed, error code=" << ret);
846             Throw(WrtDeviceApis::Commons::PlatformException);
847         }
848         else
849         {
850                 msgId = ret;    
851         }
852                         
853           LogDebug("Message ID : " << msgId);
854           if (msgId < 0)
855           {
856                  LogDebug("Message ID is invailded ");
857                  Throw(WrtDeviceApis::Commons::PlatformException);
858           }
859           
860           msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
861
862           setId(convertId(msgId));
863         setFolderType(Api::Messaging::DRAFTBOX);
864         setMessageStatus(Api::Messaging::MESSAGE_STATUS_DRAFT);
865           LogDebug("Message Status " <<  getMessageStatus());
866
867 #if 0
868           if ( folder_list_view.nCount )
869                 msg_release_list_struct(&folder_list_view);
870 #endif    
871         msg_release_struct(&sendOpt);
872
873     }
874     Catch(WrtDeviceApis::Commons::PlatformException) {
875         LogError("remove message error");
876         if (m_messageData) {
877             //releasing platform message structure
878             msg_release_struct(&m_messageData);
879             msg_release_struct(&sendOpt);
880         }
881         throw;
882     }
883
884 }
885
886 void Sms::readRecipientList(msg_struct_t& messageData)
887 {
888         std::string phoneNumber;
889 //      int recipientCount = msg_get_address_count(messageData);
890 //      LogDebug("Recipient count " << recipientCount);
891
892         msg_struct_list_s *addr_list;
893         msg_get_list_handle(messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
894
895         int tempInt = 0;
896         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
897
898         int recipientCount = addr_list->nCount;
899         LogDebug("Recipient count " << recipientCount);
900
901         
902         for (int i = 0; i < recipientCount; ++i) 
903 {
904                 msg_get_int_value(messageData, MSG_MESSAGE_DIRECTION_INT, &tempInt);
905                 int type = tempInt;
906
907                 if (MSG_DIRECTION_TYPE_MT == type) 
908                 {
909                         msg_struct_t addr_info = addr_list->msg_struct_info[i];
910                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
911                         phoneNumber = strNumber;
912                 
913                         if (validatePhoneNumber(phoneNumber)) 
914                         {
915                                 setSourceAddress(phoneNumber);
916                                 setFrom(phoneNumber);
917                         }
918                         LogDebug("MT number: " << phoneNumber);
919             }
920                 else if (MSG_DIRECTION_TYPE_MO == type) 
921                 {
922                         msg_struct_t addr_info = addr_list->msg_struct_info[i];
923                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
924                         LogDebug("MO number: " << strNumber);
925                         appendToRecipients(strNumber);
926                 } 
927                 else 
928                 {
929                         LogError("Wrong type of recipient");
930                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong type of recipient");
931         }
932     }
933 }
934
935 void Sms::readBody(msg_struct_t& messageData)
936 {
937     //set abstraction body value
938     int tempInt;
939     msg_get_int_value(messageData, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
940     LogError("body Size : " <<tempInt);
941     char msgText[tempInt+1];
942     memset(msgText, 0, tempInt+1);
943     msg_get_str_value(messageData, MSG_MESSAGE_SMS_DATA_STR, msgText, tempInt);
944 //    setBody(msg_sms_get_message_body(messageData));
945     LogError("body : " <<msgText);
946     setBody(msgText);
947 }
948
949 void Sms::readFolder(msg_struct_t& messageData)
950 {
951     int tempInt = 0;
952     msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
953     LogError("Folder : " <<tempInt);
954
955     switch (tempInt) {
956     case MSG_INBOX_ID:
957         setFolderType(Api::Messaging::INBOX);
958         break;
959     case MSG_OUTBOX_ID:
960         setFolderType(Api::Messaging::OUTBOX);
961         break;
962     case MSG_SENTBOX_ID:
963         setFolderType(Api::Messaging::SENTBOX);
964         break;
965     case MSG_DRAFT_ID:
966         setFolderType(Api::Messaging::DRAFTBOX);
967         break;
968     default:
969         LogError("Wrong folder id");
970         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Unsupported folder id.");
971     }
972 }
973
974 void Sms::readDateTime(msg_struct_t& messageData)
975 {
976 //  tm* time = localtime(msg_get_time(messageData));
977 //    tm dateT = getDateTime();
978     int tempInt = 0;
979     msg_get_int_value(messageData, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
980
981     LogError("readDateTime : " <<tempInt);
982
983     tm* time = localtime((time_t*)&tempInt);
984     if (!time) {
985         LogError("localtime failed");
986         Throw(WrtDeviceApis::Commons::PlatformException);
987     }
988     setDateTime(*time);
989 }
990
991 void Sms::readReadStatus(msg_struct_t& messageData)
992 {
993     // get read status
994     bool tempBool = 0;
995     msg_get_bool_value(messageData, MSG_MESSAGE_READ_BOOL, &tempBool);
996     LogError("readReadStatus : " <<tempBool);
997     setReadStatus(tempBool);
998 }
999
1000 void Sms::readSize(msg_struct_t& messageData)
1001 {
1002     int tempInt;
1003     msg_get_int_value(messageData, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1004 //    setSize(msg_get_message_body_size(messageData));
1005     LogError("readSize : " <<tempInt);
1006     setSize(tempInt);
1007 }
1008
1009 msg_struct_t Sms::createNewCopyOfPLatformMsg(const msg_struct_t src) const
1010 {
1011         int tempInt;
1012         bool tempBool;
1013         
1014         msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1015
1016         msg_get_int_value(src, MSG_MESSAGE_STORAGE_ID_INT, &tempInt);
1017         msg_set_int_value(msg, MSG_MESSAGE_STORAGE_ID_INT, tempInt);
1018
1019         msg_get_int_value(src, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1020         msg_set_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, tempInt);
1021
1022         msg_get_int_value(src, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1023         char tempStr[tempInt+1];
1024         memset(tempStr, 0, tempInt+1);
1025
1026         msg_get_str_value(src, MSG_MESSAGE_SMS_DATA_STR, tempStr, tempInt);
1027         msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, tempStr, strlen(tempStr));
1028
1029         msg_get_int_value(src, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
1030         msg_set_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, tempInt);
1031
1032 //    msg_set_network_status(msg, msg_get_network_status(src));
1033         msg_get_int_value(src, MSG_MESSAGE_NETWORK_STATUS_INT, &tempInt);
1034         msg_set_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT,  tempInt);
1035
1036 //    msg_set_encode_type(msg, msg_get_encode_type(src));
1037         msg_get_int_value(src, MSG_MESSAGE_ENCODE_TYPE_INT, &tempInt);
1038         msg_set_int_value(msg, MSG_MESSAGE_ENCODE_TYPE_INT,  tempInt);
1039
1040 //    msg_set_read_status(msg, msg_is_read(src));
1041         msg_get_bool_value(src, MSG_MESSAGE_READ_BOOL, &tempBool);
1042         msg_set_bool_value(msg, MSG_MESSAGE_READ_BOOL, tempBool);
1043
1044 //        msg_set_protect_status(msg, msg_is_protected(src));
1045         msg_get_bool_value(src, MSG_MESSAGE_PROTECTED_BOOL, &tempBool);
1046         msg_set_bool_value(msg, MSG_MESSAGE_PROTECTED_BOOL, tempBool);
1047
1048 //        msg_set_priority_info(msg, msg_get_priority_info(src));
1049         msg_get_int_value(src, MSG_MESSAGE_PRIORITY_INT, &tempInt);
1050         msg_set_int_value(msg, MSG_MESSAGE_PRIORITY_INT,  tempInt);
1051
1052 //        msg_set_direction_info(msg, msg_get_direction_info(src));
1053         msg_get_int_value(src, MSG_MESSAGE_DIRECTION_INT, &tempInt);
1054         msg_set_int_value(msg, MSG_MESSAGE_DIRECTION_INT,  tempInt);
1055
1056 //        msg_set_port(msg, msg_get_dest_port(src), msg_get_src_port(src));
1057         msg_get_int_value(src, MSG_MESSAGE_DEST_PORT_INT, &tempInt);
1058         msg_set_int_value(msg, MSG_MESSAGE_DEST_PORT_INT,  tempInt);
1059         msg_get_int_value(src, MSG_MESSAGE_SRC_PORT_INT, &tempInt);
1060         msg_set_int_value(msg, MSG_MESSAGE_SRC_PORT_INT,  tempInt);
1061
1062     return msg;
1063 }
1064 }
1065 }
1066 }