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