merge wrt-plugins-tizen_0.2.0-12
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Messaging / JSMessagingService.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. 
15 */
16
17
18 /*
19  * @file        JSPluginTemplete.cpp
20  * @author      Jaehyun Park (jaehyun77.park@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSPluginTemplete class
23  */
24
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>
34
35 #include <API/Messaging/ReqReceiverMessage.h>
36 #include <Tizen/Common/JSTizenExceptionFactory.h>
37 #include <Tizen/Common/JSTizenException.h>
38 #include <Tizen/Common/SecurityExceptions.h>
39
40 #include "MessagingErrorMsg.h"
41 #include "ConverterMessage.h"
42 #include "MessagingListener.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"
48
49 using namespace std;
50 using namespace DPL;
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;
56
57 namespace TizenApis {
58 namespace Tizen1_0 {
59
60         JSClassRef JSMessagingService::m_jsClassRef = NULL;
61         JSClassDefinition JSMessagingService::m_classInfo = {
62                 0,
63                 kJSClassAttributeNone,
64                 "MessageService",
65                 NULL,
66                 m_property,
67                 m_function,
68                 initialize,
69                 finalize,
70                 NULL, //hasProperty,
71                 NULL, //getProperty,
72                 NULL, //setProperty,
73                 NULL, //deleteProperty,Geolocation
74                 NULL, //getPropertyNames,
75                 NULL,
76                 NULL,
77                 hasInstance,
78                 NULL
79         };
80
81         JSStaticValue JSMessagingService::m_property[] =
82         {
83             {"id",                      getProperty, NULL, kJSPropertyAttributeReadOnly},
84             {"type",                            getProperty, NULL, kJSPropertyAttributeReadOnly},
85             {"name",                            getProperty, NULL, kJSPropertyAttributeReadOnly},
86             {"messagingStorage",                                getProperty, NULL, kJSPropertyAttributeReadOnly},
87             { 0, 0, 0, 0 }
88         };
89
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 },
98                         { 0, 0, 0 }
99         };
100
101         const JSClassRef JSMessagingService::getClassRef() {
102                 if (!m_jsClassRef) {
103                         m_jsClassRef = JSClassCreate(&m_classInfo);
104                 }
105                 return m_jsClassRef;
106         }
107
108         void JSMessagingService::initialize(JSContextRef context, JSObjectRef object) {
109                 LogDebug("creation messaging Service instance");
110         }
111
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));
116                 delete priv;
117         }
118
119         bool JSMessagingService::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
120                 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
121         }
122
123         JSObjectRef JSMessagingService::createJSObject(JSContextRef context, const Api::Messaging::IMessagingServicePtr &messagingService)
124         {
125                 JSMessagingServicePriv* priv = new JSMessagingServicePriv( context, messagingService);  //make private class.
126                 return JSObjectMake(context, getClassRef(), priv);
127         }
128
129         JSValueRef JSMessagingService::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
130         {
131                 LogDebug("<<< ");
132                 JSValueRef retVal = NULL;
133
134                 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(object));        //get private object
135             if (priv)
136                 {
137                         IMessagingServicePtr imessagingService = priv->getObject();
138                         Try{
139                                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
140
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());
149                                         
150                                         std::stringstream stream;
151                                         stream << imessagingService->getAccountID();
152                                         if (stream.fail()) {        
153                                                 ThrowMsg(WrtDeviceApis::Commons::UnknownException,"Couldn't convert e-mail account id");
154                                         }
155                                         
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);
161                                 } else{
162                                         retVal = JSValueMakeUndefined(context);
163                                 }
164
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);                    
169                         }
170
171                         LogDebug(">>>");
172                         return retVal;
173                         
174                 }
175                 else
176                 {       
177                         LogDebug(" Private Object is NULL ");
178                         return JSTizenExceptionFactory::postException(context, exception, 
179                                         JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
180                 }
181                 
182         }
183
184         JSValueRef JSMessagingService::createMessage(JSContextRef context, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount,
185                 const JSValueRef arguments[], JSValueRef* exception) 
186         {
187
188                 LogInfo("<<<");
189                 LogDebug("arumentConunt:" << argumentCount);
190
191                 JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
192                 if (priv)
193                 {
194                         AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
195                                 priv->getContext(),
196                                 MessagingExportedNames[MESSAGING_FUNCTION_API_CREATE_MESSAGE]);
197                         TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
198                                 
199                         try {
200                                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
201                                 JSContextRef l_globalContext = converter->toJSGlobalContext(thisObject);
202                                 Validator check(context, exception);
203
204                                 IMessagingServicePtr imessagingService = priv->getObject();                             //get MessagingService.
205                                 JSValueRef objMsg = NULL;
206                                 
207                                 Api::Messaging::MessageType msgType = (Api::Messaging::MessageType)imessagingService->getType();
208                                 LogInfo("msgType :" << msgType);
209         
210                                 if (msgType == Api::Messaging::EMAIL)
211                                 {
212                                         Api::Messaging::EmailAccountInfo account = imessagingService->getCurrentEmailAccount();
213                                         LogDebug("Account Address:" << &account);
214                                         objMsg = JSMessage::createJSObject(l_globalContext, account, msgType);          //make message JSValueRef.
215                                 }
216                                 else
217                                 {
218                                         objMsg = JSMessage::createJSObject(l_globalContext, msgType);           //make message JSValueRef.
219                                 }
220
221                                 //set Properties
222                                 if ( argumentCount == 1)
223                                 {
224                                         IMessagePtr msg = converter->toIMessage(objMsg) ;
225
226                                         if (!msg)
227                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException,"platform exception");
228
229                                         LogDebug ("##### msg type is " << msg->getId());
230                                                                                 
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"));
238
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);   
247
248                                         //subject
249                                          if (!JSValueIsUndefined(l_globalContext, subjectData) ) 
250                                          {
251                                                 LogDebug ( " Subject : " << converter->toString(subjectData) );                                         
252                                                 std::string subject = converter->toString(subjectData);
253                                                  switch (msgType) 
254                                                  {
255                                                 case Api::Messaging::MMS:
256                                                 {
257                                                     Api::Messaging::IMmsPtr mms = Api::Messaging::MessageFactory::convertToMms(msg);
258                                                     mms->setSubject(subject);
259                                                     break;
260                                                 }
261                                                 case Api::Messaging::EMAIL:
262                                                 {
263                                                     Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
264                                                     email->setSubject(subject);
265                                                     break;
266                                                 }
267                                                   default:
268                                                   {
269                                                     LogError("message not supported");
270                                                     //Throw(WrtDeviceApis::Commons::UnsupportedException);
271                                                     //break;
272                                                   }
273                                                 }
274                                          }
275                                         //to
276                                         if (!JSValueIsUndefined(l_globalContext, toData) )
277                                         {
278                                                 Api::Messaging::Recipients to = converter->toRecipients(toData);
279                                                 LogDebug("setting to field, size=" << to.getRecipientSize());
280                                                 msg->setToRecipients(to);
281                                         }
282                                         //cc
283                                         if (msgType == Api::Messaging::EMAIL )
284                                         {
285                                                 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
286                                                 if (!JSValueIsUndefined(l_globalContext, ccData))
287                                                 {
288                                                         Api::Messaging::Recipients cc = converter->toRecipients(ccData);
289                                                 email->setCcRecipients(cc);
290                                                 }
291
292                                                 if (!JSValueIsUndefined(l_globalContext, bccData))
293                                                 {
294                                                         Api::Messaging::Recipients bcc = converter->toRecipients(bccData);
295                                                 email->setBccRecipients(bcc);
296                                                 }
297
298                                                 if (!JSValueIsUndefined(l_globalContext, htmlBodyData))
299                                                 {
300                                                         std::string body = converter->toString(htmlBodyData);
301                                                         LogDebug("html body : " << body);
302                                                         email->setHtmlBody(body);
303                                                 }
304
305                                                 if (!JSValueIsUndefined(l_globalContext, priorityData))
306                                                 {
307                                                         email->setPriority(converter->toMessagePriority(priorityData));
308                                                 }
309                                                 
310                                         }
311
312                                         if (!JSValueIsUndefined(l_globalContext, plainBodyData) )
313                                         {
314                                                 std::string body = converter->toString(plainBodyData);
315                                                 LogDebug("plain body  : " << body);
316                                                 msg->setBody(body);
317                                         }
318                         
319                                 }
320                                 return objMsg;
321                                                                 
322                         }
323                         Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
324                         return JSTizenExceptionFactory::postException(context, exception, 
325                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);    
326                         }
327                         Catch(WrtDeviceApis::Commons::ConversionException) {
328                         return JSTizenExceptionFactory::postException(context, exception, 
329                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);        
330                         }
331
332                         LogInfo(">>>");
333                         return JSValueMakeUndefined(context);
334                 }
335                 else
336                 {
337                         LogDebug(" Private Object is NULL ");
338                         return JSTizenExceptionFactory::postException(context, exception, 
339                                         JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
340                 }
341
342         }
343
344 JSValueRef JSMessagingService::sendMessage(JSContextRef context,
345         JSObjectRef function,
346         JSObjectRef thisObject,
347         size_t argumentCount,
348         const JSValueRef arguments[],
349         JSValueRef* exception)
350 {
351     LogDebug("entered");
352
353     JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
354
355     if (!priv) {
356         LogError("Null pointer");
357         return JSTizenExceptionFactory::postException(context, exception, 
358                            JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);         
359     }
360
361     JSContextRef globalContext = priv->getContext();
362
363     LogInfo(">>> argument count : " << argumentCount);
364
365     if (argumentCount < 2) {
366                 return JSTizenExceptionFactory::postException(context, exception, 
367                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
368     }
369
370     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
371     WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr recipientCallbackManager;
372
373     ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);   
374         Try {           
375
376                 if ( argumentCount > 1 && argumentCount < 4)
377                 {
378                         if ( argumentCount > 1)
379                         {       
380                                 if (JSValueIsObject(context, arguments[1]) && !Validator(context).isCallback(arguments[1])) //check
381                                 {       
382                                         MessageSendCallback callbacks = converter->toMessageSendCallback(arguments[1]);
383                                         callbackManager->setOnSuccess( converter->toFunctionOrNull(callbacks.onSuccess) );
384                                 }
385                                 else
386                                 {
387                                 callbackManager->setOnSuccess(converter->toFunction(arguments[1]));
388                         }
389
390                                 if (    argumentCount == 3)
391                                 {
392                                         callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
393                                 }
394                         }
395                 }
396                 else
397                 {
398                         ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,"invalid argument exception");
399                 }
400
401               //check permission.
402                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
403                         priv->getContext(),
404                         MessagingExportedNames[MESSAGING_FUNCTION_API_SEND_MESSAGE]);
405                 TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);
406                 
407            //create PrivateData
408         EventSendMessagePrivateDataPtr privateData( new EventSendMessagePrivateData(callbackManager, recipientCallbackManager) );
409
410         Api::Messaging::EventSendMessagePtr event(new Api::Messaging::EventSendMessage());
411
412           LogDebug("Event size");
413
414           IMessagingServicePtr imessagingService = priv->getObject();
415         
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));
422           
423         //event->setForAsynchronousCall(&JSMessagingListener::getInstance());
424         //event->setForAsynchronousCall(&MessagingControllerSingleton::Instance());
425           event->setForAsynchronousCall(&MessagingController::getInstance());
426      
427         Api::Messaging::ReqReceiverMessageSingleton::Instance().sendMessage(event);     //send message
428
429         return converter->toJSValueRef(event->opId);
430     }
431     Catch(WrtDeviceApis::Commons::ConversionException) {
432                 return JSTizenExceptionFactory::postException(context, exception, 
433                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);        
434     }
435     Catch(WrtDeviceApis::Commons::NullPointerException) {
436                 return JSTizenExceptionFactory::postException(context, exception, 
437                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);        
438     }
439     Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
440         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
441                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
442     }
443
444     return JSValueMakeNull(context);
445 }
446
447 JSValueRef JSMessagingService::loadMessageBody(JSContextRef context,
448         JSObjectRef function,
449         JSObjectRef thisObject,
450         size_t argumentCount,
451         const JSValueRef arguments[],
452         JSValueRef* exception)
453 {
454     LogDebug("entered");
455
456     JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
457
458     if (!priv) {
459         LogError("Null pointer");
460                 return JSTizenExceptionFactory::postException(context, exception, 
461                    JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
462     }
463
464     JSContextRef globalContext = priv->getContext();            //create global Context
465     LogInfo(">>> argument count : " << argumentCount);
466
467         if (argumentCount < 2) {
468                 return JSTizenExceptionFactory::postException(context, exception, 
469                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
470         }
471
472         Validator check(context, exception);    //create check
473
474         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
475         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext); //create converter
476
477         Try {
478                 //create event
479                 EventMessagingServicePrivateDataPtr privateData( new EventMessagingServicePrivateData(callbackManager) );
480
481                 //check permission.
482                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
483                         priv->getContext(),
484                         MessagingExportedNames[MESSAGING_FUNCTION_API_LOAD_MESSAGE_BODY]);
485                         TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);
486
487                 if ( JSValueIsObject(context, arguments[0]) )
488                 {
489                         switch(argumentCount){
490                                 case 3:
491                                 {
492                                         if ( Validator(context).isCallback(arguments[2]))
493                                         {
494                                                 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
495                                         }
496                                         else
497                                         {
498                                                 return JSTizenExceptionFactory::postException(context, exception, 
499                                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
500                                         }
501                                 }
502                                 case 2:
503                                 {
504                                         if ( Validator(context).isCallback(arguments[1]))
505                                         {
506                                                 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[1]));
507                                         }
508                                         else
509                                         {
510                                                 return JSTizenExceptionFactory::postException(context, exception, 
511                                                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
512                                         }       
513                                 }
514                         }
515
516                         Api::Messaging::EventMessagingServicePtr event(new Api::Messaging::EventMessagingService());     //create even
517                         event->m_message = converter->toIMessage(arguments[0]) ;
518                         if ( event->m_message )
519                         {       
520                                 int msgType = event->m_message->getMessageType();
521                                 LogDebug("Message Type =" << event->m_message->getMessageType());
522                                 if (msgType == Api::Messaging::EMAIL)
523                                 {
524                                         Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(event->m_message);
525                                         if (email && email->isBodyDownloaded() > 0)
526                                         {
527                                                 callbackManager->callOnSuccess(arguments[0]);
528                                                 return JSValueMakeNull(context);
529                                         }
530                                 }
531                                 else 
532                                 {
533                                         callbackManager->callOnSuccess(arguments[0]);
534                                         return JSValueMakeNull(context);
535                                 }
536
537                                 privateData->setMessageJSValueRef(arguments[0]);        //set Message JSValueRef.
538                         }
539
540                         IMessagingServicePtr imessagingService = priv->getObject();
541                         int opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_DOWNLOAD_BODY);
542                         LogDebug("Operation ID is = " << opId);
543
544                         event->opId = opId;
545                         imessagingService->setMessageToOpId(opId, event->m_message);
546                         event->setEventType(EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_BODY);
547                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
548                         event->setForAsynchronousCall(&MessagingController::getInstance());
549
550                         Api::Messaging::ReqReceiverMessageSingleton::Instance().loadMessageBody(event); //load message Body
551
552                         return converter->toJSValueRef(event->opId);
553                 }
554                 else
555                 {
556                         return JSTizenExceptionFactory::postException(context, exception, 
557                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
558                 }
559
560         }
561         Catch(WrtDeviceApis::Commons::ConversionException) {
562                 return JSTizenExceptionFactory::postException(context, exception, 
563                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
564         }
565         Catch(WrtDeviceApis::Commons::NullPointerException) {
566                 return JSTizenExceptionFactory::postException(context, exception, 
567                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
568         }
569         Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
570                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
571                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
572         }
573         Catch(WrtDeviceApis::Commons::PlatformException) {
574                 callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
575                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
576         }
577
578         return JSValueMakeNull(context);
579
580 }
581
582 JSValueRef JSMessagingService::loadMessageAttachment(JSContextRef context,
583         JSObjectRef function,
584         JSObjectRef thisObject,
585         size_t argumentCount,
586         const JSValueRef arguments[],
587         JSValueRef* exception)
588 {
589     LogDebug("entered");
590
591     JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
592
593         if (!priv) {
594                 LogError("Private Object is Null pointer");
595                 return JSTizenExceptionFactory::postException(context, exception, 
596                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
597         }
598         
599         JSContextRef globalContext = priv->getContext();                //create global Context
600         LogInfo(">>> argument count : " << argumentCount);
601
602         if (argumentCount < 2) {
603                 return JSTizenExceptionFactory::postException(context, exception, 
604                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
605         }
606
607         Validator check(context, exception);    //create check
608
609         WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
610         ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext); //create converter
611
612         Try {
613
614                 //check permission.
615                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
616                         priv->getContext(),
617                         MessagingExportedNames[MESSAGING_FUNCTION_API_LOAD_MESSAGE_ATTACHMENT]);
618                         TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);       
619
620                 if (JSValueIsObject(context, arguments[0]) && !JSValueIsNull(context, arguments[0]))
621                 {
622                         EventMessagingServicePrivateDataPtr privateData( new EventMessagingServicePrivateData(callbackManager) );
623                         if (!privateData)
624                                         return JSTizenExceptionFactory::postException(context, exception, 
625                                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
626                         
627                         switch(argumentCount){
628                                 case 3:
629                                 {
630                                         if ( Validator(context).isCallback(arguments[2]))
631                                         {
632                                                 callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
633                                         }
634                                         else
635                                         {
636                                                 return JSTizenExceptionFactory::postException(context, exception, 
637                                                         JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
638                                         }
639                                 }
640                                 case 2:
641                                 {
642                                         if ( Validator(context).isCallback(arguments[1]))
643                                         {
644                                                 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[1]));
645                                         }
646                                         else
647                                         {
648                                                 return JSTizenExceptionFactory::postException(context, exception, 
649                                                          JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
650                                         }       
651                                 }
652                         }
653
654                         Api::Messaging::EventMessagingServicePtr event(new Api::Messaging::EventMessagingService());     //create event
655                         event->setEventType(EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_LOAD_MESSAGE_ATTACHMENT);
656                         event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
657                         event->setForAsynchronousCall(&MessagingController::getInstance());
658                         
659                         Api::Messaging::IAttachmentPtr iAttchmentPtr = converter->toIAttachment(arguments[0]);
660                         if (iAttchmentPtr)
661                         {       
662                                 event->m_attachment = iAttchmentPtr;
663                                 event->m_message = event->m_attachment->getMessage();
664                                                                         
665                                 if (!event->m_message)
666                                         return JSTizenExceptionFactory::postException(context, exception, 
667                                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
668
669                                 privateData->setMessageJSValueRef(arguments[0]); //set attachment JSValueRef.
670                         }
671                         else
672                         {
673                                 return JSTizenExceptionFactory::postException(context, exception, 
674                                         JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
675                         }
676                         
677                         IMessagingServicePtr imessagingService = priv->getObject();
678                         if (imessagingService)
679                         {       
680                                 int opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_DOWNLOAD_ATTACHMENT);
681                                 LogDebug("Operation ID is = " << opId);
682
683                                 event->opId = opId;
684                                 imessagingService->setMessageToOpId(opId, event->m_message);
685                                 
686                                 //JSValueRef pendingOperation = WrtDeviceApis::CommonsJavaScript::makePendingOperation(globalContext, event);   //create pendingOperation.
687                                 Api::Messaging::ReqReceiverMessageSingleton::Instance().loadMessageAttachment(event);   //load message Body
688                                 
689                                 return converter->toJSValueRef(event->opId);
690                         }
691                         else
692                         {
693                                 return JSTizenExceptionFactory::postException(context, exception, 
694                                         JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
695                         }
696                         
697                         
698                 }
699                 else
700                 {
701                         return JSTizenExceptionFactory::postException(context, exception, 
702                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
703                 }
704
705         }
706         Catch(WrtDeviceApis::Commons::ConversionException) {
707                         return JSTizenExceptionFactory::postException(context, exception, 
708                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
709         }
710         Catch(WrtDeviceApis::Commons::NullPointerException) {
711                         return JSTizenExceptionFactory::postException(context, exception, 
712                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
713         }
714         Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
715                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
716                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
717         }
718         Catch(WrtDeviceApis::Commons::PlatformException) {
719                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
720                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
721         }
722
723         return JSValueMakeNull(context);
724 }
725
726 JSValueRef JSMessagingService::sync(JSContextRef context, JSObjectRef function, JSObjectRef thisObject,
727                         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
728 {
729
730         LogDebug("entered");
731         JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
732
733         if (priv) {
734
735                 JSContextRef globalContext = priv->getContext();                //get global Context
736
737                 LogInfo(">>> argument count : " << argumentCount);
738
739                 if (argumentCount > 4 || argumentCount < 2 ) {
740                         return JSTizenExceptionFactory::postException(context, exception, 
741                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
742                 }
743
744                 Validator check(context, exception);    //check context
745
746                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
747                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext); //create converter
748
749                 IMessagingServicePtr imessagingService = priv->getObject();                             //get MessagingService.
750
751                 Api::Messaging::MessageType msgType = (Api::Messaging::MessageType)imessagingService->getType();
752                 LogInfo("msgType :" << msgType);
753
754                 Try {
755
756                         int limit = -1; //sync limit
757
758                         if (JSValueIsObject(context, arguments[0]) && Validator(context).isCallback(arguments[0])) //check
759                         {
760                                 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[0]));
761                                 if (converter->toFunctionOrNull(arguments[1]) && Validator(context).isCallback(arguments[1]) )
762                                 {
763                                         callbackManager->setOnError(converter->toFunctionOrNull(arguments[1]));
764                                 }
765
766                                 //check permission.
767                                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
768                                         priv->getContext(),
769                                         MessagingExportedNames[MESSAGING_FUNCTION_API_SYNC]);
770                                 TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);       
771                                 
772                                 EventMessagingServicePrivateDataPtr privateData( new EventMessagingServicePrivateData(callbackManager) );
773
774                                 Api::Messaging::EventMessagingServicePtr event(new Api::Messaging::EventMessagingService());     //create event
775
776                                 if (argumentCount == 3 && !JSValueIsNull(context, arguments[2]))
777                                         limit = converter->toInt(arguments[2]);
778
779                                 LogDebug("limit : " << limit);
780
781                                 event->opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_SYNC);
782                                 event->m_sync_account_id = imessagingService->getAccountID() ;
783                                 event->m_sync_limit = limit;
784                                 event->m_messagingService = imessagingService;
785
786                                 event->setEventType(EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_SYNC);
787                                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
788                                 event->setForAsynchronousCall(&MessagingController::getInstance());
789
790                                 Api::Messaging::ReqReceiverMessageSingleton::Instance().sync(event);    //load message Body
791
792                                 return converter->toJSValueRef(event->opId);
793
794                         }
795                         else
796                         {
797                                 return JSTizenExceptionFactory::postException(context, exception, 
798                                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
799                         }
800                 }
801                 Catch(WrtDeviceApis::Commons::ConversionException) {
802                         return JSTizenExceptionFactory::postException(context, exception, 
803                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
804                 }
805                 Catch(WrtDeviceApis::Commons::NullPointerException) {
806                         return JSTizenExceptionFactory::postException(context, exception, 
807                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
808                 }
809                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
810                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
811                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
812                 }
813                 Catch(WrtDeviceApis::Commons::PlatformException) {
814                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
815                                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
816                 }
817         
818         }
819         else
820         {               
821                 LogError("Null pointer");
822                 return JSTizenExceptionFactory::postException(context, exception, 
823                    JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
824         }
825
826         return JSValueMakeNull(context);
827
828 }
829
830
831 JSValueRef JSMessagingService::syncFolder(JSContextRef context, JSObjectRef function, JSObjectRef thisObject,
832                         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
833 {
834         LogDebug("entered");
835
836         JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));
837
838         if (priv) {
839                 JSContextRef globalContext = priv->getContext();                //get global Context
840
841 #if 0
842         AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
843                 globalContext,
844                 MESSAGING_FUNCTION_API_SYNC);
845 #endif
846                 LogInfo(">>> argument count : " << argumentCount);
847
848                 if (argumentCount > 4 || argumentCount < 1) {
849                         return JSTizenExceptionFactory::postException(context, exception, 
850                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
851                 }
852
853                 Validator check(context, exception);    //check context
854
855                 WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr callbackManager = JSCallbackManager::createObject(globalContext);
856                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(globalContext); //create converter
857
858                 IMessagingServicePtr imessagingService = priv->getObject();                     //get MessagingService.
859
860                 Api::Messaging::MessageType msgType = (Api::Messaging::MessageType)imessagingService->getType();
861                 LogInfo("msgType :" << msgType);
862
863                 Try {
864
865                         int limit = -1;
866
867                         if (JSValueIsObject(context, arguments[0]) && !Validator(context).isCallback(arguments[0])) //check
868                         {       
869                                 AceSecurityStatus status = MESSAGING_CHECK_ACCESS(
870                                         priv->getContext(),
871                                         MessagingExportedNames[MESSAGING_FUNCTION_API_SYNC_FOLDER]);
872                                 TIZEN_ASYNC_CBM_ACCESS_HANDLER(status, context, callbackManager);
873
874                                 EventMessagingServicePrivateDataPtr privateData( new EventMessagingServicePrivateData(callbackManager) );
875                                 //private->setSyncAccountID( imessagingService->getAccountID());                  //set account ID
876
877                                 Api::Messaging::EventMessagingServicePtr event(new Api::Messaging::EventMessagingService());     //create event
878                                 
879                         
880                                 IMessageFolderPtr folder = converter->toIMessageFolder(arguments[0]);
881                                 if ( folder )
882                                 {
883                                         //event->m_folder_name = folder->getName();             //it will be changed to the folder ptr
884                                         event->m_folder_name = folder->getPath();               //it will be changed to the folder ptr
885                                 }
886
887                                 if ( argumentCount > 1 )
888                                 {
889                                         if (JSValueIsObject(context, arguments[1]) && Validator(context).isCallback(arguments[1])) //check
890                                         {
891                                                 callbackManager->setOnSuccess(converter->toFunctionOrNull(arguments[1]));
892                                         }
893
894                                         if ( argumentCount > 2 )
895                                         {
896                                                 if (JSValueIsObject(context, arguments[2]) && Validator(context).isCallback(arguments[2])) //check
897                                                 {
898                                                         callbackManager->setOnError(converter->toFunctionOrNull(arguments[2]));
899                                                 }
900                                         }
901
902                                         if (argumentCount == 4 && !JSValueIsNull(context, arguments[3]))
903                                                 limit = converter->toInt(arguments[3]);
904                                         
905                                 }
906
907                                 LogDebug("limit : " << limit);
908                                 LogDebug("folderName : " << event->m_folder_name);
909                                 event->opId = imessagingService->createOpId(MESSAGING_SERVICE_OP_SYNC_FOLDER);
910                                 event ->m_sync_account_id = imessagingService->getAccountID() ;
911                                 event ->m_sync_limit = limit;
912                                 event->m_messagingService = imessagingService;
913
914                                 event->setEventType(EventMessagingService::MESSAGING_SERVICE_EVENT_TYPE_SYNC_FOLDER);
915                                 event->setPrivateData(DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(privateData));
916                                 event->setForAsynchronousCall(&MessagingController::getInstance());
917
918                                 Api::Messaging::ReqReceiverMessageSingleton::Instance().syncFolder(event);      //load message Body
919
920                                 return converter->toJSValueRef(event->opId);
921                 
922                         }
923                         else
924                         {
925                                 return JSTizenExceptionFactory::postException(context, exception, 
926                                                 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
927                         }
928                 }
929                 Catch(WrtDeviceApis::Commons::UnsupportedException) {
930                         return JSTizenExceptionFactory::postException(context, exception, 
931                                         JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);
932                 }
933                 Catch(WrtDeviceApis::Commons::ConversionException) {
934                         return JSTizenExceptionFactory::postException(context, exception, 
935                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
936                 }
937                 Catch(WrtDeviceApis::Commons::NullPointerException) {
938                         return JSTizenExceptionFactory::postException(context, exception, 
939                                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
940                 }
941                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
942                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
943                                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT));
944                 }
945                 Catch(WrtDeviceApis::Commons::PlatformException) {
946                         callbackManager->callOnError(JSTizenExceptionFactory::makeErrorObject(context,          
947                                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN));
948                 }
949                 
950         }
951         else
952         {
953                 LogError("Null pointer");
954                 return JSTizenExceptionFactory::postException(context, exception, 
955                    JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
956         }
957
958         return JSValueMakeNull(context);
959
960
961 }
962
963 JSValueRef JSMessagingService::cancelOperation(JSContextRef context, JSObjectRef function, JSObjectRef thisObject,
964                         size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
965 {
966         LogDebug("entered");
967
968         JSMessagingServicePriv* priv = static_cast<JSMessagingServicePriv*>(JSObjectGetPrivate(thisObject));    //get private object
969
970         if (priv)
971         {
972                 IMessagingServicePtr imessagingService = priv->getObject();
973                 Try
974                 {
975                         LogInfo(">>> argument count : " << argumentCount);
976
977                         if ( argumentCount == 1 && !Validator(context).isCallback(arguments[0]))
978                         {
979                                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
980                                 Api::Messaging::MessageType msgType = (Api::Messaging::MessageType)imessagingService->getType();
981                                 LogInfo("msgType :" << msgType);
982
983                                 int opId = converter->toLong( arguments[0] );   //Fetch operation ID
984                                 int handle = imessagingService->getHandleFromOpId(opId);
985                                 if ( handle < 0 )
986                                 {
987                                         ThrowMsg(WrtDeviceApis::Commons::NotFoundException,"Operation ID Not found");
988                                 }
989                                 
990                                 int opType = imessagingService->getOpTypeFromOpId(opId);
991                                 LogInfo("operation ID :" << opId << " operation Type : "  << opType);                   
992
993                                 if ( opType == Api::Messaging::MESSAGING_SERVICE_OP_SEND_MESSAGE)
994                                 {       
995                                         IMessagePtr msg = imessagingService->getMessageFromOpId(opId);
996                                         if (msg)
997                                         {
998                                                 LogDebug("Call Cancel Function");
999                                                 msg->sendCancel(handle);
1000                                                 imessagingService->deleteOpId(opId);
1001                                         }
1002                                 }
1003                                 else
1004                                 {       //for email.
1005                                         if (msgType == Api::Messaging::EMAIL)
1006                                         {       
1007                                                 if ( opType == Api::Messaging::MESSAGING_SERVICE_OP_DOWNLOAD_BODY )
1008                                                 {       
1009                                                         IMessagePtr msg = imessagingService->getMessageFromOpId(opId);
1010                                                         Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
1011                                                         LogDebug("Cancel Download Body , handle = " << handle);
1012                                                         email->downloadBodyCancel(handle);
1013                                                 }
1014                                                 else if (  opType == Api::Messaging::MESSAGING_SERVICE_OP_SYNC )
1015                                                 {       
1016                                                         LogDebug("Cancel Sync , handle = " << handle);
1017                                                         imessagingService->syncCancel(handle);
1018                                                 }
1019                                                 else if ( opType == Api::Messaging::MESSAGING_SERVICE_OP_SYNC_FOLDER )
1020                                                 {
1021                                                         LogDebug("Cancel Sync Folder, handle = " << handle);
1022                                                         imessagingService->syncFolderCancel(handle);
1023                                                 }
1024                                                 imessagingService->deleteOpId(opId);
1025                                         }
1026                                         else
1027                                         {
1028                                                 ThrowMsg(WrtDeviceApis::Commons::UnsupportedException, "Operation Type is mismatched");
1029                                         }
1030                                 }
1031                                 
1032                         }
1033                         else
1034                         {
1035                                 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException,"invalid argument exception");
1036                         }
1037
1038                 }
1039                 Catch (WrtDeviceApis::Commons::InvalidArgumentException){
1040                         LogError("Exception: " << _rethrown_exception.GetMessage());
1041                         return JSTizenExceptionFactory::postException(context, exception, 
1042                                 JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);                                            
1043                 }
1044                 Catch (WrtDeviceApis::Commons::NotFoundException){
1045                         LogError("Exception: " << _rethrown_exception.GetMessage());
1046                         return JSTizenExceptionFactory::postException(context, exception, 
1047                                 JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);                                                
1048                 }
1049                 Catch (WrtDeviceApis::Commons::UnsupportedException){
1050                         LogError("Exception: " << _rethrown_exception.GetMessage());
1051                         return JSTizenExceptionFactory::postException(context, exception, 
1052                                 JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);                                                
1053                 } 
1054                 Catch (WrtDeviceApis::Commons::Exception){
1055                         LogError("Exception: " << _rethrown_exception.GetMessage());
1056                         return JSTizenExceptionFactory::postException(context, exception, 
1057                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);                    
1058                 }
1059                 
1060         }
1061         else
1062         {       
1063                 LogDebug(" Private Object is NULL ");
1064                 return JSTizenExceptionFactory::postException(context, exception, 
1065                                         JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
1066         }
1067
1068         LogDebug(">>>");
1069         return JSValueMakeNull(context);
1070 }
1071
1072 }
1073 }
1074