Update change log and spec for wrt-plugins-tizen_0.2.73
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Messaging / Mms.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18 /**
19  *
20  *
21  * @file       Mms.cpp
22  * @author     Pawel Misiak (p.misiak@samsung.com)
23  * @version    0.1
24  * @brief
25  */
26 #include "Mms.h"
27
28 #include <vector>
29 #include <dpl/log/log.h>
30 #include <Commons/Exception.h>
31 #include <API/Messaging/ReqReceiverMessage.h>
32 #include "Messaging.h"
33 #include "CallbackMgr.h"
34 #include "MsgServiceHandleMgr.h"
35
36 extern "C" {
37 #include <msg.h>
38 #include <msg_storage.h>
39 }
40
41
42 namespace {
43 const char* TEXT_AREA = "Text";
44 const char* TEXT_FILE_EXTENSION = ".txt";
45 const int WHITE_COLOR = 0xffffff;
46 const int BLACK_COLOR = 0x000000;
47 const int ROOT_LAYOUT_WIDTH = 100;
48 const int ROOT_LAYOUT_HEIGHT = 100;
49 const char* EMPTY_ID = "0";
50 }
51
52 using namespace std;
53 using namespace TizenApis::Api::Messaging;
54
55 namespace TizenApis {
56 namespace Platform {
57 namespace Messaging {
58
59 Mms::Mms(const string& id) :
60     IMessage(MMS, id),
61     m_bodyFilePath(string(tmpnam(NULL)) + TEXT_FILE_EXTENSION),
62     m_messageData(NULL)
63 {
64     LogDebug("enter");
65     LogDebug("m_id=" << getIdRef());
66     LogDebug("m_msgType=" << getMessageType());
67     if (getIdRef().empty()) {
68         createNewMessage();
69     } else {
70         readExistingMessage();
71     }
72 }
73
74 Mms::Mms(const string& id, const msg_struct_t msg_data) :
75         IMessage(MMS, id),
76         m_bodyFilePath(string(tmpnam(NULL)) + TEXT_FILE_EXTENSION),
77         m_messageData(msg_data)
78 {
79         LogDebug("enter");
80         LogDebug("m_id=" << getIdRef());
81         LogDebug("m_msgType=" << getMessageType());
82         readRecipientList(m_messageData);
83         readPriority(m_messageData);
84         readSubject(m_messageData);
85         readBodyAndAttachments(m_messageData);
86         readFolder(m_messageData);
87         readDateTime(m_messageData);
88         readReadStatus(m_messageData);
89         setMessageStatus(Api::Messaging::MESSAGE_STATUS_LOADED);
90
91 }
92
93 Mms::~Mms()
94 {
95     LogDebug("enter");
96
97     if (m_messageData) {
98         // release platform message structure
99 //        msg_release_message(&m_messageData);
100         msg_release_struct(&m_messageData);
101     }
102
103     //remove tmp file
104     //trying to remove file, return value is skipped
105     if (!m_bodyFilePath.empty()) {
106         LogDebug("remove tmp file=" << m_bodyFilePath);
107         (void) ::remove(m_bodyFilePath.c_str());
108     }
109 }
110
111 void Mms::createNewMessage()
112 {
113     if (m_messageData) {
114         msg_release_struct(&m_messageData);
115         m_messageData = NULL;
116     }
117
118     char strSubject[MAX_SUBJECT_LEN] = {0,};
119
120     m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
121     msg_struct_t sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
122     msg_struct_t option = NULL;
123
124     // initialize platform message structure
125     msg_set_int_value(m_messageData, MSG_MESSAGE_TYPE_INT, MSG_TYPE_MMS);
126     msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
127     msg_set_str_value(m_messageData, MSG_MESSAGE_SUBJECT_STR, strSubject, MAX_SUBJECT_LEN);
128
129     setMmsType(MULTIPART_MIXED);
130     setReadStatus(false);
131     setMessageStatus(Api::Messaging::MESSAGE_STATUS_CREATED);
132
133     LogDebug("Message created successfully, Id: " << getIdRef());
134 }
135
136 void Mms::readExistingMessage()
137 {
138         LogDebug("entered");
139
140         if (getIdRef().empty() || (EMPTY_ID == getIdRef())) 
141         {
142                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Empty id.");
143         }
144
145         msg_release_struct(&m_messageData);
146         m_messageData = NULL;
147         msg_struct_t sendOpt = NULL;
148
149         Try {
150
151                 m_messageData = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
152                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
153
154                 // trying to get message from platform
155                 if (MSG_SUCCESS != msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), m_messageData, sendOpt)) 
156                 {
157                         LogError("get message error");
158                         Throw(WrtDeviceApis::Commons::PlatformException);
159                 }
160                 LogDebug("message found with msgId=" << getIdRef());
161
162                 // read all mms dat to abstraction layer
163                 readRecipientList(m_messageData);
164                 readPriority(m_messageData);
165                 readSubject(m_messageData);
166                 readBodyAndAttachments(m_messageData);
167                 readFolder(m_messageData);
168                 readDateTime(m_messageData);
169                 readReadStatus(m_messageData);
170                 setMessageStatus(Api::Messaging::MESSAGE_STATUS_LOADED);
171
172                 msg_release_struct(&sendOpt);
173         }
174         Catch(WrtDeviceApis::Commons::PlatformException) {
175                 LogError("exception");
176                 // nothing to do
177                 msg_release_struct(&sendOpt);
178         }
179 }
180
181 void Mms::readDateTime(msg_struct_t& messageData)
182 {
183 //  tm* time = localtime(msg_get_time(messageData));
184 //    tm dateT = getDateTime();
185     int tempInt = 0;
186     msg_get_int_value(messageData, MSG_MESSAGE_DISPLAY_TIME_INT, &tempInt);
187
188     LogError("readDateTime : " <<tempInt);
189
190     tm* time = localtime((time_t*)&tempInt);
191     if (!time) {
192         LogError("localtime failed");
193         Throw(WrtDeviceApis::Commons::PlatformException);
194     }
195     setDateTime(*time);
196 }
197
198 void Mms::readReadStatus(msg_struct_t& messageData)
199 {
200     bool tempBool = 0;
201     msg_get_bool_value(messageData, MSG_MESSAGE_READ_BOOL, &tempBool);
202     setReadStatus(tempBool);
203 }
204
205 void Mms::readFolder(msg_struct_t& messageData)
206 {
207     int tempValue = 0;
208     msg_get_int_value(messageData, MSG_MESSAGE_FOLDER_ID_INT, &tempValue);
209
210     switch (tempValue) {
211     case MSG_INBOX_ID:
212         setFolderType(Api::Messaging::INBOX);
213         break;
214     case MSG_OUTBOX_ID:
215         setFolderType(Api::Messaging::OUTBOX);
216         break;
217     case MSG_SENTBOX_ID:
218         setFolderType(Api::Messaging::SENTBOX);
219         break;
220     case MSG_DRAFT_ID:
221         setFolderType(Api::Messaging::DRAFTBOX);
222         break;
223     default:
224         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Unsupported folder id.");
225     }
226 }
227
228 void Mms::readPriority(msg_struct_t& messageData)
229 {
230 //    switch (msg_get_priority_info(messageData)) {
231     int tempInt = 0;
232     msg_get_int_value(messageData, MSG_MESSAGE_PRIORITY_INT, &tempInt);
233     switch (tempInt) {
234     case MSG_MESSAGE_PRIORITY_LOW:
235         setPriority(MessagePriority::LOW);
236         break;
237     case MSG_MESSAGE_PRIORITY_NORMAL:
238         setPriority(MessagePriority::NORMAL);
239         break;
240     case MSG_MESSAGE_PRIORITY_HIGH:
241         setPriority(MessagePriority::HIGH);
242         break;
243     default:
244         LogError("Wrong priority type");
245         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong priority type");
246     }
247 }
248
249 void Mms::readRecipientList(msg_struct_t& messageData)
250 {
251         std::string phoneNumber;
252 //      int recipientCount = msg_get_address_count(messageData);
253 //      LogDebug("Recipient count " << recipientCount);
254
255         msg_struct_list_s *addr_list;
256         msg_get_list_handle(messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
257
258         int tempInt = 0;
259         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
260
261         int recipientCount = addr_list->nCount;
262         LogDebug("Recipient count " << recipientCount);
263
264         for (int i = 0; i < recipientCount; ++i) 
265         {
266 //        int type = msg_get_direction_info(messageData);
267                 msg_get_int_value(messageData, MSG_MESSAGE_DIRECTION_INT, &tempInt);
268                 int type = tempInt;
269                 LogDebug("Direction: " << type);
270
271                 if (MSG_DIRECTION_TYPE_MT == type) 
272                 {
273                         msg_struct_t addr_info = addr_list->msg_struct_info[i];
274                         memset(strNumber, 0x00, sizeof(MAX_ADDRESS_VAL_LEN));
275                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
276                         phoneNumber = strNumber;
277                 
278                         if (validatePhoneNumber(phoneNumber)) 
279                         {
280                                 setSourceAddress(phoneNumber);
281                                 setFrom(phoneNumber);
282                         }
283                         LogDebug("MT number: " << phoneNumber);
284                 } 
285                 else if (MSG_DIRECTION_TYPE_MO == type) 
286                 {
287                         msg_struct_t addr_info = addr_list->msg_struct_info[i];
288                         memset(strNumber, 0x00, sizeof(MAX_ADDRESS_VAL_LEN));
289                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
290                         msg_get_int_value(addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
291                         LogDebug("MO number: " << strNumber);
292                         LogDebug("Type : " << tempInt);
293                         switch (tempInt) 
294                         {
295                                 case MSG_RECIPIENTS_TYPE_TO:
296                                         appendToRecipients(strNumber);
297                                         break;
298                                 case MSG_RECIPIENTS_TYPE_CC:
299                                         appendCcRecipients(strNumber);
300                                         break;
301                                 case MSG_RECIPIENTS_TYPE_BCC:
302                                         appendBccRecipients(strNumber);
303                                         break;
304                                 default:
305                                 LogError("Wrong type of recipient");
306                         }
307                 } 
308                 else 
309                 {
310                         LogError("Wrong type of recipient");
311                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong type of recipient");
312                 }
313         }
314 }
315
316 void Mms::readSubject(msg_struct_t& messageData)
317 {
318     char strTemp[MAX_SUBJECT_LEN] = {0};
319     msg_get_str_value(messageData, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
320 //    setSubject(msg_get_subject(messageData));
321     LogError("readSubject : " << strTemp);
322     setSubject(strTemp);
323 }
324
325 void Mms::readBodyAndAttachments(msg_struct_t& messageData)
326 {
327
328         int pageLen, mediaLen, attachLen, tempInt;
329
330         msg_struct_t mms_struct = NULL;
331
332         msg_list_handle_t page_list = NULL;
333         msg_list_handle_t media_list = NULL;
334         msg_list_handle_t attach_list = NULL;
335         
336         msg_struct_t page = NULL;
337         msg_struct_t media = NULL;
338         msg_struct_t attach = NULL;
339
340         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
341         msg_get_mms_struct(messageData, mms_struct );
342
343         char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
344         char szFileName[MSG_FILENAME_LEN_MAX] = {0,};
345
346         msg_get_list_handle(mms_struct, MSG_MMS_PAGE_LIST_HND, (void **)&page_list);
347         pageLen = msg_list_length(page_list);
348
349         if (pageLen > 0) {
350                 setMmsType(MULTIPART_RELATED);
351         } else {
352                 setMmsType(MULTIPART_MIXED);
353         }
354
355         LogDebug("page count: " << pageLen);
356
357         for (int p = 0; p < pageLen; ++p) 
358         {
359                 LogDebug("page " << p);
360                 page = (msg_struct_t)msg_list_nth_data(page_list, p);
361                 
362                 if (NULL  == page) {
363                         LogError("returned page is null, continue");
364                         continue;
365                 }
366                 
367                 msg_get_list_handle(page, MSG_MMS_PAGE_MEDIA_LIST_HND, (void **)&media_list);
368                 mediaLen = msg_list_length(media_list);
369
370                 for (int m = 0; m < mediaLen; ++m) 
371                 {
372                         LogDebug("media file " << m);
373                         media = (msg_struct_t)msg_list_nth_data(media_list, m);
374                         if (NULL == media) 
375                         {
376                                 LogError("returned media is null, continue");
377                                 continue;
378                         }
379
380                         msg_get_int_value(media, MSG_MMS_MEDIA_TYPE_INT, &tempInt);
381
382                         memset(szFilePath, 0, sizeof(szFilePath));
383                         msg_get_str_value(media, MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
384                         
385                         if ((0 == p) && (MMS_SMIL_MEDIA_TEXT == tempInt)) 
386                         {
387                                 //text value on first page goes to body attribute
388                                 LogDebug("setting body from " << szFilePath);
389                                 
390                                 FILE* f = NULL;
391                                 f = fopen(szFilePath, "r");
392                                 if (!f) 
393                                 {
394                                         LogError("cannot read file with body" << szFilePath);
395                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
396                                         "Cannot read file with body");
397                                 }
398                                 fseek(f, 0, SEEK_END);
399                                 long int size = ftell(f);
400                                 fseek(f, 0, SEEK_SET);
401                                 char* data = new (nothrow) char[size + 1];
402                                 if (data) {
403                                         memset(data, 0, size + 1);
404                                         fread(data, 1, size, f);
405                                         setBody(data);
406                                         delete[] data;
407                                 } else {
408                                         LogError("body is not set");
409                                 }
410                                 LogDebug("message body: '" << getBody() << "'");
411                                 fclose(f);
412                         } 
413                         else 
414                         {
415                                 LogDebug("adding attachment " << szFilePath);
416
417                                 IAttachmentPtr attachment = appendAttachment(szFilePath, false);
418                                 //attachment->setMessage(this); //set IMessagePtr
419
420                                 memset(szFileName, 0, sizeof(szFileName));
421                                 msg_get_str_value(media, MSG_MMS_MEDIA_FILENAME_STR, szFileName, sizeof(szFileName));
422                                 
423                                 if (NULL != szFileName &&       strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0) 
424                                 {
425                                         LogDebug("renaming to " << szFileName);
426                                         attachment->rename(szFileName);
427                                 }
428                                 else if (MMS_SMIL_MEDIA_TEXT == tempInt) 
429                                 {
430                                         std::stringstream newName;
431                                         newName << "body_page_" << p + 1 << ".txt";
432                                         LogDebug("renaming to " << newName.str());
433                                         attachment->rename(newName.str());
434                                 }
435                         }
436                 }
437         }
438
439         msg_get_list_handle(mms_struct, MSG_MMS_ATTACH_LIST_HND, (void **)&attach_list);
440         attachLen= msg_list_length(attach_list);
441
442         LogDebug("attachment count: " << attachLen);
443         for (int a = 0; a < attachLen; ++a) 
444         {
445                 attach = (msg_struct_t)msg_list_nth_data(attach_list, a);
446                 if (NULL == attach) 
447                 {
448                         LogError("attachment is null, continue");
449                         continue;
450                 }
451
452                 memset(szFilePath, 0, sizeof(szFilePath));
453                 memset(szFileName, 0, sizeof(szFileName));
454
455                 msg_get_str_value(attach, MSG_MMS_ATTACH_FILEPATH_STR, szFilePath, sizeof(szFilePath));
456                 msg_get_str_value(attach, MSG_MMS_ATTACH_FILENAME_STR, szFileName, sizeof(szFileName));
457                 
458                 IAttachmentPtr att = appendAttachment(szFilePath, false);
459                 if (NULL != szFileName &&
460                         strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0) {
461                         LogDebug("renaming to " << szFileName);
462                         att->rename(szFileName);
463                 }
464         }
465 }
466
467 bool Mms::hasAttachment()
468 {
469         std::size_t attachmentSize = getAttachmentsCount();
470         if ( attachmentSize > 0)
471                 return true;
472         else
473                 return false;
474 }
475
476 void Mms::update(bool draftsOnly)
477 {
478     LogDebug("updating m_id=" << getIdRef());
479
480     if (!m_messageData) {
481         //error if platform message not exists
482         LogError("message can not be updated");
483         Throw(WrtDeviceApis::Commons::PlatformException);
484     }
485
486     //update mms data from abstraction
487     if (!draftsOnly || getCurrentFolder() == Api::Messaging::DRAFTBOX) {
488         updateSubject();
489         updateBodyAndAttachments();
490         updateRecipientList();
491         updatePriority();
492     }
493     updateReadStatus();
494 }
495
496 void Mms::updatePriority()
497 {
498     if (isPriorityValid()) {
499         return;
500     }
501     int priority = -1;
502     LogInfo("MMS updating priority");
503
504     //set priority in platform
505     switch (getPriority()) {
506     case MessagePriority::LOW:
507         priority = MSG_MESSAGE_PRIORITY_LOW;
508         break;
509     case MessagePriority::NORMAL:
510         priority = MSG_MESSAGE_PRIORITY_NORMAL;
511         break;
512     case MessagePriority::HIGH:
513         priority = MSG_MESSAGE_PRIORITY_HIGH;
514         break;
515     default:
516         LogError("Wrong priority");
517         Throw(WrtDeviceApis::Commons::PlatformException);
518     }
519     if (MSG_SUCCESS !=
520 //        msg_set_priority_info(m_messageData, MSG_MESSAGE_PRIORITY_LOW)) {
521         msg_set_int_value(m_messageData, MSG_MESSAGE_PRIORITY_INT, priority)){
522         
523         LogError("Error during setting priority");
524         Throw(WrtDeviceApis::Commons::PlatformException);
525     }
526 }
527
528 void Mms::updateRecipientList()
529 {
530     // check if abstraction recipient value has been changed
531     if (getToRecipients().isValid() &&
532         getBccRecipients().isValid() &&
533         getCcRecipients().isValid()) {
534         return;
535     }
536
537     LogInfo("MMS updating platform recipients");
538
539     // reset addresses in platform structure
540 //    msg_reset_address(m_messageData);
541         msg_struct_list_s *addr_list = NULL;
542         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
543         int nToCnt = 0;
544         nToCnt = addr_list->nCount;
545
546         LogInfo("addr_list->nCount" << addr_list->nCount);
547         
548         if (nToCnt > 0)         
549         {               
550                 for (int i = 0; i < nToCnt; i++) {      
551                         msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, "", 0);
552                         msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_DISPLAYNAME_STR, "", 0);
553                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
554                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, MSG_RECIPIENTS_TYPE_UNKNOWN);
555                         msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_CONTACT_ID_INT, 0);
556                 }
557                 nToCnt = 0;
558                 addr_list->nCount = nToCnt;
559         }               
560
561         LogInfo("addr_list->nCount" << addr_list->nCount);
562
563         vector<string> to = getToRecipients().getRecipients();
564         vector<string> cc = getCcRecipients().getRecipients();
565         vector<string> bcc = getBccRecipients().getRecipients();
566
567         // update addresses in platform structure
568         if (to.size()) 
569         {
570                 LogInfo("updating to");
571                 for (size_t i = 0; i < to.size(); i++) 
572                 {
573                         if (i >= MAX_TO_ADDRESS_CNT) 
574                         {
575                                 LogError("max number of recipient exceeded");
576                                 break;
577                         }
578                         LogDebug("adding to[" << i << "]=" << to[i]);
579
580                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
581                                 MSG_RECIPIENTS_TYPE_TO) != MSG_SUCCESS) 
582                         {
583                                 LogError("problem with adding to address");
584                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
585                         }
586                         
587                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
588                                 const_cast<char*>(to[i].c_str()), to[i].size()) != MSG_SUCCESS) 
589                         {
590                                 LogError("problem with adding to address");
591                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
592                         }
593                         nToCnt ++;
594                 }
595                 addr_list->nCount = nToCnt;
596                 LogError("nToCnt : " << nToCnt);
597                 LogError("addr_list->nCount: " <<addr_list->nCount);
598                 
599         }
600         
601         LogInfo("addr_list->nCount" << addr_list->nCount);
602
603         if (cc.size()) 
604         {
605                 LogInfo("updating cc");
606                 for (size_t i = 0; i < cc.size(); i++) 
607                 {
608                         if (nToCnt >= MAX_TO_ADDRESS_CNT) 
609                         {
610                                 LogError("max number of recipient exceeded");
611                                 break;
612                         }
613                         LogDebug("adding cc[" << i << "]=" << cc[i]);
614
615                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
616                                 MSG_RECIPIENTS_TYPE_CC) != MSG_SUCCESS) 
617                         {
618                                 LogError("problem with adding cc address");
619                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
620                         }
621                         
622                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
623                                 const_cast<char*>(cc[i].c_str()), cc[i].size()) != MSG_SUCCESS) 
624                         {
625                                 LogError("problem with adding cc address");
626                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
627                         }
628                         nToCnt ++;
629                 }
630                 addr_list->nCount = nToCnt;
631                 LogError("nToCnt : " << nToCnt);
632                 LogError("addr_list->nCount: " <<addr_list->nCount);
633         }
634
635         LogInfo("addr_list->nCount" << addr_list->nCount);
636
637         if (bcc.size()) 
638         {
639                 LogInfo("updating bcc");
640                 for (size_t i = 0; i < bcc.size(); i++) 
641                 {
642                         if (nToCnt >= MAX_TO_ADDRESS_CNT) 
643                         {
644                                 LogError("max number of recipient exceeded");
645                                 break;
646                         }
647                         LogDebug("adding bcc[" << i << "]=" << bcc[i]);
648
649                         if (msg_set_int_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
650                                 MSG_RECIPIENTS_TYPE_BCC) != MSG_SUCCESS) 
651                         {
652                                 LogError("problem with adding bcc address");
653                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
654                         }
655                         
656                         if(msg_set_str_value(addr_list->msg_struct_info[i], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
657                                 const_cast<char*>(bcc[i].c_str()), bcc[i].size()) != MSG_SUCCESS) 
658                         {
659                                 LogError("problem with adding bcc address");
660                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
661                         }
662                         nToCnt ++;
663                 }
664                 addr_list->nCount = nToCnt;
665                 LogError("nToCnt : " << nToCnt);
666                 LogError("addr_list->nCount: " <<addr_list->nCount);
667         }
668
669         LogInfo("addr_list->nCount" << addr_list->nCount);              
670         setToValidity(true);
671         setCcValidity(true);
672         setBccValidity(true);
673 }
674
675 void Mms::updateSubject()
676 {
677     // check if abstraction subject value has been changed
678     if (isSubjectValid()) {
679         return;
680     }
681     LogInfo("updating platform subject: " << getSubjectRef());
682     if (MSG_SUCCESS !=
683 //        msg_set_subject(m_messageData, getSubjectRef().c_str())) {
684         msg_set_str_value(m_messageData, MSG_MESSAGE_SUBJECT_STR, const_cast<char*>(getSubjectRef().c_str()), MAX_SUBJECT_LEN)) {
685
686         LogError("problem with setting subject");
687         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
688     }
689     setSubjectValidity(true);
690 }
691
692 void Mms::updateBodyAndAttachments()
693 {
694                 // check if attachment or body source address value has been changed
695                 if (isAttachmentsValid() && isBodyValid()) {
696                         return;
697                 }
698         
699 //              MMS_MESSAGE_DATA_S *mmsData = NULL;
700                 msg_struct_t mms_struct = NULL;
701 //              msg_struct_t old_mms_struct = NULL;// THIS
702 //              int ret;// THIS
703
704                 LogInfo("updating platform body and attachment");
705         
706                 Try
707                 {
708                         msg_struct_t region[1];
709                         msg_struct_t page[1];
710                         msg_struct_t media[1];
711                         msg_struct_t smil_text[1];
712                         
713                         if (getMessageType() != MMS) {
714                                 LogError("Wrong msg type");
715                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong msg type");
716                         }
717 //                      mmsData = msg_mms_create_message();
718                         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
719 //                      old_mms_struct = msg_create_struct(MSG_STRUCT_MMS);// THIS
720
721 //                      ret = msg_get_mms_struct(m_messageData, old_mms_struct);// THIS
722 //                      LogError("ret " << ret);// THIS
723 //                      ret = msg_release_struct(&old_mms_struct);// THIS
724 //                      LogError("ret " << ret);// THIS
725
726                         if (!mms_struct) {
727                                 LogError("create message body failed");
728                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "create message body failed");
729                         }
730         
731                         //body
732                         if (!getBodyRef().empty()) {
733                                 FILE* f = NULL;
734                                 f = fopen(m_bodyFilePath.c_str(), "w");
735                                 if (!f) {
736                                         LogError("cannot create file with body" << m_bodyFilePath);
737                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
738                                                          "cannot create file with body");
739                                 }
740                                 LogDebug("body file: " << m_bodyFilePath);
741                                 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
742                          
743                                 fclose(f);
744
745                                 LogDebug("getBodyRef(): " << getBodyRef());
746
747                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_INT, ROOT_LAYOUT_WIDTH);
748                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
749                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_BGCOLOR_INT, WHITE_COLOR);
750                                 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_PERCENT_BOOL, true);
751                                 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_PERCENT_BOOL, true);
752                                 
753                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_REGION, &region[0]);
754                                 msg_set_str_value(region[0], MSG_MMS_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
755                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_INT, 0);
756                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_TOP_INT, 0);
757                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_INT, ROOT_LAYOUT_WIDTH);
758                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
759                                 msg_set_int_value(region[0], MSG_MMS_REGION_BGCOLOR_INT, WHITE_COLOR);
760                                 
761                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_PERCENT_BOOL, true);
762                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_TOP_PERCENT_BOOL, true);
763                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_PERCENT_BOOL, true);
764                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_PERCENT_BOOL, true);
765
766                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_PAGE, &page[0]);
767                                 msg_set_int_value(page[0], MSG_MMS_PAGE_PAGE_DURATION_INT, 0);
768                                 
769                                 msg_mms_add_item(page[0], MSG_STRUCT_MMS_MEDIA, &media[0]);
770                                 msg_set_int_value(media[0], MSG_MMS_MEDIA_TYPE_INT, MMS_SMIL_MEDIA_TEXT);
771                                 msg_set_str_value(media[0], MSG_MMS_MEDIA_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
772                                 int error;
773                                 error = msg_set_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, const_cast<char*>(m_bodyFilePath.c_str()),MSG_FILEPATH_LEN_MAX);
774                                 LogDebug("bodyFile Path= " << m_bodyFilePath);
775                                 LogDebug("error= " << error);
776                                 char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
777                                 memset(szFilePath, 0, sizeof(szFilePath));
778                                 msg_get_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
779                                 LogDebug("bodyFile Path= " << m_bodyFilePath);
780
781                                 msg_get_struct_handle(media[0], MSG_MMS_MEDIA_SMIL_TEXT_HND, &smil_text[0]);
782                                 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_COLOR_INT, BLACK_COLOR);
783                                 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_SIZE_INT, MMS_SMIL_FONT_SIZE_NORMAL);
784                                 msg_set_bool_value(smil_text[0], MSG_MMS_SMIL_TEXT_BOLD_BOOL, true);
785                                 
786                         }
787                         
788                         //attachments
789                         struct stat buffer;
790                         int errnum = 0;
791
792                         msg_struct_t attachment[20];                    
793                         
794                         for (size_t i = 0; i < getAttachmentsRef().size(); ++i) {
795                                 Api::Messaging::IAttachmentPtr att = getAttachment(i);
796                                 if (!att) {
797                                         continue;
798                                 }
799         
800                                 LogDebug("Add attachment=" << att->getFullPath());
801                                 //checking file
802                                 errnum = stat(att->getFullPath().c_str(), &buffer);
803                                 if (errnum) {
804                                         LogError("Opening file: " <<
805                                                          att->getFullPath().c_str() <<
806                                                          " " << strerror(errnum));
807                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
808                                                          "cannot open attachment file");
809                                 }
810         
811                                 //this function should return valid pointer
812
813                                 msg_mms_add_item(mms_struct, MSG_STRUCT_MMS_ATTACH, &attachment[i]);
814                                 msg_set_str_value(attachment[i], MSG_MMS_ATTACH_FILEPATH_STR, const_cast<char*>(att->getFullPath().c_str()), MSG_FILEPATH_LEN_MAX);
815                                 
816                                 LogDebug("Attachment added");
817         
818                                 //reset errno
819                                 errnum = 0;
820                         }
821         
822                 if (MSG_SUCCESS !=msg_set_mms_struct(m_messageData, mms_struct))
823                 {
824                         LogError("set message body error");
825                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "set message body error");
826                 }
827
828                 msg_release_struct(&mms_struct);
829
830                 setAttachmentsValidity(true);
831                 setBodyValidity(true);
832                 
833                 }
834                 Catch(WrtDeviceApis::Commons::PlatformException) {
835                         LogError("Platform error");
836                         msg_release_struct(&mms_struct);
837                         throw;
838                 }
839 }
840
841
842 void Mms::updateReadStatus()
843 {
844     if (isReadStatusValid()) {
845         return;
846     }
847
848     LogInfo("updating platform read status: " << isRead());
849 //    if (MSG_SUCCESS != msg_set_read_status(m_messageData, isRead())) {
850     if (MSG_SUCCESS != msg_set_bool_value(m_messageData, MSG_MESSAGE_READ_BOOL, isRead())) {
851         LogError("problem with setting subject");
852         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
853     }
854
855     setReadStatusValidity(true);
856 }
857
858 void Mms::updateIsRead()
859 {
860     LogDebug("updating m_id=" << getIdRef());
861
862     if (!m_messageData) {
863         //error if platform message not exists
864         LogError("message can not be updated");
865         Throw(WrtDeviceApis::Commons::PlatformException);
866     }
867         
868     // check if abstraction from m_isReadChange value has been changed
869     if (isReadChangeStatusValid()) {
870         // do not update if not changed
871         return;
872     }
873
874     Try
875     {
876         if (this->getIdRef().empty()) {
877             LogError("existing msgId is zero, remove msg not done");
878             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
879                      "existing msgId is zero, remove msg not done");
880         }
881
882         if (MSG_SUCCESS !=
883             msg_update_read_status(MsgGetCommonHandle(), convertId(getIdRef()), isReadChangeStatus()))
884         {
885             LogError("Failed to update isRead");
886             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update isRead");
887         }
888     }
889     Catch(WrtDeviceApis::Commons::PlatformException) {
890         LogError("platform error occurs");
891     }
892     setisReadChangeStatusValidity(true);
893
894 }
895
896 void Mms::updateMessage()
897 {
898         LogDebug("updating m_id=" << getIdRef());
899         msg_error_t err = MSG_SUCCESS;
900         msg_struct_t msg = NULL;
901         msg_struct_t sendOpt = NULL;
902         
903         if (!m_messageData) {
904                 //error if platform message not exists
905                 LogError("message can not be updated");
906                 Throw(WrtDeviceApis::Commons::PlatformException);
907         }
908
909         
910         Try
911         {
912                 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
913                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);                        
914                 
915                 err = msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg, sendOpt);
916                 
917                 if (err != MSG_SUCCESS)
918                 {
919                         LogError("Get Message Failed!");
920                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
921                 }
922         
923                 if (this->getIdRef().empty()) {
924                         LogError("existing msgId is zero, update msg not done");
925                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
926                                         "existing msgId is zero, update msg not done");
927                 }
928                 
929                 update(TRUE);
930                 
931                 if (MSG_SUCCESS != msg_update_message(MsgGetCommonHandle(), m_messageData, sendOpt))
932                 {
933                         LogError("Failed to update message");
934                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
935                 }
936                 msg_release_struct(&msg);
937                 msg_release_struct(&sendOpt);
938         }
939         Catch(WrtDeviceApis::Commons::PlatformException) {
940                 msg_release_struct(&msg);
941                 msg_release_struct(&sendOpt);
942                 LogError("platform error occurs");
943         }
944 }
945
946
947 void Mms::createSendMessage()
948 {
949     LogDebug("convert m_id= " << convertId(getIdRef()));        
950
951     //prepare for add sms to draft folder
952     if (!m_messageData) {
953         //error if platform message not exists
954         LogError("message can not be updated");
955         Throw(WrtDeviceApis::Commons::PlatformException);
956     }
957
958     //update all sms data
959     if (getCurrentFolder() == Api::Messaging::DRAFTBOX) {
960                 LogDebug("update all mms data");
961         updateSubject();
962         updateBodyAndAttachments();
963         updateRecipientList();
964         updatePriority();
965     }
966
967     Try
968     {
969         int msgId = 0;
970 //        MSG_SENDINGOPT_S option = { false, false, false };
971 //        option.option.smsSendOpt.bReplyPath = true;
972         // trying to get message from platform
973
974 //        msg_struct_t sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
975 //        msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
976
977
978 //        const msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(DRAFTBOX);
979
980 //        msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
981 //        msg_set_int_value(m_messageData, MSG_MESSAGE_FOLDER_ID_INT, 4);
982 //        msg_set_int_value(m_messageData, MSG_MESSAGE_NETWORK_STATUS_INT, MSG_NETWORK_NOT_SEND);
983
984         setId(convertId(msgId));
985         setFolderType(Api::Messaging::DRAFTBOX);
986         setMessageStatus(Api::Messaging::MESSAGE_STATUS_DRAFT);
987     }
988     Catch(WrtDeviceApis::Commons::PlatformException) {
989         LogError("remove message error");
990         if (m_messageData) {
991             //releasing platform message structure
992 //            msg_release_message(&m_messageData);
993             msg_release_struct(&m_messageData);         
994         }
995         throw;
996     }
997
998 }       
999
1000 void Mms::addMessageToDraft()
1001 {
1002     LogDebug("convert m_id= " << convertId(getIdRef()));        
1003
1004     msg_struct_t sendOpt = NULL;
1005
1006     //prepare for add sms to draft folder
1007     if (!m_messageData) {
1008         //error if platform message not exists
1009         LogError("message can not be updated");
1010         Throw(WrtDeviceApis::Commons::PlatformException);
1011     }
1012
1013     //update all sms data
1014     if (getCurrentFolder() == Api::Messaging::DRAFTBOX) {
1015         updateSubject();
1016         updateBodyAndAttachments();
1017         updateRecipientList();
1018         updatePriority();
1019     }
1020
1021     Try
1022     {
1023         int msgId = -1;
1024         sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1025
1026         // trying to get message from platform
1027
1028 //        const msg_folder_id_t platfromFolderId =
1029 //        Messaging::convertFolderToPlatform(DRAFTBOX);
1030
1031         // trying to add message
1032         int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
1033         if (ret < MSG_SUCCESS) {
1034             LogError("msg_add_message failed, error code=" << ret);
1035             Throw(WrtDeviceApis::Commons::PlatformException);
1036         }
1037         else
1038         {
1039             msgId = ret;        
1040         }
1041
1042         LogDebug("Message ID : " << msgId);
1043         if (msgId < 0)
1044         {
1045             LogDebug("Message ID is invailded ");
1046             Throw(WrtDeviceApis::Commons::PlatformException);
1047         }
1048           
1049         msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1050
1051         setId(convertId(msgId));
1052         setFolderType(Api::Messaging::DRAFTBOX);
1053         setMessageStatus(Api::Messaging::MESSAGE_STATUS_DRAFT);
1054
1055         msg_release_struct(&sendOpt);
1056  
1057     }
1058     Catch(WrtDeviceApis::Commons::PlatformException) {
1059         LogError("remove message error");
1060 //      msg_release_struct(&m_messageData);
1061         msg_release_struct(&sendOpt);
1062          throw;
1063     }
1064
1065 }       
1066
1067 void Mms::readAllData()
1068 {
1069     readExistingMessage();
1070 }
1071
1072 void Mms::moveToFolder(const FolderType newFolder)
1073 {
1074 /*
1075     update();
1076
1077     Try
1078     {
1079         msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1080                 newFolder);
1081
1082         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1083                                    platfromFolderId) != MSG_SUCCESS) {
1084             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error during movinf message");
1085         }
1086     }
1087
1088     Catch(WrtDeviceApis::Commons::PlatformException) {
1089         LogError("remove message error");
1090         throw;
1091     }
1092 */
1093 }
1094
1095 void Mms::moveToFolder(const string& newFolder)
1096 {
1097 /*
1098     update();
1099
1100     Try
1101     {
1102         msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1103                 newFolder);
1104
1105         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1106                                    platfromFolderId) != MSG_SUCCESS) {
1107             LogError("msg_move_msg_to_folder error");
1108             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_move_msg_to_folder error");
1109         }
1110     }
1111
1112     Catch(WrtDeviceApis::Commons::PlatformException) {
1113         LogError("remove message error");
1114         throw;
1115     }
1116     */
1117 }
1118
1119 void Mms::copyToFolder(const FolderType newFolder)
1120 {
1121 /*
1122     update();
1123
1124     msg_message_t msg = msg_new_message();
1125
1126     Try
1127     {
1128         MSG_SENDINGOPT_S option = { false, false, false };
1129         option.option.smsSendOpt.bReplyPath = true;
1130         if (MSG_SUCCESS ==
1131             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1132                             &option)) {
1133             const msg_folder_id_t platfromFolderId =
1134                 Messaging::convertFolderToPlatform(newFolder);
1135             msg_set_message_id(msg, 0);
1136             msg_set_folder_id(msg, platfromFolderId);
1137
1138             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1139             if (error != MSG_SUCCESS) {
1140                 LogError("msg_add_message failed, error code=" << error);
1141                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1142             }
1143         }
1144         if (msg) {
1145             msg_release_message(&msg);
1146         }
1147     }
1148
1149     Catch(WrtDeviceApis::Commons::PlatformException) {
1150         LogError("remove message error");
1151         if (msg) {
1152             msg_release_message(&msg);
1153         }
1154         throw;
1155     }
1156 */
1157 }
1158
1159 void Mms::copyToFolder(const string& newFolder)
1160 {
1161 /*
1162     update();
1163
1164     msg_message_t msg = msg_new_message();
1165
1166     Try
1167     {
1168         MSG_SENDINGOPT_S option = { false, false, false };
1169         option.option.smsSendOpt.bReplyPath = true;
1170         if (MSG_SUCCESS ==
1171             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1172                             &option)) {
1173             const msg_folder_id_t platfromFolderId =
1174                 Messaging::convertFolderToPlatform(newFolder);
1175             msg_set_message_id(msg, 0);
1176             msg_set_folder_id(msg, platfromFolderId);
1177
1178             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1179             if (error != MSG_SUCCESS) {
1180                 LogError("msg_add_message failed, error code=" << error);
1181                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1182             }
1183         }
1184         if (msg) {
1185             msg_release_message(&msg);
1186         }
1187     }
1188
1189     Catch(WrtDeviceApis::Commons::PlatformException) {
1190         LogError("remove message error");
1191         if (msg) {
1192             msg_release_message(&msg);
1193         }
1194         throw;
1195     }
1196 */
1197 }
1198
1199 void Mms::remove()
1200 {
1201     LogDebug("delete m_id=" << getIdRef());
1202
1203     Try
1204     {
1205         if (this->getIdRef().empty()) {
1206             LogError("existing msgId is zero, remove msg not done");
1207             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1208                      "existing msgId is zero, remove msg not done");
1209         }
1210
1211         if (MSG_SUCCESS !=
1212             msg_delete_message(MsgGetCommonHandle(),
1213                                convertId(getIdRef()))) {
1214             LogError("Failed to delete Message");
1215             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to delete Message");
1216         }
1217     }
1218
1219     Catch(WrtDeviceApis::Commons::PlatformException) {
1220         LogError("platform error occurs");
1221     }
1222 }
1223
1224 int Mms::send()
1225 {
1226         LogDebug("sending message, id=" << getIdRef());
1227         Try
1228         {
1229                 //prepare for sending sms/mms
1230                 update();
1231
1232                 LogDebug("Start Sending Message...");
1233
1234                 LogDebug("trying to send mms");
1235                 msg_error_t err = CallbackMgrSingleton::Instance().registerAndSend(msg_mms_send_message, m_messageData, this);
1236
1237                 if (err != MSG_SUCCESS) 
1238                 {
1239                         LogError("Sending Message (submit request) failed!!! err=" << err);
1240                         setMessageStatus(MESSAGE_STATUS_FAILED);
1241                         goto ERROR;
1242                 }
1243
1244                 setMessageStatus(MESSAGE_STATUS_SENDING);
1245
1246                 LogDebug("Sending Message request is submitted!");
1247         }
1248         Catch(WrtDeviceApis::Commons::PlatformException) {
1249                 LogError("message is not send, manually run callback");
1250                 goto ERROR;
1251         }
1252
1253         return 0;
1254
1255         ERROR:
1256         ReqReceiverMessage *requestReceiver = getRequestReceiver();
1257         if (requestReceiver) {
1258                 LogError("send Error");
1259                 EventMessagingServicePtr event = getMessagingServiceEvent();
1260                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1261                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService>::ManualAnswer(event);
1262         }
1263         return 0;
1264 }
1265
1266 void Mms::sendingCallback(msg_struct_t sent_status)
1267 {
1268 //      LogInfo("sendingCallback callback received. Req id = " <<
1269 //      sent_status->reqId << " Status = " << (int)sent_status->status);
1270 //      msg_struct_t request_data = NULL;
1271 //      msg_get_struct_handle(sent_status, MSG_STRUCT_REQUEST_INFO, (void **)request_data);
1272 //      msg_get_int_value(request_data, MSG_REQUEST_REQUESTID_INT, &tempInt);
1273         int status = MSG_NETWORK_SEND_FAIL;
1274         msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
1275         if (MSG_NETWORK_SEND_SUCCESS == status) {
1276         LogDebug("sending ok");
1277         setSendingStatusOk();
1278         setMessageStatus(MESSAGE_STATUS_SENT);
1279     } else {
1280         LogError("sending failed: " <<
1281                 static_cast<unsigned int>(status));
1282         setSendingStatusFailed();
1283            setMessageStatus(MESSAGE_STATUS_FAILED);
1284     }
1285 }
1286
1287 void Mms::sendCancel(int handle)
1288 {
1289     LogDebug("sending message, id=" << getIdRef());
1290     //#warning "TODO"
1291     LogDebug("handle =" << handle);
1292 }
1293
1294 void Mms::setSendingStatusOk()
1295 {
1296     //success callback should be executed here
1297         ReqReceiverMessage *requestReceiver = getRequestReceiver();
1298         msg_struct_list_s *addr_list = NULL;
1299         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1300         int nToCnt;
1301         
1302         if (requestReceiver) {
1303                 LogInfo("calling JS success callback");
1304                 EventMessagingServicePtr event = getMessagingServiceEvent();
1305                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1306
1307                 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1308
1309                 nToCnt = addr_list->nCount;
1310
1311                 for ( int index=0; index < nToCnt; index++)
1312                 {
1313                         msg_get_str_value(addr_list->msg_struct_info[0], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1314                         event->m_successRecipients.push_back(strNumber);
1315                 }
1316           
1317                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1318         }
1319 }
1320
1321 void Mms::setSendingStatusFailed()
1322 {       
1323     //error callback should be executed here
1324         EventOnSendingFailedEmitterPtr emitter = getEmitter();
1325         msg_struct_list_s *addr_list = NULL;
1326         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1327         int nToCnt;
1328         
1329         if (emitter) 
1330         {
1331                 EventOnSendingFailedPtr event(new EventOnSendingFailed);
1332                 event->setError(EventOnSendingFailed::UNKNOWN); // TODO error codes
1333                 emitter->emit(event);
1334         } 
1335         else 
1336         {
1337                 ReqReceiverMessage *requestReceiver = getRequestReceiver();
1338                 if (requestReceiver) 
1339                 {
1340                         LogError("calling JS error callback");
1341                         EventSendMessagePtr event = getSendMessageEvent();
1342                         event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1343
1344                         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_STRUCT, (void **)&addr_list);
1345                  
1346                         nToCnt = addr_list->nCount;
1347                 
1348                         for ( int index=0; index < nToCnt; index++)
1349                         {
1350                                 msg_get_str_value(addr_list->msg_struct_info[index], MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1351                                 event->m_failRecipients.push_back(strNumber);
1352                         }
1353                 }
1354         }
1355 }
1356
1357 Api::Messaging::FolderType Mms::toFolder(const std::string &folder)
1358 {
1359     if (folder == "INBOX") {
1360         return INBOX;
1361     } else if (folder == "OUTBOX") {
1362         return OUTBOX;
1363     } else if (folder == "SENTBOX") {
1364         return SENTBOX;
1365     } else if (folder == "DRAFTBOX") {
1366         return DRAFTBOX;
1367     }
1368     ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Invalid folder");
1369 }
1370 }
1371 }
1372 }