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