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