upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Messaging / JSMessageAttachment.cpp
index 7a24fa0..50fb378 100755 (executable)
  * @version     0.1
  */
 
-#include <dpl/log.h>
+#include <dpl/log/log.h>
 #include <CommonsJavaScript/Converter.h>
 #include <CommonsJavaScript/JSUtils.h>
 #include "ConverterMessage.h"
 #include "JSMessageAttachment.h"
-#include "JSAttachmentArray.h"         //attachment array
 #include <Tizen/Filesystem/JSFile.h>
 #include <Tizen/Filesystem/EventGetNodeData.h>
 #include <API/Filesystem/IManager.h>
 #include <Tizen/Common/JSTizenExceptionFactory.h>
 #include <Tizen/Common/JSTizenException.h>
-
+#include <API/Messaging/AttachmentFactory.h>
 #include "MessagingErrorMsg.h"
+#include <Messaging/Attachment.h>
 
+#if 0
 namespace {
 const char* MSG_ATTACHMENT_MIMETYPE            = "MIMEType";
 const char* MSG_ATTACHMENT_MESSAGEID           = "messageId";
@@ -42,6 +43,7 @@ const char* MSG_ATTACHMENT_CONTENTLOCATION    = "contentLocation";
 const char* MSG_ATTACHMENT_CONTENTID   = "contentId";
 const char* MSG_ATTACHMENT_LOADED              = "loaded";
 }
