e12ae06de760381a309511ba48f79a973cbca945
[platform/framework/web/wrt-plugins-tizen.git] / src / Messaging / JSMessage.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 <dpl/assert.h>
19 #include "MessageFactory.h"
20 #include "MessagePriority.h"
21 #include <CommonsJavaScript/JSCallbackManager.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include "ConverterMessage.h"
25 #include "JSMessage.h"
26 #include "JSRecipientArray.h"
27 #include "JSMessagePrivateObject.h"
28 #include "MessagingListener.h"
29 #include "ReqReceiverMessage.h"
30
31 #include <JSTizenExceptionFactory.h>
32 #include <JSTizenException.h>
33 #include <SecurityExceptions.h>
34
35 #include <CommonsJavaScript/ScopedJSStringRef.h>
36 // ==== new common headers
37 #include <GlobalContextManager.h>
38 #include <PlatformException.h>
39 #include <JSWebAPIError.h>
40 #include <JSUtil.h>
41 #include <ArgumentValidator.h>
42 // ====
43
44 #include "MessagingErrorMsg.h"
45 #include "JSMessageAttachment.h"
46 #include "plugin_config.h"
47
48 #include "Sms.h"
49
50 using namespace std;
51 using namespace DeviceAPI::Common;
52 using namespace WrtDeviceApis::Commons;
53 using namespace WrtDeviceApis::CommonsJavaScript;
54
55 namespace DeviceAPI {
56 namespace Messaging {
57 JSClassRef JSMessage::m_jsClassRef = NULL;
58
59 #define MESSAGE_ATTRIBUTE_ID "id"
60 #define MESSAGE_ATTRIBUTE_SERVICE_ID "serviceId"
61 #define MESSAGE_ATTRIBUTE_CONVERSATION_ID "conversationId"
62 #define MESSAGE_ATTRIBUTE_FOLDER_ID  "folderId"
63 #define MESSAGE_ATTRIBUTE_TYPE "type"
64 #define MESSAGE_ATTRIBUTE_TIMESTAMP "timestamp"
65 #define MESSAGE_ATTRIBUTE_FROM "from"
66 #define MESSAGE_ATTRIBUTE_TO "to" // used also in dictionary
67 #define MESSAGE_ATTRIBUTE_CC "cc" // used also in dictionary
68 #define MESSAGE_ATTRIBUTE_BCC  "bcc" // used also in dictionary
69 #define MESSAGE_ATTRIBUTE_BODY "body"
70 #define MESSAGE_ATTRIBUTE_IS_READ "isRead"
71 #define MESSAGE_ATTRIBUTE_IS_HIGH_PRIORITY "isHighPriority" // used also in dictionary
72 #define MESSAGE_ATTRIBUTE_SUBJECT "subject" // used also in dictionary
73 #define MESSAGE_ATTRIBUTE_IN_RESPONSE_TO "inResponseTo"
74 #define MESSAGE_ATTRIBUTE_MESSAGE_STATUS "messageStatus"
75 #define MESSAGE_ATTRIBUTE_ATTACHMENTS "attachments"
76 #define MESSAGE_ATTRIBUTE_HAS_ATTACHMENT "hasAttachment"
77
78 #define MESSAGE_DICTIONARY_ATTRIBUTE_PLAIN_BODY "plainBody"
79 #define MESSAGE_DICTIONARY_ATTRIBUTE_HTML_BODY "htmlBody"
80
81 JSClassDefinition JSMessage::m_classInfo = {
82     0,
83     kJSClassAttributeNone,
84     "Message",
85     0,
86     NULL, // m_property not used here - access properties by setProperty()/getProperty()
87     NULL, // m_function
88     initialize,
89     finalize,
90     NULL, //hasProperty,
91     JSMessage::getProperty, //getProperty,
92     JSMessage::setProperty, //setProperty,
93     NULL, //deleteProperty,
94     JSMessage::getPropertyNames, //getPropertyNames,
95     NULL, //callAsFunction,
96     NULL, //callAsConstructor,
97     NULL, //hasInstance,
98     NULL, //convertToType,
99 };
100
101 // JSMessage properties - used in: getProperty(), setProperty(), getPropertyNames()
102 JSStaticValue JSMessage::m_property[] = {
103     { MESSAGE_ATTRIBUTE_ID, getMessageId, NULL, kJSPropertyAttributeReadOnly },
104     { MESSAGE_ATTRIBUTE_SERVICE_ID, getAccountID, NULL, kJSPropertyAttributeReadOnly},
105     { MESSAGE_ATTRIBUTE_CONVERSATION_ID, getConversationId, NULL, kJSPropertyAttributeReadOnly },
106     { MESSAGE_ATTRIBUTE_FOLDER_ID, getFolder, NULL, kJSPropertyAttributeReadOnly },
107     { MESSAGE_ATTRIBUTE_TYPE, getMessageType, NULL, kJSPropertyAttributeReadOnly },
108     { MESSAGE_ATTRIBUTE_TIMESTAMP, getTime, NULL, kJSPropertyAttributeReadOnly },
109     { MESSAGE_ATTRIBUTE_FROM, getSourceAddress, NULL, kJSPropertyAttributeReadOnly },
110     { MESSAGE_ATTRIBUTE_TO, getDestinationAddress, setDestinationAddress, kJSPropertyAttributeNone },
111     { MESSAGE_ATTRIBUTE_CC, getCcAddress, setCcAddress, kJSPropertyAttributeNone },
112     { MESSAGE_ATTRIBUTE_BCC, getBccAddress, setBccAddress, kJSPropertyAttributeNone },
113     { MESSAGE_ATTRIBUTE_BODY, getMessageBody, setMessageBody, kJSPropertyAttributeNone },
114     { MESSAGE_ATTRIBUTE_IS_READ, getIsRead, setIsRead, kJSPropertyAttributeNone },
115     { MESSAGE_ATTRIBUTE_IS_HIGH_PRIORITY, getMessagePriority, setMessagePriority, kJSPropertyAttributeNone },
116     { MESSAGE_ATTRIBUTE_SUBJECT, getSubject, setSubject, kJSPropertyAttributeNone },
117     { MESSAGE_ATTRIBUTE_IN_RESPONSE_TO, getInResponseTo, NULL, kJSPropertyAttributeNone },
118     { MESSAGE_ATTRIBUTE_MESSAGE_STATUS, getMessageStatus, NULL, kJSPropertyAttributeReadOnly},
119     { MESSAGE_ATTRIBUTE_ATTACHMENTS, getAttachments, setAttachments, kJSPropertyAttributeNone },
120     { MESSAGE_ATTRIBUTE_HAS_ATTACHMENT, hasAttachment, NULL, kJSPropertyAttributeReadOnly},
121     { 0, 0, 0, 0 }
122 };
123
124
125 JSValueRef JSMessage::createJSObject(JSContextRef context,
126                 EmailAccountInfo& account,
127                 MessageType msgType,
128                 const std::string& msgId)
129 {
130         IMessagePtr msg;
131         LoggerD("createJSObject with account ");
132
133         Try
134         {
135                 if (msgType == EMAIL)
136                 {
137                         LoggerD("Account Address:" << &account);
138                         LoggerD("Account Data ,ID" << account.getId() << "name:" << account.getName() << " Account:" << account.getAddress());
139
140                         msg = MessageFactory::createMessage(msgType, account, msgId);
141                         if (!msg)
142                                 return JSValueMakeUndefined(context);
143                 }
144                 else
145                 {
146                         Throw(WrtDeviceApis::Commons::UnknownException);   // unsupported type
147                 }
148         }
149         Catch(WrtDeviceApis::Commons::UnknownException) {
150                 LoggerE("wrong message type, object not created");
151                 return JSValueMakeUndefined(context);
152         }
153
154         return createJSObject(context, msg);
155 }
156
157 JSValueRef JSMessage::createJSObject(JSContextRef context,
158         EventUpdateMessageAnswerReceiver* listener,
159         MessageType msgType,
160         const string& msgId)
161 {
162         IMessagePtr msg;
163         Try
164         {
165                 // create message depending on type
166                 if (msgType == SMS || msgType == MMS || msgType == EMAIL)
167                 {
168                         msg = MessageFactory::createMessage(msgType, msgId);
169                         if (!msg)
170                                 return JSValueMakeUndefined(context);
171                 }
172                 else
173                 {
174                         Throw(WrtDeviceApis::Commons::UnknownException);   // unsupported type
175                 }
176         }
177         Catch(WrtDeviceApis::Commons::UnknownException) {
178                 LoggerE("wrong message type, object not created");
179                 return JSValueMakeUndefined(context);
180         }
181         return createJSObject(context, msg, listener);
182 }
183
184 JSValueRef JSMessage::createJSObject(JSContextRef context,
185         const IMessagePtr& msg,
186         EventUpdateMessageAnswerReceiver* listener)
187 {
188         JSClassRef jsClassRef = JSClassCreate(getClassInfo());
189         JSMessagePrivateObject* priv = new JSMessagePrivateObject(context, msg);
190         priv->setUpdateMsgReceiver(listener);
191         JSObjectRef jsValueRef = JSObjectMake(context, jsClassRef, static_cast<void*>(priv));
192         JSClassRelease(jsClassRef);
193         if (NULL == jsValueRef) {
194                 LoggerE("object creation error");
195                 return JSValueMakeUndefined(context);
196         }
197         return jsValueRef;
198 }
199
200 JSValueRef JSMessage::createJSObject(JSContextRef context,
201         const IMessagePtr& msg)
202 {
203         LoggerD("createJSObject");
204         JSClassRef jsClassRef = JSClassCreate(getClassInfo());
205         LoggerD("jsClassRef success");
206         JSMessagePrivateObject* priv = new JSMessagePrivateObject(context, msg);
207         LoggerD("priv success");
208
209         JSObjectRef jsValueRef = JSObjectMake(context, jsClassRef, static_cast<void*>(priv));
210         LoggerD("JSObjectMake success");
211         JSClassRelease(jsClassRef);
212         if (NULL == jsValueRef) {
213                 LoggerE("object creation error");
214                 return JSValueMakeUndefined(context);
215         }
216         return jsValueRef;
217 }
218
219 JSValueRef JSMessage::createJSObject(JSContextRef context,
220         MessageType msgType,
221         const string& msgId)
222 {
223         IMessagePtr msg;
224         Try
225         {
226                 // create message depending on type
227                 if (msgType == SMS || msgType == MMS || msgType == EMAIL)
228                 {
229                         msg = MessageFactory::createMessage(msgType, msgId);
230                 }
231                 else
232                 {
233                         Throw(WrtDeviceApis::Commons::UnknownException);   // unsupported type
234                 }
235         }
236         Catch(WrtDeviceApis::Commons::UnknownException) {
237                 LoggerE("wrong message type, object not created");
238                 return JSValueMakeUndefined(context);
239         }
240         return createJSObject(context, msg, NULL);
241 }
242
243 JSValueRef JSMessage::createDummyMessageJSObject(JSContextRef context,
244                 MessageType msgType)
245 {
246         IMessagePtr msg;
247         if (msgType == EMAIL )
248         {
249                 msg = MessageFactory::createEmailMessage();
250         }
251         else
252         {
253                 LoggerE("message type is invalid");
254                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
255         }
256         return createJSObject(context, msg);
257 }
258
259 void JSMessage::initialize(JSContextRef context,
260         JSObjectRef object)
261 {
262         LoggerI("enter");
263
264         if (!JSObjectGetPrivate(object))
265         {
266                 IMessagePtr msg(new Sms());
267                 JSMessagePrivateObject *priv = new JSMessagePrivateObject(context, msg);
268                 if (!JSObjectSetPrivate(object, priv))
269                 {
270                         LoggerI("set Private Failed...");
271                         delete priv;
272                 }
273         }
274 }
275
276 void JSMessage::finalize(JSObjectRef object)
277 {
278         JSMessagePrivateObject* priv = static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
279         if (priv) {
280                 LoggerD("deleting private object");
281                 delete priv;
282                 JSObjectSetPrivate(object, NULL);
283         }
284 }
285
286 bool JSMessage::hasProperty(JSContextRef context,
287         JSObjectRef object,
288         JSStringRef propertyName)
289 {
290         return false;
291 }
292
293 JSValueRef JSMessage::getProperty(JSContextRef context,
294         JSObjectRef object,
295         JSStringRef propertyName,
296         JSValueRef* exception)
297 {
298     LoggerI("Main getProperty()");
299     int index = 0;
300     // for non-NULL property names
301     while(m_property[index].name != NULL) {
302         // if property name matches
303         if(JSStringIsEqualToUTF8CString(propertyName,m_property[index].name)) {
304             if(m_property[index].getProperty != NULL) {
305                 // if getProperty() function exists - call it
306                 return m_property[index].getProperty(context, object, propertyName, exception);
307             } else {
308                 //otherwise return undefined
309                 return JSValueMakeUndefined(context);
310             }
311         }
312         index++;
313     }
314     return NULL;
315 }
316
317 bool JSMessage::setProperty(JSContextRef context,
318         JSObjectRef object,
319         JSStringRef propertyName,
320         JSValueRef value,
321         JSValueRef* exception)
322 {
323     LoggerI("Main setProperty()");
324     int index = 0;
325     // for non-NULL property names
326     while(m_property[index].name != NULL) {
327         // if property name matches
328         if(JSStringIsEqualToUTF8CString(propertyName,m_property[index].name)) {
329             if(m_property[index].setProperty != NULL) {
330                 // if setProperty() function exists - call it
331                 return m_property[index].setProperty(context, object, propertyName, value, exception);
332             } else {
333                 // otherwise return true (lack of function - readonly attribute)
334                 return true;
335             }
336         }
337         index++;
338     }
339     return false;
340 }
341
342 bool JSMessage::deleteProperty(JSContextRef context,
343         JSObjectRef object,
344         JSStringRef propertyName,
345         JSValueRef* exception)
346 {
347         LoggerI("enter");
348         return true;
349 }
350
351 void JSMessage::getPropertyNames(JSContextRef context,
352         JSObjectRef object,
353         JSPropertyNameAccumulatorRef propertyNames)
354 {
355         LoggerI("enter");
356
357     JSStringRef propertyName = NULL;
358     int index = 0;
359     // repeat for each declared property - until null on property name
360     while(m_property[index].name != NULL) {
361         LoggerD("Property: " << m_property[index].name);
362         propertyName = JSStringCreateWithUTF8CString(m_property[index].name);
363         JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
364         JSStringRelease(propertyName);
365         index++;
366     }
367 }
368
369 JSValueRef JSMessage::callAsFunction(JSContextRef context,
370         JSObjectRef object,
371         JSObjectRef thisObject,
372         size_t argumentCount,
373         const JSValueRef arguments[],
374         JSValueRef* exception)
375 {
376         LoggerI("enter");
377         return JSValueMakeUndefined(context);
378 }
379
380 bool JSMessage::hasInstance(JSContextRef context,
381         JSObjectRef constructor,
382         JSValueRef possibleInstance,
383         JSValueRef* exception)
384 {
385         LoggerI("enter");
386         return true;
387 }
388
389 JSObjectRef JSMessage::constructor(JSContextRef context,
390         JSObjectRef constructor,
391         size_t argumentCount,
392         const JSValueRef arguments[],
393         JSValueRef* exception)
394 {
395     LoggerD("entered");
396
397     try {
398         ArgumentValidator validator(context, argumentCount, arguments);
399         std::string msgTypeString = validator.toString(0);
400         MessageType msgType = stringToMessageType(msgTypeString);
401         JSObjectRef dictionary = validator.toObject(1, true);
402         // converter is still used for some parameter
403         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
404
405         IMessagePtr msg;
406         if ( msgType == EMAIL )
407         {
408             msg = MessageFactory::createEmailMessage();
409         }
410         else if (msgType == MMS || msgType == SMS)
411         {
412             msg = MessageFactory::createMessage(msgType);       //create message
413         }
414         else
415         {
416             // this should never happen - message type checking done in stringToMessageType()
417             LoggerE("message type is invalid");
418             throw TypeMismatchException("Invalid message type");
419         }
420
421         if (msg == NULL) {
422             LoggerD("Message Creation failed");
423             throw DeviceAPI::Common::UnknownException("Message has been not created");
424         }
425
426         if (dictionary != NULL)
427         {
428             LoggerD ("##### msg type is " << msgType);
429
430             JSValueRef subjectData = JSUtil::getProperty(context, dictionary, MESSAGE_ATTRIBUTE_SUBJECT);
431             JSValueRef toData = JSUtil::getProperty(context, dictionary, MESSAGE_ATTRIBUTE_TO);
432             JSValueRef ccData = JSUtil::getProperty(context, dictionary, MESSAGE_ATTRIBUTE_CC);
433             JSValueRef bccData = JSUtil::getProperty(context, dictionary, MESSAGE_ATTRIBUTE_BCC);
434             JSValueRef plainBodyData = JSUtil::getProperty(context, dictionary, MESSAGE_DICTIONARY_ATTRIBUTE_PLAIN_BODY);
435             JSValueRef htmlBodyData = JSUtil::getProperty(context, dictionary, MESSAGE_DICTIONARY_ATTRIBUTE_HTML_BODY);
436             JSValueRef priorityData = JSUtil::getProperty(context, dictionary, MESSAGE_ATTRIBUTE_IS_HIGH_PRIORITY);
437
438             //subject
439             if (!JSValueIsUndefined(context, subjectData) )
440             {
441                 std::string subject = JSUtil::JSValueToString(context, subjectData);
442                 LoggerD (" Subject : " << subject);
443                 switch (msgType)
444                 {
445                     case MMS:
446                     {
447                         IMmsPtr mms = MessageFactory::convertToMms(msg);
448                         mms->setSubject(subject);
449                         break;
450                     }
451                     case EMAIL:
452                     {
453                         IEmailPtr email = MessageFactory::convertToEmail(msg);
454                         email->setSubject(subject);
455                         break;
456                     }
457                     default:
458                     {
459                         LoggerE("message not supported");
460                         //Throw(WrtDeviceApis::Commons::UnsupportedException);
461                         //break;
462                     }
463                 }
464             }
465             //to
466             if (!JSValueIsUndefined(context, toData) )
467             {
468                 Recipients to = converter->toRecipients(toData);
469                 LoggerD("setting to field, size=" << to.getRecipientSize());
470                 msg->setToRecipients(to);
471             }
472             //cc
473             if (msgType == EMAIL )
474             {
475                 IEmailPtr email = MessageFactory::convertToEmail(msg);
476                 if (!JSValueIsUndefined(context, ccData))
477                 {
478                     Recipients cc = converter->toRecipients(ccData);
479                     email->setCcRecipients(cc);
480                 }
481
482                 if (!JSValueIsUndefined(context, bccData))
483                 {
484                     Recipients bcc = converter->toRecipients(bccData);
485                     email->setBccRecipients(bcc);
486                 }
487
488                 if (!JSValueIsUndefined(context, htmlBodyData))
489                 {
490                     std::string body = JSUtil::JSValueToString(context, htmlBodyData);
491                     LoggerD("html body : " << body);
492                     email->setHtmlBody(body);
493                 }
494
495                 if (!JSValueIsUndefined(context, priorityData))
496                 {
497                     email->setPriority(converter->toMessagePriority(priorityData));
498                 }
499             }
500             if (!JSValueIsUndefined(context, plainBodyData) )
501             {
502                 std::string body = JSUtil::JSValueToString(context, plainBodyData);
503                 LoggerD("plain body  : " << body);
504                 msg->setBody(body);
505             }
506         }
507
508         //return
509         JSClassRef jsClassRef = JSClassCreate(JSMessage::getClassInfo());
510         LoggerD("jsClassRef success");
511         JSMessagePrivateObject* priv = new JSMessagePrivateObject(context, msg);
512         LoggerD("priv success");
513
514         JSObjectRef jsObjRef = JSObjectMake(context, jsClassRef, static_cast<void*>(priv));
515         LoggerD("JSObjectMake success");
516         JSClassRelease(jsClassRef);
517         if (NULL == jsObjRef) {
518             LoggerE("JSObject creation error");
519             throw DeviceAPI::Common::UnknownException("JSObject make error");
520         }
521         return jsObjRef;
522     }
523     catch(BasePlatformException &bex) {
524         LoggerE("Message creation failed: " << bex.getMessage());
525         JSObjectRef error = JSWebAPIError::makeJSWebAPIError(context, bex);
526         *exception = error;
527         return NULL;
528     }
529     catch(...) {
530         DeviceAPI::Common::UnknownException err("UnknownError in Message constructor.");
531         JSObjectRef error = JSWebAPIError::makeJSWebAPIError(context, err);
532         *exception = error;
533         return NULL;
534     }
535     return NULL;
536 }
537
538 JSValueRef JSMessage::convertToType(JSContextRef context,
539         JSObjectRef object,
540         JSType type,
541         JSValueRef* exception)
542 {
543     LoggerI("enter");
544     return JSValueMakeUndefined(context);
545 }
546
547 JSValueRef JSMessage::getAttachments(JSContextRef context,
548         JSObjectRef object,
549         JSStringRef propertyName,
550         JSValueRef* exception)
551 {
552         LoggerI("enter");
553         try {
554         GlobalContextManager *gcm = GlobalContextManager::getInstance();
555         if(gcm == NULL) {
556             LoggerE("Failed to get GlobalContextManager");
557             throw DeviceAPI::Common::UnknownException("Unable to receive global context");
558         }
559                 JSContextRef globalContext = gcm->getGlobalContext(context);
560
561                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
562                 IMessagePtr msg = converter->toIMessage(object);
563                 MessageType msgType = msg->getMessageType();
564                 LoggerI("msgType=" << msgType);
565                 // prepare common values
566                 JSCallbackManagerPtr emptyCallbackMgr = JSCallbackManager::createObject(globalContext, NULL, NULL);
567
568                 switch (msgType) {
569                         case MMS:
570                         case EMAIL:
571                         {
572
573                                 std::vector<IAttachmentPtr> attachments;
574                                 std::vector<IAttachmentPtr>::iterator it;
575
576                                 if ( msgType == MMS )
577                                 {
578                                         AttachmentsPtr mmsAttachments =
579                                         DPL::StaticPointerCast<Attachments>(MessageFactory::convertToMms(msg));
580                                         attachments = mmsAttachments->getAttachments();
581                                 }
582                                 else
583                                 {
584                                         AttachmentsPtr emailAttachments =
585                                         DPL::StaticPointerCast<Attachments>(MessageFactory::convertToEmail(msg));
586                                         attachments = emailAttachments->getAttachments();
587                                 }
588
589                                 int count = attachments.size();
590                                 LoggerD( "count : " << count);
591
592                                 JSObjectRef jsMessageAttachmentObject[count];   //make
593
594                                 for (unsigned int index = 0 ; index < attachments.size(); index++ )
595                                 {
596                                         LoggerD( "Attachment ID : " << attachments[index]->getAttachmentID());
597                                         jsMessageAttachmentObject[index] = JSMessageAttachment::createJS(globalContext, attachments[index] );
598                                 }
599                                 JSObjectRef result = JSObjectMakeArray(globalContext, count, jsMessageAttachmentObject, NULL);
600                                 return result;
601                         }
602                         case SMS:
603                         {
604                                 JSObjectRef arrayValue = JSObjectMakeArray(globalContext, 0, NULL, NULL);
605                                 if (NULL == arrayValue)
606                                 {
607                                         LoggerE("Could not create js array object");
608                                         return JSValueMakeUndefined(context);
609                                 }
610                                 return arrayValue;
611                         }
612                         default:
613                                 LoggerE("Not supported message type");
614                 /* unsupported type -> type is integral message attribute
615                  * so error at this place means internal api error*/
616                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
617                 break;
618                 }
619         }
620     catch(BasePlatformException &bex) {
621         LoggerE("Attachments getting failure: " << bex.getMessage());
622         return JSValueMakeUndefined(context);
623     }
624     catch(...) {
625         LoggerE("UnknownError when getting attachments.");
626         return JSValueMakeUndefined(context);
627     }
628 }
629
630
631 JSValueRef JSMessage::getBccAddress(JSContextRef context,
632         JSObjectRef object,
633         JSStringRef propertyName,
634         JSValueRef* exception)
635 {
636         LoggerD("enter");
637         try {
638                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
639                 IMessagePtr msg = converter->toIMessage(object);
640                 MessageType msgType = msg->getMessageType();
641
642                 switch (msgType)
643                 {
644                         case EMAIL:
645                         {
646                                 IEmailPtr email = MessageFactory::convertToEmail(msg);
647                                 RecipientsPtr recipient = email->getBccRecipientsPtr();
648                                 JSObjectRef arrayValue = JSObjectMakeArray(context, 0, NULL, NULL);
649                                 if (NULL == arrayValue)
650                                 {
651                                         LoggerE("Could not create js array object");
652                                         return JSValueMakeUndefined(context);
653                                 }
654                 JSValueRef referr;
655                                 for(size_t i = 0; i < recipient->getRecipientSize(); i++)
656                                 {
657                     referr = NULL;
658                     JSObjectSetPropertyAtIndex(context, arrayValue, i,
659                             converter->toJSValueRef(recipient->getRecipient(i)), &referr);
660                     if(referr != NULL)
661                     {
662                         /* previously it was DPL ConversionException converted in
663                          * catch into TypeMismatch but it's more an internal error */
664                         throw DeviceAPI::Common::UnknownException("Could not insert value into js array");
665                     }
666                                 }
667                                 return arrayValue;
668                         }
669                         case SMS:
670                         case MMS:
671                         {
672                                 JSObjectRef arrayValue = JSObjectMakeArray(context, 0, NULL, NULL);
673                                 if (NULL == arrayValue)
674                                 {
675                                         LoggerE("Could not create js array object");
676                                         return JSValueMakeUndefined(context);
677                                 }
678                                 return arrayValue;
679                         }
680                         default:
681                 LoggerE("not supported message type");
682                 /* unsupported type -> type is integral message attribute
683                  * so error at this place means internal api error*/
684                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
685                 break;
686                 }
687         }
688     catch(BasePlatformException &bex) {
689         LoggerE("BCC addressess getting failure: " << bex.getMessage());
690         return JSValueMakeUndefined(context);
691     }
692     catch(...) {
693         LoggerE("UnknownError when getting BCC addressess.");
694         return JSValueMakeUndefined(context);
695     }
696 }
697
698 JSValueRef JSMessage::getBody(JSContextRef context,
699         JSObjectRef object,
700         JSStringRef propertyName,
701         JSValueRef* exception)
702 {
703     try {
704         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
705         IMessagePtr msg = converter->toIMessage(object);
706         string body = msg->getBody();
707         return converter->toJSValueRef(body);
708     }
709     catch(BasePlatformException &bex) {
710         /* Currently this exception will not be caught here as converter
711          * is using od-style, DPL exceptions. This catch sections
712          * is prepared for future converter refactoring */
713         LoggerE("Message body getting failure: " << bex.getMessage());
714         return JSValueMakeUndefined(context);
715     }
716     catch(...) {
717         LoggerE("UnknownError when getting Message body.");
718         return JSValueMakeUndefined(context);;
719     }
720 }
721
722 JSValueRef JSMessage::getCcAddress(JSContextRef context,
723         JSObjectRef object,
724         JSStringRef propertyName,
725         JSValueRef* exception)
726 {
727         LoggerD("enter");
728         try {
729                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
730                 IMessagePtr msg = converter->toIMessage(object);
731                 MessageType msgType = msg->getMessageType();
732
733                 switch (msgType)
734                 {
735                         case EMAIL:
736                         {
737                                 IEmailPtr email = MessageFactory::convertToEmail(msg);
738                                 RecipientsPtr recipient = email->getCcRecipientsPtr();
739                                 JSObjectRef arrayValue = JSObjectMakeArray(context, 0, NULL, NULL);
740                                 if (NULL == arrayValue)
741                                 {
742                                         LoggerE("Could not create js array object");
743                                         return JSValueMakeUndefined(context);
744                                 }
745                 JSValueRef referr;
746                                 for(size_t i = 0; i < recipient->getRecipientSize(); i++)
747                                 {
748                     referr = NULL;
749                     JSObjectSetPropertyAtIndex(context, arrayValue, i,
750                             converter->toJSValueRef(recipient->getRecipient(i)), &referr);
751                     if(referr != NULL)
752                         {
753                         /* previously it was DPL ConversionException converted in
754                          * catch into TypeMismatch but it's more an internal error */
755                         throw DeviceAPI::Common::UnknownException("Could not insert value into js array");
756                         }
757                                 }
758                                 return arrayValue;
759                         }
760                         case MMS:
761                         case SMS:
762                         {
763                                 JSObjectRef arrayValue = JSObjectMakeArray(context, 0, NULL, NULL);
764                                 if (NULL == arrayValue)
765                                 {
766                                         LoggerE("Could not create js array object");
767                                         return JSValueMakeUndefined(context);
768                                 }
769                                 return arrayValue;
770                         }
771                         default:
772                                 LoggerE("not supported message type");
773                 /* unsupported type -> type is integral message attribute
774                  * so error at this place means internal api error*/
775                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
776                 break;
777                 }
778     }
779     catch(BasePlatformException &bex) {
780         LoggerE("CC addressess getting failure: " << bex.getMessage());
781         return JSValueMakeUndefined(context);
782     }
783     catch(...) {
784         LoggerE("UnknownError when getting CC addressess.");
785         return JSValueMakeUndefined(context);
786     }
787 }
788
789 JSValueRef JSMessage::getDestinationAddress(JSContextRef context,
790         JSObjectRef object,
791         JSStringRef propertyName,
792         JSValueRef* exception)
793 {
794         LoggerI("enter");
795         try {
796                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
797                 IMessagePtr msg = converter->toIMessage(object);
798                 RecipientsPtr recipient = msg->getToRecipientsPtr();
799                 JSObjectRef arrayValue = JSObjectMakeArray(context, 0, NULL, NULL);
800                 if (NULL == arrayValue)
801                 {
802                         LoggerE("Could not create js array object");
803                         return JSValueMakeUndefined(context);
804                 }
805                 LoggerE("getRecipientSize() : " << recipient->getRecipientSize());
806         JSValueRef referr;
807                 for(size_t i = 0; i < recipient->getRecipientSize(); i++)
808                 {
809             referr = NULL;
810             JSObjectSetPropertyAtIndex(context, arrayValue, i, converter->toJSValueRef(recipient->getRecipient(i)), &referr);
811             if(referr != NULL)
812             {
813                 /* previously it was DPL ConversionException converted in
814                  * catch into TypeMismatch but it's more an internal error */
815                 throw DeviceAPI::Common::UnknownException("Could not insert value into js array");
816                         }
817                 }
818                 return arrayValue;
819         }
820     catch(BasePlatformException &bex) {
821         LoggerE("Destination addressess getting failure: " << bex.getMessage());
822         return JSValueMakeUndefined(context);
823     }
824     catch(...) {
825         LoggerE("UnknownError when getting destination addressess.");
826         return JSValueMakeUndefined(context);
827     }
828 }
829
830 JSValueRef JSMessage::getIsRead(JSContextRef context,
831         JSObjectRef object,
832         JSStringRef propertyName,
833         JSValueRef* exception)
834 {
835         try {
836                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
837                 IMessagePtr msg = converter->toIMessage(object);
838                 bool isRead = msg->isRead();
839                 return converter->toJSValueRef(isRead);
840         }
841     catch(BasePlatformException &bex) {
842         /* Currently this exception will not be caught here as converter
843          * is using od-style, DPL exceptions. This catch sections
844          * is prepared for future converter refactoring */
845         LoggerE("Checking if message is read failure: " << bex.getMessage());
846         return JSValueMakeUndefined(context);
847     }
848     catch(...) {
849         LoggerE("UnknownError when checking if message is read.");
850         return JSValueMakeUndefined(context);
851     }
852 }
853
854 JSValueRef JSMessage::getMessageId(JSContextRef context,
855         JSObjectRef object,
856         JSStringRef propertyName,
857         JSValueRef* exception)
858 {
859         try {
860                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
861                 IMessagePtr msg = converter->toIMessage(object);
862                 const string& id = msg->getIdRef();
863                 LoggerD("msgId=" << id);
864                 LoggerD("msg->getMessageStatus()=" << msg->getMessageStatus());
865                 if((id.size() == 0) && msg->getMessageStatus() == MESSAGE_STATUS_CREATED)
866                 {
867                         return JSValueMakeNull(context);
868                 }
869                 return converter->toJSValueRef(id);
870         }
871     catch(BasePlatformException &bex) {
872         /* Currently this exception will not be caught here as converter
873          * is using od-style, DPL exceptions. This catch sections
874          * is prepared for future converter refactoring */
875         LoggerE("Getting message id failure: " << bex.getMessage());
876         return JSValueMakeUndefined(context);
877     }
878     catch(...) {
879         LoggerE("UnknownError when getting message id.");
880         return JSValueMakeUndefined(context);
881     }
882 }
883
884 JSValueRef JSMessage::getMessagePriority(JSContextRef context,
885         JSObjectRef object,
886         JSStringRef propertyName,
887         JSValueRef* exception)
888 {
889         try {
890                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
891                 IMessagePtr msg = converter->toIMessage(object);
892                 MessageType msgType = msg->getMessageType();
893
894                 switch (msgType) {
895                         case SMS:
896                         case MMS:
897                                 LoggerE("priority : false");
898                                 return converter->toJSValueRef(false);
899                         case EMAIL:
900                         {
901                                 IEmailPtr email = MessageFactory::convertToEmail(msg);
902                                 int tmpint = email->getPriority();
903                                 LoggerE("priority : " << tmpint);
904                                 if(tmpint == MessagePriority::HIGH)
905                                 {
906                                         return converter->toJSValueRef(true);
907                                 }
908                                 return converter->toJSValueRef(false);
909                         }
910                         default:
911                                 LoggerE("unsupported message type");
912                 /* unsupported type -> type is integral message attribute
913                  * so error at this place means internal api error*/
914                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
915                 break;
916                 }
917         }
918     catch(BasePlatformException &bex) {
919         LoggerE("Getting message priority failure: " << bex.getMessage());
920         return JSValueMakeUndefined(context);
921     }
922     catch(...) {
923         LoggerE("UnknownError when getting message priority.");
924         return JSValueMakeUndefined(context);
925     }
926 }
927
928 JSValueRef JSMessage::getMessageType(JSContextRef context,
929         JSObjectRef object,
930         JSStringRef propertyName,
931         JSValueRef* exception)
932 {
933         try {
934                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
935                 IMessagePtr msg = converter->toIMessage(object);
936                 MessageType msgType = msg->getMessageType();
937
938                 return converter->toJSValueRef(converter->toMessageType(msgType));
939         }
940     catch(BasePlatformException &bex) {
941         /* Currently this exception will not be caught here as converter
942          * is using od-style, DPL exceptions. This catch sections
943          * is prepared for future converter refactoring */
944         LoggerE("Getting message type failure: " << bex.getMessage());
945         return JSValueMakeUndefined(context);
946     }
947     catch(...) {
948         LoggerE("UnknownError when getting message type.");
949         return JSValueMakeUndefined(context);
950     }
951 }
952
953 JSValueRef JSMessage::getSourceAddress(JSContextRef context,
954         JSObjectRef object,
955         JSStringRef propertyName,
956         JSValueRef* exception)
957 {
958         try {
959                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
960                 IMessagePtr msg = converter->toIMessage(object);
961                 string from = msg->getSourceAddress();
962                 LoggerD("from =" << from);
963                 if(from.size() == 0)
964                 {
965                         LoggerD("JSValueMakeNull");
966                         return JSValueMakeNull(context);
967                 }
968                 return converter->toJSValueRef(from);
969         }
970     catch(BasePlatformException &bex) {
971         /* Currently this exception will not be caught here as converter
972          * is using od-style, DPL exceptions. This catch sections
973          * is prepared for future converter refactoring */
974         LoggerE("Getting message source address failure: " << bex.getMessage());
975         return JSValueMakeUndefined(context);
976     }
977     catch(...) {
978         LoggerE("UnknownError when getting message source address.");
979         return JSValueMakeUndefined(context);
980     }
981 }
982
983 JSValueRef JSMessage::getSubject(JSContextRef context,
984         JSObjectRef object,
985         JSStringRef propertyName,
986         JSValueRef* exception)
987 {
988         try {
989                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
990                 IMessagePtr msg = converter->toIMessage(object);
991                 MessageType msgType = msg->getMessageType();
992
993                 switch (msgType) {
994                         case MMS:
995                         {
996                                 IMmsPtr mms = MessageFactory::convertToMms(msg);
997                                 //JSValueMakeUndefined
998                                 return converter->toJSValueRef(mms->getSubject());
999                         }
1000                         case EMAIL:
1001                         {
1002                                 IEmailPtr email = MessageFactory::convertToEmail(msg);
1003                                 return converter->toJSValueRef(email->getSubject());
1004                         }
1005                         case SMS:
1006                                 return converter->toJSValueRef("");
1007                         default:
1008                                 LoggerE("message not support subject");
1009                 /* unsupported type -> type is integral message attribute
1010                  * so error at this place means internal api error*/
1011                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
1012                 break;
1013                 }
1014         }
1015     catch(BasePlatformException &bex) {
1016         LoggerE("Getting message subject failure: " << bex.getMessage());
1017         return JSValueMakeUndefined(context);
1018     }
1019     catch(...) {
1020         LoggerE("UnknownError when getting message subject.");
1021         return JSValueMakeUndefined(context);
1022     }
1023 }
1024
1025 JSValueRef JSMessage::getTime(JSContextRef context,
1026         JSObjectRef object,
1027         JSStringRef propertyName,
1028         JSValueRef* exception)
1029 {
1030         try {
1031                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1032                 IMessagePtr msg = converter->toIMessage(object);
1033                 struct tm dateTime = msg->getDateTime();
1034                 const string& id = msg->getIdRef();
1035                 LoggerD("msg->getMessageStatus()=" << msg->getMessageStatus());
1036                 LoggerI("id.size() : " << id.size());
1037                 if((id.size() == 0) && msg->getMessageStatus() == MESSAGE_STATUS_CREATED)
1038                 {
1039                         LoggerI("JSValueMakeNull");
1040                         return JSValueMakeNull(context);
1041                 }
1042                 return converter->toJSValueRef(dateTime);
1043         }
1044     catch(BasePlatformException &bex) {
1045         /* Currently this exception will not be caught here as converter
1046          * is using od-style, DPL exceptions. This catch sections
1047          * is prepared for future converter refactoring */
1048         LoggerE("Getting message time failure: " << bex.getMessage());
1049         return JSValueMakeUndefined(context);
1050     }
1051     catch(...) {
1052         LoggerE("UnknownError when getting message time.");
1053         return JSValueMakeUndefined(context);
1054     }
1055 }
1056
1057 JSValueRef JSMessage::getFolder(JSContextRef context,
1058         JSObjectRef object,
1059         JSStringRef propertyName,
1060         JSValueRef* exception)
1061 {
1062         try {
1063                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1064                 IMessagePtr msg = converter->toIMessage(object);
1065                 FolderType folder = msg->getCurrentFolder();
1066
1067                 int tmpInt = msg->getMessageStatus();
1068
1069                 LoggerE("folder : " << folder);
1070                 LoggerE("msg->getMessageStatus() : " << tmpInt);
1071
1072                 if(tmpInt == MESSAGE_STATUS_CREATED)
1073                 {
1074                         return JSValueMakeNull(context);
1075                 }
1076
1077                 tmpInt = (int)folder;
1078                 return converter->toJSValueRef(converter->convertIntToString(tmpInt));
1079     }
1080     catch(BasePlatformException &bex) {
1081         /* Currently this exception will not be caught here as converter
1082          * is using od-style, DPL exceptions. This catch sections
1083          * is prepared for future converter refactoring */
1084         LoggerE("Getting message folder failure: " << bex.getMessage());
1085         return JSValueMakeUndefined(context);
1086     }
1087     catch(...) {
1088         LoggerE("UnknownError when getting message folder.");
1089         return JSValueMakeUndefined(context);
1090     }
1091 }
1092
1093 JSValueRef JSMessage::getMessageBody(JSContextRef context,
1094         JSObjectRef object,
1095         JSStringRef propertyName,
1096         JSValueRef* exception)
1097 {
1098         LoggerI("getMessageBody");
1099
1100         try {
1101         GlobalContextManager *gcm = GlobalContextManager::getInstance();
1102         if(gcm == NULL) {
1103             LoggerE("Failed to get GlobalContextManager");
1104             throw DeviceAPI::Common::UnknownException("Unable to receive global context");
1105         }
1106                 JSContextRef globalContext = gcm->getGlobalContext(context);
1107
1108                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext);
1109                 IMessagePtr msg = converter->toIMessage(object);        //get message point
1110
1111                 return JSMessageBody::createJS(globalContext, msg);
1112         }
1113     catch(BasePlatformException &bex) {
1114         LoggerE("Getting message body failure: " << bex.getMessage());
1115         return JSValueMakeUndefined(context);
1116     }
1117     catch(...) {
1118         LoggerE("UnknownError when getting message body.");
1119         return JSValueMakeUndefined(context);
1120     }
1121 }
1122
1123 JSValueRef JSMessage::getAccountID(JSContextRef context,
1124         JSObjectRef object,
1125         JSStringRef propertyName,
1126         JSValueRef* exception)
1127 {
1128         LoggerI("getAccountID");
1129
1130     try {
1131         GlobalContextManager *gcm = GlobalContextManager::getInstance();
1132         if(gcm == NULL) {
1133             LoggerE("Failed to get GlobalContextManager");
1134             throw DeviceAPI::Common::UnknownException("Unable to receive global context");
1135         }
1136         JSContextRef globalContext = gcm->getGlobalContext(context);
1137
1138                 ConverterMessageFactory::ConverterType converter =  ConverterMessageFactory::getConverter(globalContext);
1139
1140                 IMessagePtr msg = converter->toIMessage(object);        //get message point
1141
1142                 LoggerI("create JS");
1143                 //getAccountID
1144                 if (msg->getMessageType() == EMAIL)
1145                 {
1146                         IEmailPtr email = MessageFactory::convertToEmail(msg);
1147                         if(email->getAccountID() == -1)
1148                         {
1149                                 return JSValueMakeNull(context);
1150                         }
1151                         std::stringstream stream;
1152                         stream << email->getAccountID();
1153                         if (stream.fail()) {
1154                                 throw DeviceAPI::Common::UnknownException("Couldn't convert e-mail account id");
1155                         }
1156
1157                         return converter->toJSValueRef(stream.str());
1158                 }
1159                 else if (msg->getMessageType() == SMS)
1160                 {
1161                         std::stringstream stream;
1162                         stream << SMS_ACCOUNT_ID;
1163                         if (stream.fail()) {
1164                 throw DeviceAPI::Common::UnknownException("Couldn't convert sms account id");
1165                         }
1166
1167                         return converter->toJSValueRef(stream.str());
1168                 }
1169                 else if (msg->getMessageType() == MMS)
1170                 {
1171                         std::stringstream stream;
1172                         stream << MMS_ACCOUNT_ID;
1173                         if (stream.fail()) {
1174                 throw DeviceAPI::Common::UnknownException("Couldn't convert mms account id");
1175                         }
1176
1177                         return converter->toJSValueRef(stream.str());
1178                 }
1179                 else
1180                 {
1181                         return JSValueMakeUndefined(context);
1182                 }
1183         }
1184     catch(BasePlatformException &bex) {
1185         LoggerE("Getting account id failure: " << bex.getMessage());
1186         return JSValueMakeUndefined(context);
1187     }
1188     catch(...) {
1189         LoggerE("UnknownError when getting account id.");
1190         return JSValueMakeUndefined(context);
1191     }
1192 }
1193
1194 JSValueRef JSMessage::getUID(JSContextRef context,
1195                         JSObjectRef object,
1196                         JSStringRef propertyName,
1197                         JSValueRef* exception)
1198 {
1199         LoggerI("getUID");
1200
1201     try {
1202         GlobalContextManager *gcm = GlobalContextManager::getInstance();
1203         if(gcm == NULL) {
1204             LoggerE("Failed to get GlobalContextManager");
1205             throw DeviceAPI::Common::UnknownException("Unable to receive global context");
1206         }
1207         JSContextRef globalContext = gcm->getGlobalContext(context);
1208
1209                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext);
1210
1211                 IMessagePtr msg = converter->toIMessage(object); //get message point
1212
1213                 LoggerI("create JS");
1214                 //getAccountID
1215                 if (msg->getMessageType() == EMAIL)
1216                 {
1217                         IEmailPtr email = MessageFactory::convertToEmail(msg);
1218                         return converter->toJSValueRef(email->getUID());
1219                 }
1220                 else
1221                 {
1222                         return JSValueMakeUndefined(context);
1223                 }
1224         }
1225     catch(BasePlatformException &bex) {
1226         LoggerE("Getting account id failure: " << bex.getMessage());
1227         return JSValueMakeUndefined(context);
1228     }
1229     catch(...) {
1230         LoggerE("UnknownError when getting account id.");
1231         return JSValueMakeUndefined(context);
1232     }
1233 }
1234
1235 bool JSMessage::setAttachments(JSContextRef context,
1236         JSObjectRef object,
1237         JSStringRef propertyName,
1238         JSValueRef value,
1239         JSValueRef* exception)
1240 {
1241         LoggerI("enter");
1242
1243     try {
1244         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1245
1246                 vector<IAttachmentPtr> attachments;
1247                 if (JSIsArrayValue(context, value)) {
1248                         JSObjectRef valueObj = converter->toJSObjectRef(value);
1249                         unsigned int len = JSGetArrayLength(context, valueObj);
1250                         LoggerD("Array Length = " << len);
1251                         for (unsigned int i = 0; i < len; ++i)
1252                         {
1253                                 JSValueRef att = JSObjectGetPropertyAtIndex(context, valueObj, i, NULL);
1254                                 if (JSValueIsUndefined(context, att) || JSValueIsNull(context, att)) {
1255                                         LoggerW("Invalid array element. Skipping.");
1256                                         continue;
1257                                 }
1258                                 IAttachmentPtr attachment = converter->toIAttachment(att);
1259
1260                                 if(attachment->getIsValidAttachment() == false)
1261                                 {
1262                     LoggerD("invalid attachment : " << i);
1263                     throw DeviceAPI::Common::InvalidValuesException("Invalid attachment in array");
1264                                 }
1265
1266                                 LoggerD("Adding attachment , shotname: " << attachment->getShortName());
1267                                 attachments.push_back(attachment);
1268                         }
1269                 }
1270                 else
1271                 {
1272                         IAttachmentPtr attachment = converter->toIAttachment(value);
1273                         LoggerD("Adding attachment , shotname: " << attachment->getShortName());
1274                         if(attachment->getIsValidAttachment() == false)
1275                         {
1276                                 LoggerD("invalid attachment");
1277                                 throw DeviceAPI::Common::InvalidValuesException("Invalid attachments array");
1278                         }
1279                         attachments.push_back(attachment);
1280                 }
1281
1282                 LoggerD("Attachment Size =" << attachments.size());
1283                 if ( attachments.size() > 0)
1284                 {
1285                         IMessagePtr msg = converter->toIMessage(object);
1286                         if (msg)
1287                         {
1288                                 MessageType msgType = msg->getMessageType();
1289                                 switch (msgType) {
1290                                         case MMS:
1291                                         {
1292                                                 IMmsPtr mms = MessageFactory::convertToMms(msg);
1293                                                 mms->setAttachments(attachments);
1294                                                 break;
1295                                         }
1296                                         case EMAIL:
1297                                         {
1298                                                 IEmailPtr email = MessageFactory::convertToEmail(msg);
1299                                                 email->setAttachments(attachments);
1300                                                 break;
1301                                         }
1302                                         default:
1303                         LoggerE("not supported message type");
1304                         throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
1305                         break;
1306                                 }
1307                                 return true;
1308                         }
1309                         else
1310                         {
1311                                 LoggerE("Message converter failed...");
1312                                 throw DeviceAPI::Common::UnknownException("Message conversion failure");
1313                         }
1314                 }
1315         }
1316     catch(BasePlatformException &bex) {
1317         LoggerE("Setting attachments failed: " << bex.getMessage());
1318         return true;
1319     }
1320     catch(...) {
1321         LoggerE("UnknownError when setting attachments.");
1322         return true;
1323     }
1324     return true;
1325 }
1326
1327
1328 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
1329 bool JSMessage::setAttachments(JSContextRef context,
1330         JSObjectRef object,
1331         JSStringRef propertyName,
1332         JSValueRef value,
1333         JSValueRef* exception)
1334 {
1335     LoggerI("enter");
1336
1337     Try
1338     {
1339         JSMessagePrivateObject* priv =
1340             static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
1341
1342         if (!priv) {
1343             return JSTizenExceptionFactory::postException(context, exception,
1344                            JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1345         }
1346     }
1347     Catch(WrtDeviceApis::Commons::NullPointerException) {
1348         LoggerE("Error on pointer, null value");
1349            return JSTizenExceptionFactory::postException(context, exception,
1350                            JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1351     }
1352
1353     vector<string> attachments;
1354     Try
1355     {
1356         ConverterMessageFactory::ConverterType converter =
1357             ConverterMessageFactory::getConverter(context);
1358         if (JSIsArrayValue(context, value)) {
1359             JSObjectRef valueObj = converter->toJSObjectRef(value);
1360
1361             // extract path from each JSFile object
1362             unsigned int len = JSGetArrayLength(context, valueObj);
1363             for (unsigned int i = 0; i < len; ++i) {
1364                 JSValueRef att = JSGetArrayElement(context, valueObj, i);
1365                 if (JSValueIsUndefined(context,
1366                                        att) || JSValueIsNull(context, att)) {
1367                     LoggerW("Invalid array element. Skipping.");
1368                     continue;
1369                 }
1370                 DeviceAPI::Filesystem::INodePtr node = converter->toFilesystemNode(
1371                         att);
1372
1373                 LoggerD("Adding attachment: " << node->getPath()->getFullPath());
1374                 attachments.push_back(node->getPath()->getFullPath());
1375             }
1376         } else {
1377             LoggerW("Invalid or null element passed as attachment array." <<
1378                        "Setting empty vector.");
1379         }
1380
1381         // set up new attachments list
1382         IMessagePtr msg = converter->toIMessage(object);
1383         MessageType msgType = msg->getMessageType();
1384         switch (msgType) {
1385         case MMS:
1386         {
1387             IMmsPtr mms = MessageFactory::convertToMms(msg);
1388             mms->setAttachments(attachments, true);
1389             break;
1390         }
1391         case EMAIL:
1392         {
1393             IEmailPtr email = MessageFactory::convertToEmail(msg);
1394             email->setAttachments(attachments, true);
1395             break;
1396         }
1397         case SMS:
1398
1399             return false; // ignore
1400
1401         default:
1402             LoggerE("not supported message type");
1403             Throw(WrtDeviceApis::Commons::InvalidArgumentException);
1404         }
1405         return true;
1406     }
1407     Catch(WrtDeviceApis::Commons::ConversionException) {
1408         LoggerE("Error on conversion");
1409           return JSTizenExceptionFactory::postException(context, exception,
1410                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1411     }
1412     Catch(WrtDeviceApis::Commons::NullPointerException) {
1413         LoggerE("Error on pointer, null value");
1414           return JSTizenExceptionFactory::postException(context, exception,
1415                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1416     }
1417     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
1418         LoggerE("Invalid argument");
1419            return JSTizenExceptionFactory::postException(context, exception,
1420                            JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1421     }
1422     return false;
1423 }
1424 #endif
1425
1426 bool JSMessage::setBccAddress(JSContextRef context,
1427         JSObjectRef object,
1428         JSStringRef propertyName,
1429         JSValueRef value,
1430         JSValueRef* exception)
1431 {
1432         try {
1433                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1434                 IMessagePtr msg = converter->toIMessage(object);
1435                 Recipients bcc = converter->toRecipients(value);
1436                 LoggerD("setting bcc field, size=" << bcc.getRecipientSize());
1437
1438                 MessageType msgType = msg->getMessageType();
1439                 switch (msgType) {
1440                         case SMS:
1441                         case MMS:
1442                                 return false;   // ignore
1443
1444                         case EMAIL:
1445                         {
1446                                 IEmailPtr email = MessageFactory::convertToEmail(msg);
1447                                 email->setBccRecipients(bcc);
1448                                 break;
1449                         }
1450                         default:
1451                 LoggerE("not supported message type");
1452                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
1453                 break;
1454                 }
1455                 return true;
1456         }
1457     catch(BasePlatformException &bex) {
1458         LoggerE("Setting BCC addresses failed: " << bex.getMessage());
1459         return true;
1460     }
1461     catch(...) {
1462         LoggerE("UnknownError when setting BCC addresses.");
1463         return true;
1464     }
1465     return true;
1466 }
1467
1468 bool JSMessage::setBody(JSContextRef context,
1469         JSObjectRef object,
1470         JSStringRef propertyName,
1471         JSValueRef value,
1472         JSValueRef* exception)
1473 {
1474         try {
1475                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1476                 IMessagePtr msg = converter->toIMessage(object);
1477                 string body = converter->toString(value);
1478                 msg->setBody(body);
1479                 return true;
1480         }
1481     catch(BasePlatformException &bex) {
1482         /* Currently this exception will not be caught here as converter
1483          * is using od-style, DPL exceptions. This catch sections
1484          * is prepared for future converter refactoring */
1485         LoggerE("Setting BCC addresses failed: " << bex.getMessage());
1486         return true;
1487     }
1488     catch(...) {
1489         LoggerE("UnknownError when setting BCC addresses.");
1490         return true;
1491     }
1492         return true;
1493 }
1494
1495 bool JSMessage::setCcAddress(JSContextRef context,
1496         JSObjectRef object,
1497         JSStringRef propertyName,
1498         JSValueRef value,
1499         JSValueRef* exception)
1500 {
1501         try {
1502                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1503                 IMessagePtr msg = converter->toIMessage(object);
1504                 Recipients cc = converter->toRecipients(value);
1505                 LoggerD("setting cc field, size=" << cc.getRecipientSize());
1506
1507                 MessageType msgType = msg->getMessageType();
1508                 switch (msgType) {
1509                         case SMS:
1510                         case MMS:
1511                                 return true;   // ignore
1512                         case EMAIL:
1513                         {
1514                                 IEmailPtr email = MessageFactory::convertToEmail(msg);
1515                                 email->setCcRecipients(cc);
1516                                 break;
1517                         }
1518                         default:
1519                 LoggerE("unsuported message type");
1520                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
1521                 break;
1522         }
1523         return true;
1524         }
1525     catch(BasePlatformException &bex) {
1526         LoggerE("Setting CC addresses failed: " << bex.getMessage());
1527         return true;
1528     }
1529     catch(...) {
1530         LoggerE("UnknownError when setting CC addresses.");
1531         return true;
1532     }
1533         return true;
1534 }
1535
1536 bool JSMessage::setDestinationAddress(JSContextRef context,
1537         JSObjectRef object,
1538         JSStringRef propertyName,
1539         JSValueRef value,
1540         JSValueRef * exception)
1541 {
1542         LoggerI("enter");
1543         try
1544         {
1545                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1546                 IMessagePtr msg = converter->toIMessage(object);
1547                 Recipients to = converter->toRecipients(value);
1548                 LoggerD("setting to field, size=" << to.getRecipientSize());
1549                 msg->setToRecipients(to);
1550                 return true;
1551         }
1552     catch(BasePlatformException &bex) {
1553         /* Currently this exception will not be caught here as converter
1554          * is using od-style, DPL exceptions. This catch sections
1555          * is prepared for future converter refactoring */
1556         LoggerE("Setting destination addresses failed: " << bex.getMessage());
1557         return true;
1558     }
1559     catch(...) {
1560         LoggerE("UnknownError when setting destination addresses.");
1561         return true;
1562     }
1563     return true;
1564 }
1565
1566 bool JSMessage::setIsRead(JSContextRef context,
1567         JSObjectRef object,
1568         JSStringRef propertyName,
1569         JSValueRef value,
1570         JSValueRef * exception)
1571 {
1572         try {
1573                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1574                 IMessagePtr msg = converter->toIMessage(object);
1575                 msg->setReadStatus(converter->toBool(value));
1576                 msg->setisReadChangeStatus(converter->toBool(value));
1577                 return true;
1578         }
1579     catch(BasePlatformException &bex) {
1580         /* Currently this exception will not be caught here as converter
1581          * is using od-style, DPL exceptions. This catch sections
1582          * is prepared for future converter refactoring */
1583         LoggerE("Setting destination addresses failed: " << bex.getMessage());
1584         return true;
1585     }
1586     catch(...) {
1587         LoggerE("UnknownError when setting destination addresses.");
1588         return true;
1589     }
1590     return true;
1591 }
1592
1593 bool JSMessage::setMessagePriority(JSContextRef context,
1594         JSObjectRef object,
1595         JSStringRef propertyName,
1596         JSValueRef value,
1597         JSValueRef * exception)
1598 {
1599         Try
1600         {
1601                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1602                 IMessagePtr msg = converter->toIMessage(object);
1603                 MessageType msgType = msg->getMessageType();
1604                 switch (msgType) {
1605                         case MMS:
1606                         case SMS:
1607                 // do not set value but show that this attribute is supported in object
1608                 return true;
1609                         case EMAIL:
1610                 msg->setPriority(converter->toMessagePriority(value));
1611                 break;
1612
1613                         default:
1614                 LoggerE("unsuported message type");
1615                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
1616                 break;
1617                 }
1618                 return true;
1619         }
1620     catch(BasePlatformException &bex) {
1621         LoggerE("Setting message priority failed: " << bex.getMessage());
1622         return true;
1623     }
1624     catch(...) {
1625         LoggerE("UnknownError when setting message priority.");
1626         return true;
1627     }
1628     return true;
1629 }
1630
1631 bool JSMessage::setSubject(JSContextRef context,
1632         JSObjectRef object,
1633         JSStringRef propertyName,
1634         JSValueRef value,
1635         JSValueRef * exception)
1636 {
1637         try {
1638                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1639                 IMessagePtr msg = converter->toIMessage(object);
1640                 MessageType msgType = msg->getMessageType();
1641                 string subject = converter->toString(value);
1642                 switch (msgType) {
1643                         case MMS:
1644                         {
1645                                 IMmsPtr mms = MessageFactory::convertToMms(msg);
1646                                 mms->setSubject(subject);
1647                                 break;
1648                         }
1649                         case EMAIL:
1650                         {
1651                                 IEmailPtr email = MessageFactory::convertToEmail(msg);
1652                                 email->setSubject(subject);
1653                                 break;
1654                         }
1655                         case SMS:
1656                                 return true;   // ignore
1657
1658                         default:
1659                                 LoggerE("message not supported");
1660                 throw DeviceAPI::Common::NotSupportedException("Unsupported message type");
1661                 break;
1662                 }
1663                 return true;
1664         }
1665     catch(BasePlatformException &bex) {
1666         LoggerE("Setting message priority failed: " << bex.getMessage());
1667         return true;
1668     }
1669     catch(...) {
1670         LoggerE("UnknownError when setting message priority.");
1671         return true;
1672     }
1673     return true;
1674 }
1675
1676 bool JSMessage::setMessageBody(JSContextRef context,
1677         JSObjectRef object,
1678         JSStringRef propertyName,
1679         JSValueRef value,
1680         JSValueRef* exception)
1681 {
1682     return true;
1683 }
1684
1685 JSValueRef JSMessage::getConversationId(JSContextRef context,
1686         JSObjectRef object,
1687         JSStringRef propertyName,
1688         JSValueRef* exception)
1689 {
1690         LoggerD("getConversationId");
1691
1692         try {
1693                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1694                 int convId = 0;
1695                 IMessagePtr msg = converter->toIMessage(object);
1696
1697                 LoggerD("msg->getMessageStatus()=" << msg->getMessageStatus());
1698                 LoggerD("msg.getConvId()=" << msg->getConvId());
1699                 convId = msg->getConvId();
1700                 if((msg->getConvId() == -1) && msg->getMessageStatus() == MESSAGE_STATUS_CREATED)
1701                 {
1702                         return JSValueMakeNull(context);
1703                 }
1704                 return converter->toJSValueRef(converter->convertIntToString(convId));
1705         }
1706     catch(BasePlatformException &bex) {
1707         /* Currently this exception will not be caught here as converter
1708          * is using od-style, DPL exceptions. This catch sections
1709          * is prepared for future converter refactoring */
1710         LoggerE("Getting conversation id failure: " << bex.getMessage());
1711         return JSValueMakeUndefined(context);
1712     }
1713     catch(...) {
1714         LoggerE("UnknownError when getting conversation id.");
1715         return JSValueMakeUndefined(context);
1716     }
1717 }
1718
1719 JSValueRef JSMessage::getInResponseTo(JSContextRef context,
1720                 JSObjectRef object,
1721                 JSStringRef propertyName,
1722                 JSValueRef* exception)
1723 {
1724         LoggerD("getInResponseTo");
1725
1726     try {
1727                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1728                 int convId = -1;
1729                 int msgId = 0;
1730                 IMessagePtr msg = converter->toIMessage(object);
1731
1732                 if (msg->getMessageType() != EMAIL)
1733                 {
1734                         LoggerD("not EMAIL type return empty string");
1735                         return converter->toJSValueRef("");
1736                 }
1737
1738                 IEmailPtr email = MessageFactory::convertToEmail(msg);
1739                 msgId = email->getUID();
1740
1741                 LoggerD("msg.getConvId()=" << msg->getConvId());
1742                 convId = msg->getConvId();
1743
1744                 if (convId == msgId)
1745                 {
1746                         LoggerD("Not forwared and replied, return empty string");
1747                         return converter->toJSValueRef("");
1748                 }
1749
1750                 return converter->toJSValueRef(converter->convertIntToString(convId));
1751         }
1752     catch(BasePlatformException &bex) {
1753         /* Currently this exception will not be caught here as converter
1754          * is using od-style, DPL exceptions. This catch sections
1755          * is prepared for future converter refactoring */
1756         LoggerE("Getting in response to failure: " << bex.getMessage());
1757         return JSValueMakeUndefined(context);
1758     }
1759     catch(...) {
1760         LoggerE("UnknownError when getting in response to.");
1761         return JSValueMakeUndefined(context);
1762     }
1763 }
1764
1765 JSValueRef JSMessage::getMessageStatus(JSContextRef context,
1766                         JSObjectRef object,
1767                         JSStringRef propertyName,
1768                         JSValueRef* exception)
1769 {
1770         LoggerI("getMessageStatus");
1771
1772         try
1773         {
1774                 ConverterMessageFactory::ConverterType converter =  ConverterMessageFactory::getConverter(context);
1775
1776                 IMessagePtr msg = converter->toIMessage(object); //get message point
1777
1778                 LoggerI("create JS");
1779                 return converter->toJSValueRef(converter->toMessageStatusType(msg->getMessageStatus()));
1780         }
1781     catch(BasePlatformException &bex) {
1782         /* Currently this exception will not be caught here as converter
1783          * is using od-style, DPL exceptions. This catch sections
1784          * is prepared for future converter refactoring */
1785         LoggerE("Getting message status failure: " << bex.getMessage());
1786         return JSValueMakeUndefined(context);
1787     }
1788     catch(...) {
1789         LoggerE("UnknownError when getting message status.");
1790         return JSValueMakeUndefined(context);
1791     }
1792 }
1793
1794
1795 JSValueRef JSMessage::hasAttachment(JSContextRef context,
1796                         JSObjectRef object,
1797                         JSStringRef propertyName,
1798                         JSValueRef* exception)
1799 {
1800         LoggerI("hasAttachment");
1801
1802         Try {
1803         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1804
1805                 IMessagePtr msg = converter->toIMessage(object); //get message point
1806
1807                 bool hasAttachmentFlag = false;
1808
1809                 if ( msg->getMessageType() == EMAIL)
1810                 {
1811                         IEmailPtr email = MessageFactory::convertToEmail(msg);
1812
1813                         if (email->hasAttachment())
1814                                 hasAttachmentFlag = true;
1815                 }
1816                 else if  ( msg->getMessageType() == MMS)
1817                 {
1818                         IMmsPtr mms = MessageFactory::convertToMms(msg);
1819
1820                         if (mms->hasAttachment())
1821                                 hasAttachmentFlag = true;
1822                 }
1823                 else
1824                 {
1825                         hasAttachmentFlag = false;
1826                 }
1827                 return converter->toJSValueRef(hasAttachmentFlag);
1828         }
1829     catch(BasePlatformException &bex) {
1830         /* Currently this exception will not be caught here as converter
1831          * is using od-style, DPL exceptions. This catch sections
1832          * is prepared for future converter refactoring */
1833         LoggerE("Getting hasAttachment flag failure: " << bex.getMessage());
1834         return JSValueMakeUndefined(context);
1835     }
1836     catch(...) {
1837         LoggerE("UnknownError when getting hasAttachment flag.");
1838         return JSValueMakeUndefined(context);
1839     }
1840 }
1841
1842 // Utility functions.
1843 MessageType JSMessage::stringToMessageType(std::string type)
1844 {
1845     MessageType retval = UNKNOWN;
1846     if (type.compare("messaging.sms") == 0) {
1847         retval = SMS;
1848     }
1849     else if (type.compare("messaging.mms") == 0) {
1850         retval = MMS;
1851     }
1852     else if (type.compare("messaging.email") == 0) {
1853         retval = EMAIL;
1854     }
1855     else{
1856         throw DeviceAPI::Common::TypeMismatchException("Invalid MessageServiceTag");
1857     }
1858     return retval;
1859 }
1860
1861 }
1862 }