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