Update change log and spec for wrt-plugins-tizen_0.4.29
[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                 attach = (msg_struct_t)msg_list_nth_data(attach_list, a);
503                 if (NULL == attach) 
504                 {
505                         LoggerE("attachment is null, continue");
506                         continue;
507                 }
508
509                 memset(szFilePath, 0, sizeof(szFilePath));
510                 memset(szFileName, 0, sizeof(szFileName));
511
512                 msg_get_str_value(attach, MSG_MMS_ATTACH_FILEPATH_STR, szFilePath, sizeof(szFilePath));
513                 msg_get_str_value(attach, MSG_MMS_ATTACH_FILENAME_STR, szFileName, sizeof(szFileName));
514                 
515                 IAttachmentPtr att = appendAttachment(szFilePath, false);
516                 if ((szFileName[0] =! '\0') &&
517                         strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0) {
518                         LoggerD("renaming to " << szFileName);
519                         att->rename(szFileName);
520                 }
521         }
522         msg_release_struct(&mms_struct);
523 }
524
525 bool Mms::hasAttachment()
526 {
527         std::size_t attachmentSize = getAttachmentsCount();
528         if ( attachmentSize > 0)
529                 return true;
530         else
531                 return false;
532 }
533
534 void Mms::update(bool draftsOnly)
535 {
536     LoggerD("updating m_id=" << getIdRef());
537
538     if (!m_messageData) {
539         //error if platform message not exists
540         LoggerE("message can not be updated");
541         Throw(WrtDeviceApis::Commons::PlatformException);
542     }
543
544     //update mms data from abstraction
545     if (!draftsOnly || getCurrentFolder() == DRAFTBOX) {
546         updateSubject();
547         updateBodyAndAttachments();
548         updateRecipientList();
549         updatePriority();
550     }
551     updateReadStatus();
552 }
553
554 void Mms::updatePriority()
555 {
556     if (isPriorityValid()) {
557         return;
558     }
559     int priority = -1;
560     LoggerI("MMS updating priority");
561
562     //set priority in platform
563     switch (getPriority()) {
564     case MessagePriority::LOW:
565         priority = MSG_MESSAGE_PRIORITY_LOW;
566         break;
567     case MessagePriority::NORMAL:
568         priority = MSG_MESSAGE_PRIORITY_NORMAL;
569         break;
570     case MessagePriority::HIGH:
571         priority = MSG_MESSAGE_PRIORITY_HIGH;
572         break;
573     default:
574         LoggerE("Wrong priority");
575         Throw(WrtDeviceApis::Commons::PlatformException);
576     }
577     LoggerI("priority : " << priority);
578
579     if (MSG_SUCCESS !=
580         msg_set_int_value(m_messageData, MSG_MESSAGE_PRIORITY_INT, priority)){
581         
582         LoggerE("Error during setting priority");
583         Throw(WrtDeviceApis::Commons::PlatformException);
584     }
585 }
586
587 void Mms::updateRecipientList()
588 {
589     // check if abstraction recipient value has been changed
590     if (getToRecipients().isValid() &&
591         getBccRecipients().isValid() &&
592         getCcRecipients().isValid()) {
593         return;
594     }
595
596     LoggerI("MMS updating platform recipients");
597
598     // reset addresses in platform structure
599 //    msg_reset_address(m_messageData);
600         msg_struct_list_s *addr_list = NULL;
601         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
602         int nToCnt = 0;
603         nToCnt = addr_list->nCount;
604
605         LoggerI("addr_list->nCount" << addr_list->nCount);
606         
607         if (nToCnt > 0)         
608         {               
609                 for (int i = 0; i < nToCnt; i++) {      
610                         msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, (char *)"", 0);
611                         msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_DISPLAYNAME_STR, (char *)"", 0);
612                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
613                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_UNKNOWN);
614                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_CONTACT_ID_INT, 0);
615                 }
616                 nToCnt = 0;
617                 addr_list->nCount = nToCnt;
618         }               
619
620         LoggerI("addr_list->nCount" << addr_list->nCount);
621
622         vector<string> to = getToRecipients().getRecipients();
623         vector<string> cc = getCcRecipients().getRecipients();
624         vector<string> bcc = getBccRecipients().getRecipients();
625
626         // update addresses in platform structure
627         if (to.size()) 
628         {
629                 LoggerI("updating to");
630                 for (size_t i = 0; i < to.size(); i++) 
631                 {
632                         if (i >= MAX_TO_ADDRESS_CNT) 
633                         {
634                                 LoggerE("max number of recipient exceeded");
635                                 break;
636                         }
637                         LoggerD("adding to[" << i << "]=" << to[i]);
638
639                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
640
641                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
642                                 MSG_RECIPIENTS_TYPE_TO) != MSG_SUCCESS) 
643                         {
644                                 LoggerE("problem with adding to address");
645                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
646                         }
647                         
648                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
649                                 const_cast<char*>(to[i].c_str()), to[i].size()) != MSG_SUCCESS) 
650                         {
651                                 LoggerE("problem with adding to address");
652                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
653                         }
654                         nToCnt ++;
655                 }
656                 addr_list->nCount = nToCnt;
657                 LoggerE("nToCnt : " << nToCnt);
658                 LoggerE("addr_list->nCount: " <<addr_list->nCount);
659                 
660         }
661         
662         LoggerI("addr_list->nCount" << addr_list->nCount);
663
664         if (cc.size()) 
665         {
666                 LoggerI("updating cc");
667                 for (size_t i = 0; i < cc.size(); i++) 
668                 {
669                         if (nToCnt >= MAX_TO_ADDRESS_CNT) 
670                         {
671                                 LoggerE("max number of recipient exceeded");
672                                 break;
673                         }
674                         LoggerD("adding cc[" << i << "]=" << cc[i]);
675
676                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
677
678                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
679                                 MSG_RECIPIENTS_TYPE_CC) != MSG_SUCCESS) 
680                         {
681                                 LoggerE("problem with adding cc address");
682                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
683                         }
684                         
685                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
686                                 const_cast<char*>(cc[i].c_str()), cc[i].size()) != MSG_SUCCESS) 
687                         {
688                                 LoggerE("problem with adding cc address");
689                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
690                         }
691                         nToCnt ++;
692                 }
693                 addr_list->nCount = nToCnt;
694                 LoggerE("nToCnt : " << nToCnt);
695                 LoggerE("addr_list->nCount: " <<addr_list->nCount);
696         }
697
698         LoggerI("addr_list->nCount" << addr_list->nCount);
699
700         if (bcc.size()) 
701         {
702                 LoggerI("updating bcc");
703                 for (size_t i = 0; i < bcc.size(); i++) 
704                 {
705                         if (nToCnt >= MAX_TO_ADDRESS_CNT) 
706                         {
707                                 LoggerE("max number of recipient exceeded");
708                                 break;
709                         }
710                         LoggerD("adding bcc[" << i << "]=" << bcc[i]);
711
712                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
713
714                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
715                                 MSG_RECIPIENTS_TYPE_BCC) != MSG_SUCCESS) 
716                         {
717                                 LoggerE("problem with adding bcc address");
718                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
719                         }
720                         
721                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
722                                 const_cast<char*>(bcc[i].c_str()), bcc[i].size()) != MSG_SUCCESS) 
723                         {
724                                 LoggerE("problem with adding bcc address");
725                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
726                         }
727                         nToCnt ++;
728                 }
729                 addr_list->nCount = nToCnt;
730                 LoggerE("nToCnt : " << nToCnt);
731                 LoggerE("addr_list->nCount: " <<addr_list->nCount);
732         }
733
734         LoggerI("addr_list->nCount" << addr_list->nCount);              
735         setToValidity(true);
736         setCcValidity(true);
737         setBccValidity(true);
738 }
739
740 void Mms::updateSubject()
741 {
742     // check if abstraction subject value has been changed
743     if (isSubjectValid()) {
744         return;
745     }
746     LoggerI("updating platform subject: " << getSubjectRef());
747     if (MSG_SUCCESS !=
748 //        msg_set_subject(m_messageData, getSubjectRef().c_str())) {
749         msg_set_str_value(m_messageData, MSG_MESSAGE_SUBJECT_STR, const_cast<char*>(getSubjectRef().c_str()), MAX_SUBJECT_LEN)) {
750
751         LoggerE("problem with setting subject");
752         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
753     }
754     setSubjectValidity(true);
755 }
756
757 void Mms::updateBodyAndAttachments()
758 {
759                 // check if attachment or body source address value has been changed
760                 if (isAttachmentsValid() && isBodyValid()) {
761                         return;
762                 }
763         
764 //              MMS_MESSAGE_DATA_S *mmsData = NULL;
765                 msg_struct_t mms_struct = NULL;
766 //              msg_struct_t old_mms_struct = NULL;// THIS
767 //              int ret;// THIS
768
769                 LoggerI("updating platform body and attachment");
770         
771                 Try
772                 {
773                         msg_struct_t region[1];
774                         msg_struct_t page[1];
775                         msg_struct_t media[1];
776                         msg_struct_t smil_text[1];
777                         
778                         if (getMessageType() != MMS) {
779                                 LoggerE("Wrong msg type");
780                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong msg type");
781                         }
782 //                      mmsData = msg_mms_create_message();
783                         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
784 //                      old_mms_struct = msg_create_struct(MSG_STRUCT_MMS);// THIS
785
786 //                      ret = msg_get_mms_struct(m_messageData, old_mms_struct);// THIS
787 //                      LoggerE("ret " << ret);// THIS
788 //                      ret = msg_release_struct(&old_mms_struct);// THIS
789 //                      LoggerE("ret " << ret);// THIS
790
791                         if (!mms_struct) {
792                                 LoggerE("create message body failed");
793                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "create message body failed");
794                         }
795         
796                         //body
797                         if (!getBodyRef().empty()) {
798                                 FILE* f = NULL;
799                                 f = fopen(m_bodyFilePath.c_str(), "w");
800                                 if (!f) {
801                                         LoggerE("cannot create file with body" << m_bodyFilePath);
802                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
803                                                          "cannot create file with body");
804                                 }
805                                 LoggerD("body file: " << m_bodyFilePath);
806                                 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
807                          
808                                 fclose(f);
809
810                                 LoggerD("getBodyRef(): " << getBodyRef());
811
812                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_INT, ROOT_LAYOUT_WIDTH);
813                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
814                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_BGCOLOR_INT, WHITE_COLOR);
815                                 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_PERCENT_BOOL, true);
816                                 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_PERCENT_BOOL, true);
817                                 
818                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_REGION, &region[0]);
819                                 msg_set_str_value(region[0], MSG_MMS_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
820                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_INT, 0);
821                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_TOP_INT, 0);
822                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_INT, ROOT_LAYOUT_WIDTH);
823                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
824                                 msg_set_int_value(region[0], MSG_MMS_REGION_BGCOLOR_INT, WHITE_COLOR);
825                                 
826                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_PERCENT_BOOL, true);
827                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_TOP_PERCENT_BOOL, true);
828                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_PERCENT_BOOL, true);
829                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_PERCENT_BOOL, true);
830
831                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_PAGE, &page[0]);
832                                 msg_set_int_value(page[0], MSG_MMS_PAGE_PAGE_DURATION_INT, 0);
833                                 
834                                 msg_mms_add_item(page[0], MSG_STRUCT_MMS_MEDIA, &media[0]);
835                                 msg_set_int_value(media[0], MSG_MMS_MEDIA_TYPE_INT, MMS_SMIL_MEDIA_TEXT);
836                                 msg_set_str_value(media[0], MSG_MMS_MEDIA_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
837                                 int error;
838                                 error = msg_set_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, const_cast<char*>(m_bodyFilePath.c_str()),MSG_FILEPATH_LEN_MAX);
839                                 LoggerD("bodyFile Path= " << m_bodyFilePath);
840                                 LoggerD("error= " << error);
841                                 char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
842                                 memset(szFilePath, 0, sizeof(szFilePath));
843                                 msg_get_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
844                                 LoggerD("bodyFile Path= " << m_bodyFilePath);
845
846                                 msg_get_struct_handle(media[0], MSG_MMS_MEDIA_SMIL_TEXT_HND, &smil_text[0]);
847                                 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_COLOR_INT, BLACK_COLOR);
848                                 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_SIZE_INT, MMS_SMIL_FONT_SIZE_NORMAL);
849                                 msg_set_bool_value(smil_text[0], MSG_MMS_SMIL_TEXT_BOLD_BOOL, true);
850                                 
851                         }
852                         
853                         //attachments
854                         struct stat buffer;
855                         int errnum = 0;
856
857                         msg_struct_t attachment[20];                    
858                         
859                         for (size_t i = 0; i < getAttachmentsRef().size(); ++i) {
860                                 IAttachmentPtr att = getAttachment(i);
861                                 if (!att) {
862                                         continue;
863                                 }
864         
865                                 LoggerD("Add attachment=" << att->getFullPath());
866                                 //checking file
867                                 errnum = stat(att->getFullPath().c_str(), &buffer);
868                                 LoggerD("errnum=" << errnum);
869
870                                 if (errnum != 0) {
871                                         LoggerE("Opening file: " << att->getFullPath().c_str());
872                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
873                                                          "cannot open attachment file");
874                                 }
875         
876                                 //this function should return valid pointer
877
878                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_ATTACH, &attachment[i]);
879                                 msg_set_str_value(attachment[i], MSG_MMS_ATTACH_FILEPATH_STR, const_cast<char*>(att->getFullPath().c_str()), MSG_FILEPATH_LEN_MAX);
880                                 
881                                 LoggerD("Attachment added");
882         
883                                 //reset errno
884                                 errnum = 0;
885                         }
886         
887                 if (MSG_SUCCESS !=msg_set_mms_struct(m_messageData, mms_struct))
888                 {
889                         LoggerE("set message body error");
890                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "set message body error");
891                 }
892
893                 msg_release_struct(&mms_struct);
894
895                 setAttachmentsValidity(true);
896                 setBodyValidity(true);
897                 
898                 }
899                 Catch(WrtDeviceApis::Commons::PlatformException) {
900                         LoggerE("Platform error");
901                         msg_release_struct(&mms_struct);
902                         throw;
903                 }
904 }
905
906
907 void Mms::updateReadStatus()
908 {
909     if (isReadStatusValid()) {
910         return;
911     }
912
913     LoggerI("updating platform read status: " << isRead());
914 //    if (MSG_SUCCESS != msg_set_read_status(m_messageData, isRead())) {
915     if (MSG_SUCCESS != msg_set_bool_value(m_messageData, MSG_MESSAGE_READ_BOOL, isRead())) {
916         LoggerE("problem with setting subject");
917         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
918     }
919
920     setReadStatusValidity(true);
921 }
922
923 void Mms::updateIsRead()
924 {
925     LoggerD("updating m_id=" << getIdRef());
926
927     if (!m_messageData) {
928         //error if platform message not exists
929         LoggerE("message can not be updated");
930         Throw(WrtDeviceApis::Commons::PlatformException);
931     }
932         
933     // check if abstraction from m_isReadChange value has been changed
934     if (isReadChangeStatusValid()) {
935         // do not update if not changed
936         return;
937     }
938
939     Try
940     {
941         if (this->getIdRef().empty()) {
942             LoggerE("existing msgId is zero, remove msg not done");
943             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
944                      "existing msgId is zero, remove msg not done");
945         }
946
947         if (MSG_SUCCESS !=
948             msg_update_read_status(MsgGetCommonHandle(), convertId(getIdRef()), isReadChangeStatus()))
949         {
950             LoggerE("Failed to update isRead");
951             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update isRead");
952         }
953     }
954     Catch(WrtDeviceApis::Commons::PlatformException) {
955         LoggerE("platform error occurs");
956     }
957     setisReadChangeStatusValidity(true);
958
959 }
960
961 void Mms::updateMessage()
962 {
963         LoggerD("updating m_id=" << getIdRef());
964         msg_error_t err = MSG_SUCCESS;
965         msg_struct_t msg = NULL;
966         msg_struct_t sendOpt = NULL;
967         
968         if (!m_messageData) {
969                 //error if platform message not exists
970                 LoggerE("message can not be updated");
971                 Throw(WrtDeviceApis::Commons::PlatformException);
972         }
973
974         
975         Try
976         {
977                 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
978                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);                        
979                 
980                 err = msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg, sendOpt);
981                 
982                 if (err != MSG_SUCCESS)
983                 {
984                         LoggerE("Get Message Failed!");
985                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
986                 }
987         
988                 if (this->getIdRef().empty()) {
989                         LoggerE("existing msgId is zero, update msg not done");
990                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
991                                         "existing msgId is zero, update msg not done");
992                 }
993                 
994                 update(TRUE);
995                 
996                 if (MSG_SUCCESS != msg_update_message(MsgGetCommonHandle(), m_messageData, sendOpt))
997                 {
998                         LoggerE("Failed to update message");
999                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
1000                 }
1001                 msg_release_struct(&msg);
1002                 msg_release_struct(&sendOpt);
1003         }
1004         Catch(WrtDeviceApis::Commons::PlatformException) {
1005                 msg_release_struct(&msg);
1006                 msg_release_struct(&sendOpt);
1007                 LoggerE("platform error occurs");
1008         }
1009 }
1010
1011
1012 void Mms::createSendMessage()
1013 {
1014     LoggerD("convert m_id= " << convertId(getIdRef())); 
1015
1016     //prepare for add sms to draft folder
1017     if (!m_messageData) {
1018         //error if platform message not exists
1019         LoggerE("message can not be updated");
1020         Throw(WrtDeviceApis::Commons::PlatformException);
1021     }
1022
1023     //update all sms data
1024     if (getCurrentFolder() == DRAFTBOX) {
1025                 LoggerD("update all mms data");
1026         updateSubject();
1027         updateBodyAndAttachments();
1028         updateRecipientList();
1029         updatePriority();
1030     }
1031
1032     Try
1033     {
1034         int msgId = 0;
1035 //        MSG_SENDINGOPT_S option = { false, false, false };
1036 //        option.option.smsSendOpt.bReplyPath = true;
1037         // trying to get message from platform
1038
1039 //        msg_struct_t sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1040 //        msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
1041
1042
1043 //        const msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(DRAFTBOX);
1044
1045 //        msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1046 //        msg_set_int_value(m_messageData, MSG_MESSAGE_FOLDER_ID_INT, 4);
1047 //        msg_set_int_value(m_messageData, MSG_MESSAGE_NETWORK_STATUS_INT, MSG_NETWORK_NOT_SEND);
1048
1049         setId(convertId(msgId));
1050         setFolderType(DRAFTBOX);
1051         setMessageStatus(MESSAGE_STATUS_DRAFT);
1052     }
1053     Catch(WrtDeviceApis::Commons::PlatformException) {
1054         LoggerE("remove message error");
1055         if (m_messageData) {
1056             //releasing platform message structure
1057 //            msg_release_message(&m_messageData);
1058             msg_release_struct(&m_messageData);         
1059         }
1060         throw;
1061     }
1062
1063 }       
1064
1065 void Mms::addMessageToDraft()
1066 {
1067         LoggerD("convert m_id= " << convertId(getIdRef()));
1068
1069         msg_struct_t sendOpt = NULL;
1070
1071         //prepare for add sms to draft folder
1072         if (!m_messageData) {
1073                 //error if platform message not exists
1074                 LoggerE("message can not be updated");
1075                 Throw(WrtDeviceApis::Commons::PlatformException);
1076         }
1077
1078         //update all mms data
1079         if (getCurrentFolder() == DRAFTBOX) {
1080                 updateSubject();
1081                 updateBodyAndAttachments();
1082                 updateRecipientList();
1083                 updatePriority();
1084         }
1085
1086         Try
1087         {
1088                 int msgId = -1;
1089                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1090
1091                 // trying to add message
1092                 LoggerD("add message");
1093                 int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
1094                 if (ret < MSG_SUCCESS) {
1095                         LoggerE("msg_add_message failed, error code=" << ret);
1096                         Throw(WrtDeviceApis::Commons::PlatformException);
1097                 }
1098                 else
1099                 {
1100                         msgId = ret;
1101                 }
1102
1103                 LoggerD("Message ID : " << msgId);
1104                 if (msgId < 0)
1105                 {
1106                         LoggerD("Message ID is invailded ");
1107                         Throw(WrtDeviceApis::Commons::PlatformException);
1108                 }
1109
1110                 msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1111
1112                 setId(convertId(msgId));
1113                 readAllData();
1114
1115                 msg_release_struct(&sendOpt);
1116
1117         }
1118         Catch(WrtDeviceApis::Commons::PlatformException) {
1119                 LoggerE("add draft message error");
1120                 msg_release_struct(&sendOpt);
1121                 throw;
1122         }
1123
1124 }               
1125
1126 void Mms::readAllData()
1127 {
1128         LoggerD("entered");
1129
1130         if (getIdRef().empty() || (EMPTY_ID == getIdRef())) 
1131         {
1132                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
1133         }
1134
1135         msg_release_struct(&m_messageData);
1136         m_messageData = NULL;
1137         msg_struct_t sendOpt = NULL;
1138
1139         Try {
1140
1141                 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
1142                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1143
1144                 // trying to get message from platform
1145                 if (MSG_SUCCESS != msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), m_messageData, sendOpt)) 
1146                 {
1147                         LoggerE("get message error");
1148                         Throw(WrtDeviceApis::Commons::PlatformException);
1149                 }
1150                 LoggerD("message found with msgId=" << getIdRef());
1151
1152                 // read all mms dat to abstraction layer
1153                 readConversationId(m_messageData);
1154 //              readRecipientList(m_messageData);
1155                 readPriority(m_messageData);
1156                 readSubject(m_messageData);
1157                 readBodyAndAttachments(m_messageData);
1158                 readFolder(m_messageData);
1159                 readDateTime(m_messageData);
1160                 readReadStatus(m_messageData);
1161                 readMessageStatus(m_messageData);
1162
1163                 msg_release_struct(&sendOpt);
1164         }
1165         Catch(WrtDeviceApis::Commons::PlatformException) {
1166                 LoggerE("exception");
1167                 // nothing to do
1168                 msg_release_struct(&sendOpt);
1169         }
1170
1171 }
1172
1173 void Mms::moveToFolder(const FolderType newFolder)
1174 {
1175 /*
1176     update();
1177
1178     Try
1179     {
1180         msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1181                 newFolder);
1182
1183         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1184                                    platfromFolderId) != MSG_SUCCESS) {
1185             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error during movinf message");
1186         }
1187     }
1188
1189     Catch(WrtDeviceApis::Commons::PlatformException) {
1190         LoggerE("remove message error");
1191         throw;
1192     }
1193 */
1194 }
1195
1196 void Mms::moveToFolder(const string& newFolder)
1197 {
1198 /*
1199     update();
1200
1201     Try
1202     {
1203         msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1204                 newFolder);
1205
1206         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1207                                    platfromFolderId) != MSG_SUCCESS) {
1208             LoggerE("msg_move_msg_to_folder error");
1209             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_move_msg_to_folder error");
1210         }
1211     }
1212
1213     Catch(WrtDeviceApis::Commons::PlatformException) {
1214         LoggerE("remove message error");
1215         throw;
1216     }
1217     */
1218 }
1219
1220 void Mms::copyToFolder(const FolderType newFolder)
1221 {
1222 /*
1223     update();
1224
1225     msg_message_t msg = msg_new_message();
1226
1227     Try
1228     {
1229         MSG_SENDINGOPT_S option = { false, false, false };
1230         option.option.smsSendOpt.bReplyPath = true;
1231         if (MSG_SUCCESS ==
1232             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1233                             &option)) {
1234             const msg_folder_id_t platfromFolderId =
1235                 Messaging::convertFolderToPlatform(newFolder);
1236             msg_set_message_id(msg, 0);
1237             msg_set_folder_id(msg, platfromFolderId);
1238
1239             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1240             if (error != MSG_SUCCESS) {
1241                 LoggerE("msg_add_message failed, error code=" << error);
1242                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1243             }
1244         }
1245         if (msg) {
1246             msg_release_message(&msg);
1247         }
1248     }
1249
1250     Catch(WrtDeviceApis::Commons::PlatformException) {
1251         LoggerE("remove message error");
1252         if (msg) {
1253             msg_release_message(&msg);
1254         }
1255         throw;
1256     }
1257 */
1258 }
1259
1260 void Mms::copyToFolder(const string& newFolder)
1261 {
1262 /*
1263     update();
1264
1265     msg_message_t msg = msg_new_message();
1266
1267     Try
1268     {
1269         MSG_SENDINGOPT_S option = { false, false, false };
1270         option.option.smsSendOpt.bReplyPath = true;
1271         if (MSG_SUCCESS ==
1272             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1273                             &option)) {
1274             const msg_folder_id_t platfromFolderId =
1275                 Messaging::convertFolderToPlatform(newFolder);
1276             msg_set_message_id(msg, 0);
1277             msg_set_folder_id(msg, platfromFolderId);
1278
1279             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1280             if (error != MSG_SUCCESS) {
1281                 LoggerE("msg_add_message failed, error code=" << error);
1282                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1283             }
1284         }
1285         if (msg) {
1286             msg_release_message(&msg);
1287         }
1288     }
1289
1290     Catch(WrtDeviceApis::Commons::PlatformException) {
1291         LoggerE("remove message error");
1292         if (msg) {
1293             msg_release_message(&msg);
1294         }
1295         throw;
1296     }
1297 */
1298 }
1299
1300 void Mms::remove()
1301 {
1302     LoggerD("delete m_id=" << getIdRef());
1303
1304     Try
1305     {
1306         if (this->getIdRef().empty()) {
1307             LoggerE("existing msgId is zero, remove msg not done");
1308             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1309                      "existing msgId is zero, remove msg not done");
1310         }
1311
1312         if (MSG_SUCCESS !=
1313             msg_delete_message(MsgGetCommonHandle(),
1314                                convertId(getIdRef()))) {
1315             LoggerE("Failed to delete Message");
1316             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to delete Message");
1317         }
1318     }
1319
1320     Catch(WrtDeviceApis::Commons::PlatformException) {
1321         LoggerE("platform error occurs");
1322     }
1323 }
1324
1325 int Mms::send()
1326 {
1327         LoggerD("sending message, id=" << getIdRef());
1328         Try
1329         {
1330                 //prepare for sending sms/mms
1331                 update();
1332
1333                 LoggerD("Start Sending Message...");
1334
1335                 LoggerD("trying to send mms");
1336                 msg_error_t err = CallbackMgrSingleton::Instance().registerAndSend(msg_mms_send_message, m_messageData, this);
1337
1338                 if (err != MSG_SUCCESS) 
1339                 {
1340                         LoggerE("Sending Message (submit request) failed!!! err=" << err);
1341                         setMessageStatus(MESSAGE_STATUS_FAILED);
1342                         goto ERROR;
1343                 }
1344
1345                 setMessageStatus(MESSAGE_STATUS_SENDING);
1346
1347                 LoggerD("Sending Message request is submitted!");
1348         }
1349         Catch(WrtDeviceApis::Commons::PlatformException) {
1350                 LoggerE("message is not send, manually run callback");
1351                 goto ERROR;
1352         }
1353
1354         return 0;
1355
1356         ERROR:
1357         ReqReceiverMessage *requestReceiver = getRequestReceiver();
1358         if (requestReceiver) {
1359                 LoggerE("send Error");
1360                 EventMessagingServicePtr event = getMessagingServiceEvent();
1361                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1362                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService>::ManualAnswer(event);
1363         }
1364         return 0;
1365 }
1366
1367 void Mms::sendingCallback(msg_struct_t sent_status)
1368 {
1369 //      LoggerI("sendingCallback callback received. Req id = " <<
1370 //      sent_status->reqId << " Status = " << (int)sent_status->status);
1371 //      msg_struct_t request_data = NULL;
1372 //      msg_get_struct_handle(sent_status, MSG_STRUCT_REQUEST_INFO, (void **)request_data);
1373 //      msg_get_int_value(request_data, MSG_REQUEST_REQUESTID_INT, &tempInt);
1374         int status = MSG_NETWORK_SEND_FAIL;
1375         msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
1376         
1377         LoggerD("status : " << status);
1378         
1379         if (MSG_NETWORK_SEND_SUCCESS == status) {
1380                 LoggerD("sending ok");
1381                 setSendingStatusOk();
1382                 setMessageStatus(MESSAGE_STATUS_SENT);
1383         } else {
1384                 LoggerE("sending failed: " << static_cast<unsigned int>(status));
1385                 setSendingStatusFailed();
1386                 setMessageStatus(MESSAGE_STATUS_FAILED);
1387         }
1388 }
1389
1390 void Mms::sendCancel(int handle)
1391 {
1392     LoggerD("sending message, id=" << getIdRef());
1393     //#warning "TODO"
1394     LoggerD("handle =" << handle);
1395 }
1396
1397 void Mms::setSendingStatusOk()
1398 {
1399     //success callback should be executed here
1400         ReqReceiverMessage *requestReceiver = getRequestReceiver();
1401         msg_struct_list_s *addr_list = NULL;
1402         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1403         int nToCnt;
1404         
1405         if (requestReceiver) {
1406                 LoggerI("calling JS success callback");
1407                 EventMessagingServicePtr event = getMessagingServiceEvent();
1408                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1409
1410                 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1411
1412                 nToCnt = addr_list->nCount;
1413
1414                 for ( int index=0; index < nToCnt; index++)
1415                 {
1416                         msg_get_str_value(addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1417                         event->m_successRecipients.push_back(strNumber);
1418                 }
1419           
1420                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1421         }
1422 }
1423
1424 void Mms::setSendingStatusFailed()
1425 {       
1426     //error callback should be executed here
1427         EventOnSendingFailedEmitterPtr emitter = getEmitter();
1428         msg_struct_list_s *addr_list = NULL;
1429         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1430         int nToCnt;
1431         
1432         if (emitter) 
1433         {
1434                 EventOnSendingFailedPtr event(new EventOnSendingFailed);
1435                 event->setError(EventOnSendingFailed::UNKNOWN); // TODO error codes
1436                 emitter->emit(event);
1437         } 
1438         else 
1439         {
1440                 ReqReceiverMessage *requestReceiver = getRequestReceiver();
1441                 EventMessagingServicePtr event = getMessagingServiceEvent();
1442                 if (requestReceiver) 
1443                 {
1444                         LoggerE("calling JS error callback");
1445                         event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1446                         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1447                         nToCnt = addr_list->nCount;
1448
1449                         LoggerD("nToCnt : " << nToCnt);
1450
1451                         for ( int index=0; index < nToCnt; index++)
1452                         {
1453                                 msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1454                                 event->m_failRecipients.push_back(strNumber);
1455                         }
1456
1457                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1458                 }
1459         }
1460 }
1461
1462 FolderType Mms::toFolder(const std::string &folder)
1463 {
1464     if (folder == "INBOX") {
1465         return INBOX;
1466     } else if (folder == "OUTBOX") {
1467         return OUTBOX;
1468     } else if (folder == "SENTBOX") {
1469         return SENTBOX;
1470     } else if (folder == "DRAFTBOX") {
1471         return DRAFTBOX;
1472     }
1473     ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Invalid folder");
1474 }
1475 }
1476 }