8ac4798c0c950a9dc2d89c71ac1575c111c0d9b3
[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 "MessagingErrorMsg.h"
45 #include "JSMessageAttachment.h"
46 #include "plugin_config.h"
47
48 using namespace std;
49 using namespace TizenApis::Api::Messaging;
50 using namespace TizenApis::Commons;
51 using namespace WrtDeviceApis::Commons;
52 using namespace WrtDeviceApis::CommonsJavaScript;
53
54 namespace TizenApis {
55 namespace Tizen1_0 {
56 JSClassRef JSMessage::m_jsClassRef = NULL;
57
58 JSClassDefinition JSMessage::m_classInfo = {
59     0,
60     kJSClassAttributeNone,
61     "Message",
62     0,
63     m_property,
64     NULL,               //m_function
65     initialize,
66     finalize,
67     NULL, //hasProperty,
68     NULL, //getProperty,
69     NULL, //setProperty,
70     NULL, //deleteProperty,
71     NULL, //getPropertyNames,
72     NULL, //callAsFunction,
73     NULL, //callAsConstructor,
74     NULL, //hasInstance,
75     NULL, //convertToType,
76 };
77
78 // JSMessage properties
79 JSStaticValue JSMessage::m_property[] = {
80     { "id", getMessageId, NULL, kJSPropertyAttributeReadOnly },
81     { "serviceId", getAccountID, NULL, kJSPropertyAttributeReadOnly},
82     { "conversationId", getConversationId, NULL, kJSPropertyAttributeReadOnly },
83     { "folderId", getFolder, NULL, kJSPropertyAttributeReadOnly },
84     { "type", getMessageType, NULL, kJSPropertyAttributeReadOnly },
85     { "timestamp", getTime, NULL, kJSPropertyAttributeReadOnly },
86     { "from", getSourceAddress, NULL, kJSPropertyAttributeReadOnly },
87     { "to", getDestinationAddress, setDestinationAddress, kJSPropertyAttributeNone },
88     { "cc", getCcAddress, setCcAddress, kJSPropertyAttributeNone },
89     { "bcc", getBccAddress, setBccAddress, kJSPropertyAttributeNone },
90     { "body", getMessageBody, setMessageBody, kJSPropertyAttributeNone },
91     { "isRead", getIsRead, setIsRead, kJSPropertyAttributeNone },
92     //{ "hasAttachment", getAttachmentExistence, NULL, kJSPropertyAttributeReadOnly },
93     { "priority", getMessagePriority, setMessagePriority, kJSPropertyAttributeNone },
94     { "subject", getSubject, setSubject, kJSPropertyAttributeNone },
95     { "inResponseTo", getInResponseTo, NULL, kJSPropertyAttributeNone },
96     { "messageStatus", getMessageStatus, NULL, kJSPropertyAttributeReadOnly},
97     { "attachments", getAttachments, NULL, kJSPropertyAttributeReadOnly },
98     //{ "uid", getUID, NULL, kJSPropertyAttributeReadOnly},
99     { 0, 0, 0, 0 }
100 };
101
102 JSValueRef JSMessage::createJSObject(JSContextRef context,
103                 Api::Messaging::EmailAccountInfo& account,
104                 Api::Messaging::MessageType msgType,
105                 const std::string& msgId)
106 {
107         Api::Messaging::IMessagePtr msg;
108         LogDebug("createJSObject with account ");
109
110         Try
111         {
112                 if (msgType == Api::Messaging::EMAIL) {
113                     LogDebug("Account Address:" << &account);
114                     LogDebug("Account Data ,ID" << account.getId() << "name:" << account.getName() << " Account:" << account.getAddress());
115
116                     msg = Api::Messaging::MessageFactory::createMessage(msgType, account, msgId);
117                     if (!msg)
118                                 return JSValueMakeUndefined(context);
119                 } else {
120                     Throw(WrtDeviceApis::Commons::UnknownException);   // unsupported type
121                 }
122         }
123         Catch(WrtDeviceApis::Commons::UnknownException) {
124             LogError("wrong message type, object not created");
125             return JSValueMakeUndefined(context);
126         }
127         
128         return createJSObject(context, msg);
129 }
130
131 JSValueRef JSMessage::createJSObject(JSContextRef context,
132         Api::Messaging::EventUpdateMessageAnswerReceiver* listener,
133         Api::Messaging::MessageType msgType,
134         const string& msgId)
135 {
136     Api::Messaging::IMessagePtr msg;
137     Try
138     {
139         // create message depending on type
140         if (msgType == Api::Messaging::SMS || msgType == Api::Messaging::MMS || msgType == Api::Messaging::EMAIL) {
141             msg = Api::Messaging::MessageFactory::createMessage(msgType, msgId);
142             if (!msg)
143                 return JSValueMakeUndefined(context);
144         } else {
145             Throw(WrtDeviceApis::Commons::UnknownException);   // unsupported type
146         }
147     }
148     Catch(WrtDeviceApis::Commons::UnknownException) {
149         LogError("wrong message type, object not created");
150         return JSValueMakeUndefined(context);
151     }
152     return createJSObject(context, msg, listener);
153 }
154
155 JSValueRef JSMessage::createJSObject(JSContextRef context,
156         const Api::Messaging::IMessagePtr& msg,
157         Api::Messaging::EventUpdateMessageAnswerReceiver* listener)
158 {
159     JSClassRef jsClassRef = JSClassCreate(getClassInfo());
160     JSMessagePrivateObject* priv = new JSMessagePrivateObject(context, msg);
161     priv->setUpdateMsgReceiver(listener);
162     JSObjectRef jsValueRef =
163         JSObjectMake(context, jsClassRef, static_cast<void*>(priv));
164     JSClassRelease(jsClassRef);
165     if (NULL == jsValueRef) {
166         LogError("object creation error");
167         return JSValueMakeUndefined(context);
168     }
169     return jsValueRef;
170 }
171
172 JSValueRef JSMessage::createJSObject(JSContextRef context,
173         const Api::Messaging::IMessagePtr& msg)
174 {
175         LogDebug("createJSObject");
176         JSClassRef jsClassRef = JSClassCreate(getClassInfo());
177         LogDebug("jsClassRef success");
178         JSMessagePrivateObject* priv = new JSMessagePrivateObject(context, msg);
179         LogDebug("priv success");
180
181         JSObjectRef jsValueRef = JSObjectMake(context, jsClassRef, static_cast<void*>(priv));
182         LogDebug("JSObjectMake success");
183         JSClassRelease(jsClassRef);
184         if (NULL == jsValueRef) {
185             LogError("object creation error");
186             return JSValueMakeUndefined(context);
187         }
188         return jsValueRef;
189 }
190
191 JSValueRef JSMessage::createJSObject(JSContextRef context,
192         Api::Messaging::MessageType msgType,
193         const string& msgId)
194 {
195     Api::Messaging::IMessagePtr msg;
196     Try
197     {
198         // create message depending on type
199         if (msgType == Api::Messaging::SMS || msgType == Api::Messaging::MMS || msgType == Api::Messaging::EMAIL) {
200             msg = Api::Messaging::MessageFactory::createMessage(msgType, msgId);
201         } else {
202             Throw(WrtDeviceApis::Commons::UnknownException);   // unsupported type
203         }
204     }
205     Catch(WrtDeviceApis::Commons::UnknownException) {
206         LogError("wrong message type, object not created");
207         return JSValueMakeUndefined(context);
208     }
209     return createJSObject(context, msg, NULL);
210 }
211
212 void JSMessage::initialize(JSContextRef context,
213         JSObjectRef object)
214 {
215     LogInfo("enter");
216     JSMessagePrivateObject* priv =
217         static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
218     LogDebug("priv=" << priv);
219     if (!priv) {
220         LogWarning("empty jsObject creation");
221     }
222 }
223
224 void JSMessage::finalize(JSObjectRef object)
225 {
226     JSMessagePrivateObject* priv =
227         static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
228     if (priv) {
229         LogDebug("deleting private object");
230         delete priv;
231         JSObjectSetPrivate(object, NULL);
232     }
233 }
234
235 bool JSMessage::hasProperty(JSContextRef context,
236         JSObjectRef object,
237         JSStringRef propertyName)
238 {
239     return false;
240 }
241
242 JSValueRef JSMessage::getProperty(JSContextRef context,
243         JSObjectRef object,
244         JSStringRef propertyName,
245         JSValueRef* exception)
246 {
247     LogError("should not enter");
248     return JSValueMakeUndefined(context);
249 }
250
251 bool JSMessage::setProperty(JSContextRef context,
252         JSObjectRef object,
253         JSStringRef propertyName,
254         JSValueRef value,
255         JSValueRef* exception)
256 {
257     LogError("should not enter");
258     return false;
259 }
260
261 bool JSMessage::deleteProperty(JSContextRef context,
262         JSObjectRef object,
263         JSStringRef propertyName,
264         JSValueRef* exception)
265 {
266     LogInfo("enter");
267     return true;
268 }
269
270 void JSMessage::getPropertyNames(JSContextRef context,
271         JSObjectRef object,
272         JSPropertyNameAccumulatorRef propertyNames)
273 {
274     LogInfo("enter");
275 }
276
277 JSValueRef JSMessage::callAsFunction(JSContextRef context,
278         JSObjectRef object,
279         JSObjectRef thisObject,
280         size_t argumentCount,
281         const JSValueRef arguments[],
282         JSValueRef* exception)
283 {
284     LogInfo("enter");
285     return JSValueMakeUndefined(context);
286 }
287
288 bool JSMessage::hasInstance(JSContextRef context,
289         JSObjectRef constructor,
290         JSValueRef possibleInstance,
291         JSValueRef* exception)
292 {
293     LogInfo("enter");
294     return true;
295 }
296
297 JSValueRef JSMessage::convertToType(JSContextRef context,
298         JSObjectRef object,
299         JSType type,
300         JSValueRef* exception)
301 {
302     LogInfo("enter");
303     return JSValueMakeUndefined(context);
304 }
305
306 JSValueRef JSMessage::getAttachments(JSContextRef context,
307         JSObjectRef object,
308         JSStringRef propertyName,
309         JSValueRef* exception)
310 {
311     LogInfo("enter");
312     Try
313     {
314         ConverterMessageFactory::ConverterType converter =
315             ConverterMessageFactory::getConverter(context);
316         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
317         Api::Messaging::MessageType msgType = msg->getMessageType();
318
319         // prepare common values
320         JSCallbackManagerPtr emptyCallbackMgr = JSCallbackManager::createObject(
321                 converter->toJSGlobalContext(object),
322                 NULL,
323                 NULL);
324
325         switch (msgType) {
326                 case Api::Messaging::MMS:
327                 case Api::Messaging::EMAIL:
328                 {
329                                 
330                         std::vector<IAttachmentPtr> attachments;
331                         std::vector<IAttachmentPtr>::iterator it;
332                         
333                         if ( msgType == Api::Messaging::MMS )
334                         {
335                                 Api::Messaging::AttachmentsPtr mmsAttachments =
336                                         DPL::StaticPointerCast<Api::Messaging::Attachments>(Api::Messaging::MessageFactory::convertToEmail(msg));
337                                 attachments = mmsAttachments->getAttachments();
338                         }
339                         else
340                         {
341                                 Api::Messaging::AttachmentsPtr emailAttachments =
342                                         DPL::StaticPointerCast<Api::Messaging::Attachments>(Api::Messaging::MessageFactory::convertToEmail(msg));
343                                 attachments = emailAttachments->getAttachments();
344                         }
345
346                         int count = attachments.size();
347                         LogDebug( "count : " << count);
348
349                         JSObjectRef jsMessageAttachmentObject[count];   //make
350                         
351                         for (int index = 0 ; index < attachments.size(); index++ )
352                         {
353                                 LogDebug( "Attachment ID : " << attachments[index]->getAttachmentID());
354                                 jsMessageAttachmentObject[index] = JSMessageAttachment::createJS(context, attachments[index] );
355                         }                       
356                         JSObjectRef result = JSObjectMakeArray(context, count, jsMessageAttachmentObject, NULL);
357                         
358                         return result;
359                                                         
360                 }
361                 case Api::Messaging::SMS:
362                     return JSValueMakeUndefined(context);   // ignore
363
364                 default:
365                     LogError("not supported message type");
366                     Throw(WrtDeviceApis::Commons::InvalidArgumentException);
367         }
368     }
369     Catch(WrtDeviceApis::Commons::ConversionException) {
370         LogError("Error on conversion");
371           return JSTizenExceptionFactory::postException(context, exception, 
372                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
373     }
374     Catch(WrtDeviceApis::Commons::NullPointerException) {
375         LogError("Error on pointer, null value");
376           return JSTizenExceptionFactory::postException(context, exception, 
377                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
378     }
379     return JSValueMakeUndefined(context);
380 }
381
382
383 JSValueRef JSMessage::getBccAddress(JSContextRef context,
384         JSObjectRef object,
385         JSStringRef propertyName,
386         JSValueRef* exception)
387 {
388     Try
389     {
390         ConverterMessageFactory::ConverterType converter =
391             ConverterMessageFactory::getConverter(context);
392         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
393         Api::Messaging::MessageType msgType = msg->getMessageType();
394
395         switch (msgType) {
396         case Api::Messaging::EMAIL:
397         {
398             Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
399             Api::Messaging::RecipientsPtr recipient = email->getBccRecipientsPtr();
400             return JSRecipientArray::createArray(converter->toJSGlobalContext(
401                                                      object), recipient);
402         }
403         case Api::Messaging::SMS:
404         case Api::Messaging::MMS:
405             return JSValueMakeUndefined(context);   // ignore
406
407         default:
408             LogError("not supported message type");
409                  return JSTizenExceptionFactory::postException(context, exception, 
410                         JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
411             break;
412         }
413     }
414     Catch(WrtDeviceApis::Commons::ConversionException) {
415         LogError("Error on conversion");
416           return JSTizenExceptionFactory::postException(context, exception, 
417                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
418     }
419     Catch(WrtDeviceApis::Commons::NullPointerException) {
420         LogError("Error on pointer, null value");
421           return JSTizenExceptionFactory::postException(context, exception, 
422                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
423     }
424
425     return JSValueMakeUndefined(context);
426 }
427
428 JSValueRef JSMessage::getBody(JSContextRef context,
429         JSObjectRef object,
430         JSStringRef propertyName,
431         JSValueRef* exception)
432 {
433     Try
434     {
435         ConverterMessageFactory::ConverterType converter =
436             ConverterMessageFactory::getConverter(context);
437         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
438         string body = msg->getBody();
439         return converter->toJSValueRef(body);
440     }
441     Catch(WrtDeviceApis::Commons::ConversionException) {
442         LogError("Error on conversion");
443           return JSTizenExceptionFactory::postException(context, exception, 
444                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
445     }
446     Catch(WrtDeviceApis::Commons::NullPointerException) {
447         LogError("Error on pointer, null value");
448           return JSTizenExceptionFactory::postException(context, exception, 
449                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
450     }
451
452     return JSValueMakeUndefined(context);
453 }
454
455 JSValueRef JSMessage::getCcAddress(JSContextRef context,
456         JSObjectRef object,
457         JSStringRef propertyName,
458         JSValueRef* exception)
459 {
460     Try
461     {
462         ConverterMessageFactory::ConverterType converter =
463             ConverterMessageFactory::getConverter(context);
464         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
465         Api::Messaging::MessageType msgType = msg->getMessageType();
466
467         switch (msgType) {
468         case Api::Messaging::EMAIL:
469         {
470             Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
471             Api::Messaging::RecipientsPtr recipient = email->getCcRecipientsPtr();
472             return JSRecipientArray::createArray(converter->toJSGlobalContext(
473                                                      object), recipient);
474         }
475         case Api::Messaging::MMS:
476         case Api::Messaging::SMS:
477             return JSValueMakeUndefined(context);   // ignore
478
479         default:
480             LogError("not supported message type");
481                  return JSTizenExceptionFactory::postException(context, exception, 
482                            JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);    // unsupported type
483
484             break;
485         }
486     }
487     Catch(WrtDeviceApis::Commons::ConversionException) {
488         LogError("Error on conversion");
489           return JSTizenExceptionFactory::postException(context, exception, 
490                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
491     }
492     Catch(WrtDeviceApis::Commons::NullPointerException) {
493         LogError("Error on pointer, null value");
494           return JSTizenExceptionFactory::postException(context, exception, 
495                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
496     }
497
498     return JSValueMakeUndefined(context);
499 }
500
501 JSValueRef JSMessage::getDestinationAddress(JSContextRef context,
502         JSObjectRef object,
503         JSStringRef propertyName,
504         JSValueRef* exception)
505 {
506     LogInfo("enter");
507     Try
508     {
509         ConverterMessageFactory::ConverterType converter =
510             ConverterMessageFactory::getConverter(context);
511         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
512         Api::Messaging::RecipientsPtr recipient = msg->getToRecipientsPtr();
513         return JSRecipientArray::createArray(converter->toJSGlobalContext(
514                                                  object), recipient);
515     }
516     Catch(WrtDeviceApis::Commons::ConversionException) {
517         LogError("Error on conversion");
518           return JSTizenExceptionFactory::postException(context, exception, 
519                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
520     }
521     Catch(WrtDeviceApis::Commons::NullPointerException) {
522         LogError("Error on pointer, null value");
523           return JSTizenExceptionFactory::postException(context, exception, 
524                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
525     }
526
527     return JSValueMakeUndefined(context);
528 }
529
530 JSValueRef JSMessage::getIsRead(JSContextRef context,
531         JSObjectRef object,
532         JSStringRef propertyName,
533         JSValueRef* exception)
534 {
535     Try
536     {
537         ConverterMessageFactory::ConverterType converter =
538             ConverterMessageFactory::getConverter(context);
539         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
540         bool isRead = msg->isRead();
541         return converter->toJSValueRef(isRead);
542     }
543     Catch(WrtDeviceApis::Commons::ConversionException) {
544         LogError("Error on conversion");
545           return JSTizenExceptionFactory::postException(context, exception, 
546                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
547     }
548     Catch(WrtDeviceApis::Commons::NullPointerException) {
549         LogError("Error on pointer, null value");
550           return JSTizenExceptionFactory::postException(context, exception, 
551                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
552     }
553
554     return JSValueMakeUndefined(context);
555 }
556
557 JSValueRef JSMessage::getMessageId(JSContextRef context,
558         JSObjectRef object,
559         JSStringRef propertyName,
560         JSValueRef* exception)
561 {
562     Try
563     {
564         ConverterMessageFactory::ConverterType converter =
565             ConverterMessageFactory::getConverter(context);
566         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
567         const string& id = msg->getIdRef();
568         LogDebug("msgId=" << id);
569         return converter->toJSValueRef(id);
570     }
571     Catch(WrtDeviceApis::Commons::ConversionException) {
572         LogError("Error on conversion");
573           return JSTizenExceptionFactory::postException(context, exception, 
574                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
575     }
576     Catch(WrtDeviceApis::Commons::NullPointerException) {
577         LogError("Error on pointer, null value");
578           return JSTizenExceptionFactory::postException(context, exception, 
579                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
580     }
581
582     return JSValueMakeUndefined(context);
583 }
584
585 JSValueRef JSMessage::getMessagePriority(JSContextRef context,
586         JSObjectRef object,
587         JSStringRef propertyName,
588         JSValueRef* exception)
589 {
590     Try
591     {
592         ConverterMessageFactory::ConverterType converter =
593             ConverterMessageFactory::getConverter(context);
594         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
595         Api::Messaging::MessageType msgType = msg->getMessageType();
596
597         switch (msgType) {
598         case Api::Messaging::SMS:
599         case Api::Messaging::MMS:
600             return JSValueMakeUndefined(context);
601
602         case Api::Messaging::EMAIL:
603         {
604             Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
605             return converter->toJSValueRef(static_cast<Api::Messaging::MessagePriority>(*
606                                                                              email));
607         }
608
609         default:
610             LogError("unsupported message type");
611                 return JSTizenExceptionFactory::postException(context, exception, 
612                                   JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
613             break;
614         }
615     }
616     Catch(WrtDeviceApis::Commons::ConversionException) {
617         LogError("Error on conversion");
618           return JSTizenExceptionFactory::postException(context, exception, 
619                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
620     }
621     Catch(WrtDeviceApis::Commons::NullPointerException) {
622         LogError("Error on pointer, null value");
623           return JSTizenExceptionFactory::postException(context, exception, 
624                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
625     }
626
627     return JSValueMakeUndefined(context);
628 }
629
630 JSValueRef JSMessage::getMessageType(JSContextRef context,
631         JSObjectRef object,
632         JSStringRef propertyName,
633         JSValueRef* exception)
634 {
635     Try
636     {
637         ConverterMessageFactory::ConverterType converter =
638             ConverterMessageFactory::getConverter(context);
639         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
640         Api::Messaging::MessageType msgType = msg->getMessageType();
641
642         return converter->toJSValueRef(converter->toMessageType(msgType));
643     }
644     Catch(WrtDeviceApis::Commons::ConversionException) {
645         LogError("Error on conversion");
646           return JSTizenExceptionFactory::postException(context, exception, 
647                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
648     }
649     Catch(WrtDeviceApis::Commons::NullPointerException) {
650         LogError("Error on pointer, null value");
651           return JSTizenExceptionFactory::postException(context, exception, 
652                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
653     }
654
655     return JSValueMakeUndefined(context);
656 }
657
658 JSValueRef JSMessage::getSourceAddress(JSContextRef context,
659         JSObjectRef object,
660         JSStringRef propertyName,
661         JSValueRef* exception)
662 {
663     Try
664     {
665         ConverterMessageFactory::ConverterType converter =
666             ConverterMessageFactory::getConverter(context);
667         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
668
669         //This property is set up by the device or the web runtime environment.
670         //This property should only be taken into account for Email.
671         // TODO Verify with Tizen.
672         //        if (msg->getMessageType() != Api::Messaging::EMAIL && msg->getMessageType() != Api::VIRTUAL_MESSAGE) {
673         //            return JSValueMakeUndefined(context);
674         //        }
675         return converter->toJSValueRef(msg->getSourceAddress());
676     }
677     Catch(WrtDeviceApis::Commons::ConversionException) {
678         LogError("Error on conversion");
679           return JSTizenExceptionFactory::postException(context, exception, 
680                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
681     }
682     Catch(WrtDeviceApis::Commons::NullPointerException) {
683         LogError("Error on pointer, null value");
684           return JSTizenExceptionFactory::postException(context, exception, 
685                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
686     }
687
688     return JSValueMakeUndefined(context);
689 }
690
691 JSValueRef JSMessage::getSubject(JSContextRef context,
692         JSObjectRef object,
693         JSStringRef propertyName,
694         JSValueRef* exception)
695 {
696     Try
697     {
698         ConverterMessageFactory::ConverterType converter =
699             ConverterMessageFactory::getConverter(context);
700         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
701         Api::Messaging::MessageType msgType = msg->getMessageType();
702
703         switch (msgType) {
704         case Api::Messaging::MMS:
705         {
706             Api::Messaging::IMmsPtr mms = Api::Messaging::MessageFactory::convertToMms(msg);
707             return converter->toJSValueRef(mms->getSubject());
708         }
709         case Api::Messaging::EMAIL:
710         {
711             Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
712             return converter->toJSValueRef(email->getSubject());
713         }
714         case Api::Messaging::SMS:
715             return JSValueMakeUndefined(context);   // ignore
716         default:
717             LogError("message not support subject");
718                 return JSTizenExceptionFactory::postException(context, exception, 
719                                   JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
720             break;
721         }
722     }
723     Catch(WrtDeviceApis::Commons::ConversionException) {
724         LogError("Error on conversion");
725           return JSTizenExceptionFactory::postException(context, exception, 
726                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
727     }
728     Catch(WrtDeviceApis::Commons::NullPointerException) {
729         LogError("Error on pointer, null value");
730           return JSTizenExceptionFactory::postException(context, exception, 
731                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
732     }
733     Catch(WrtDeviceApis::Commons::UnsupportedException) {
734         return JSTizenExceptionFactory::postException(context, exception, 
735                          JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);
736     }
737     return JSValueMakeUndefined(context);
738 }
739
740 JSValueRef JSMessage::getTime(JSContextRef context,
741         JSObjectRef object,
742         JSStringRef propertyName,
743         JSValueRef* exception)
744 {
745     Try
746     {
747         ConverterMessageFactory::ConverterType converter =
748             ConverterMessageFactory::getConverter(context);
749         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
750         struct tm dateTime = msg->getDateTime();
751         return converter->toJSValueRef(dateTime);
752     }
753     Catch(WrtDeviceApis::Commons::ConversionException) {
754         LogError("Error on conversion");
755           return JSTizenExceptionFactory::postException(context, exception, 
756                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
757     }
758     Catch(WrtDeviceApis::Commons::NullPointerException) {
759         LogError("Error on pointer, null value");
760           return JSTizenExceptionFactory::postException(context, exception, 
761                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
762     }
763     return JSValueMakeUndefined(context);
764 }
765
766 JSValueRef JSMessage::getFolder(JSContextRef context,
767         JSObjectRef object,
768         JSStringRef propertyName,
769         JSValueRef* exception)
770 {
771     Try
772     {
773         ConverterMessageFactory::ConverterType converter =
774             ConverterMessageFactory::getConverter(context);
775         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
776         Api::Messaging::FolderType folder = msg->getCurrentFolder();
777         return converter->toJSValueRef(folder);
778     }
779     Catch(WrtDeviceApis::Commons::ConversionException) {
780         LogError("Error on conversion");
781           return JSTizenExceptionFactory::postException(context, exception, 
782                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
783     }
784     Catch(WrtDeviceApis::Commons::NullPointerException) {
785         LogError("Error on pointer, null value");
786           return JSTizenExceptionFactory::postException(context, exception, 
787                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
788     }
789     return JSValueMakeUndefined(context);
790 }
791
792 JSValueRef JSMessage::getMessageBody(JSContextRef context,
793         JSObjectRef object,
794         JSStringRef propertyName,
795         JSValueRef* exception)
796 {
797     LogInfo("getMessageBody");
798
799     Try
800     {
801           JSMessagePrivateObject* priv = static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
802           Assert(priv && "Private object is NULL.");
803
804           JSContextRef globalContext = priv->getContext();
805
806           ConverterMessageFactory::ConverterType converter =
807           ConverterMessageFactory::getConverter(globalContext);
808
809           Api::Messaging::IMessagePtr msg = converter->toIMessage(object);      //get message point
810           LogInfo("create JS");
811
812         return JSMessageBody::createJS(globalContext, msg);
813     }
814     Catch(WrtDeviceApis::Commons::ConversionException) {
815         LogError("Error on conversion");
816           return JSTizenExceptionFactory::postException(context, exception, 
817                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
818     }
819     Catch(WrtDeviceApis::Commons::NullPointerException) {
820         LogError("Error on pointer, null value");
821           return JSTizenExceptionFactory::postException(context, exception, 
822                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
823     }
824     return JSValueMakeUndefined(context);
825 }
826
827 JSValueRef JSMessage::getAccountID(JSContextRef context,
828         JSObjectRef object,
829         JSStringRef propertyName,
830         JSValueRef* exception)
831 {
832     LogInfo("getAccountID");
833
834     Try
835     {
836           JSMessagePrivateObject* priv = static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
837           Assert(priv && "Private object is NULL.");
838
839           JSContextRef globalContext = priv->getContext();
840
841           ConverterMessageFactory::ConverterType converter =  ConverterMessageFactory::getConverter(globalContext);
842
843           Api::Messaging::IMessagePtr msg = converter->toIMessage(object);      //get message point
844
845           LogInfo("create JS");
846           //getAccountID
847           if (msg->getMessageType() == Api::Messaging::EMAIL)
848           {
849                 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
850                 
851                 std::stringstream stream;
852                 stream << email->getAccountID();
853                 if (stream.fail()) {
854                         ThrowMsg(WrtDeviceApis::Commons::UnknownException,
855                                          "Couldn't convert e-mail account id");
856                 }
857                 
858                 return converter->toJSValueRef(stream.str());
859           }
860           else
861           {
862                  return JSValueMakeUndefined(context);
863           }
864     }
865     Catch(WrtDeviceApis::Commons::ConversionException) {
866         LogError("Error on conversion");
867           return JSTizenExceptionFactory::postException(context, exception, 
868                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
869     }
870     Catch(WrtDeviceApis::Commons::NullPointerException) {
871         LogError("Error on pointer, null value");
872           return JSTizenExceptionFactory::postException(context, exception, 
873                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
874     }
875     return JSValueMakeUndefined(context);
876 }
877
878 JSValueRef JSMessage::getUID(JSContextRef context,
879                         JSObjectRef object,
880                         JSStringRef propertyName,
881                         JSValueRef* exception)
882         {
883                 LogInfo("getUID");
884
885                 Try
886                 {
887                   JSMessagePrivateObject* priv = static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
888                   Assert(priv && "Private object is NULL.");
889
890                   JSContextRef globalContext = priv->getContext();
891
892                   ConverterMessageFactory::ConverterType converter =  ConverterMessageFactory::getConverter(globalContext);
893
894                   Api::Messaging::IMessagePtr msg = converter->toIMessage(object); //get message point
895
896                   LogInfo("create JS");
897                   //getAccountID
898                   if (msg->getMessageType() == Api::Messaging::EMAIL)
899                   {
900                         Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
901                         return converter->toJSValueRef(email->getUID());
902                   }
903                   else
904                   {
905                          return JSValueMakeUndefined(context);
906                   }
907                 }
908                 Catch(WrtDeviceApis::Commons::ConversionException) {
909                         LogError("Error on conversion");
910                   return JSTizenExceptionFactory::postException(context, exception, 
911                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
912                 }
913                 Catch(WrtDeviceApis::Commons::NullPointerException) {
914                         LogError("Error on pointer, null value");
915                   return JSTizenExceptionFactory::postException(context, exception, 
916                                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
917                 }
918                 return JSValueMakeUndefined(context);
919         }
920
921 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
922 bool JSMessage::setAttachments(JSContextRef context,
923         JSObjectRef object,
924         JSStringRef propertyName,
925         JSValueRef value,
926         JSValueRef* exception)
927 {
928     LogInfo("enter");
929
930     Try
931     {
932         JSMessagePrivateObject* priv =
933             static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
934
935         if (!priv) {
936             return JSTizenExceptionFactory::postException(context, exception, 
937                            JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
938         }
939     }
940     Catch(WrtDeviceApis::Commons::NullPointerException) {
941         LogError("Error on pointer, null value");
942            return JSTizenExceptionFactory::postException(context, exception, 
943                            JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
944     }
945
946     vector<string> attachments;
947     Try
948     {
949         ConverterMessageFactory::ConverterType converter =
950             ConverterMessageFactory::getConverter(context);
951         if (JSIsArrayValue(context, value)) {
952             JSObjectRef valueObj = converter->toJSObjectRef(value);
953
954             // extract path from each JSFile object
955             unsigned int len = JSGetArrayLength(context, valueObj);
956             for (unsigned int i = 0; i < len; ++i) {
957                 JSValueRef att = JSGetArrayElement(context, valueObj, i);
958                 if (JSValueIsUndefined(context,
959                                        att) || JSValueIsNull(context, att)) {
960                     LogWarning("Invalid array element. Skipping.");
961                     continue;
962                 }
963                 Api::Filesystem::INodePtr node = converter->toFilesystemNode(
964                         att);
965
966                 LogDebug("Adding attachment: " << node->getPath()->getFullPath());
967                 attachments.push_back(node->getPath()->getFullPath());
968             }
969         } else {
970             LogWarning("Invalid or null element passed as attachment array." <<
971                        "Setting empty vector.");
972         }
973
974         // set up new attachments list
975         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
976         Api::Messaging::MessageType msgType = msg->getMessageType();
977         switch (msgType) {
978         case Api::Messaging::MMS:
979         {
980             Api::Messaging::IMmsPtr mms = Api::Messaging::MessageFactory::convertToMms(msg);
981             mms->setAttachments(attachments, true);
982             break;
983         }
984         case Api::Messaging::EMAIL:
985         {
986             Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
987             email->setAttachments(attachments, true);
988             break;
989         }
990         case Api::Messaging::SMS:
991
992             return false; // ignore
993
994         default:
995             LogError("not supported message type");
996             Throw(WrtDeviceApis::Commons::InvalidArgumentException);
997         }
998         return true;
999     }
1000     Catch(WrtDeviceApis::Commons::ConversionException) {
1001         LogError("Error on conversion");
1002           return JSTizenExceptionFactory::postException(context, exception, 
1003                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1004     }
1005     Catch(WrtDeviceApis::Commons::NullPointerException) {
1006         LogError("Error on pointer, null value");
1007           return JSTizenExceptionFactory::postException(context, exception, 
1008                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1009     }
1010     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
1011         LogError("Invalid argument");
1012            return JSTizenExceptionFactory::postException(context, exception, 
1013                            JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1014     }
1015     return false;
1016 }
1017 #endif
1018
1019 bool JSMessage::setBccAddress(JSContextRef context,
1020         JSObjectRef object,
1021         JSStringRef propertyName,
1022         JSValueRef value,
1023         JSValueRef* exception)
1024 {
1025     Try
1026     {
1027         ConverterMessageFactory::ConverterType converter =
1028             ConverterMessageFactory::getConverter(context);
1029         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1030         Api::Messaging::Recipients bcc = converter->toRecipients(value);
1031         LogDebug("setting bcc field, size=" << bcc.getRecipientSize());
1032
1033         Api::Messaging::MessageType msgType = msg->getMessageType();
1034         switch (msgType) {
1035         case Api::Messaging::SMS:
1036         case Api::Messaging::MMS:
1037             return false;   // ignore
1038
1039         case Api::Messaging::EMAIL:
1040         {
1041             Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
1042             email->setBccRecipients(bcc);
1043             break;
1044         }
1045         default:
1046             LogError("unsupported message type");
1047             return JSTizenExceptionFactory::postException(context, exception, 
1048                                           JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
1049         }
1050         return true;
1051     }
1052      Catch(WrtDeviceApis::Commons::ConversionException) {
1053                 LogError("Error on conversion");
1054           return JSTizenExceptionFactory::postException(context, exception, 
1055                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1056         }
1057         Catch(WrtDeviceApis::Commons::NullPointerException) {
1058                 LogError("Error on pointer, null value");
1059           return JSTizenExceptionFactory::postException(context, exception, 
1060                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1061         }
1062     Catch(WrtDeviceApis::Commons::PlatformException) {
1063         LogError("Platform execution");
1064           return JSTizenExceptionFactory::postException(context, exception, 
1065                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1066     }
1067
1068     return false;
1069 }
1070
1071 bool JSMessage::setBody(JSContextRef context,
1072         JSObjectRef object,
1073         JSStringRef propertyName,
1074         JSValueRef value,
1075         JSValueRef* exception)
1076 {
1077     Try
1078     {
1079         ConverterMessageFactory::ConverterType converter =
1080             ConverterMessageFactory::getConverter(context);
1081         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1082         string body = converter->toString(value);
1083         msg->setBody(body);
1084         return true;
1085     }
1086         Catch(WrtDeviceApis::Commons::ConversionException) {
1087                 LogError("Error on conversion");
1088           return JSTizenExceptionFactory::postException(context, exception, 
1089                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1090         }
1091         Catch(WrtDeviceApis::Commons::NullPointerException) {
1092                 LogError("Error on pointer, null value");
1093           return JSTizenExceptionFactory::postException(context, exception, 
1094                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1095         }
1096         Catch(WrtDeviceApis::Commons::PlatformException) {
1097                 LogError("Platform execution");
1098           return JSTizenExceptionFactory::postException(context, exception, 
1099                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1100         }
1101     return false;
1102 }
1103
1104 bool JSMessage::setCcAddress(JSContextRef context,
1105         JSObjectRef object,
1106         JSStringRef propertyName,
1107         JSValueRef value,
1108         JSValueRef* exception)
1109 {
1110     Try
1111     {
1112         ConverterMessageFactory::ConverterType converter =
1113             ConverterMessageFactory::getConverter(context);
1114         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1115         Api::Messaging::Recipients cc = converter->toRecipients(value);
1116         LogDebug("setting cc field, size=" << cc.getRecipientSize());
1117
1118         Api::Messaging::MessageType msgType = msg->getMessageType();
1119         switch (msgType) {
1120         case Api::Messaging::SMS:
1121         case Api::Messaging::MMS:
1122             return false;   // ignore
1123
1124         case Api::Messaging::EMAIL:
1125         {
1126             Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
1127             email->setCcRecipients(cc);
1128             break;
1129         }
1130         default:
1131             LogError("unsuported message type");
1132             return JSTizenExceptionFactory::postException(context, exception, 
1133                                           JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);        // unsupported type
1134             break;
1135         }
1136         return true;
1137     }
1138
1139         Catch(WrtDeviceApis::Commons::ConversionException) {
1140                 LogError("Error on conversion");
1141           return JSTizenExceptionFactory::postException(context, exception, 
1142                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1143         }
1144         Catch(WrtDeviceApis::Commons::NullPointerException) {
1145                 LogError("Error on pointer, null value");
1146           return JSTizenExceptionFactory::postException(context, exception, 
1147                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1148         }
1149         Catch(WrtDeviceApis::Commons::PlatformException) {
1150                 LogError("Platform execution");
1151           return JSTizenExceptionFactory::postException(context, exception, 
1152                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1153         }
1154
1155     return false;
1156 }
1157
1158 bool JSMessage::setDestinationAddress(JSContextRef context,
1159         JSObjectRef object,
1160         JSStringRef propertyName,
1161         JSValueRef value,
1162         JSValueRef * exception)
1163 {
1164     LogInfo("enter");
1165     Try
1166     {
1167         ConverterMessageFactory::ConverterType converter =
1168             ConverterMessageFactory::getConverter(context);
1169         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1170         Api::Messaging::Recipients to = converter->toRecipients(value);
1171         LogDebug("setting to field, size=" << to.getRecipientSize());
1172         msg->setToRecipients(to);
1173         return true;
1174     }
1175         Catch(WrtDeviceApis::Commons::ConversionException) {
1176                 LogError("Error on conversion");
1177           return JSTizenExceptionFactory::postException(context, exception, 
1178                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1179         }
1180         Catch(WrtDeviceApis::Commons::NullPointerException) {
1181                 LogError("Error on pointer, null value");
1182           return JSTizenExceptionFactory::postException(context, exception, 
1183                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1184         }
1185         Catch(WrtDeviceApis::Commons::PlatformException) {
1186                 LogError("Platform execution");
1187           return JSTizenExceptionFactory::postException(context, exception, 
1188                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1189         }
1190
1191     return false;
1192 }
1193
1194 bool JSMessage::setIsRead(JSContextRef context,
1195         JSObjectRef object,
1196         JSStringRef propertyName,
1197         JSValueRef value,
1198         JSValueRef * exception)
1199 {
1200     Try
1201     {
1202         ConverterMessageFactory::ConverterType converter =
1203             ConverterMessageFactory::getConverter(context);
1204         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1205         msg->setReadStatus(converter->toBool(value));
1206         msg->setisReadChangeStatus(converter->toBool(value));
1207         return true;
1208     }
1209         Catch(WrtDeviceApis::Commons::ConversionException) {
1210                 LogError("Error on conversion");
1211           return JSTizenExceptionFactory::postException(context, exception, 
1212                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1213         }
1214         Catch(WrtDeviceApis::Commons::NullPointerException) {
1215                 LogError("Error on pointer, null value");
1216           return JSTizenExceptionFactory::postException(context, exception, 
1217                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1218         }
1219
1220     return false;
1221 }
1222
1223 bool JSMessage::setMessagePriority(JSContextRef context,
1224         JSObjectRef object,
1225         JSStringRef propertyName,
1226         JSValueRef value,
1227         JSValueRef * exception)
1228 {
1229     Try
1230     {
1231         ConverterMessageFactory::ConverterType converter =
1232             ConverterMessageFactory::getConverter(context);
1233         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1234         Api::Messaging::MessageType msgType = msg->getMessageType();
1235         switch (msgType) {
1236         case Api::Messaging::MMS:
1237         case Api::Messaging::SMS:
1238             return false;   // ignore
1239
1240         case Api::Messaging::EMAIL:
1241             msg->setPriority(converter->toMessagePriority(value));
1242             break;
1243
1244         default:
1245             LogError("unsuported message type");
1246             Throw(WrtDeviceApis::Commons::ConversionException);
1247             break;
1248         }
1249         return true;
1250     }
1251         Catch(WrtDeviceApis::Commons::ConversionException) {
1252                 LogError("Error on conversion");
1253           return JSTizenExceptionFactory::postException(context, exception, 
1254                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1255         }
1256         Catch(WrtDeviceApis::Commons::NullPointerException) {
1257                 LogError("Error on pointer, null value");
1258           return JSTizenExceptionFactory::postException(context, exception, 
1259                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1260         }
1261         Catch(WrtDeviceApis::Commons::PlatformException) {
1262                 LogError("Platform execution");
1263           return JSTizenExceptionFactory::postException(context, exception, 
1264                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1265         }
1266
1267     return false;
1268 }
1269
1270 bool JSMessage::setSubject(JSContextRef context,
1271         JSObjectRef object,
1272         JSStringRef propertyName,
1273         JSValueRef value,
1274         JSValueRef * exception)
1275 {
1276     Try
1277     {
1278         ConverterMessageFactory::ConverterType converter =
1279             ConverterMessageFactory::getConverter(context);
1280         Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1281         Api::Messaging::MessageType msgType = msg->getMessageType();
1282         string subject = converter->toString(value);
1283         switch (msgType) {
1284         case Api::Messaging::MMS:
1285         {
1286             Api::Messaging::IMmsPtr mms = Api::Messaging::MessageFactory::convertToMms(msg);
1287             mms->setSubject(subject);
1288             break;
1289         }
1290         case Api::Messaging::EMAIL:
1291         {
1292             Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
1293             email->setSubject(subject);
1294             break;
1295         }
1296         case Api::Messaging::SMS:
1297             return false;   // ignore
1298
1299         default:
1300             LogError("message not supported");
1301             Throw(WrtDeviceApis::Commons::UnsupportedException);
1302             break;
1303         }
1304         return true;
1305     }
1306         Catch(WrtDeviceApis::Commons::ConversionException) {
1307                 LogError("Error on conversion");
1308           return JSTizenExceptionFactory::postException(context, exception, 
1309                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1310         }
1311         Catch(WrtDeviceApis::Commons::NullPointerException) {
1312                 LogError("Error on pointer, null value");
1313           return JSTizenExceptionFactory::postException(context, exception, 
1314                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1315         }
1316         Catch(WrtDeviceApis::Commons::PlatformException) {
1317                 LogError("Platform execution");
1318           return JSTizenExceptionFactory::postException(context, exception, 
1319                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1320         }
1321       Catch(WrtDeviceApis::Commons::UnsupportedException) {
1322         return JSTizenExceptionFactory::postException(context, exception, 
1323                          JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);
1324     }
1325
1326     return false;
1327 }
1328
1329 bool JSMessage::setMessageBody(JSContextRef context,
1330         JSObjectRef object,
1331         JSStringRef propertyName,
1332         JSValueRef value,
1333         JSValueRef* exception)
1334 {
1335     Try
1336     {
1337         LogDebug("Set Message Body.");
1338         return true;
1339     }
1340         Catch(WrtDeviceApis::Commons::ConversionException) {
1341                 LogError("Error on conversion");
1342           return JSTizenExceptionFactory::postException(context, exception, 
1343                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1344         }
1345         Catch(WrtDeviceApis::Commons::NullPointerException) {
1346                 LogError("Error on pointer, null value");
1347           return JSTizenExceptionFactory::postException(context, exception, 
1348                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1349         }
1350         Catch(WrtDeviceApis::Commons::PlatformException) {
1351                 LogError("Platform execution");
1352           return JSTizenExceptionFactory::postException(context, exception, 
1353                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1354         }
1355
1356     return false;
1357 }
1358
1359 JSValueRef JSMessage::getConversationId(JSContextRef context,
1360         JSObjectRef object,
1361         JSStringRef propertyName,
1362         JSValueRef* exception)
1363 {
1364         LogDebug("getConversationId");
1365
1366     Try
1367     {
1368                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1369                 int convId = 0;
1370                 Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1371                 Api::Messaging::EventGetConversationIdPtr event(new Api::Messaging::EventGetConversationId());
1372                 event->setConversationMsgPtr(msg);
1373                 event->setForSynchronousCall();
1374                 Api::Messaging::ReqReceiverMessageSingleton::Instance().getConversationId(event);
1375
1376                 convId = event->getConversationId();
1377
1378         return converter->toJSValueRef(convId);
1379     }
1380         Catch(WrtDeviceApis::Commons::ConversionException) {
1381                 LogError("Error on conversion");
1382           return JSTizenExceptionFactory::postException(context, exception, 
1383                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1384         }
1385         Catch(WrtDeviceApis::Commons::NullPointerException) {
1386                 LogError("Error on pointer, null value");
1387           return JSTizenExceptionFactory::postException(context, exception, 
1388                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1389         }
1390         Catch(WrtDeviceApis::Commons::PlatformException) {
1391                 LogError("Platform execution");
1392           return JSTizenExceptionFactory::postException(context, exception, 
1393                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1394         }
1395
1396     return JSValueMakeUndefined(context);
1397 }
1398
1399 JSValueRef JSMessage::getInResponseTo(JSContextRef context,
1400         JSObjectRef object,
1401         JSStringRef propertyName,
1402         JSValueRef* exception)
1403 {
1404     LogDebug("getInResponseTo");
1405
1406     Try
1407     {
1408                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
1409                 int convId = 0;
1410                 int msgId = 0;
1411                 Api::Messaging::IMessagePtr msg = converter->toIMessage(object);
1412                 if (msg->getMessageType() == EMAIL)
1413                 {
1414                         Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
1415                         msgId = email->getUID();
1416
1417                         Api::Messaging::EventGetConversationIdPtr event(new Api::Messaging::EventGetConversationId());
1418                         event->setConversationMsgPtr(msg);
1419                         event->setForSynchronousCall();
1420                         Api::Messaging::ReqReceiverMessageSingleton::Instance().getConversationId(event);
1421                         convId = event->getConversationId();
1422
1423                         if (convId == msgId)
1424                                 convId = -1; // error case, original message
1425                 }
1426                 else
1427                 {
1428                         convId = -1;
1429                 }
1430
1431         return converter->toJSValueRef(convId);
1432     }
1433         Catch(WrtDeviceApis::Commons::ConversionException) {
1434                 LogError("Error on conversion");
1435           return JSTizenExceptionFactory::postException(context, exception, 
1436                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1437         }
1438         Catch(WrtDeviceApis::Commons::NullPointerException) {
1439                 LogError("Error on pointer, null value");
1440           return JSTizenExceptionFactory::postException(context, exception, 
1441                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1442         }
1443         Catch(WrtDeviceApis::Commons::PlatformException) {
1444                 LogError("Platform execution");
1445           return JSTizenExceptionFactory::postException(context, exception, 
1446                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1447         }
1448
1449     return JSValueMakeUndefined(context);
1450 }
1451
1452 JSValueRef JSMessage::getMessageStatus(JSContextRef context,
1453                         JSObjectRef object,
1454                         JSStringRef propertyName,
1455                         JSValueRef* exception)
1456 {
1457         LogInfo("getMessageStatus");
1458
1459         Try
1460         {
1461           JSMessagePrivateObject* priv = static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
1462           Assert(priv && "Private object is NULL.");
1463
1464           JSContextRef globalContext = priv->getContext();
1465
1466           ConverterMessageFactory::ConverterType converter =  ConverterMessageFactory::getConverter(globalContext);
1467
1468           Api::Messaging::IMessagePtr msg = converter->toIMessage(object); //get message point
1469
1470           LogInfo("create JS");
1471           return converter->toJSValueRef(converter->toMessageStatusType(msg->getMessageStatus()));
1472
1473         }
1474         Catch(WrtDeviceApis::Commons::ConversionException) {
1475                 LogError("Error on conversion");
1476           return JSTizenExceptionFactory::postException(context, exception, 
1477                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1478         }
1479         Catch(WrtDeviceApis::Commons::NullPointerException) {
1480                 LogError("Error on pointer, null value");
1481           return JSTizenExceptionFactory::postException(context, exception, 
1482                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1483         }
1484         
1485         return JSValueMakeUndefined(context);
1486 }
1487
1488 #if 0
1489 JSValueRef JSMessage::getAttachmentExistence(JSContextRef context,
1490                         JSObjectRef object,
1491                         JSStringRef propertyName,
1492                         JSValueRef* exception)
1493 {
1494         LogInfo("getAttachmentExistence");
1495
1496         Try
1497         {
1498           JSMessagePrivateObject* priv = static_cast<JSMessagePrivateObject*>(JSObjectGetPrivate(object));
1499           Assert(priv && "Private object is NULL.");
1500
1501           JSContextRef globalContext = priv->getContext();
1502
1503           ConverterMessageFactory::ConverterType converter =  ConverterMessageFactory::getConverter(globalContext);
1504
1505           Api::Messaging::IMessagePtr msg = converter->toIMessage(object); //get message point
1506
1507           LogInfo("create JS");
1508           return converter->toJSValueRef(converter->toMessageStatusType(msg->getMessageStatus()));
1509
1510         }
1511         Catch(WrtDeviceApis::Commons::ConversionException) {
1512                 LogError("Error on conversion");
1513           return JSTizenExceptionFactory::postException(context, exception, 
1514                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
1515         }
1516         Catch(WrtDeviceApis::Commons::NullPointerException) {
1517                 LogError("Error on pointer, null value");
1518           return JSTizenExceptionFactory::postException(context, exception, 
1519                  JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1520         }
1521         
1522         return JSValueMakeUndefined(context);
1523 }
1524 #endif
1525
1526 }
1527 }