+#endif
 
 namespace TizenApis {
 namespace Tizen1_0 {
@@ -50,6 +52,7 @@ using namespace TizenApis::Commons;
 using namespace TizenApis::Api::Messaging;
 using namespace WrtDeviceApis::Commons;
 using namespace WrtDeviceApis::CommonsJavaScript;
+using namespace TizenApis::Platform::Messaging;
 
 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
 
@@ -70,18 +73,19 @@ JSClassDefinition JSMessageAttachment::m_classInfo = {
     NULL, //deleteProperty,
     NULL, //getPropertyNames,
     NULL, //callAsFunction,
-    NULL, //callAsConstructor,
+    constructor, //callAsConstructor,
     NULL, //hasInstance,
     NULL, //convertToType,
 };
 
 JSStaticValue JSMessageAttachment::m_property[] = {
     //{ "MIMEType", JSMessageAttachment::getMIMEType, NULL, kJSPropertyAttributeReadOnly },
+    { "id", JSMessageAttachment::getContentID, NULL, kJSPropertyAttributeNone },
     { "messageId", JSMessageAttachment::getMessageID, NULL, kJSPropertyAttributeReadOnly },
     //{ "contentLocation", JSMessageAttachment::getContentLocation, NULL, kJSPropertyAttributeNone },
-    { "contentId", JSMessageAttachment::getContentID, NULL, kJSPropertyAttributeNone },
+    { "mimeType", JSMessageAttachment::getMIMEType, NULL, kJSPropertyAttributeReadOnly },
     { "loaded", JSMessageAttachment::getLoaded, NULL, kJSPropertyAttributeReadOnly },
-    { "file", JSMessageAttachment::getFile, NULL, kJSPropertyAttributeReadOnly },
+    { "filePath", JSMessageAttachment::getFile, NULL, kJSPropertyAttributeReadOnly },
     { 0, 0, 0, 0 }
 };
 const JSClassDefinition* JSMessageAttachment::getClassInfo()
@@ -100,7 +104,16 @@ JSClassRef JSMessageAttachment::getClassRef()
 void JSMessageAttachment::initialize(JSContextRef context,
         JSObjectRef object)
 {
-    LogDebug("enter");
+       LogDebug("enter");
+       if (!JSObjectGetPrivate(object)) {
+               LogDebug("Private object not set... setting it.");
+               IAttachmentPtr attachment(new Attachment());
+                                               
+               JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
+               if (!JSObjectSetPrivate(object, priv)) {
+                       delete priv;
+               }
+       }
 }
 
 void JSMessageAttachment::finalize(JSObjectRef object)
@@ -189,6 +202,12 @@ JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
                  
                  WrtDeviceApis::CommonsJavaScript::Converter converter(context);
                  LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
+
+                 if ( attachment->getAttachmentID() < 0)
+                 {
+                        JSValueMakeUndefined(context);
+                 }
+                 
                  return converter.toJSValueRef(attachment->getAttachmentID());
            }
            Catch(WrtDeviceApis::Commons::InvalidArgumentException)
@@ -244,6 +263,68 @@ JSValueRef JSMessageAttachment::getFile(JSContextRef context,
           JSStringRef propertyName,
           JSValueRef* exception)
 {
+       LogDebug("enter");
+
+       JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
+       if (priv)
+               LogDebug("priv is vaild");
+       JSContextRef globalContext = priv ? priv->getContext() : context;
+       
+       Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
+       
+       if ( attachment )
+       {
+               Try
+               {       
+                       WrtDeviceApis::CommonsJavaScript::Converter converter(context);
+                       
+                       if ( (attachment->getVirtualPath()).length() > 0)
+                       {
+                               return converter.toJSValueRef(attachment->getVirtualPath());
+                       }
+                       else
+                       {
+                               std::string fullPath = attachment->getFullPath();
+                               LogDebug("fullPath=" << fullPath);
+                               if (fullPath.empty()) {
+                                       return JSValueMakeNull(context);
+                               }
+                               else
+                               {       
+                                       std::string virtualpath = AttachmentFactory::getVirtualPathFromRealPath(globalContext, fullPath);
+                                       if ( virtualpath.length() == 0 )
+                                       {
+                                               virtualpath = AttachmentFactory::getVirtualPathFromEmailServiceFolder(globalContext, fullPath);
+                                               attachment->setVirtualPath(virtualpath);        //set virtual path
+                                       }
+
+                                       return converter.toJSValueRef(virtualpath);                                             
+                               }
+                       }
+
+               }
+               Catch(WrtDeviceApis::Commons::InvalidArgumentException)
+               {
+                  LogError("invalid value conversion");
+                  return JSTizenExceptionFactory::postException(context, exception, 
+                          JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
+               }
+               Catch(WrtDeviceApis::Commons::Exception)
+               {
+                   LogError("Unkown Exception");
+                   return JSTizenExceptionFactory::postException(context, exception, 
+                               JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
+               }
+               return JSValueMakeUndefined(context);
+               
+       }
+       else
+       {
+               return JSTizenExceptionFactory::postException(context, exception, 
+                       JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
+       }
+       
+#if 0 // MESSAGING ATTACHMENT IS BLOCKED
                LogDebug("enter");
                Try
                {
@@ -311,7 +392,9 @@ JSValueRef JSMessageAttachment::getFile(JSContextRef context,
                                JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
                }
                return JSValueMakeUndefined(context);
-               
+#endif
+return JSValueMakeUndefined(context);
+
 }
 
 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
@@ -339,7 +422,91 @@ JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObje
     return thisPrivate;
 }
 
+JSObjectRef JSMessageAttachment::constructor(JSContextRef context, 
+               JSObjectRef constructor, 
+               size_t argumentCount, 
+               const JSValueRef arguments[], 
+               JSValueRef* exception)
+{
+       LogDebug("entered");
+
+       JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(constructor));
+       JSContextRef globalContext = priv ? priv->getContext() : context;
+       
+       if (globalContext)
+       {
+               try
+               {
+                       LogDebug("argumentCount=" << argumentCount);
+                       if ( argumentCount < 1 ) {
+                               LogError("Wrong argument count");
+                               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
+                               return NULL;
+                       }
+                       else
+                       {       
+                               ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
+                               
+                               std::string path = converter->toString(arguments[0]);   //filepath
+                               std::string mimetype;
+                               
+                               if ( argumentCount == 2)
+                                       mimetype = converter->toString(arguments[1]);   //filepath
+
+                               //IPathPtr src = Utils::fromVirtualPath(globalContext, path);
+                               path = AttachmentFactory::getRealPathFromVirtualPath(globalContext,path);
+                               LogDebug("real path = " << path);
+                               Api::Messaging::IAttachmentPtr attachment = AttachmentFactory::createAttachment(
+                                                                                   path,
+                                                                                   mimetype);
+                               if (attachment)
+                               {
+                                       JSClassRef jsClassRef = JSClassCreate(getClassInfo());
+                                       LogDebug("jsClassRef success");
+                                       JSMessageAttachmentPrivate* priv = new JSMessageAttachmentPrivate(globalContext, attachment);
+                                       LogDebug("priv success");
+                                       
+                                       JSObjectRef jsObjRef = JSObjectMake(globalContext, jsClassRef, static_cast<void*>(priv));
+                                       LogDebug("JSObjectMake success");
+                                       JSClassRelease(jsClassRef);
+                                       if (NULL == jsObjRef) {
+                                               LogError("object creation error");
+                                               ThrowMsg(WrtDeviceApis::Commons::PlatformException, "JSObject make error"); 
+                                       }
+                                       return jsObjRef;
+                               }
+                               else
+                               {
+                                       LogDebug("attachment can't create");
+                                       ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "attachment create failed..."); 
+                               }
+                       }
+
+               }       
+               Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
+                       *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
+                       return NULL;
+               }
+               Catch(WrtDeviceApis::Commons::ConversionException) {
+                       *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
+                       return NULL;
+               }
+               Catch(WrtDeviceApis::Commons::PlatformException) {
+                       *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);     
+                       return NULL;
+               }
+       }
+       else
+       {
+               LogDebug(" globalContext is NULL ");
+               *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);     
+               return NULL;
+       }
+
+       return NULL;
 }
-}
 
 
+}
+}
+