2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file JSPluginTemplete.cpp
20 * @author Jaehyun Park (jaehyun77.park@samsung.com)
22 * @brief Implementation of the JSPluginTemplete class
25 #include "JSMessagingService.h"
26 #include "MessagingController.h"
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/Validator.h>
29 #include <CommonsJavaScript/JSUtils.h>
30 #include <CommonsJavaScript/JSCallbackManager.h>
31 #include <CommonsJavaScript/Utils.h>
32 #include <CommonsJavaScript/ScopedJSStringRef.h>
33 #include <Commons/StringUtils.h>
35 #include <API/Messaging/ReqReceiverMessage.h>
36 #include <Tizen/Common/JSTizenExceptionFactory.h>
37 #include <Tizen/Common/JSTizenException.h>
38 #include <Tizen/Common/SecurityExceptions.h>
40 #include "MessagingErrorMsg.h"
41 #include "ConverterMessage.h"
42 #include "JSMessagingListener.h"
43 #include "JSMessagingStorage.h"
44 #include "JSMessage.h"
45 #include "EventSendMessagePrivateData.h" //for send message
46 #include "EventMessagingServicePrivateData.h"
47 #include "plugin_config.h"
51 using namespace WrtDeviceApis;
52 using namespace WrtDeviceApis::Commons;
53 using namespace WrtDeviceApis::CommonsJavaScript;
54 using namespace TizenApis::Api::Messaging;
55 using namespace TizenApis::Commons;
60 JSClassRef JSMessagingService::m_jsClassRef = NULL;
61 JSClassDefinition JSMessagingService::m_classInfo = {
63 kJSClassAttributeNone,
73 NULL, //deleteProperty,Geolocation
74 NULL, //getPropertyNames,
81 JSStaticValue JSMessagingService::m_property[] =
83 {"id", getProperty, NULL, kJSPropertyAttributeReadOnly},
84 {"type", getProperty, NULL, kJSPropertyAttributeReadOnly},
85 {"name", getProperty, NULL, kJSPropertyAttributeReadOnly},
86 {"messagingStorage", getProperty, NULL, kJSPropertyAttributeReadOnly},
90 JSStaticFunction JSMessagingService::m_function[] = {
91 { "createMessage", JSMessagingService::createMessage, kJSPropertyAttributeNone },
92 { "sendMessage", JSMessagingService::sendMessage, kJSPropertyAttributeNone },
93 { "loadMessageBody", JSMessagingService::loadMessageBody, kJSPropertyAttributeNone },
94 { "loadMessageAttachment", JSMessagingService::loadMessageAttachment, kJSPropertyAttributeNone },
95 { "sync", JSMessagingService::sync, kJSPropertyAttributeNone },
96 { "syncFolder", JSMessagingService::syncFolder, kJSPropertyAttributeNone },
97 { "cancelOperation", JSMessagingService::cancelOperation, kJSPropertyAttributeNone },
101 const JSClassRef JSMessagingService::getClassRef() {
103 m_jsClassRef = JSClassCreate(&m_classInfo);
108 void JSMessagingService::initialize(JSContextRef context, JSObjectRef object) {
109 LogDebug("creation messaging Service instance");
112 void JSMessagingService::finalize(JSObjectRef object) {
113 LogDebug("finalize messaging instance");
114 //JSMessagingServicePriv *priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(object));
115 JSMessagingServicePriv *priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(object));
119 bool JSMessagingService::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
120 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
123 JSObjectRef JSMessagingService::createJSObject(JSContextRef context, const Api::Messaging::IMessagingServicePtr &messagingService)
125 JSMessagingServicePriv* priv = new JSMessagingServicePriv( context, messagingService); //make private class.
126 return JSObjectMake(context, getClassRef(), priv);
129 JSValueRef JSMessagingService::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
132 JSValueRef retVal = NULL;
134 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(object)); //get private object
137 IMessagingServicePtr imessagingService = priv->getObject();
139 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
141 if (JSStringIsEqualToUTF8CString(propertyName, "type")) {
142 LogDebug("type" << ":" << imessagingService->getType());
143 retVal = converter->toJSValueRef(converter->toMessageType(imessagingService->getType()));
144 } else if (JSStringIsEqualToUTF8CString(propertyName, "name")) {
145 LogDebug("name" << ":" << imessagingService->getName());
146 retVal = converter->toJSValueRef(imessagingService->getName());
147 } else if (JSStringIsEqualToUTF8CString(propertyName, "id")) {
148 LogDebug("accountId" << ":" << imessagingService->getAccountID());
150 std::stringstream stream;
151 stream << imessagingService->getAccountID();
153 ThrowMsg(WrtDeviceApis::Commons::UnknownException,"Couldn't convert e-mail account id");
156 retVal = converter->toJSValueRef(stream.str());
157 } else if (JSStringIsEqualToUTF8CString(propertyName, "messagingStorage")) {
158 LogDebug("messagingStorage" << ":" << imessagingService->getAccountID());
159 JSContextRef l_globalContext = converter->toJSGlobalContext(object);
160 retVal = JSUtils::makeObject(l_globalContext, JSMessagingStorage::getClassRef(), NULL);
162 retVal = JSValueMakeUndefined(context);
165 } Catch (WrtDeviceApis::Commons::Exception){
166 LogError("Exception: " << _rethrown_exception.GetMessage());
167 return JSTizenExceptionFactory::postException(context, exception,
168 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
177 LogDebug(" Private Object is NULL ");
178 return JSTizenExceptionFactory::postException(context, exception,
179 JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
184 JSValueRef JSMessagingService::createMessage(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
185 const JSValueRef arguments[], JSValueRef* exception)
189 LogDebug("arumentConunt:" << argumentCount);
191 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
194 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
196 MessagingExportedNames[MESSAGING_FUNCTION_API_CREATE_MESSAGE]);
197 TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
200 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
201 JSContextRef l_globalContext = converter->toJSGlobalContext(thisObject);
202 Validator check(context, exception);
204 IMessagingServicePtr imessagingService = priv->getObject(); //get MessagingService.
205 JSValueRef objMsg = NULL;
207 Api::Messaging::MessageType msgType = (Api::Messaging::MessageType)imessagingService->getType();
208 LogInfo("msgType :" << msgType);
210 if (msgType == Api::Messaging::EMAIL)
212 Api::Messaging::EmailAccountInfo account = imessagingService->getCurrentEmailAccount();
213 LogDebug("Account Address:" << &account);
214 objMsg = JSMessage::createJSObject(l_globalContext, account, msgType); //make message JSValueRef.
218 objMsg = JSMessage::createJSObject(l_globalContext, msgType); //make message JSValueRef.
222 if ( argumentCount == 1)
224 IMessagePtr msg = converter->toIMessage(objMsg) ;
227 ThrowMsg(WrtDeviceApis::Commons::PlatformException,"platform exception");
229 LogDebug ("##### msg type is " << msg->getId());
231 const ScopedJSStringRef subjectStr(JSStringCreateWithUTF8CString("subject"));
232 const ScopedJSStringRef toStr(JSStringCreateWithUTF8CString("to"));
233 const ScopedJSStringRef ccStr(JSStringCreateWithUTF8CString("cc"));
234 const ScopedJSStringRef bccStr(JSStringCreateWithUTF8CString("bcc"));
235 const ScopedJSStringRef plainBodyStr(JSStringCreateWithUTF8CString("plainBody"));
236 const ScopedJSStringRef htmlBodyStr(JSStringCreateWithUTF8CString("htmlBody"));
237 const ScopedJSStringRef priorityStr(JSStringCreateWithUTF8CString("priority"));
239 JSObjectRef arg = converter->toJSObjectRef(arguments[0]);
240 JSValueRef subjectData = JSObjectGetProperty(l_globalContext, arg, subjectStr.get(), NULL);
241 JSValueRef toData = JSObjectGetProperty(l_globalContext, arg, toStr.get(), NULL);
242 JSValueRef ccData = JSObjectGetProperty(l_globalContext, arg, ccStr.get(), NULL);
243 JSValueRef bccData = JSObjectGetProperty(l_globalContext, arg, bccStr.get(), NULL);
244 JSValueRef plainBodyData = JSObjectGetProperty(l_globalContext, arg, plainBodyStr.get(), NULL);
245 JSValueRef htmlBodyData = JSObjectGetProperty(l_globalContext, arg, htmlBodyStr.get(), NULL);
246 JSValueRef priorityData = JSObjectGetProperty(l_globalContext, arg, priorityStr.get(), NULL);
249 if (!JSValueIsUndefined(l_globalContext, subjectData) )
251 LogDebug ( " Subject : " << converter->toString(subjectData) );
252 std::string subject = converter->toString(subjectData);
255 case Api::Messaging::MMS:
257 Api::Messaging::IMmsPtr mms = Api::Messaging::MessageFactory::convertToMms(msg);
258 mms->setSubject(subject);
261 case Api::Messaging::EMAIL:
263 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
264 email->setSubject(subject);
269 LogError("message not supported");
270 //Throw(WrtDeviceApis::Commons::UnsupportedException);
276 if (!JSValueIsUndefined(l_globalContext, toData) )
278 Api::Messaging::Recipients to = converter->toRecipients(toData);
279 LogDebug("setting to field, size=" << to.getRecipientSize());
280 msg->setToRecipients(to);
283 if (msgType == Api::Messaging::EMAIL )
285 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
286 if (!JSValueIsUndefined(l_globalContext, ccData))
288 Api::Messaging::Recipients cc = converter->toRecipients(ccData);
289 email->setCcRecipients(cc);
292 if (!JSValueIsUndefined(l_globalContext, bccData))
294 Api::Messaging::Recipients bcc = converter->toRecipients(bccData);
295 email->setBccRecipients(bcc);
298 if (!JSValueIsUndefined(l_globalContext, htmlBodyData))
300 std::string body = converter->toString(htmlBodyData);
301 LogDebug("html body : " << body);
302 email->setHtmlBody(body);
305 if (!JSValueIsUndefined(l_globalContext, priorityData))
307 email->setPriority(converter->toMessagePriority(priorityData));
312 if (!JSValueIsUndefined(l_globalContext, plainBodyData) )
314 std::string body = converter->toString(plainBodyData);
315 LogDebug("plain body : " << body);
323 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
324 return JSTizenExceptionFactory::postException(context, exception,
325 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
327 Catch(WrtDeviceApis::Commons::ConversionException) {
328 return JSTizenExceptionFactory::postException(context, exception,
329 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
333 return JSValueMakeUndefined(context);
337 LogDebug(" Private Object is NULL ");
338 return JSTizenExceptionFactory::postException(context, exception,
339 JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
344 JSValueRef JSMessagingService::sendMessage(JSContextRef context,
345 JSObjectRef function,
346 JSObjectRef thisObject,
347 size_t argumentCount,
348 const JSValueRef arguments[],
349 JSValueRef* exception)
353 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
356 LogError("Null pointer");
357 return JSTizenExceptionFactory::postException(context, exception,
358 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
361 JSContextRef globalContext = priv->getContext();
363 LogInfo(">>> argument count : " << argumentCount);
365 if (argumentCount < 2) {
366 return JSTizenExceptionFactory::postException(context, exception,
367 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
370 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
371 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr recipientCallbackManager;
373 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
376 if ( argumentCount > 1 && argumentCount < 4)
378 if ( argumentCount > 1)
380 if (JSValueIsObject(context, arguments[1]) && !Validator(context).isCallback(arguments[1])) //check
382 MessageSendCallback callbacks = converter->toMessageSendCallback(arguments[1]);
383 callbackManager->setOnSuccess( converter->toFunctionOrNull(callbacks.onSuccess) );
387 callbackManager->setOnSuccess(converter->toFunction(arguments[1]));
390 if ( argumentCount == 3)
392 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
398 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,"invalid argument exception");
402 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
404 MessagingExportedNames[MESSAGING_FUNCTION_API_SEND_MESSAGE]);
405 TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);
408 EventSendMessagePrivateDataPtr privateData( new EventSendMessagePrivateData(callbackManager, recipientCallbackManager) );
410 Api::Messaging::EventSendMessagePtr event(new Api::Messaging::EventSendMessage());
412 LogDebug("Event size");
414 IMessagingServicePtr imessagingService = priv->getObject();
416 event->opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_SEND_MESSAGE);
417 event->msg = converter->toIMessage(arguments[0]);
418 imessagingService->setMessageToOpId( event->opId, event->msg);
419 event->store = true; //always store message in sendbox after send , email Type.
420 event->m_messagingService = imessagingService;
421 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
423 //event->setForAsynchronousCall(&JSMessagingListener::getInstance());
424 //event->setForAsynchronousCall(&MessagingControllerSingleton::Instance());
425 event->setForAsynchronousCall(&MessagingController::getInstance());
427 Api::Messaging::ReqReceiverMessageSingleton::Instance().sendMessage(event); //send message
429 return converter->toJSValueRef(event->opId);
431 Catch(WrtDeviceApis::Commons::ConversionException) {
432 return JSTizenExceptionFactory::postException(context, exception,
433 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
435 Catch(WrtDeviceApis::Commons::NullPointerException) {
436 return JSTizenExceptionFactory::postException(context, exception,
437 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
439 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
440 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
441 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
444 return JSValueMakeNull(context);
447 JSValueRef JSMessagingService::loadMessageBody(JSContextRef context,
448 JSObjectRef function,
449 JSObjectRef thisObject,
450 size_t argumentCount,
451 const JSValueRef arguments[],
452 JSValueRef* exception)
456 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
459 LogError("Null pointer");
460 return JSTizenExceptionFactory::postException(context, exception,
461 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
464 JSContextRef globalContext = priv->getContext(); //create global Context
465 LogInfo(">>> argument count : " << argumentCount);
467 if (argumentCount < 2) {
468 return JSTizenExceptionFactory::postException(context, exception,
469 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
472 Validator check(context, exception); //create check
474 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
475 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext); //create converter
479 EventMessagingServicePrivateDataPtr privateData( new EventMessagingServicePrivateData(callbackManager) );
482 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
484 MessagingExportedNames[MESSAGING_FUNCTION_API_LOAD_MESSAGE_BODY]);
485 TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);
487 if (JSValueIsObject(context, arguments[0]) && Validator(context).isCallback(arguments[0])) //check
489 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[0]));
490 if (converter->toFunctionOrNull(arguments[1]) && Validator(context).isCallback(arguments[1]) )
492 callbackManager->setOnError(converter->toFunctionOrNull(arguments[1]));
495 Api::Messaging::EventMessagingServicePtr event(new Api::Messaging::EventMessagingService()); //create event
496 if ( JSValueIsObject(context, arguments[2]) )
498 event->m_message = converter->toIMessage(arguments[2]) ;
499 LogDebug("Message Type =" << event->m_message->getMessageType());
500 privateData->setMessageJSValueRef(arguments[2]); //set Message JSValueRef.
504 return JSTizenExceptionFactory::postException(context, exception,
505 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
508 IMessagingServicePtr imessagingService = priv->getObject();
509 int opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_DOWNLOAD_BODY);
510 LogDebug("Operation ID is = " << opId);
513 imessagingService->setMessageToOpId(opId, event->m_message);
514 event->setEventType(EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY);
515 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
516 event->setForAsynchronousCall(&MessagingController::getInstance());
518 Api::Messaging::ReqReceiverMessageSingleton::Instance().loadMessageBody(event); //load message Body
520 return converter->toJSValueRef(event->opId);
524 return JSTizenExceptionFactory::postException(context, exception,
525 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
529 Catch(WrtDeviceApis::Commons::ConversionException) {
530 return JSTizenExceptionFactory::postException(context, exception,
531 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
533 Catch(WrtDeviceApis::Commons::NullPointerException) {
534 return JSTizenExceptionFactory::postException(context, exception,
535 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
537 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
538 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
539 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
541 Catch(WrtDeviceApis::Commons::PlatformException) {
542 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
543 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
546 return JSValueMakeNull(context);
550 JSValueRef JSMessagingService::loadMessageAttachment(JSContextRef context,
551 JSObjectRef function,
552 JSObjectRef thisObject,
553 size_t argumentCount,
554 const JSValueRef arguments[],
555 JSValueRef* exception)
559 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
562 LogError("Private Object is Null pointer");
563 return JSTizenExceptionFactory::postException(context, exception,
564 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
567 JSContextRef globalContext = priv->getContext(); //create global Context
568 LogInfo(">>> argument count : " << argumentCount);
570 if (argumentCount < 2) {
571 return JSTizenExceptionFactory::postException(context, exception,
572 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
575 Validator check(context, exception); //create check
577 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
578 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext); //create converter
582 if (JSValueIsObject(context, arguments[0]) && Validator(context).isCallback(arguments[0])) //check
584 EventMessagingServicePrivateDataPtr privateData( new EventMessagingServicePrivateData(callbackManager) );
586 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[0]));
587 if (converter->toFunctionOrNull(arguments[1]) && Validator(context).isCallback(arguments[1]) )
589 callbackManager->setOnError(converter->toFunctionOrNull(arguments[1]));
593 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
595 MessagingExportedNames[MESSAGING_FUNCTION_API_LOAD_MESSAGE_ATTACHMENT]);
596 TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);
598 IMessagingServicePtr imessagingService = priv->getObject();
599 Api::Messaging::EventMessagingServicePtr event(new Api::Messaging::EventMessagingService()); //create event
603 event->setEventType(EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT);
604 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
605 event->setForAsynchronousCall(&MessagingController::getInstance());
607 if (argumentCount == 3 && !JSValueIsNull(context, arguments[2]))
609 Api::Messaging::IAttachmentPtr iAttchmentPtr = converter->toIAttachment(arguments[2]);
610 privateData->setMessageJSValueRef(arguments[2]); //set Message JSValueRef.
613 event->m_attachment = iAttchmentPtr;
614 event->m_message = event->m_attachment->getMessage();
616 if (!event->m_message)
617 return JSTizenExceptionFactory::postException(context, exception,
618 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
622 return JSTizenExceptionFactory::postException(context, exception,
623 JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
628 return JSTizenExceptionFactory::postException(context, exception,
629 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
632 int opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_DOWNLOAD_ATTACHMENT);
633 LogDebug("Operation ID is = " << opId);
636 imessagingService->setMessageToOpId(opId, event->m_message);
638 LogInfo("pendingOperation");
639 //JSValueRef pendingOperation = WrtDeviceApis::CommonsJavaScript::makePendingOperation(globalContext, event); //create pendingOperation.
640 Api::Messaging::ReqReceiverMessageSingleton::Instance().loadMessageAttachment(event); //load message Body
642 return converter->toJSValueRef(event->opId);
647 return JSTizenExceptionFactory::postException(context, exception,
648 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
652 Catch(WrtDeviceApis::Commons::ConversionException) {
653 return JSTizenExceptionFactory::postException(context, exception,
654 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
656 Catch(WrtDeviceApis::Commons::NullPointerException) {
657 return JSTizenExceptionFactory::postException(context, exception,
658 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
660 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
661 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
662 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
664 Catch(WrtDeviceApis::Commons::PlatformException) {
665 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
666 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
669 return JSValueMakeNull(context);
672 JSValueRef JSMessagingService::sync(JSContextRef context, JSObjectRef function, JSObjectRef thisObject,
673 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
677 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
681 JSContextRef globalContext = priv->getContext(); //get global Context
683 LogInfo(">>> argument count : " << argumentCount);
685 if (argumentCount > 4 || argumentCount < 2 ) {
686 return JSTizenExceptionFactory::postException(context, exception,
687 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
690 Validator check(context, exception); //check context
692 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
693 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext); //create converter
695 IMessagingServicePtr imessagingService = priv->getObject(); //get MessagingService.
697 Api::Messaging::MessageType msgType = (Api::Messaging::MessageType)imessagingService->getType();
698 LogInfo("msgType :" << msgType);
702 int limit = -1; //sync limit
704 if (JSValueIsObject(context, arguments[0]) && Validator(context).isCallback(arguments[0])) //check
706 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[0]));
707 if (converter->toFunctionOrNull(arguments[1]) && Validator(context).isCallback(arguments[1]) )
709 callbackManager->setOnError(converter->toFunctionOrNull(arguments[1]));
713 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
715 MessagingExportedNames[MESSAGING_FUNCTION_API_SYNC]);
716 TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);
718 EventMessagingServicePrivateDataPtr privateData( new EventMessagingServicePrivateData(callbackManager) );
720 Api::Messaging::EventMessagingServicePtr event(new Api::Messaging::EventMessagingService()); //create event
722 if (argumentCount == 3 && !JSValueIsNull(context, arguments[2]))
723 limit = converter->toInt(arguments[2]);
725 LogDebug("limit : " << limit);
727 event->opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_SYNC);
728 event->m_sync_account_id = imessagingService->getAccountID() ;
729 event->m_sync_limit = limit;
730 event->m_messagingService = imessagingService;
732 event->setEventType(EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_SYNC);
733 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
734 event->setForAsynchronousCall(&MessagingController::getInstance());
736 Api::Messaging::ReqReceiverMessageSingleton::Instance().sync(event); //load message Body
738 return converter->toJSValueRef(event->opId);
743 return JSTizenExceptionFactory::postException(context, exception,
744 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
747 Catch(WrtDeviceApis::Commons::ConversionException) {
748 return JSTizenExceptionFactory::postException(context, exception,
749 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
751 Catch(WrtDeviceApis::Commons::NullPointerException) {
752 return JSTizenExceptionFactory::postException(context, exception,
753 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
755 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
756 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
757 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
759 Catch(WrtDeviceApis::Commons::PlatformException) {
760 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
761 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
767 LogError("Null pointer");
768 return JSTizenExceptionFactory::postException(context, exception,
769 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
772 return JSValueMakeNull(context);
777 JSValueRef JSMessagingService::syncFolder(JSContextRef context, JSObjectRef function, JSObjectRef thisObject,
778 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
782 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
785 JSContextRef globalContext = priv->getContext(); //get global Context
788 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
790 MESSAGING_FUNCTION_API_SYNC);
792 LogInfo(">>> argument count : " << argumentCount);
794 if (argumentCount > 4 || argumentCount < 1) {
795 return JSTizenExceptionFactory::postException(context, exception,
796 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
799 Validator check(context, exception); //check context
801 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
802 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext); //create converter
804 IMessagingServicePtr imessagingService = priv->getObject(); //get MessagingService.
807 Api::Messaging::MessageType msgType = (Api::Messaging::MessageType)imessagingService->getType();
808 LogInfo("msgType :" << msgType);
814 if (JSValueIsObject(context, arguments[0]) && !Validator(context).isCallback(arguments[0])) //check
817 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
819 MessagingExportedNames[MESSAGING_FUNCTION_API_SYNC_FOLDER]);
820 TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);
822 EventMessagingServicePrivateDataPtr privateData( new EventMessagingServicePrivateData(callbackManager) );
823 //private->setSyncAccountID( imessagingService->getAccountID()); //set account ID
825 Api::Messaging::EventMessagingServicePtr event(new Api::Messaging::EventMessagingService()); //create event
828 IMessageFolderPtr folder = converter->toIMessageFolder(arguments[0]);
831 event->m_folder_name = folder->getName(); //it will be changed to the folder ptr
834 if ( argumentCount > 1 )
836 if (JSValueIsObject(context, arguments[1]) && Validator(context).isCallback(arguments[1])) //check
838 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[1]));
841 if ( argumentCount > 2 )
843 if (JSValueIsObject(context, arguments[2]) && Validator(context).isCallback(arguments[2])) //check
845 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
849 if (argumentCount == 4 && !JSValueIsNull(context, arguments[3]))
850 limit = converter->toInt(arguments[3]);
854 LogDebug("limit : " << limit);
855 LogDebug("folderName : " << event->m_folder_name);
856 event->opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_SYNC_FOLDER);
857 event ->m_sync_account_id = imessagingService->getAccountID() ;
858 event ->m_sync_limit = limit;
859 event->m_messagingService = imessagingService;
861 event->setEventType(EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER);
862 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
863 event->setForAsynchronousCall(&MessagingController::getInstance());
865 Api::Messaging::ReqReceiverMessageSingleton::Instance().syncFolder(event); //load message Body
867 return converter->toJSValueRef(event->opId);
872 return JSTizenExceptionFactory::postException(context, exception,
873 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
876 Catch(WrtDeviceApis::Commons::ConversionException) {
877 return JSTizenExceptionFactory::postException(context, exception,
878 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
880 Catch(WrtDeviceApis::Commons::NullPointerException) {
881 return JSTizenExceptionFactory::postException(context, exception,
882 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
884 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
885 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
886 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
888 Catch(WrtDeviceApis::Commons::PlatformException) {
889 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,
890 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
896 LogError("Null pointer");
897 return JSTizenExceptionFactory::postException(context, exception,
898 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
901 return JSValueMakeNull(context);
906 JSValueRef JSMessagingService::cancelOperation(JSContextRef context, JSObjectRef function, JSObjectRef thisObject,
907 size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
911 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject)); //get private object
915 IMessagingServicePtr imessagingService = priv->getObject();
918 LogInfo(">>> argument count : " << argumentCount);
920 if ( argumentCount == 1 && !Validator(context).isCallback(arguments[0]))
922 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
923 Api::Messaging::MessageType msgType = (Api::Messaging::MessageType)imessagingService->getType();
924 LogInfo("msgType :" << msgType);
926 int opId = converter->toLong( arguments[0] ); //Fetch operation ID
927 int handle = imessagingService->getHandleFromOpId(opId);
930 ThrowMsg(WrtDeviceApis::Commons::NotFoundException,"Operation ID Not found");
933 int opType = imessagingService->getOpTypeFromOpId(opId);
934 LogInfo("operation ID :" << opId << " operation Type : " << opType);
936 if ( opType == Api::Messaging::MESSAGING_SERVICE_OP_SEND_MESSAGE)
938 IMessagePtr msg = imessagingService->getMessageFromOpId(opId);
941 LogDebug("Call Cancel Function");
942 msg->sendCancel(handle);
943 imessagingService->deleteOpId(opId);
948 if (msgType == Api::Messaging::EMAIL)
950 if ( opType == Api::Messaging::MESSAGING_SERVICE_OP_DOWNLOAD_BODY )
952 IMessagePtr msg = imessagingService->getMessageFromOpId(opId);
953 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
954 LogDebug("Cancel Download Body , handle = " << handle);
955 email->downloadBodyCancel(handle);
957 else if ( opType == Api::Messaging::MESSAGING_SERVICE_OP_SYNC )
959 LogDebug("Cancel Sync , handle = " << handle);
960 imessagingService->syncCancel(handle);
962 else if ( opType == Api::Messaging::MESSAGING_SERVICE_OP_SYNC_FOLDER )
964 LogDebug("Cancel Sync Folder, handle = " << handle);
965 imessagingService->syncFolderCancel(handle);
967 imessagingService->deleteOpId(opId);
971 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "Operation Type is mismatched");
978 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,"invalid argument exception");
982 Catch (WrtDeviceApis::Commons::InvalidArgumentException){
983 LogError("Exception: " << _rethrown_exception.GetMessage());
984 return JSTizenExceptionFactory::postException(context, exception,
985 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
987 Catch (WrtDeviceApis::Commons::NotFoundException){
988 LogError("Exception: " << _rethrown_exception.GetMessage());
989 return JSTizenExceptionFactory::postException(context, exception,
990 JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
992 Catch (WrtDeviceApis::Commons::UnsupportedException){
993 LogError("Exception: " << _rethrown_exception.GetMessage());
994 return JSTizenExceptionFactory::postException(context, exception,
995 JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);
997 Catch (WrtDeviceApis::Commons::Exception){
998 LogError("Exception: " << _rethrown_exception.GetMessage());
999 return JSTizenExceptionFactory::postException(context, exception,
1000 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
1006 LogDebug(" Private Object is NULL ");
1007 return JSTizenExceptionFactory::postException(context, exception,
1008 JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
1012 return JSValueMakeNull(context);