wrt-plugins-tizen_0.4.23
[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_list_handle_t addr_list = NULL;
307         msg_struct_t addr_info = NULL;
308
309         msg_get_list_handle(messageData, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
310
311         int tempInt = 0;
312         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
313
314         int recipientCount = msg_list_length(addr_list);
315         LoggerD("Recipient count " << recipientCount);
316
317         for (int i = 0; i < recipientCount; ++i) 
318         {
319 //        int type = msg_get_direction_info(messageData);
320                 msg_get_int_value(messageData, MSG_MESSAGE_DIRECTION_INT, &tempInt);
321                 int type = tempInt;
322                 LoggerD("Direction: " << type);
323
324                 if (MSG_DIRECTION_TYPE_MT == type) 
325                 {
326                         addr_info = (msg_struct_t)msg_list_nth_data(addr_list, i);
327                         memset(strNumber, 0x00, sizeof(MAX_ADDRESS_VAL_LEN));
328                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
329                         phoneNumber = strNumber;
330                 
331                         if (validatePhoneNumber(phoneNumber)) 
332                         {
333                                 setSourceAddress(phoneNumber);
334                                 setFrom(phoneNumber);
335                         }
336                         LoggerD("MT number: " << phoneNumber);
337                 } 
338                 else if (MSG_DIRECTION_TYPE_MO == type) 
339                 {
340                         addr_info = (msg_struct_t)msg_list_nth_data(addr_list, i);
341                         memset(strNumber, 0x00, sizeof(MAX_ADDRESS_VAL_LEN));
342                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
343                         msg_get_int_value(addr_info, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, &tempInt);
344                         LoggerD("MO number: " << strNumber);
345                         LoggerD("Type : " << tempInt);
346                         switch (tempInt) 
347                         {
348                                 case MSG_RECIPIENTS_TYPE_TO:
349                                         appendToRecipients(strNumber);
350                                         break;
351                                 case MSG_RECIPIENTS_TYPE_CC:
352                                         appendCcRecipients(strNumber);
353                                         break;
354                                 case MSG_RECIPIENTS_TYPE_BCC:
355                                         appendBccRecipients(strNumber);
356                                         break;
357                                 default:
358                                 LoggerE("Wrong type of recipient");
359                         }
360                 } 
361                 else 
362                 {
363                         LoggerE("Wrong type of recipient");
364                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong type of recipient");
365                 }
366         }
367 }
368
369 void Mms::readSubject(msg_struct_t& messageData)
370 {
371     char strTemp[MAX_SUBJECT_LEN] = {0};
372     msg_get_str_value(messageData, MSG_MESSAGE_SUBJECT_STR, strTemp, MAX_SUBJECT_LEN);
373 //    setSubject(msg_get_subject(messageData));
374     LoggerE("readSubject : " << strTemp);
375     setSubject(strTemp);
376 }
377
378 void Mms::readBodyAndAttachments(msg_struct_t& messageData)
379 {
380
381         int pageLen, mediaLen, attachLen, tempInt, ret;
382
383         msg_struct_t mms_struct = NULL;
384
385         msg_list_handle_t page_list = NULL;
386         msg_list_handle_t media_list = NULL;
387         msg_list_handle_t attach_list = NULL;
388         
389         msg_struct_t page = NULL;
390         msg_struct_t media = NULL;
391         msg_struct_t attach = NULL;
392
393         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
394         ret = msg_get_mms_struct(messageData, mms_struct);
395
396         if (ret != MSG_SUCCESS) 
397         {
398                 LoggerE("cannot get mms struct");
399                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "cannot get mms struct");
400         }
401
402         char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
403         char szFileName[MSG_FILENAME_LEN_MAX] = {0,};
404
405         msg_get_list_handle(mms_struct, MSG_MMS_PAGE_LIST_HND, (void **)&page_list);
406         pageLen = msg_list_length(page_list);
407
408         if (pageLen > 0) {
409                 setMmsType(MULTIPART_RELATED);
410         } else {
411                 setMmsType(MULTIPART_MIXED);
412         }
413
414         LoggerD("page count: " << pageLen);
415
416         for (int p = 0; p < pageLen; ++p) 
417         {
418                 LoggerD("page " << p);
419                 page = (msg_struct_t)msg_list_nth_data(page_list, p);
420                 
421                 if (NULL  == page) {
422                         LoggerE("returned page is null, continue");
423                         continue;
424                 }
425                 
426                 msg_get_list_handle(page, MSG_MMS_PAGE_MEDIA_LIST_HND, (void **)&media_list);
427                 mediaLen = msg_list_length(media_list);
428
429                 for (int m = 0; m < mediaLen; ++m) 
430                 {
431                         LoggerD("media file " << m);
432                         media = (msg_struct_t)msg_list_nth_data(media_list, m);
433                         if (NULL == media) 
434                         {
435                                 LoggerE("returned media is null, continue");
436                                 continue;
437                         }
438
439                         msg_get_int_value(media, MSG_MMS_MEDIA_TYPE_INT, &tempInt);
440
441                         memset(szFilePath, 0, sizeof(szFilePath));
442                         msg_get_str_value(media, MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
443                         
444                         if ((0 == p) && (MMS_SMIL_MEDIA_TEXT == tempInt)) 
445                         {
446                                 //text value on first page goes to body attribute
447                                 LoggerD("setting body from " << szFilePath);
448                                 
449                                 FILE* f = NULL;
450                                 f = fopen(szFilePath, "r");
451                                 if (!f) 
452                                 {
453                                         LoggerE("cannot read file with body" << szFilePath);
454                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Cannot read file with body");
455                                 }
456                                 fseek(f, 0, SEEK_END);
457                                 long int size = ftell(f);
458                                 fseek(f, 0, SEEK_SET);
459                                 char* data = new (nothrow) char[size + 1];
460                                 size_t error;
461                                 if (data) {
462                                         memset(data, 0, size + 1);
463                                         error = fread(data, 1, size, f);
464                                         setBody(data);
465                                         delete[] data;
466                                 } else {
467                                         LoggerE("body is not set");
468                                 }
469                                 LoggerD("message body: '" << getBody() << "'");
470                                 fclose(f);
471                         } 
472                         else 
473                         {
474                                 LoggerD("adding attachment " << szFilePath);
475
476                                 IAttachmentPtr attachment = appendAttachment(szFilePath, false);
477                                 //attachment->setMessage(this); //set IMessagePtr
478
479                                 memset(szFileName, 0, sizeof(szFileName));
480                                 msg_get_str_value(media, MSG_MMS_MEDIA_FILENAME_STR, szFileName, sizeof(szFileName));
481                                 if ((szFileName[0] =! '\0' )&& (strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0)) 
482                                 {
483                                         LoggerD("renaming to " << szFileName);
484                                         attachment->rename(szFileName);
485                                 }
486                                 else if (MMS_SMIL_MEDIA_TEXT == tempInt) 
487                                 {
488                                         std::stringstream newName;
489                                         newName << "body_page_" << p + 1 << ".txt";
490                                         LoggerD("renaming to " << newName.str());
491                                         attachment->rename(newName.str());
492                                 }
493                         }
494                 }
495         }
496
497         msg_get_list_handle(mms_struct, MSG_MMS_ATTACH_LIST_HND, (void **)&attach_list);
498         
499         attachLen= msg_list_length(attach_list);
500
501         LoggerD("attachment count: " << attachLen);
502         for (int a = 0; a < attachLen; ++a) 
503         {
504                 attach = (msg_struct_t)msg_list_nth_data(attach_list, a);
505                 if (NULL == attach) 
506                 {
507                         LoggerE("attachment is null, continue");
508                         continue;
509                 }
510
511                 memset(szFilePath, 0, sizeof(szFilePath));
512                 memset(szFileName, 0, sizeof(szFileName));
513
514                 msg_get_str_value(attach, MSG_MMS_ATTACH_FILEPATH_STR, szFilePath, sizeof(szFilePath));
515                 msg_get_str_value(attach, MSG_MMS_ATTACH_FILENAME_STR, szFileName, sizeof(szFileName));
516                 
517                 IAttachmentPtr att = appendAttachment(szFilePath, false);
518                 if ((szFileName[0] =! '\0') &&
519                         strnlen(szFileName, MSG_FILENAME_LEN_MAX) > 0) {
520                         LoggerD("renaming to " << szFileName);
521                         att->rename(szFileName);
522                 }
523         }
524         msg_release_struct(&mms_struct);
525 }
526
527 bool Mms::hasAttachment()
528 {
529         std::size_t attachmentSize = getAttachmentsCount();
530         if ( attachmentSize > 0)
531                 return true;
532         else
533                 return false;
534 }
535
536 void Mms::update(bool draftsOnly)
537 {
538     LoggerD("updating m_id=" << getIdRef());
539
540     if (!m_messageData) {
541         //error if platform message not exists
542         LoggerE("message can not be updated");
543         Throw(WrtDeviceApis::Commons::PlatformException);
544     }
545
546     //update mms data from abstraction
547     if (!draftsOnly || getCurrentFolder() == DRAFTBOX) {
548         updateSubject();
549         updateBodyAndAttachments();
550         updateRecipientList();
551         updatePriority();
552     }
553     updateReadStatus();
554 }
555
556 void Mms::updatePriority()
557 {
558     if (isPriorityValid()) {
559         return;
560     }
561     int priority = -1;
562     LoggerI("MMS updating priority");
563
564     //set priority in platform
565     switch (getPriority()) {
566     case MessagePriority::LOW:
567         priority = MSG_MESSAGE_PRIORITY_LOW;
568         break;
569     case MessagePriority::NORMAL:
570         priority = MSG_MESSAGE_PRIORITY_NORMAL;
571         break;
572     case MessagePriority::HIGH:
573         priority = MSG_MESSAGE_PRIORITY_HIGH;
574         break;
575     default:
576         LoggerE("Wrong priority");
577         Throw(WrtDeviceApis::Commons::PlatformException);
578     }
579     LoggerI("priority : " << priority);
580
581     if (MSG_SUCCESS !=
582         msg_set_int_value(m_messageData, MSG_MESSAGE_PRIORITY_INT, priority)){
583         
584         LoggerE("Error during setting priority");
585         Throw(WrtDeviceApis::Commons::PlatformException);
586     }
587 }
588
589 void Mms::updateRecipientList()
590 {
591     // check if abstraction recipient value has been changed
592     if (getToRecipients().isValid() &&
593         getBccRecipients().isValid() &&
594         getCcRecipients().isValid()) {
595         return;
596     }
597
598     LoggerI("MMS updating platform recipients");
599
600         int nToCnt = 0;
601
602         msg_list_clear(m_messageData, MSG_MESSAGE_ADDR_LIST_HND);
603
604         vector<string> to = getToRecipients().getRecipients();
605         vector<string> cc = getCcRecipients().getRecipients();
606         vector<string> bcc = getBccRecipients().getRecipients();
607
608         // update addresses in platform structure
609         if (to.size()) 
610         {
611                 LoggerI("updating to");
612                 for (size_t i = 0; i < to.size(); i++) 
613                 {
614                         msg_struct_t tmpAddr;
615
616                         if (i >= MAX_TO_ADDRESS_CNT) 
617                         {
618                                 LoggerE("max number of recipient exceeded");
619                                 break;
620                         }
621                         LoggerD("adding to[" << i << "]=" << to[i]);
622
623                         msg_list_add_item(m_messageData, MSG_MESSAGE_ADDR_LIST_HND, &tmpAddr);
624
625                         msg_set_int_value(tmpAddr, MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
626
627                         if (msg_set_int_value(tmpAddr, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
628                                 MSG_RECIPIENTS_TYPE_TO) != MSG_SUCCESS) 
629                         {
630                                 LoggerE("problem with adding to address");
631                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
632                         }
633                         
634                         if(msg_set_str_value(tmpAddr, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
635                                 const_cast<char*>(to[i].c_str()), to[i].size()) != MSG_SUCCESS) 
636                         {
637                                 LoggerE("problem with adding to address");
638                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding to address");
639                         }
640                         nToCnt ++;
641                 }
642                 LoggerE("nToCnt : " << nToCnt);
643         }
644         
645
646         if (cc.size()) 
647         {
648                 LoggerI("updating cc");
649                 for (size_t i = 0; i < cc.size(); i++) 
650                 {
651                         msg_struct_t tmpAddr;
652
653                         if (nToCnt >= MAX_TO_ADDRESS_CNT) 
654                         {
655                                 LoggerE("max number of recipient exceeded");
656                                 break;
657                         }
658                         LoggerD("adding cc[" << i << "]=" << cc[i]);
659
660                         msg_list_add_item(m_messageData, MSG_MESSAGE_ADDR_LIST_HND, &tmpAddr);
661
662                         msg_set_int_value(tmpAddr, MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
663
664                         if (msg_set_int_value(tmpAddr, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
665                                 MSG_RECIPIENTS_TYPE_CC) != MSG_SUCCESS) 
666                         {
667                                 LoggerE("problem with adding cc address");
668                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
669                         }
670                         
671                         if(msg_set_str_value(tmpAddr, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
672                                 const_cast<char*>(cc[i].c_str()), cc[i].size()) != MSG_SUCCESS) 
673                         {
674                                 LoggerE("problem with adding cc address");
675                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding cc address");
676                         }
677                         nToCnt ++;
678                 }
679                 LoggerE("nToCnt : " << nToCnt);
680         }
681
682
683         if (bcc.size()) 
684         {
685                 LoggerI("updating bcc");
686                 for (size_t i = 0; i < bcc.size(); i++) 
687                 {
688                         msg_struct_t tmpAddr;
689
690                         if (nToCnt >= MAX_TO_ADDRESS_CNT) 
691                         {
692                                 LoggerE("max number of recipient exceeded");
693                                 break;
694                         }
695                         LoggerD("adding bcc[" << i << "]=" << bcc[i]);
696
697                         msg_list_add_item(m_messageData, MSG_MESSAGE_ADDR_LIST_HND, &tmpAddr);
698
699                         msg_set_int_value(tmpAddr, MSG_ADDRESS_INFO_ADDRESS_TYPE_INT, MSG_ADDRESS_TYPE_PLMN);
700
701                         if (msg_set_int_value(tmpAddr, MSG_ADDRESS_INFO_RECIPIENT_TYPE_INT, 
702                                 MSG_RECIPIENTS_TYPE_BCC) != MSG_SUCCESS) 
703                         {
704                                 LoggerE("problem with adding bcc address");
705                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
706                         }
707                         
708                         if(msg_set_str_value(tmpAddr, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, 
709                                 const_cast<char*>(bcc[i].c_str()), bcc[i].size()) != MSG_SUCCESS) 
710                         {
711                                 LoggerE("problem with adding bcc address");
712                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with adding bcc address");
713                         }
714                         nToCnt ++;
715                 }
716                 LoggerE("nToCnt : " << nToCnt);
717         }
718
719         setToValidity(true);
720         setCcValidity(true);
721         setBccValidity(true);
722 }
723
724 void Mms::updateSubject()
725 {
726     // check if abstraction subject value has been changed
727     if (isSubjectValid()) {
728         return;
729     }
730     LoggerI("updating platform subject: " << getSubjectRef());
731     if (MSG_SUCCESS !=
732 //        msg_set_subject(m_messageData, getSubjectRef().c_str())) {
733         msg_set_str_value(m_messageData, MSG_MESSAGE_SUBJECT_STR, const_cast<char*>(getSubjectRef().c_str()), MAX_SUBJECT_LEN)) {
734
735         LoggerE("problem with setting subject");
736         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
737     }
738     setSubjectValidity(true);
739 }
740
741 void Mms::updateBodyAndAttachments()
742 {
743                 // check if attachment or body source address value has been changed
744                 if (isAttachmentsValid() && isBodyValid()) {
745                         return;
746                 }
747         
748 //              MMS_MESSAGE_DATA_S *mmsData = NULL;
749                 msg_struct_t mms_struct = NULL;
750 //              msg_struct_t old_mms_struct = NULL;// THIS
751 //              int ret;// THIS
752
753                 LoggerI("updating platform body and attachment");
754         
755                 Try
756                 {
757                         msg_struct_t region[1];
758                         msg_struct_t page[1];
759                         msg_struct_t media[1];
760                         msg_struct_t smil_text[1];
761                         
762                         if (getMessageType() != MMS) {
763                                 LoggerE("Wrong msg type");
764                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Wrong msg type");
765                         }
766 //                      mmsData = msg_mms_create_message();
767                         mms_struct = msg_create_struct(MSG_STRUCT_MMS);
768 //                      old_mms_struct = msg_create_struct(MSG_STRUCT_MMS);// THIS
769
770 //                      ret = msg_get_mms_struct(m_messageData, old_mms_struct);// THIS
771 //                      LoggerE("ret " << ret);// THIS
772 //                      ret = msg_release_struct(&old_mms_struct);// THIS
773 //                      LoggerE("ret " << ret);// THIS
774
775                         if (!mms_struct) {
776                                 LoggerE("create message body failed");
777                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "create message body failed");
778                         }
779         
780                         //body
781                         if (!getBodyRef().empty()) {
782                                 FILE* f = NULL;
783                                 f = fopen(m_bodyFilePath.c_str(), "w");
784                                 if (!f) {
785                                         LoggerE("cannot create file with body" << m_bodyFilePath);
786                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
787                                                          "cannot create file with body");
788                                 }
789                                 LoggerD("body file: " << m_bodyFilePath);
790                                 fwrite(getBodyRef().c_str(), strlen(getBodyRef().c_str()), 1, f);
791                          
792                                 fclose(f);
793
794                                 LoggerD("getBodyRef(): " << getBodyRef());
795
796                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_INT, ROOT_LAYOUT_WIDTH);
797                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
798                                 msg_set_int_value(mms_struct, MSG_MMS_ROOTLAYOUT_BGCOLOR_INT, WHITE_COLOR);
799                                 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_WIDTH_PERCENT_BOOL, true);
800                                 msg_set_bool_value(mms_struct, MSG_MMS_ROOTLAYOUT_HEIGHT_PERCENT_BOOL, true);
801                                 
802                                 msg_list_add_item(mms_struct, MSG_STRUCT_MMS_REGION, &region[0]);
803                                 msg_set_str_value(region[0], MSG_MMS_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
804                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_INT, 0);
805                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_TOP_INT, 0);
806                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_INT, ROOT_LAYOUT_WIDTH);
807                                 msg_set_int_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_INT, ROOT_LAYOUT_HEIGHT);
808                                 msg_set_int_value(region[0], MSG_MMS_REGION_BGCOLOR_INT, WHITE_COLOR);
809                                 
810                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_LEFT_PERCENT_BOOL, true);
811                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_TOP_PERCENT_BOOL, true);
812                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_WIDTH_PERCENT_BOOL, true);
813                                 msg_set_bool_value(region[0], MSG_MMS_REGION_LENGTH_HEIGHT_PERCENT_BOOL, true);
814
815                                 msg_list_add_item(mms_struct, MSG_STRUCT_MMS_PAGE, &page[0]);
816                                 msg_set_int_value(page[0], MSG_MMS_PAGE_PAGE_DURATION_INT, 0);
817                                 
818                                 msg_list_add_item(page[0], MSG_STRUCT_MMS_MEDIA, &media[0]);
819                                 msg_set_int_value(media[0], MSG_MMS_MEDIA_TYPE_INT, MMS_SMIL_MEDIA_TEXT);
820                                 msg_set_str_value(media[0], MSG_MMS_MEDIA_REGION_ID_STR, const_cast<char*>(TEXT_AREA), 4);
821                                 int error;
822                                 error = msg_set_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, const_cast<char*>(m_bodyFilePath.c_str()),MSG_FILEPATH_LEN_MAX);
823                                 LoggerD("bodyFile Path= " << m_bodyFilePath);
824                                 LoggerD("error= " << error);
825                                 char szFilePath[MSG_FILEPATH_LEN_MAX] = {0,};
826                                 memset(szFilePath, 0, sizeof(szFilePath));
827                                 msg_get_str_value(media[0], MSG_MMS_MEDIA_FILEPATH_STR, szFilePath, sizeof(szFilePath));
828                                 LoggerD("bodyFile Path= " << m_bodyFilePath);
829
830                                 msg_get_struct_handle(media[0], MSG_MMS_MEDIA_SMIL_TEXT_HND, &smil_text[0]);
831                                 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_COLOR_INT, BLACK_COLOR);
832                                 msg_set_int_value(smil_text[0], MSG_MMS_SMIL_TEXT_SIZE_INT, MMS_SMIL_FONT_SIZE_NORMAL);
833                                 msg_set_bool_value(smil_text[0], MSG_MMS_SMIL_TEXT_BOLD_BOOL, true);
834                                 
835                         }
836                         
837                         //attachments
838                         struct stat buffer;
839                         int errnum = 0;
840
841                         msg_struct_t attachment[20];                    
842                         
843                         for (size_t i = 0; i < getAttachmentsRef().size(); ++i) {
844                                 IAttachmentPtr att = getAttachment(i);
845                                 if (!att) {
846                                         continue;
847                                 }
848         
849                                 LoggerD("Add attachment=" << att->getFullPath());
850                                 //checking file
851                                 errnum = stat(att->getFullPath().c_str(), &buffer);
852                                 LoggerD("errnum=" << errnum);
853
854                                 if (errnum != 0) {
855                                         LoggerE("Opening file: " << att->getFullPath().c_str());
856                                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
857                                                          "cannot open attachment file");
858                                 }
859         
860                                 //this function should return valid pointer
861
862                                 msg_list_add_item(mms_struct, MSG_STRUCT_MMS_ATTACH, &attachment[i]);
863                                 msg_set_str_value(attachment[i], MSG_MMS_ATTACH_FILEPATH_STR, const_cast<char*>(att->getFullPath().c_str()), MSG_FILEPATH_LEN_MAX);
864                                 
865                                 LoggerD("Attachment added");
866         
867                                 //reset errno
868                                 errnum = 0;
869                         }
870         
871                 if (MSG_SUCCESS !=msg_set_mms_struct(m_messageData, mms_struct))
872                 {
873                         LoggerE("set message body error");
874                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "set message body error");
875                 }
876
877                 msg_release_struct(&mms_struct);
878
879                 setAttachmentsValidity(true);
880                 setBodyValidity(true);
881                 
882                 }
883                 Catch(WrtDeviceApis::Commons::PlatformException) {
884                         LoggerE("Platform error");
885                         msg_release_struct(&mms_struct);
886                         throw;
887                 }
888 }
889
890
891 void Mms::updateReadStatus()
892 {
893     if (isReadStatusValid()) {
894         return;
895     }
896
897     LoggerI("updating platform read status: " << isRead());
898 //    if (MSG_SUCCESS != msg_set_read_status(m_messageData, isRead())) {
899     if (MSG_SUCCESS != msg_set_bool_value(m_messageData, MSG_MESSAGE_READ_BOOL, isRead())) {
900         LoggerE("problem with setting subject");
901         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Problem with setting subject");
902     }
903
904     setReadStatusValidity(true);
905 }
906
907 void Mms::updateIsRead()
908 {
909     LoggerD("updating m_id=" << getIdRef());
910
911     if (!m_messageData) {
912         //error if platform message not exists
913         LoggerE("message can not be updated");
914         Throw(WrtDeviceApis::Commons::PlatformException);
915     }
916         
917     // check if abstraction from m_isReadChange value has been changed
918     if (isReadChangeStatusValid()) {
919         // do not update if not changed
920         return;
921     }
922
923     Try
924     {
925         if (this->getIdRef().empty()) {
926             LoggerE("existing msgId is zero, remove msg not done");
927             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
928                      "existing msgId is zero, remove msg not done");
929         }
930
931         if (MSG_SUCCESS !=
932             msg_update_read_status(MsgGetCommonHandle(), convertId(getIdRef()), isReadChangeStatus()))
933         {
934             LoggerE("Failed to update isRead");
935             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update isRead");
936         }
937     }
938     Catch(WrtDeviceApis::Commons::PlatformException) {
939         LoggerE("platform error occurs");
940     }
941     setisReadChangeStatusValidity(true);
942
943 }
944
945 void Mms::updateMessage()
946 {
947         LoggerD("updating m_id=" << getIdRef());
948         msg_error_t err = MSG_SUCCESS;
949         msg_struct_t msg = NULL;
950         msg_struct_t sendOpt = NULL;
951         
952         if (!m_messageData) {
953                 //error if platform message not exists
954                 LoggerE("message can not be updated");
955                 Throw(WrtDeviceApis::Commons::PlatformException);
956         }
957
958         
959         Try
960         {
961                 msg = msg_create_struct(MSG_STRUCT_MESSAGE_INFO);
962                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);                        
963                 
964                 err = msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg, sendOpt);
965                 
966                 if (err != MSG_SUCCESS)
967                 {
968                         LoggerE("Get Message Failed!");
969                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
970                 }
971         
972                 if (this->getIdRef().empty()) {
973                         LoggerE("existing msgId is zero, update msg not done");
974                         ThrowMsg(WrtDeviceApis::Commons::PlatformException,
975                                         "existing msgId is zero, update msg not done");
976                 }
977                 
978                 update(TRUE);
979                 
980                 if (MSG_SUCCESS != msg_update_message(MsgGetCommonHandle(), m_messageData, sendOpt))
981                 {
982                         LoggerE("Failed to update message");
983                         ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to update message");
984                 }
985                 msg_release_struct(&msg);
986                 msg_release_struct(&sendOpt);
987         }
988         Catch(WrtDeviceApis::Commons::PlatformException) {
989                 msg_release_struct(&msg);
990                 msg_release_struct(&sendOpt);
991                 LoggerE("platform error occurs");
992         }
993 }
994
995
996 void Mms::createSendMessage()
997 {
998     LoggerD("convert m_id= " << convertId(getIdRef())); 
999
1000     //prepare for add sms to draft folder
1001     if (!m_messageData) {
1002         //error if platform message not exists
1003         LoggerE("message can not be updated");
1004         Throw(WrtDeviceApis::Commons::PlatformException);
1005     }
1006
1007     //update all sms data
1008     if (getCurrentFolder() == DRAFTBOX) {
1009                 LoggerD("update all mms data");
1010         updateSubject();
1011         updateBodyAndAttachments();
1012         updateRecipientList();
1013         updatePriority();
1014     }
1015
1016     Try
1017     {
1018         int msgId = 0;
1019 //        MSG_SENDINGOPT_S option = { false, false, false };
1020 //        option.option.smsSendOpt.bReplyPath = true;
1021         // trying to get message from platform
1022
1023 //        msg_struct_t sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1024 //        msg_set_bool_value(sendOpt, MSG_SEND_OPT_SETTING_BOOL, false);
1025
1026
1027 //        const msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(DRAFTBOX);
1028
1029 //        msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1030 //        msg_set_int_value(m_messageData, MSG_MESSAGE_FOLDER_ID_INT, 4);
1031 //        msg_set_int_value(m_messageData, MSG_MESSAGE_NETWORK_STATUS_INT, MSG_NETWORK_NOT_SEND);
1032
1033         setId(convertId(msgId));
1034         setFolderType(DRAFTBOX);
1035         setMessageStatus(MESSAGE_STATUS_DRAFT);
1036     }
1037     Catch(WrtDeviceApis::Commons::PlatformException) {
1038         LoggerE("remove message error");
1039         if (m_messageData) {
1040             //releasing platform message structure
1041 //            msg_release_message(&m_messageData);
1042             msg_release_struct(&m_messageData);         
1043         }
1044         throw;
1045     }
1046
1047 }       
1048
1049 void Mms::addMessageToDraft()
1050 {
1051         LoggerD("convert m_id= " << convertId(getIdRef()));
1052
1053         msg_struct_t sendOpt = NULL;
1054
1055         //prepare for add sms to draft folder
1056         if (!m_messageData) {
1057                 //error if platform message not exists
1058                 LoggerE("message can not be updated");
1059                 Throw(WrtDeviceApis::Commons::PlatformException);
1060         }
1061
1062         //update all mms data
1063         if (getCurrentFolder() == DRAFTBOX) {
1064                 updateSubject();
1065                 updateBodyAndAttachments();
1066                 updateRecipientList();
1067                 updatePriority();
1068         }
1069
1070         Try
1071         {
1072                 int msgId = -1;
1073                 sendOpt = msg_create_struct(MSG_STRUCT_SENDOPT);
1074
1075                 // trying to add message
1076                 LoggerD("add message");
1077                 int ret = msg_add_message(MsgGetCommonHandle(), m_messageData, sendOpt);
1078                 if (ret < MSG_SUCCESS) {
1079                         LoggerE("msg_add_message failed, error code=" << ret);
1080                         Throw(WrtDeviceApis::Commons::PlatformException);
1081                 }
1082                 else
1083                 {
1084                         msgId = ret;
1085                 }
1086
1087                 LoggerD("Message ID : " << msgId);
1088                 if (msgId < 0)
1089                 {
1090                         LoggerD("Message ID is invailded ");
1091                         Throw(WrtDeviceApis::Commons::PlatformException);
1092                 }
1093
1094                 msg_set_int_value(m_messageData, MSG_MESSAGE_ID_INT, msgId);
1095
1096                 setId(convertId(msgId));
1097                 setFolderType(DRAFTBOX);
1098                 setMessageStatus(MESSAGE_STATUS_DRAFT);
1099
1100                 msg_release_struct(&sendOpt);
1101
1102         }
1103         Catch(WrtDeviceApis::Commons::PlatformException) {
1104                 LoggerE("add draft message error");
1105                 msg_release_struct(&sendOpt);
1106                 throw;
1107         }
1108
1109 }               
1110
1111 void Mms::readAllData()
1112 {
1113     readExistingMessage();
1114 }
1115
1116 void Mms::moveToFolder(const FolderType newFolder)
1117 {
1118 /*
1119     update();
1120
1121     Try
1122     {
1123         msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1124                 newFolder);
1125
1126         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1127                                    platfromFolderId) != MSG_SUCCESS) {
1128             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Error during movinf message");
1129         }
1130     }
1131
1132     Catch(WrtDeviceApis::Commons::PlatformException) {
1133         LoggerE("remove message error");
1134         throw;
1135     }
1136 */
1137 }
1138
1139 void Mms::moveToFolder(const string& newFolder)
1140 {
1141 /*
1142     update();
1143
1144     Try
1145     {
1146         msg_folder_id_t platfromFolderId = Messaging::convertFolderToPlatform(
1147                 newFolder);
1148
1149         if (msg_move_msg_to_folder(MsgGetCommonHandle(), convertId(getId()),
1150                                    platfromFolderId) != MSG_SUCCESS) {
1151             LoggerE("msg_move_msg_to_folder error");
1152             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_move_msg_to_folder error");
1153         }
1154     }
1155
1156     Catch(WrtDeviceApis::Commons::PlatformException) {
1157         LoggerE("remove message error");
1158         throw;
1159     }
1160     */
1161 }
1162
1163 void Mms::copyToFolder(const FolderType newFolder)
1164 {
1165 /*
1166     update();
1167
1168     msg_message_t msg = msg_new_message();
1169
1170     Try
1171     {
1172         MSG_SENDINGOPT_S option = { false, false, false };
1173         option.option.smsSendOpt.bReplyPath = true;
1174         if (MSG_SUCCESS ==
1175             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1176                             &option)) {
1177             const msg_folder_id_t platfromFolderId =
1178                 Messaging::convertFolderToPlatform(newFolder);
1179             msg_set_message_id(msg, 0);
1180             msg_set_folder_id(msg, platfromFolderId);
1181
1182             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1183             if (error != MSG_SUCCESS) {
1184                 LoggerE("msg_add_message failed, error code=" << error);
1185                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1186             }
1187         }
1188         if (msg) {
1189             msg_release_message(&msg);
1190         }
1191     }
1192
1193     Catch(WrtDeviceApis::Commons::PlatformException) {
1194         LoggerE("remove message error");
1195         if (msg) {
1196             msg_release_message(&msg);
1197         }
1198         throw;
1199     }
1200 */
1201 }
1202
1203 void Mms::copyToFolder(const string& newFolder)
1204 {
1205 /*
1206     update();
1207
1208     msg_message_t msg = msg_new_message();
1209
1210     Try
1211     {
1212         MSG_SENDINGOPT_S option = { false, false, false };
1213         option.option.smsSendOpt.bReplyPath = true;
1214         if (MSG_SUCCESS ==
1215             msg_get_message(MsgGetCommonHandle(), convertId(getIdRef()), msg,
1216                             &option)) {
1217             const msg_folder_id_t platfromFolderId =
1218                 Messaging::convertFolderToPlatform(newFolder);
1219             msg_set_message_id(msg, 0);
1220             msg_set_folder_id(msg, platfromFolderId);
1221
1222             int error = msg_add_message(MsgGetCommonHandle(), msg, &option);
1223             if (error != MSG_SUCCESS) {
1224                 LoggerE("msg_add_message failed, error code=" << error);
1225                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "msg_add_message failed");
1226             }
1227         }
1228         if (msg) {
1229             msg_release_message(&msg);
1230         }
1231     }
1232
1233     Catch(WrtDeviceApis::Commons::PlatformException) {
1234         LoggerE("remove message error");
1235         if (msg) {
1236             msg_release_message(&msg);
1237         }
1238         throw;
1239     }
1240 */
1241 }
1242
1243 void Mms::remove()
1244 {
1245     LoggerD("delete m_id=" << getIdRef());
1246
1247     Try
1248     {
1249         if (this->getIdRef().empty()) {
1250             LoggerE("existing msgId is zero, remove msg not done");
1251             ThrowMsg(WrtDeviceApis::Commons::PlatformException,
1252                      "existing msgId is zero, remove msg not done");
1253         }
1254
1255         if (MSG_SUCCESS !=
1256             msg_delete_message(MsgGetCommonHandle(),
1257                                convertId(getIdRef()))) {
1258             LoggerE("Failed to delete Message");
1259             ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Failed to delete Message");
1260         }
1261     }
1262
1263     Catch(WrtDeviceApis::Commons::PlatformException) {
1264         LoggerE("platform error occurs");
1265     }
1266 }
1267
1268 int Mms::send()
1269 {
1270         LoggerD("sending message, id=" << getIdRef());
1271         Try
1272         {
1273                 //prepare for sending sms/mms
1274                 update();
1275
1276                 LoggerD("Start Sending Message...");
1277
1278                 LoggerD("trying to send mms");
1279                 msg_error_t err = CallbackMgrSingleton::Instance().registerAndSend(msg_mms_send_message, m_messageData, this);
1280
1281                 if (err != MSG_SUCCESS) 
1282                 {
1283                         LoggerE("Sending Message (submit request) failed!!! err=" << err);
1284                         setMessageStatus(MESSAGE_STATUS_FAILED);
1285                         goto ERROR;
1286                 }
1287
1288                 setMessageStatus(MESSAGE_STATUS_SENDING);
1289
1290                 LoggerD("Sending Message request is submitted!");
1291         }
1292         Catch(WrtDeviceApis::Commons::PlatformException) {
1293                 LoggerE("message is not send, manually run callback");
1294                 goto ERROR;
1295         }
1296
1297         return 0;
1298
1299         ERROR:
1300         ReqReceiverMessage *requestReceiver = getRequestReceiver();
1301         if (requestReceiver) {
1302                 LoggerE("send Error");
1303                 EventMessagingServicePtr event = getMessagingServiceEvent();
1304                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1305                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService>::ManualAnswer(event);
1306         }
1307         return 0;
1308 }
1309
1310 void Mms::sendingCallback(msg_struct_t sent_status)
1311 {
1312 //      LoggerI("sendingCallback callback received. Req id = " <<
1313 //      sent_status->reqId << " Status = " << (int)sent_status->status);
1314 //      msg_struct_t request_data = NULL;
1315 //      msg_get_struct_handle(sent_status, MSG_STRUCT_REQUEST_INFO, (void **)request_data);
1316 //      msg_get_int_value(request_data, MSG_REQUEST_REQUESTID_INT, &tempInt);
1317         int status = MSG_NETWORK_SEND_FAIL;
1318         msg_get_int_value(sent_status, MSG_SENT_STATUS_NETWORK_STATUS_INT, &status);
1319         
1320         LoggerD("status : " << status);
1321         
1322         if (MSG_NETWORK_SEND_SUCCESS == status) {
1323                 LoggerD("sending ok");
1324                 setSendingStatusOk();
1325                 setMessageStatus(MESSAGE_STATUS_SENT);
1326         } else {
1327                 LoggerE("sending failed: " << static_cast<unsigned int>(status));
1328                 setSendingStatusFailed();
1329                 setMessageStatus(MESSAGE_STATUS_FAILED);
1330         }
1331 }
1332
1333 void Mms::sendCancel(int handle)
1334 {
1335     LoggerD("sending message, id=" << getIdRef());
1336     //#warning "TODO"
1337     LoggerD("handle =" << handle);
1338 }
1339
1340 void Mms::setSendingStatusOk()
1341 {
1342     //success callback should be executed here
1343         ReqReceiverMessage *requestReceiver = getRequestReceiver();
1344         msg_list_handle_t addr_list = NULL;
1345         msg_struct_t addr_info = NULL;
1346
1347         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1348         int nToCnt;
1349         
1350         if (requestReceiver) {
1351                 LoggerI("calling JS success callback");
1352                 EventMessagingServicePtr event = getMessagingServiceEvent();
1353                 event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::None);
1354
1355                 msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
1356
1357                 nToCnt = msg_list_length(addr_list);
1358
1359                 for ( int index=0; index < nToCnt; index++)
1360                 {
1361                         addr_info = (msg_struct_t)msg_list_nth_data(addr_list, index);
1362                         msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1363                         event->m_successRecipients.push_back(strNumber);
1364                 }
1365           
1366                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1367         }
1368 }
1369
1370 void Mms::setSendingStatusFailed()
1371 {       
1372     //error callback should be executed here
1373         EventOnSendingFailedEmitterPtr emitter = getEmitter();
1374         msg_list_handle_t addr_list = NULL;
1375         msg_struct_t addr_info = NULL;
1376
1377         char strNumber[MAX_ADDRESS_VAL_LEN] = {0,};
1378         int nToCnt;
1379         
1380         if (emitter) 
1381         {
1382                 EventOnSendingFailedPtr event(new EventOnSendingFailed);
1383                 event->setError(EventOnSendingFailed::UNKNOWN); // TODO error codes
1384                 emitter->emit(event);
1385         } 
1386         else 
1387         {
1388                 ReqReceiverMessage *requestReceiver = getRequestReceiver();
1389                 EventMessagingServicePtr event = getMessagingServiceEvent();
1390                 if (requestReceiver) 
1391                 {
1392                         LoggerE("calling JS error callback");
1393                         event->setExceptionCode(WrtDeviceApis::Commons::ExceptionCodes::UnknownException);
1394
1395                         msg_get_list_handle(m_messageData, MSG_MESSAGE_ADDR_LIST_HND, (void **)&addr_list);
1396                         nToCnt = msg_list_length(addr_list);
1397
1398                         LoggerD("nToCnt : " << nToCnt);
1399
1400                         for ( int index=0; index < nToCnt; index++)
1401                         {
1402                                 addr_info = (msg_struct_t)msg_list_nth_data(addr_list, index);
1403                                 msg_get_str_value(addr_info, MSG_ADDRESS_INFO_ADDRESS_VALUE_STR, strNumber, MAX_ADDRESS_VAL_LEN);
1404                                 event->m_failRecipients.push_back(strNumber);
1405                         }
1406
1407                 requestReceiver->WrtDeviceApis::Commons::EventRequestReceiver< EventMessagingService >::ManualAnswer(event);
1408                 }
1409         }
1410 }
1411
1412 FolderType Mms::toFolder(const std::string &folder)
1413 {
1414     if (folder == "INBOX") {
1415         return INBOX;
1416     } else if (folder == "OUTBOX") {
1417         return OUTBOX;
1418     } else if (folder == "SENTBOX") {
1419         return SENTBOX;
1420     } else if (folder == "DRAFTBOX") {
1421         return DRAFTBOX;
1422     }
1423     ThrowMsg(WrtDeviceApis::Commons::PlatformException, "Invalid folder");
1424 }
1425 }
1426 }