Remove hardcoded path for multiuser support
[platform/framework/web/wrt-plugins-tizen.git] / src / Messaging / Mms.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 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 #include "Mms.h"
19
20 #include <vector>
21 #include <Commons/Exception.h>
22 #include "ReqReceiverMessage.h"
23 #include "Messaging.h"
24 #include "CallbackMgr.h"
25 #include "MsgServiceHandleMgr.h"
26 #include "WidgetFilePathMgr.h"
27 #include <Logger.h>
28 #include <vconf.h>
29
30 extern "C" {
31 #include <msg.h>
32 #include <msg_storage.h>
33 }
34
35
36 namespace {
37 const char* TEXT_AREA = "Text";
38 const char* TEXT_FILE_EXTENSION = ".txt";
39 const char* TEXT_DIR_SEPERATOR = "/";
40 const int WHITE_COLOR = 0xffffff;
41 const int BLACK_COLOR = 0x000000;
42 const int ROOT_LAYOUT_WIDTH = 100;
43 const int ROOT_LAYOUT_HEIGHT = 100;
44 const char* EMPTY_ID = "0";
45 }
46
47 using namespace std;
48
49 namespace DeviceAPI {
50 namespace Messaging {
51
52 Mms::Mms(const string& id) :
53     IMessage(MMS, id),
54     m_messageData(NULL)
55 {
56         LoggerD("m_id=" << getIdRef());
57
58         char buf[] = "XXXXXX";
59         mode_t mask = umask(S_IWGRP | S_IWOTH);
60         int i = mkstemp(buf);
61         LoggerD("buf : " << buf << " i : " << i);
62         umask(mask);
63         m_bodyFilePath = buf;
64         m_bodyFilePath = getWidgetPath()+TEXT_DIR_SEPERATOR+m_bodyFilePath+TEXT_FILE_EXTENSION;
65
66         if (getIdRef().empty()) {
67                 createNewMessage();
68         } else {
69                 readExistingMessage();
70         }
71 }
72
73 Mms::Mms(const string& id, const msg_struct_t msg_data) :
74         IMessage(MMS, id),
75         m_messageData(NULL)
76 {
77         LoggerD("m_id=" << getIdRef());
78
79         m_messageData = createNewCopyOfPLatformMsgWithAddressList(msg_data);
80
81         char buf[] = "XXXXXX";
82         mode_t mask = umask(S_IWGRP | S_IWOTH);
83         int i = mkstemp(buf);
84         LoggerD("buf : " << buf << " i : " << i);
85         umask(mask);
86         m_bodyFilePath = buf;
87         m_bodyFilePath = getWidgetPath()+TEXT_DIR_SEPERATOR+m_bodyFilePath+TEXT_FILE_EXTENSION;
88
89         readConversationId(m_messageData);
90         readRecipientList(m_messageData);
91         readPriority(m_messageData);
92         readSubject(m_messageData);
93         readBodyAndAttachments(m_messageData);
94         readFolder(m_messageData);
95         readDateTime(m_messageData);
96         readReadStatus(m_messageData);
97         readMessageStatus(m_messageData);
98
99 }
100
101 Mms::~Mms()
102 {
103     LoggerD("enter");
104
105     if (m_messageData) {
106         // release platform message structure
107 //        msg_release_message(&m_messageData);
108         msg_release_struct(&m_messageData);
109     }
110
111     //remove tmp file
112     //trying to remove file, return value is skipped
113     if (!m_bodyFilePath.empty()) {
114         LoggerD("remove tmp file=" << m_bodyFilePath);
115         (void) ::remove(m_bodyFilePath.c_str());
116     }
117 }
118
119 void Mms::createNewMessage()
120 {
121     if (m_messageData) {
122         msg_release_struct(&m_messageData);
123         m_messageData = NULL;
124     }
125
126     char strSubject[MAX_SUBJECT_LEN] = {0,};
127
128     m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
129     msg_struct_t sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
130 //    msg_struct_t option = NULL;
131
132     // initialize platform message structure
133     msg_set_int_value(m_messageData, MSG_MESSAGE_TYPE_INT, MSG_TYPE_MMS);
134     msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
135     msg_set_str_value(m_messageData, MSG_MESSAGE_SUBJECT_STR, strSubject, MAX_SUBJECT_LEN);
136
137     setMmsType(MULTIPART_MIXED);
138     setReadStatus(false);
139     setMessageStatus(MESSAGE_STATUS_CREATED);
140
141         msg_release_struct(&sendOpt);
142     LoggerD("Message created successfully, Id: " << getIdRef());
143 }
144
145 void Mms::readExistingMessage()
146 {
147         LoggerD("entered");
148
149         if (getIdRef().empty() || (EMPTY_ID == getIdRef())) 
150         {
151                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
152         }
153
154         msg_release_struct(&m_messageData);
155         m_messageData = NULL;
156         msg_struct_t sendOpt = NULL;
157
158         Try {
159
160                 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
161                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
162
163                 // trying to get message from platform
164                 if (MSG_SUCCESS != msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), m_messageData, sendOpt)) 
165                 {
166                         LoggerE("get message error");
167                         Throw(WrtDeviceApis::Commons::PlatformException);
168                 }
169                 LoggerD("message found with msgId=" << getIdRef());
170
171                 // read all mms dat to abstraction layer
172                 readConversationId(m_messageData);
173                 readRecipientList(m_messageData);
174                 readPriority(m_messageData);
175                 readSubject(m_messageData);
176                 readBodyAndAttachments(m_messageData);
177                 readFolder(m_messageData);
178                 readDateTime(m_messageData);
179                 readReadStatus(m_messageData);
180                 readMessageStatus(m_messageData);
181
182                 msg_release_struct(&sendOpt);
183         }
184         Catch(WrtDeviceApis::Commons::PlatformException) {
185                 LoggerE("exception");
186                 // nothing to do
187                 msg_release_struct(&sendOpt);
188         }
189 }
190
191 void Mms::readDateTime(msg_struct_t& messageData)
192 {
193 //  tm* time = localtime(msg_get_time(messageData));
194 //    tm dateT = getDateTime();
195     int tempInt = 0;
196     msg_get_int_value(messageData, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
197
198     LoggerE("readDateTime : " <<tempInt);
199
200     tm* time = localtime((time_t*)&tempInt);
201     if (!time) {
202         LoggerE("localtime failed");
203         Throw(WrtDeviceApis::Commons::PlatformException);
204     }
205     setDateTime(*time);
206 }
207
208 void Mms::readReadStatus(msg_struct_t& messageData)
209 {
210     bool tempBool = 0;
211     msg_get_bool_value(messageData, MSG_MESSAGE_READ_BOOL, &tempBool);
212     setReadStatus(tempBool);
213 }
214
215 void Mms::readFolder(msg_struct_t& messageData)
216 {
217     int tempValue = 0;
218     msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempValue);
219
220     switch (tempValue) {
221     case MSG_INBOX_ID:
222         setFolderType(INBOX);
223         break;
224     case MSG_OUTBOX_ID:
225         setFolderType(OUTBOX);
226         break;
227     case MSG_SENTBOX_ID:
228         setFolderType(SENTBOX);
229         break;
230     case MSG_DRAFT_ID:
231         setFolderType(DRAFTBOX);
232         break;
233     default:
234         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Unsupported folder id.");
235     }
236 }
237
238 void Mms::readMessageStatus(msg_struct_t& messageData)
239 {
240         int tempValue = 0;
241         msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempValue);
242
243         LoggerD("readMessageStatus folder : " << tempValue);
244
245         switch (tempValue) {
246                 case MSG_INBOX_ID:
247                         setMessageStatus(MESSAGE_STATUS_LOADED);
248                         break;
249                 case MSG_OUTBOX_ID:
250                         setMessageStatus(MESSAGE_STATUS_SENDING);
251                         break;
252                 case MSG_SENTBOX_ID:
253                         setMessageStatus(MESSAGE_STATUS_SENT);
254                         break;
255                 case MSG_DRAFT_ID:
256                         setMessageStatus(MESSAGE_STATUS_DRAFT);
257                         break;
258                 default:
259                         setMessageStatus(MESSAGE_STATUS_LOADED);
260                         break;
261         }
262 }
263
264 void Mms::readPriority(msg_struct_t& messageData)
265 {
266 //    switch (msg_get_priority_info(messageData)) {
267     int tempInt = 0;
268     msg_get_int_value(messageData, MSG_MESSAGE_PRIORITY_INT, &tempInt);
269     switch (tempInt) {
270     case MSG_MESSAGE_PRIORITY_LOW:
271         setPriority(MessagePriority::LOW);
272         break;
273     case MSG_MESSAGE_PRIORITY_NORMAL:
274         setPriority(MessagePriority::NORMAL);
275         break;
276     case MSG_MESSAGE_PRIORITY_HIGH:
277         setPriority(MessagePriority::HIGH);
278         break;
279     default:
280         LoggerE("Wrong priority type");
281         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong priority type");
282     }
283 }
284
285 void Mms::readConversationId(msg_struct_t& messageData)
286 {
287         int tempInt = 0;
288         msg_get_int_value(messageData, MSG_MESSAGE_THREAD_ID_INT, &tempInt);
289         LoggerD("conversationID : " <<tempInt);
290         setConvId(tempInt);
291 }
292
293 void Mms::readRecipientList(msg_struct_t& messageData)
294 {
295         std::string phoneNumber;
296 //      int recipientCount = msg_get_address_count(messageData);
297 //      LoggerD("Recipient count " << recipientCount);
298
299         msg_struct_list_s *addr_list;
300         msg_get_list_handle(messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
301
302         int tempInt = 0;
303         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
304
305         int recipientCount = addr_list->nCount;
306         LoggerD("Recipient count " << recipientCount);
307
308         for (int i = 0; i < recipientCount; ++i) 
309         {
310 //        int type = msg_get_direction_info(messageData);
311                 msg_get_int_value(messageData, MSG_MESSAGE_DIRECTION_INT, &tempInt);
312                 int type = tempInt;
313                 LoggerD("Direction: " << type);
314
315                 if (MSG_DIRECTION_TYPE_MT == type) 
316                 {
317                         msg_struct_t addr_info = addr_list->msg_struct_info[i];
318                         memset(strNumber, 0x00, sizeof(MAX_ADDRESS_VAL_LEN));
319                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
320                         phoneNumber = strNumber;
321                 
322                         if (validatePhoneNumber(phoneNumber)) 
323                         {
324                                 setSourceAddress(phoneNumber);
325                                 setFrom(phoneNumber);
326                         }
327                         LoggerD("MT number: " << phoneNumber);
328                 } 
329                 else if (MSG_DIRECTION_TYPE_MO == type) 
330                 {
331                         msg_struct_t addr_info = addr_list->msg_struct_info[i];
332                         memset(strNumber, 0x00, sizeof(MAX_ADDRESS_VAL_LEN));
333                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
334                         msg_get_int_value(addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
335                         LoggerD("MO number: " << strNumber);
336                         LoggerD("Type : " << tempInt);
337                         switch (tempInt) 
338                         {
339                                 case MSG_RECIPIENTS_TYPE_TO:
340                                         appendToRecipients(strNumber);
341                                         break;
342                                 case MSG_RECIPIENTS_TYPE_CC:
343                                         appendCcRecipients(strNumber);
344                                         break;
345                                 case MSG_RECIPIENTS_TYPE_BCC:
346                                         appendBccRecipients(strNumber);
347                                         break;
348                                 default:
349                                 LoggerE("Wrong type of recipient");
350                         }
351                 } 
352                 else 
353                 {
354                         LoggerE("Wrong type of recipient");
355                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong type of recipient");
356                 }
357         }
358 }
359
360 void Mms::readSubject(msg_struct_t& messageData)
361 {
362     char strTemp[MAX_SUBJECT_LEN] = {0};
363     msg_get_str_value(messageData, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
364 //    setSubject(msg_get_subject(messageData));
365     LoggerE("readSubject : " << strTemp);
366     setSubject(strTemp);
367 }
368
369 void Mms::readBodyAndAttachments(msg_struct_t& messageData)
370 {
371
372         int pageLen, mediaLen, attachLen, tempInt, ret;
373
374         msg_struct_t mms_struct = NULL;
375
376         msg_list_handle_t page_list = NULL;
377         msg_list_handle_t media_list = NULL;
378         msg_list_handle_t attach_list = NULL;
379         
380         msg_struct_t page = NULL;
381         msg_struct_t media = NULL;
382         msg_struct_t attach = NULL;
383
384         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
385         ret = msg_get_mms_struct(messageData, mms_struct);
386
387         if (ret != MSG_SUCCESS) 
388         {
389                 LoggerE("cannot get mms struct");
390                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "cannot get mms struct");
391         }
392
393         char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
394         char szFileName[MSG_FILENAME_LEN_MAX] = {0,};
395
396         msg_get_list_handle(mms_struct, MSG_MMS_PAGE_LIST_HND, (void **)&page_list);
397         pageLen = msg_list_length(page_list);
398
399         if (pageLen > 0) {
400                 setMmsType(MULTIPART_RELATED);
401         } else {
402                 setMmsType(MULTIPART_MIXED);
403         }
404
405         LoggerD("page count: " << pageLen);
406
407         for (int p = 0; p < pageLen; ++p) 
408         {
409                 LoggerD("page " << p);
410                 page = (msg_struct_t)msg_list_nth_data(page_list, p);
411                 
412                 if (NULL  == page) {
413                         LoggerE("returned page is null, continue");
414                         continue;
415                 }
416                 
417                 msg_get_list_handle(page, MSG_MMS_PAGE_MEDIA_LIST_HND, (void **)&media_list);
418                 mediaLen = msg_list_length(media_list);
419
420                 for (int m = 0; m < mediaLen; ++m) 
421                 {
422                         LoggerD("media file " << m);
423                         media = (msg_struct_t)msg_list_nth_data(media_list, m);
424                         if (NULL == media) 
425                         {
426                                 LoggerE("returned media is null, continue");
427                                 continue;
428                         }
429
430                         msg_get_int_value(media, MSG_MMS_MEDIA_TYPE_INT, &tempInt);
431
432                         memset(szFilePath, 0, sizeof(szFilePath));
433                         msg_get_str_value(media, MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
434                         
435                         if ((0 == p) && (MMS_SMIL_MEDIA_TEXT == tempInt)) 
436                         {
437                                 //text value on first page goes to body attribute
438                                 LoggerD("setting body from " << szFilePath);
439                                 
440                                 FILE* f = NULL;
441                                 f = fopen(szFilePath, "r");
442                                 if (!f) 
443                                 {
444                                         LoggerE("cannot read file with body" << szFilePath);
445                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cannot read file with body");
446                                 }
447                                 fseek(f, 0, SEEK_END);
448                                 long int size = ftell(f);
449                                 fseek(f, 0, SEEK_SET);
450                                 char* data = new (nothrow) char[size + 1];
451                                 size_t error;
452                                 if (data) {
453                                         memset(data, 0, size + 1);
454                                         error = fread(data, 1, size, f);
455                                         setBody(data);
456                                         delete[] data;
457                                 } else {
458                                         LoggerE("body is not set");
459                                 }
460                                 LoggerD("message body: '" << getBody() << "'");
461                                 fclose(f);
462                         } 
463                         else 
464                         {
465                                 LoggerD("adding attachment " << szFilePath);
466
467                                 IAttachmentPtr attachment = appendAttachment(szFilePath, false);
468                                 //attachment->setMessage(this); //set IMessagePtr
469
470                                 memset(szFileName, 0, sizeof(szFileName));
471                                 msg_get_str_value(media, MSG_MMS_MEDIA_FILENAME_STR, szFileName, sizeof(szFileName));
472                                 if ((szFileName[0] =! '\0' )&& (strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0)) 
473                                 {
474                                         LoggerD("renaming to " << szFileName);
475                                         attachment->rename(szFileName);
476                                 }
477                                 else if (MMS_SMIL_MEDIA_TEXT == tempInt) 
478                                 {
479                                         std::stringstream newName;
480                                         newName << "body_page_" << p + 1 << ".txt";
481                                         LoggerD("renaming to " << newName.str());
482                                         attachment->rename(newName.str());
483                                 }
484                         }
485                 }
486         }
487
488         msg_get_list_handle(mms_struct, MSG_MMS_ATTACH_LIST_HND, (void **)&attach_list);
489         
490         attachLen= msg_list_length(attach_list);
491
492         LoggerD("attachment count: " << attachLen);
493         for (int a = 0; a < attachLen; ++a) 
494         {
495                 std::string tempString;
496                 int tempInt;
497                 
498                 attach = (msg_struct_t)msg_list_nth_data(attach_list, a);
499                 if (NULL == attach) 
500                 {
501                         LoggerE("attachment is null, continue");
502                         continue;
503                 }
504
505                 memset(szFilePath, 0, sizeof(szFilePath));
506                 memset(szFileName, 0, sizeof(szFileName));
507
508                 msg_get_str_value(attach, MSG_MMS_ATTACH_FILEPATH_STR, szFilePath, sizeof(szFilePath));
509                 msg_get_str_value(attach, MSG_MMS_ATTACH_FILENAME_STR, szFileName, sizeof(szFileName));
510
511                 msg_get_int_value(attach, MSG_MMS_ATTACH_MIME_TYPE_INT, &tempInt);
512                 tempString = indexIntToMimeTypeString((MimeType)tempInt);
513                 
514                 IAttachmentPtr att = appendAttachment(szFilePath, false);
515                 if ((szFileName[0] =! '\0') &&
516                         strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0) {
517                         LoggerD("renaming to " << szFileName);
518                         att->rename(szFileName);
519                 }
520                 att->setMimeType(tempString);
521                 att->setAttachmentID((int)a);
522                 att->setDownloaded(true);
523         }
524         msg_release_struct(&mms_struct);
525 }
526
527 void Mms::readBody(msg_struct_t& messageData)
528 {
529
530         int pageLen, mediaLen, tempInt, ret;
531
532         msg_struct_t mms_struct = NULL;
533
534         msg_list_handle_t page_list = NULL;
535         msg_list_handle_t media_list = NULL;
536         
537         msg_struct_t page = NULL;
538         msg_struct_t media = NULL;
539
540         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
541         ret = msg_get_mms_struct(messageData, mms_struct);
542
543         if (ret != MSG_SUCCESS) 
544         {
545                 LoggerE("cannot get mms struct");
546                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "cannot get mms struct");
547         }
548
549         char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
550         char szFileName[MSG_FILENAME_LEN_MAX] = {0,};
551
552         msg_get_list_handle(mms_struct, MSG_MMS_PAGE_LIST_HND, (void **)&page_list);
553         pageLen = msg_list_length(page_list);
554
555         if (pageLen > 0) {
556                 setMmsType(MULTIPART_RELATED);
557         } else {
558                 setMmsType(MULTIPART_MIXED);
559         }
560
561         LoggerD("page count: " << pageLen);
562
563         for (int p = 0; p < pageLen; ++p) 
564         {
565                 LoggerD("page " << p);
566                 page = (msg_struct_t)msg_list_nth_data(page_list, p);
567                 
568                 if (NULL  == page) {
569                         LoggerE("returned page is null, continue");
570                         continue;
571                 }
572                 
573                 msg_get_list_handle(page, MSG_MMS_PAGE_MEDIA_LIST_HND, (void **)&media_list);
574                 mediaLen = msg_list_length(media_list);
575
576                 for (int m = 0; m < mediaLen; ++m) 
577                 {
578                         LoggerD("media file " << m);
579                         media = (msg_struct_t)msg_list_nth_data(media_list, m);
580                         if (NULL == media) 
581                         {
582                                 LoggerE("returned media is null, continue");
583                                 continue;
584                         }
585
586                         msg_get_int_value(media, MSG_MMS_MEDIA_TYPE_INT, &tempInt);
587
588                         memset(szFilePath, 0, sizeof(szFilePath));
589                         msg_get_str_value(media, MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
590                         
591                         if ((0 == p) && (MMS_SMIL_MEDIA_TEXT == tempInt)) 
592                         {
593                                 //text value on first page goes to body attribute
594                                 LoggerD("setting body from " << szFilePath);
595                                 
596                                 FILE* f = NULL;
597                                 f = fopen(szFilePath, "r");
598                                 if (!f) 
599                                 {
600                                         LoggerE("cannot read file with body" << szFilePath);
601                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cannot read file with body");
602                                 }
603                                 fseek(f, 0, SEEK_END);
604                                 long int size = ftell(f);
605                                 fseek(f, 0, SEEK_SET);
606                                 char* data = new (nothrow) char[size + 1];
607                                 size_t error;
608                                 if (data) {
609                                         memset(data, 0, size + 1);
610                                         error = fread(data, 1, size, f);
611                                         setBody(data);
612                                         delete[] data;
613                                 } else {
614                                         LoggerE("body is not set");
615                                 }
616                                 LoggerD("message body: '" << getBody() << "'");
617                                 fclose(f);
618                         } 
619                         else 
620                         {
621                                 LoggerD("adding attachment " << szFilePath);
622
623                                 IAttachmentPtr attachment = appendAttachment(szFilePath, false);
624                                 //attachment->setMessage(this); //set IMessagePtr
625
626                                 memset(szFileName, 0, sizeof(szFileName));
627                                 msg_get_str_value(media, MSG_MMS_MEDIA_FILENAME_STR, szFileName, sizeof(szFileName));
628                                 if ((szFileName[0] =! '\0' )&& (strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0)) 
629                                 {
630                                         LoggerD("renaming to " << szFileName);
631                                         attachment->rename(szFileName);
632                                 }
633                                 else if (MMS_SMIL_MEDIA_TEXT == tempInt) 
634                                 {
635                                         std::stringstream newName;
636                                         newName << "body_page_" << p + 1 << ".txt";
637                                         LoggerD("renaming to " << newName.str());
638                                         attachment->rename(newName.str());
639                                 }
640                         }
641                 }
642         }
643         msg_release_struct(&mms_struct);
644 }
645
646
647 bool Mms::hasAttachment()
648 {
649         std::size_t attachmentSize = getAttachmentsCount();
650         if ( attachmentSize > 0)
651                 return true;
652         else
653                 return false;
654 }
655
656 void Mms::update(bool draftsOnly)
657 {
658     LoggerD("updating m_id=" << getIdRef());
659
660     if (!m_messageData) {
661         //error if platform message not exists
662         LoggerE("message can not be updated");
663         Throw(WrtDeviceApis::Commons::PlatformException);
664     }
665
666     //update mms data from abstraction
667     if (!draftsOnly || getCurrentFolder() == DRAFTBOX) {
668         updateSubject();
669         updateBodyAndAttachments();
670         updateRecipientList();
671         updatePriority();
672     }
673     updateReadStatus();
674 }
675
676 void Mms::updatePriority()
677 {
678     if (isPriorityValid()) {
679         return;
680     }
681     int priority = -1;
682     LoggerI("MMS updating priority");
683
684     //set priority in platform
685     switch (getPriority()) {
686     case MessagePriority::LOW:
687         priority = MSG_MESSAGE_PRIORITY_LOW;
688         break;
689     case MessagePriority::NORMAL:
690         priority = MSG_MESSAGE_PRIORITY_NORMAL;
691         break;
692     case MessagePriority::HIGH:
693         priority = MSG_MESSAGE_PRIORITY_HIGH;
694         break;
695     default:
696         LoggerE("Wrong priority");
697         Throw(WrtDeviceApis::Commons::PlatformException);
698     }
699     LoggerI("priority : " << priority);
700
701     if (MSG_SUCCESS !=
702         msg_set_int_value(m_messageData, MSG_MESSAGE_PRIORITY_INT, priority)){
703         
704         LoggerE("Error during setting priority");
705         Throw(WrtDeviceApis::Commons::PlatformException);
706     }
707 }
708
709 void Mms::updateRecipientList()
710 {
711     // check if abstraction recipient value has been changed
712     if (getToRecipients().isValid() &&
713         getBccRecipients().isValid() &&
714         getCcRecipients().isValid()) {
715         return;
716     }
717
718     LoggerI("MMS updating platform recipients");
719
720     // reset addresses in platform structure
721 //    msg_reset_address(m_messageData);
722         msg_struct_list_s *addr_list = NULL;
723         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
724         int nToCnt = 0;
725         nToCnt = addr_list->nCount;
726
727         LoggerI("addr_list->nCount" << addr_list->nCount);
728         
729         if (nToCnt > 0)         
730         {               
731                 for (int i = 0; i < nToCnt; i++) {      
732                         msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, (char *)"", 0);
733                         msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_DISPLAYNAME_STR, (char *)"", 0);
734                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
735                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_UNKNOWN);
736                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_CONTACT_ID_INT, 0);
737                 }
738                 nToCnt = 0;
739                 addr_list->nCount = nToCnt;
740         }               
741
742         LoggerI("addr_list->nCount" << addr_list->nCount);
743
744         vector<string> to = getToRecipients().getRecipients();
745         vector<string> cc = getCcRecipients().getRecipients();
746         vector<string> bcc = getBccRecipients().getRecipients();
747
748         // update addresses in platform structure
749         if (to.size()) 
750         {
751                 LoggerI("updating to");
752                 for (size_t i = 0; i < to.size(); i++) 
753                 {
754                         if (i >= MAX_TO_ADDRESS_CNT) 
755                         {
756                                 LoggerE("max number of recipient exceeded");
757                                 break;
758                         }
759                         LoggerD("adding to[" << i << "]=" << to[i]);
760
761                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
762
763                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
764                                 MSG_RECIPIENTS_TYPE_TO) != MSG_SUCCESS) 
765                         {
766                                 LoggerE("problem with adding to address");
767                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
768                         }
769                         
770                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
771                                 const_cast<char*>(to[i].c_str()), to[i].size()) != MSG_SUCCESS) 
772                         {
773                                 LoggerE("problem with adding to address");
774                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
775                         }
776                         nToCnt ++;
777                 }
778                 addr_list->nCount = nToCnt;
779                 LoggerE("nToCnt : " << nToCnt);
780                 LoggerE("addr_list->nCount: " <<addr_list->nCount);
781                 
782         }
783         
784         LoggerI("addr_list->nCount" << addr_list->nCount);
785
786         if (cc.size()) 
787         {
788                 LoggerI("updating cc");
789                 for (size_t i = 0; i < cc.size(); i++) 
790                 {
791                         if (nToCnt >= MAX_TO_ADDRESS_CNT) 
792                         {
793                                 LoggerE("max number of recipient exceeded");
794                                 break;
795                         }
796                         LoggerD("adding cc[" << i << "]=" << cc[i]);
797
798                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
799
800                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
801                                 MSG_RECIPIENTS_TYPE_CC) != MSG_SUCCESS) 
802                         {
803                                 LoggerE("problem with adding cc address");
804                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
805                         }
806                         
807                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
808                                 const_cast<char*>(cc[i].c_str()), cc[i].size()) != MSG_SUCCESS) 
809                         {
810                                 LoggerE("problem with adding cc address");
811                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
812                         }
813                         nToCnt ++;
814                 }
815                 addr_list->nCount = nToCnt;
816                 LoggerE("nToCnt : " << nToCnt);
817                 LoggerE("addr_list->nCount: " <<addr_list->nCount);
818         }
819
820         LoggerI("addr_list->nCount" << addr_list->nCount);
821
822         if (bcc.size()) 
823         {
824                 LoggerI("updating bcc");
825                 for (size_t i = 0; i < bcc.size(); i++) 
826                 {
827                         if (nToCnt >= MAX_TO_ADDRESS_CNT) 
828                         {
829                                 LoggerE("max number of recipient exceeded");
830                                 break;
831                         }
832                         LoggerD("adding bcc[" << i << "]=" << bcc[i]);
833
834                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
835
836                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
837                                 MSG_RECIPIENTS_TYPE_BCC) != MSG_SUCCESS) 
838                         {
839                                 LoggerE("problem with adding bcc address");
840                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
841                         }
842                         
843                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
844                                 const_cast<char*>(bcc[i].c_str()), bcc[i].size()) != MSG_SUCCESS) 
845                         {
846                                 LoggerE("problem with adding bcc address");
847                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
848                         }
849                         nToCnt ++;
850                 }
851                 addr_list->nCount = nToCnt;
852                 LoggerE("nToCnt : " << nToCnt);
853                 LoggerE("addr_list->nCount: " <<addr_list->nCount);
854         }
855
856         LoggerI("addr_list->nCount" << addr_list->nCount);              
857         setToValidity(true);
858         setCcValidity(true);
859         setBccValidity(true);
860 }
861
862 void Mms::updateSubject()
863 {
864     // check if abstraction subject value has been changed
865     if (isSubjectValid()) {
866         return;
867     }
868     LoggerI("updating platform subject: " << getSubjectRef());
869     if (MSG_SUCCESS !=
870 //        msg_set_subject(m_messageData, getSubjectRef().c_str())) {
871         msg_set_str_value(m_messageData, MSG_MESSAGE_SUBJECT_STR, const_cast<char*>(getSubjectRef().c_str()), MAX_SUBJECT_LEN)) {
872
873         LoggerE("problem with setting subject");
874         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
875     }
876     setSubjectValidity(true);
877 }
878
879 void Mms::updateBodyAndAttachments()
880 {
881                 // check if attachment or body source address value has been changed
882                 if (isAttachmentsValid() && isBodyValid()) {
883                         return;
884                 }
885         
886 //              MMS_MESSAGE_DATA_S *mmsData = NULL;
887                 msg_struct_t mms_struct = NULL;
888 //              msg_struct_t old_mms_struct = NULL;// THIS
889 //              int ret;// THIS
890
891                 LoggerI("updating platform body and attachment");
892         
893                 Try
894                 {
895                         msg_struct_t region[1];
896                         msg_struct_t page[1];
897                         msg_struct_t media[1];
898                         msg_struct_t smil_text[1];
899                         
900                         if (getMessageType() != MMS) {
901                                 LoggerE("Wrong msg type");
902                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong msg type");
903                         }
904 //                      mmsData = msg_mms_create_message();
905                         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
906 //                      old_mms_struct = msg_create_struct(MSG_STRUCT_MMS);// THIS
907
908 //                      ret = msg_get_mms_struct(m_messageData, old_mms_struct);// THIS
909 //                      LoggerE("ret " << ret);// THIS
910 //                      ret = msg_release_struct(&old_mms_struct);// THIS
911 //                      LoggerE("ret " << ret);// THIS
912
913                         if (!mms_struct) {
914                                 LoggerE("create message body failed");
915                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "create message body failed");
916                         }
917         
918                         //body
919                         if (!getBodyRef().empty()) {
920                                 FILE* f = NULL;
921                                 f = fopen(m_bodyFilePath.c_str(), "w");
922                                 if (!f) {
923                                         LoggerE("cannot create file with body" << m_bodyFilePath);
924                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
925                                                          "cannot create file with body");
926                                 }
927                                 LoggerD("body file: " << m_bodyFilePath);
928                                 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
929                          
930                                 fclose(f);
931
932                                 LoggerD("getBodyRef(): " << getBodyRef());
933
934                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_INT, ROOT_LAYOUT_WIDTH);
935                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
936                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_BGCOLOR_INT, WHITE_COLOR);
937                                 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_PERCENT_BOOL, true);
938                                 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_PERCENT_BOOL, true);
939                                 
940                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_REGION, &region[0]);
941                                 msg_set_str_value(region[0], MSG_MMS_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
942                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_INT, 0);
943                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_TOP_INT, 0);
944                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_INT, ROOT_LAYOUT_WIDTH);
945                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
946                                 msg_set_int_value(region[0], MSG_MMS_REGION_BGCOLOR_INT, WHITE_COLOR);
947                                 
948                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_PERCENT_BOOL, true);
949                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_TOP_PERCENT_BOOL, true);
950                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_PERCENT_BOOL, true);
951                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_PERCENT_BOOL, true);
952
953                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_PAGE, &page[0]);
954                                 msg_set_int_value(page[0], MSG_MMS_PAGE_PAGE_DURATION_INT, 0);
955                                 
956                                 msg_mms_add_item(page[0], MSG_STRUCT_MMS_MEDIA, &media[0]);
957                                 msg_set_int_value(media[0], MSG_MMS_MEDIA_TYPE_INT, MMS_SMIL_MEDIA_TEXT);
958                                 msg_set_str_value(media[0], MSG_MMS_MEDIA_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
959                                 int error;
960                                 error = msg_set_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, const_cast<char*>(m_bodyFilePath.c_str()),MSG_FILEPATH_LEN_MAX);
961                                 LoggerD("bodyFile Path= " << m_bodyFilePath);
962                                 LoggerD("error= " << error);
963                                 char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
964                                 memset(szFilePath, 0, sizeof(szFilePath));
965                                 msg_get_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
966                                 LoggerD("bodyFile Path= " << m_bodyFilePath);
967
968                                 msg_get_struct_handle(media[0], MSG_MMS_MEDIA_SMIL_TEXT_HND, &smil_text[0]);
969                                 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_COLOR_INT, BLACK_COLOR);
970                                 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_SIZE_INT, MMS_SMIL_FONT_SIZE_NORMAL);
971                                 msg_set_bool_value(smil_text[0], MSG_MMS_SMIL_TEXT_BOLD_BOOL, true);
972                                 
973                         }
974                         
975                         //attachments
976                         struct stat buffer;
977                         int errnum = 0;
978
979                         msg_struct_t attachment[20];                    
980                         
981                         for (size_t idx = 0; idx < getAttachmentsRef().size(); ++idx) {
982                                 IAttachmentPtr att = getAttachment(idx);
983                                 if (!att) {
984                                         continue;
985                                 }
986         
987                                 LoggerD("Add attachment=" << att->getFullPath());
988                                 //checking file
989                                 errnum = stat(att->getFullPath().c_str(), &buffer);
990                                 LoggerD("errnum=" << errnum);
991
992                                 if (errnum != 0) {
993                                         LoggerE("Opening file: " << att->getFullPath().c_str());
994                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
995                                                          "cannot open attachment file");
996                                 }
997         
998                                 //this function should return valid pointer
999                                 att->setMessage(SharedFromThis());      // setMessage to setMessageId
1000                                 att->setAttachmentID((int)idx);
1001                                 att->setDownloaded(true);
1002
1003                                 int ret = mimeTypeStringToIndexInt(att->getMimeType());
1004
1005                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_ATTACH, &attachment[idx]);
1006                                 msg_set_str_value(attachment[idx], MSG_MMS_ATTACH_FILEPATH_STR, const_cast<char*>(att->getFullPath().c_str()), MSG_FILEPATH_LEN_MAX);
1007
1008                                 if(ret != MIME_UNKNOWN)
1009                                 {
1010                                         msg_set_int_value(attachment[idx], MSG_MMS_ATTACH_MIME_TYPE_INT, ret);
1011                                 }
1012                                 
1013                                 LoggerD("Attachment added");
1014         
1015                                 //reset errno
1016                                 errnum = 0;
1017                         }
1018         
1019                 if (MSG_SUCCESS !=msg_set_mms_struct(m_messageData, mms_struct))
1020                 {
1021                         LoggerE("set message body error");
1022                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "set message body error");
1023                 }
1024
1025                 msg_release_struct(&mms_struct);
1026
1027                 setAttachmentsValidity(true);
1028                 setBodyValidity(true);
1029                 
1030                 }
1031                 Catch(WrtDeviceApis::Commons::PlatformException) {
1032                         LoggerE("Platform error");
1033                         msg_release_struct(&mms_struct);
1034                         throw;
1035                 }
1036 }
1037
1038
1039 void Mms::updateReadStatus()
1040 {
1041     if (isReadStatusValid()) {
1042         return;
1043     }
1044
1045     LoggerI("updating platform read status: " << isRead());
1046 //    if (MSG_SUCCESS != msg_set_read_status(m_messageData, isRead())) {
1047     if (MSG_SUCCESS != msg_set_bool_value(m_messageData, MSG_MESSAGE_READ_BOOL, isRead())) {
1048         LoggerE("problem with setting subject");
1049         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
1050     }
1051
1052     setReadStatusValidity(true);
1053 }
1054
1055 void Mms::updateIsRead()
1056 {
1057     LoggerD("updating m_id=" << getIdRef());
1058
1059     if (!m_messageData) {
1060         //error if platform message not exists
1061         LoggerE("message can not be updated");
1062         Throw(WrtDeviceApis::Commons::PlatformException);
1063     }
1064         
1065     // check if abstraction from m_isReadChange value has been changed
1066     if (isReadChangeStatusValid()) {
1067         // do not update if not changed
1068         return;
1069     }
1070
1071     Try
1072     {
1073         if (this->getIdRef().empty()) {
1074             LoggerE("existing msgId is zero, remove msg not done");
1075             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1076                      "existing msgId is zero, remove msg not done");
1077         }
1078
1079         if (MSG_SUCCESS !=
1080             msg_update_read_status(MsgGetCommonHandle(), convertId(getIdRef()), isReadChangeStatus()))
1081         {
1082             LoggerE("Failed to update isRead");
1083             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update isRead");
1084         }
1085     }
1086     Catch(WrtDeviceApis::Commons::PlatformException) {
1087         LoggerE("platform error occurs");
1088     }
1089     setisReadChangeStatusValidity(true);
1090
1091 }
1092
1093 void Mms::updateMessage()
1094 {
1095         LoggerD("updating m_id=" << getIdRef());
1096         msg_error_t err = MSG_SUCCESS;
1097         int tempInt = 0;
1098
1099         msg_struct_t msg = NULL;
1100         msg_struct_t sendOpt = NULL;
1101         
1102         if (!m_messageData) {
1103                 //error if platform message not exists
1104                 LoggerE("message can not be updated");
1105                 Throw(WrtDeviceApis::Commons::PlatformException);
1106         }
1107
1108         
1109         Try
1110         {
1111                 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1112                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);                        
1113                 
1114                 err = msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg, sendOpt);
1115                 
1116                 if (err != MSG_SUCCESS)
1117                 {
1118                         LoggerE("msg_get_message() Failed!");
1119                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to get message for update");
1120                 }
1121         
1122                 if (this->getIdRef().empty()) {
1123                         LoggerE("existing msgId is zero, update msg not done");
1124                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1125                                         "existing msgId is zero, update msg not done");
1126                 }
1127
1128                 err = msg_get_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT, &tempInt);
1129                 if(err == MSG_SUCCESS)
1130                 {
1131                         msg_set_int_value(m_messageData, MSG_MESSAGE_NETWORK_STATUS_INT, tempInt);
1132                 }
1133
1134                 err = msg_get_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1135                 if(err == MSG_SUCCESS)
1136                 {
1137                         msg_set_int_value(m_messageData, MSG_MESSAGE_FOLDER_ID_INT, tempInt);
1138                 }
1139
1140                 update(TRUE);
1141                 
1142                 if (MSG_SUCCESS != msg_update_message(MsgGetCommonHandle(), m_messageData, sendOpt))
1143                 {
1144                         LoggerE("msg_update_message() Failed!");
1145                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
1146                 }
1147                 msg_release_struct(&msg);
1148                 msg_release_struct(&sendOpt);
1149         }
1150         Catch(WrtDeviceApis::Commons::PlatformException) {
1151                 msg_release_struct(&msg);
1152                 msg_release_struct(&sendOpt);
1153                 LoggerE("platform error occurs");
1154                 ReThrow(WrtDeviceApis::Commons::PlatformException);
1155         }
1156 }
1157
1158
1159 void Mms::createSendMessage()
1160 {
1161     LoggerD("convert m_id= " << convertId(getIdRef())); 
1162
1163     msg_struct_t sendOpt = NULL;
1164
1165     //prepare for add sms to draft folder
1166     if (!m_messageData) {
1167         //error if platform message not exists
1168         LoggerE("message can not be updated");
1169         Throw(WrtDeviceApis::Commons::PlatformException);
1170     }
1171
1172     //update all mms data
1173     if (getCurrentFolder() == DRAFTBOX) {
1174                 LoggerD("update all mms data");
1175         updateSubject();
1176         updateBodyAndAttachments();
1177         updateRecipientList();
1178         updatePriority();
1179     }
1180
1181     Try
1182     {
1183         int msgId = 0;
1184         sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1185         // trying to add message
1186
1187 //        msg_set_int_value(m_messageData, MSG_MESSAGE_NETWORK_STATUS_INT, MSG_NETWORK_NOT_SEND);
1188
1189         int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
1190         if (ret < MSG_SUCCESS) {
1191             LoggerE("msg_add_message failed, error code=" << ret);
1192             Throw(WrtDeviceApis::Commons::PlatformException);
1193         }
1194         else
1195         {
1196             msgId = ret;
1197         }
1198
1199         LoggerD("Message ID : " << msgId);
1200
1201         msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1202
1203         setId(convertId(msgId));
1204         loadDraftMessage();
1205
1206         setFolderType(OUTBOX);
1207         setMessageStatus(MESSAGE_STATUS_DRAFT);
1208
1209         msg_release_struct(&sendOpt);
1210     }
1211     Catch(WrtDeviceApis::Commons::PlatformException) {
1212         LoggerE("remove message error");
1213         msg_release_struct(&sendOpt);
1214         throw;
1215     }
1216 }       
1217
1218 void Mms::addMessageToDraft()
1219 {
1220         LoggerD("convert m_id= " << convertId(getIdRef()));
1221
1222         msg_struct_t sendOpt = NULL;
1223
1224         //prepare for add sms to draft folder
1225         if (!m_messageData) {
1226                 //error if platform message not exists
1227                 LoggerE("message can not be updated");
1228                 Throw(WrtDeviceApis::Commons::PlatformException);
1229         }
1230
1231         //update all mms data
1232         if (getCurrentFolder() == DRAFTBOX) {
1233                 updateSubject();
1234                 updateBodyAndAttachments();
1235                 updateRecipientList();
1236                 updatePriority();
1237         }
1238
1239         Try
1240         {
1241                 int msgId = -1;
1242                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1243
1244                 // trying to add message
1245                 LoggerD("add message");
1246                 int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
1247                 if (ret < MSG_SUCCESS) {
1248                         LoggerE("msg_add_message failed, error code=" << ret);
1249                         Throw(WrtDeviceApis::Commons::PlatformException);
1250                 }
1251                 else
1252                 {
1253                         msgId = ret;
1254                 }
1255
1256                 LoggerD("Message ID : " << msgId);
1257                 if (msgId < 0)
1258                 {
1259                         LoggerD("Message ID is invailded ");
1260                         Throw(WrtDeviceApis::Commons::PlatformException);
1261                 }
1262
1263                 msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1264
1265                 setId(convertId(msgId));
1266                 loadDraftMessage();
1267
1268                 msg_release_struct(&sendOpt);
1269
1270         }
1271         Catch(WrtDeviceApis::Commons::PlatformException) {
1272                 LoggerE("add draft message error");
1273                 msg_release_struct(&sendOpt);
1274                 throw;
1275         }
1276
1277 }               
1278
1279 void Mms::loadDraftMessage()
1280 {
1281         LoggerD("entered");
1282
1283         if (getIdRef().empty() || (EMPTY_ID == getIdRef())) 
1284         {
1285                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
1286         }
1287
1288         msg_release_struct(&m_messageData);
1289         m_messageData = NULL;
1290         msg_struct_t sendOpt = NULL;
1291
1292         Try {
1293
1294                 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1295                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1296
1297                 // trying to get message from platform
1298                 if (MSG_SUCCESS != msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), m_messageData, sendOpt)) 
1299                 {
1300                         LoggerE("get message error");
1301                         Throw(WrtDeviceApis::Commons::PlatformException);
1302                 }
1303                 LoggerD("message found with msgId=" << getIdRef());
1304
1305                 // read all mms dat to abstraction layer
1306                 readConversationId(m_messageData);
1307                 readPriority(m_messageData);
1308                 readSubject(m_messageData);
1309                 readFolder(m_messageData);
1310                 readDateTime(m_messageData);
1311                 readReadStatus(m_messageData);
1312                 readMessageStatus(m_messageData);
1313                 readBody(m_messageData);
1314
1315                 msg_release_struct(&sendOpt);
1316         }
1317         Catch(WrtDeviceApis::Commons::PlatformException) {
1318                 LoggerE("exception");
1319                 // nothing to do
1320                 msg_release_struct(&sendOpt);
1321         }
1322
1323 }
1324
1325 void Mms::readAllData()
1326 {
1327         LoggerD("entered");
1328
1329         if (getIdRef().empty() || (EMPTY_ID == getIdRef())) 
1330         {
1331                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
1332         }
1333
1334         msg_release_struct(&m_messageData);
1335         m_messageData = NULL;
1336         msg_struct_t sendOpt = NULL;
1337
1338         Try {
1339
1340                 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1341                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1342
1343                 // trying to get message from platform
1344                 if (MSG_SUCCESS != msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), m_messageData, sendOpt)) 
1345                 {
1346                         LoggerE("get message error");
1347                         Throw(WrtDeviceApis::Commons::PlatformException);
1348                 }
1349                 LoggerD("message found with msgId=" << getIdRef());
1350
1351                 // read all mms dat to abstraction layer
1352                 readConversationId(m_messageData);
1353 //              readRecipientList(m_messageData);
1354                 readPriority(m_messageData);
1355                 readSubject(m_messageData);
1356                 readBodyAndAttachments(m_messageData);
1357                 readFolder(m_messageData);
1358                 readDateTime(m_messageData);
1359                 readReadStatus(m_messageData);
1360                 readMessageStatus(m_messageData);
1361
1362                 msg_release_struct(&sendOpt);
1363         }
1364         Catch(WrtDeviceApis::Commons::PlatformException) {
1365                 LoggerE("exception");
1366                 // nothing to do
1367                 msg_release_struct(&sendOpt);
1368         }
1369
1370 }
1371
1372 void Mms::moveToFolder(const FolderType newFolder)
1373 {
1374 /*
1375     update();
1376
1377     Try
1378     {
1379         msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1380                 newFolder);
1381
1382         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1383                                    platfromFolderId) != MSG_SUCCESS) {
1384             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error during movinf message");
1385         }
1386     }
1387
1388     Catch(WrtDeviceApis::Commons::PlatformException) {
1389         LoggerE("remove message error");
1390         throw;
1391     }
1392 */
1393 }
1394
1395 void Mms::moveToFolder(const string& newFolder)
1396 {
1397 /*
1398     update();
1399
1400     Try
1401     {
1402         msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1403                 newFolder);
1404
1405         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1406                                    platfromFolderId) != MSG_SUCCESS) {
1407             LoggerE("msg_move_msg_to_folder error");
1408             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_move_msg_to_folder error");
1409         }
1410     }
1411
1412     Catch(WrtDeviceApis::Commons::PlatformException) {
1413         LoggerE("remove message error");
1414         throw;
1415     }
1416     */
1417 }
1418
1419 void Mms::copyToFolder(const FolderType newFolder)
1420 {
1421 /*
1422     update();
1423
1424     msg_message_t msg = msg_new_message();
1425
1426     Try
1427     {
1428         MSG_SENDINGOPT_S option = { false, false, false };
1429         option.option.smsSendOpt.bReplyPath = true;
1430         if (MSG_SUCCESS ==
1431             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1432                             &option)) {
1433             const msg_folder_id_t platfromFolderId =
1434                 Messaging::convertFolderToPlatform(newFolder);
1435             msg_set_message_id(msg, 0);
1436             msg_set_folder_id(msg, platfromFolderId);
1437
1438             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1439             if (error != MSG_SUCCESS) {
1440                 LoggerE("msg_add_message failed, error code=" << error);
1441                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1442             }
1443         }
1444         if (msg) {
1445             msg_release_message(&msg);
1446         }
1447     }
1448
1449     Catch(WrtDeviceApis::Commons::PlatformException) {
1450         LoggerE("remove message error");
1451         if (msg) {
1452             msg_release_message(&msg);
1453         }
1454         throw;
1455     }
1456 */
1457 }
1458
1459 void Mms::copyToFolder(const string& newFolder)
1460 {
1461 /*
1462     update();
1463
1464     msg_message_t msg = msg_new_message();
1465
1466     Try
1467     {
1468         MSG_SENDINGOPT_S option = { false, false, false };
1469         option.option.smsSendOpt.bReplyPath = true;
1470         if (MSG_SUCCESS ==
1471             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1472                             &option)) {
1473             const msg_folder_id_t platfromFolderId =
1474                 Messaging::convertFolderToPlatform(newFolder);
1475             msg_set_message_id(msg, 0);
1476             msg_set_folder_id(msg, platfromFolderId);
1477
1478             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1479             if (error != MSG_SUCCESS) {
1480                 LoggerE("msg_add_message failed, error code=" << error);
1481                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1482             }
1483         }
1484         if (msg) {
1485             msg_release_message(&msg);
1486         }
1487     }
1488
1489     Catch(WrtDeviceApis::Commons::PlatformException) {
1490         LoggerE("remove message error");
1491         if (msg) {
1492             msg_release_message(&msg);
1493         }
1494         throw;
1495     }
1496 */
1497 }
1498
1499 void Mms::remove()
1500 {
1501     LoggerD("delete m_id=" << getIdRef());
1502
1503     Try
1504     {
1505         if (this->getIdRef().empty()) {
1506             LoggerE("existing msgId is zero, remove msg not done");
1507             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1508                      "existing msgId is zero, remove msg not done");
1509         }
1510
1511         if (MSG_SUCCESS !=
1512             msg_delete_message(MsgGetCommonHandle(),
1513                                convertId(getIdRef()))) {
1514             LoggerE("Failed to delete Message");
1515             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to delete Message");
1516         }
1517     }
1518
1519     Catch(WrtDeviceApis::Commons::PlatformException) {
1520         LoggerE("platform error occurs");
1521     }
1522 }
1523
1524 int Mms::send()
1525 {
1526         LoggerD("sending message, id=" << getIdRef());
1527         msg_error_t err = MSG_SUCCESS;
1528
1529         Try
1530         {
1531                 //prepare for sending sms/mms
1532                 update();
1533
1534                 LoggerD("trying to send mms");
1535                 if (!getCellularOn())
1536                 {
1537                         err = MSG_ERR_TRANSPORT_ERROR;
1538                         LoggerW("MMS send error. No transport possible over network");
1539                 }
1540                 else
1541                 {
1542                         err = CallbackMgrSingleton::Instance().registerAndSend(msg_mms_send_message, m_messageData, this);
1543                 }
1544
1545                 if (err != MSG_SUCCESS) 
1546                 {
1547                         LoggerE("Sending Message (submit request) failed!!! err=" << err);
1548                         setMessageStatus(MESSAGE_STATUS_FAILED);
1549                         Throw(WrtDeviceApis::Commons::PlatformException);
1550                 }
1551
1552                 setMessageStatus(MESSAGE_STATUS_SENDING);
1553
1554                 LoggerD("Sending Message request is submitted!");
1555         }
1556         Catch(WrtDeviceApis::Commons::PlatformException) {
1557                 LoggerE("message is not send, manually run callback");
1558         ReqReceiverMessage *requestReceiver = getRequestReceiver();
1559         if (requestReceiver) {
1560                 LoggerE("send Error");
1561                 EventMessagingServicePtr event = getMessagingServiceEvent();
1562              if(err == MSG_ERR_TRANSPORT_ERROR)
1563              {
1564                  event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::PlatformWrongStateException);
1565              }
1566              else
1567              {
1568                  event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1569              }
1570                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService>::ManualAnswer(event);
1571         }
1572         }
1573
1574         return 0;
1575 }
1576
1577 void Mms::sendingCallback(msg_struct_t sent_status)
1578 {
1579         int status = MSG_NETWORK_SEND_FAIL;
1580         msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
1581         
1582         LoggerD("status : " << status);
1583         
1584         if (MSG_NETWORK_SEND_SUCCESS == status) {
1585                 LoggerD("sending ok");
1586                 setSendingStatusOk();
1587                 setMessageStatus(MESSAGE_STATUS_SENT);
1588         } else {
1589                 LoggerE("sending failed: " << static_cast<unsigned int>(status));
1590                 setSendingStatusFailed();
1591                 setMessageStatus(MESSAGE_STATUS_FAILED);
1592         }
1593 }
1594
1595 void Mms::sendCancel(int handle)
1596 {
1597     LoggerD("sending message, id=" << getIdRef());
1598     //#warning "TODO"
1599     LoggerD("handle =" << handle);
1600 }
1601
1602 void Mms::setSendingStatusOk()
1603 {
1604     //success callback should be executed here
1605         ReqReceiverMessage *requestReceiver = getRequestReceiver();
1606         msg_struct_list_s *addr_list = NULL;
1607         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1608         int nToCnt;
1609         
1610         if (requestReceiver) {
1611                 LoggerI("calling JS success callback");
1612                 EventMessagingServicePtr event = getMessagingServiceEvent();
1613                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1614
1615                 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1616
1617                 nToCnt = addr_list->nCount;
1618
1619                 for ( int index=0; index < nToCnt; index++)
1620                 {
1621                         msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1622                         event->m_successRecipients.push_back(strNumber);
1623                 }
1624           
1625                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1626         }
1627 }
1628
1629 void Mms::setSendingStatusFailed()
1630 {       
1631     //error callback should be executed here
1632         EventOnSendingFailedEmitterPtr emitter = getEmitter();
1633         msg_struct_list_s *addr_list = NULL;
1634         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1635         int nToCnt;
1636         
1637         if (emitter) 
1638         {
1639                 EventOnSendingFailedPtr event(new EventOnSendingFailed);
1640                 event->setError(EventOnSendingFailed::UNKNOWN); // TODO error codes
1641                 emitter->emit(event);
1642         } 
1643         else 
1644         {
1645                 ReqReceiverMessage *requestReceiver = getRequestReceiver();
1646                 EventMessagingServicePtr event = getMessagingServiceEvent();
1647                 if (requestReceiver) 
1648                 {
1649                         LoggerE("calling JS error callback");
1650                         event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1651                         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1652                         nToCnt = addr_list->nCount;
1653
1654                         LoggerD("nToCnt : " << nToCnt);
1655
1656                         for ( int index=0; index < nToCnt; index++)
1657                         {
1658                                 msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1659                                 event->m_failRecipients.push_back(strNumber);
1660                         }
1661
1662                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1663                 }
1664         }
1665 }
1666
1667 FolderType Mms::toFolder(const std::string &folder)
1668 {
1669     if (folder == "INBOX") {
1670         return INBOX;
1671     } else if (folder == "OUTBOX") {
1672         return OUTBOX;
1673     } else if (folder == "SENTBOX") {
1674         return SENTBOX;
1675     } else if (folder == "DRAFTBOX") {
1676         return DRAFTBOX;
1677     }
1678     ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Invalid folder");
1679 }
1680
1681 bool Mms::getCellularOn()
1682 {
1683     auto cellularOn = true;
1684     int tempInt;
1685     int tempbool;
1686
1687     vconf_get_int(VCONFKEY_TELEPHONY_SVCTYPE, &tempInt);
1688     vconf_get_bool(VCONFKEY_TELEPHONY_FLIGHT_MODE, &tempbool);
1689     if(tempInt <= VCONFKEY_TELEPHONY_SVCTYPE_SEARCH || tempbool == TRUE)
1690     {
1691         LoggerE("VCONFKEY_TELEPHONY_SVCTYPE error " << tempInt);
1692         LoggerE("VCONFKEY_TELEPHONY_FLIGHT_MODE error " << tempbool);
1693         cellularOn = false;
1694         return cellularOn;
1695     }
1696
1697     vconf_get_bool(VCONFKEY_3G_ENABLE, &tempbool);
1698     vconf_get_int(VCONFKEY_TELEPHONY_SVC_PS, &tempInt);
1699     if(tempbool == FALSE || tempInt != VCONFKEY_TELEPHONY_SVC_PS_ON)
1700     {
1701         LoggerE("VCONFKEY_3G_ENABLE error " << tempbool);
1702         LoggerE("VCONFKEY_TELEPHONY_SVC_PS_ON error " << tempInt);
1703         cellularOn = false;
1704     }
1705     return cellularOn;
1706 }
1707
1708
1709 MimeType Mms::mimeTypeStringToIndexInt(std::string inputString)
1710 {
1711         const char* szMimeStr = inputString.c_str();
1712         MimeType retInt = MIME_UNKNOWN;
1713
1714         LoggerD("szMimeStr " << szMimeStr);
1715
1716         for(int idx =0; strcmp(mimeTable[idx].szMIME, "") != 0; idx++)
1717         {
1718                 if (!strcmp(mimeTable[idx].szMIME, szMimeStr)) {
1719                         retInt = mimeTable[idx].mime;
1720                         break;
1721                 }
1722         }
1723         return retInt;
1724
1725 }
1726
1727 std::string Mms::indexIntToMimeTypeString(MimeType inputIndex)
1728 {
1729         std::string retString;
1730         
1731         for (int idx=0; mimeTable[idx].szMIME != NULL; idx++) {
1732                 if (inputIndex == mimeTable[idx].mime) {
1733                         retString = (char *)mimeTable[idx].szMIME;
1734                 }
1735         }
1736         return retString;
1737 }
1738
1739 msg_struct_t Mms::createNewCopyOfPLatformMsgWithAddressList(const msg_struct_t src) const
1740 {
1741         int tempInt, nCount, ret;
1742         bool tempBool;
1743         msg_struct_list_s *addr_list;
1744         msg_struct_list_s *new_addr_list;
1745
1746         msg_struct_t mms_struct = NULL;
1747         msg_struct_t mms_struct_dummy = NULL;   
1748
1749         msg_struct_t msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1750
1751         msg_set_int_value(msg, MSG_MESSAGE_TYPE_INT, MSG_TYPE_MMS);
1752
1753         char strSubject[MAX_SUBJECT_LEN] = {0};
1754         msg_get_str_value(src, MSG_MESSAGE_SUBJECT_STR, strSubject, MAX_SUBJECT_LEN);
1755         msg_set_str_value(msg, MSG_MESSAGE_SUBJECT_STR, strSubject, MAX_SUBJECT_LEN);
1756
1757         msg_get_int_value(src, MSG_MESSAGE_STORAGE_ID_INT, &tempInt);
1758         msg_set_int_value(msg, MSG_MESSAGE_STORAGE_ID_INT, tempInt);
1759
1760         msg_get_int_value(src, MSG_MESSAGE_THREAD_ID_INT, &tempInt);
1761         msg_set_int_value(msg, MSG_MESSAGE_THREAD_ID_INT, tempInt);
1762
1763         msg_get_int_value(src, MSG_MESSAGE_FOLDER_ID_INT, &tempInt);
1764         msg_set_int_value(msg, MSG_MESSAGE_FOLDER_ID_INT, tempInt);
1765
1766 //      msg_get_int_value(src, MSG_MESSAGE_DATA_SIZE_INT, &tempInt);
1767 //      char tempStr[tempInt+1];
1768 //      memset(tempStr, 0, tempInt+1);
1769
1770 //      msg_get_str_value(src, MSG_MESSAGE_SMS_DATA_STR, tempStr, tempInt);
1771 //      msg_set_str_value(msg, MSG_MESSAGE_SMS_DATA_STR, tempStr, strlen(tempStr));
1772 //    copy mms body and attachment
1773         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
1774         mms_struct_dummy = msg_create_struct(MSG_STRUCT_MMS);
1775         ret = msg_get_mms_struct(src, mms_struct);
1776         if (ret != MSG_SUCCESS)
1777         {
1778                 LoggerE("cannot get mms struct");
1779                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "cannot get mms struct");
1780         }
1781
1782         msg_set_mms_struct(src, mms_struct_dummy);
1783         msg_set_mms_struct(msg, mms_struct);
1784
1785         msg_get_int_value(src, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
1786         msg_set_int_value(msg, MSG_MESSAGE_DISPLAY_TIME_INT, tempInt);
1787
1788 //    msg_set_network_status(msg, msg_get_network_status(src));
1789         msg_get_int_value(src, MSG_MESSAGE_NETWORK_STATUS_INT, &tempInt);
1790         msg_set_int_value(msg, MSG_MESSAGE_NETWORK_STATUS_INT,  tempInt);
1791
1792 //    msg_set_encode_type(msg, msg_get_encode_type(src));
1793         msg_get_int_value(src, MSG_MESSAGE_ENCODE_TYPE_INT, &tempInt);
1794         msg_set_int_value(msg, MSG_MESSAGE_ENCODE_TYPE_INT,  tempInt);
1795
1796 //    msg_set_read_status(msg, msg_is_read(src));
1797         msg_get_bool_value(src, MSG_MESSAGE_READ_BOOL, &tempBool);
1798         msg_set_bool_value(msg, MSG_MESSAGE_READ_BOOL, tempBool);
1799
1800 //        msg_set_protect_status(msg, msg_is_protected(src));
1801         msg_get_bool_value(src, MSG_MESSAGE_PROTECTED_BOOL, &tempBool);
1802         msg_set_bool_value(msg, MSG_MESSAGE_PROTECTED_BOOL, tempBool);
1803
1804 //        msg_set_priority_info(msg, msg_get_priority_info(src));
1805         msg_get_int_value(src, MSG_MESSAGE_PRIORITY_INT, &tempInt);
1806         msg_set_int_value(msg, MSG_MESSAGE_PRIORITY_INT,  tempInt);
1807
1808 //        msg_set_direction_info(msg, msg_get_direction_info(src));
1809         msg_get_int_value(src, MSG_MESSAGE_DIRECTION_INT, &tempInt);
1810         msg_set_int_value(msg, MSG_MESSAGE_DIRECTION_INT,  tempInt);
1811
1812 //        msg_set_port(msg, msg_get_dest_port(src), msg_get_src_port(src));
1813         msg_get_int_value(src, MSG_MESSAGE_DEST_PORT_INT, &tempInt);
1814         msg_set_int_value(msg, MSG_MESSAGE_DEST_PORT_INT,  tempInt);
1815         msg_get_int_value(src, MSG_MESSAGE_SRC_PORT_INT, &tempInt);
1816         msg_set_int_value(msg, MSG_MESSAGE_SRC_PORT_INT,  tempInt);
1817
1818 // copy addr list
1819         msg_get_list_handle(src, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1820         msg_get_list_handle(msg, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&new_addr_list);
1821
1822         new_addr_list->nCount = addr_list->nCount;
1823
1824         for(int i=0; i<addr_list->nCount; i++)
1825         {
1826                 msg_struct_t addr_info = NULL;
1827                 msg_struct_t new_addr_info = NULL;
1828                 char addr_value[MAX_ADDRESS_VAL_LEN] = {0,};
1829                 
1830                 //get original address
1831                 addr_info = addr_list->msg_struct_info[i];
1832                 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr_value, MAX_ADDRESS_VAL_LEN);
1833                 msg_get_int_value(addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
1834
1835                 //copy original address
1836                 new_addr_info = new_addr_list->msg_struct_info[i];
1837                 msg_set_str_value(new_addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, addr_value, MAX_ADDRESS_VAL_LEN);
1838                 msg_set_int_value(new_addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, tempInt);
1839         }
1840
1841         msg_release_struct(&mms_struct_dummy);
1842         msg_release_struct(&mms_struct);
1843
1844     return msg;
1845 }
1846
1847 }
1848 }