// // Tizen Web Device API // Copyright (c) 2012 Samsung Electronics Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the License); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include #include #include #include "ConverterMessage.h" #include "JSMessageBody.h" #include "MessagingErrorMsg.h" #include "JSMessageAttachment.h" #include #include #include #include namespace DeviceAPI { namespace Messaging { using namespace DeviceAPI::Common; using namespace DeviceAPI::Messaging; using namespace WrtDeviceApis::Commons; using namespace WrtDeviceApis::CommonsJavaScript; const char* JSMessageBody::MSGID = "messageId"; const char* JSMessageBody::LOADED = "loaded"; const char* JSMessageBody::PLAINBODY = "plainBody"; const char* JSMessageBody::HTMLBODY = "htmlBody"; const char* JSMessageBody::INLINEATT = "inlineAttachments"; JSClassRef JSMessageBody::m_jsClassRef = NULL; JSClassDefinition JSMessageBody::m_classInfo = { 0, kJSClassAttributeNone, "MessageBody", 0, m_property, NULL, initialize, finalize, NULL, //hasProperty, getProperty, setProperty, NULL, //deleteProperty, getPropertyNames, NULL, //callAsFunction, NULL, //callAsConstructor, NULL, //hasInstance, NULL, //convertToType, }; JSStaticValue JSMessageBody::m_property[] = { { 0, 0, 0, 0 } }; const JSClassDefinition* JSMessageBody::getClassInfo() { return &(m_classInfo); } JSClassRef JSMessageBody::getClassRef() { if (!m_jsClassRef) { m_jsClassRef = JSClassCreate(&m_classInfo); } return m_jsClassRef; } void JSMessageBody::initialize(JSContextRef context, JSObjectRef object) { LoggerD("enter"); } void JSMessageBody::finalize(JSObjectRef object) { LoggerD("enter"); JSMessageBodyPrivate* priv = static_cast(JSObjectGetPrivate(object)); JSObjectSetPrivate(object, NULL); delete priv; } JSObjectRef JSMessageBody::createJS( JSContextRef context, const IMessagePtr &msg) { JSMessageBodyPrivate *priv = new JSMessageBodyPrivate(context, msg); return JSObjectMake(context, getClassRef(), priv); //make JSObjectRef. } IMessagePtr JSMessageBody::getMessage(JSContextRef context, JSValueRef value) { WrtDeviceApis::CommonsJavaScript::Converter converter(context); return getMessage(context, converter.toJSObjectRef(value)); } IMessagePtr JSMessageBody::getMessage(JSContextRef context, JSObjectRef object) { return getPrivate(object)->getObject(); } JSMessageBodyPrivate* JSMessageBody::getPrivate(JSObjectRef thisObject) { JSMessageBodyPrivate* thisPrivate = static_cast(JSObjectGetPrivate(thisObject)); if (!thisPrivate) { LoggerE("no private"); Throw(WrtDeviceApis::Commons::NullPointerException); } return thisPrivate; } JSValueRef JSMessageBody::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) { LoggerD("Entered"); try { IMessagePtr msg = getMessage(context, object); if (msg == NULL) { throw DeviceAPI::Common::UnknownException("Object is null"); } if (JSStringIsEqualToUTF8CString(propertyName, MSGID)) { std::string id = msg->getId(); LoggerD("msgId=" << id); LoggerD("msg->getMessageStatus()=" << msg->getMessageStatus()); if((id.size() == 0) && (msg->getMessageStatus() == MESSAGE_STATUS_CREATED)) { return JSValueMakeNull(context); } return JSUtil::toJSValueRef(context, id); } else if (JSStringIsEqualToUTF8CString(propertyName, LOADED)) { if (msg->getMessageType() == EMAIL) { IEmailPtr email = MessageFactory::convertToEmail(msg); return JSUtil::toJSValueRef(context, email->isBodyDownloaded() > 0 ? true : false); } else { LoggerD("msgId=" << msg->getId()); LoggerD("msg->getMessageStatus()=" << msg->getMessageStatus()); if ((msg->getId().size() == 0) && msg->getMessageStatus() == MESSAGE_STATUS_CREATED) { return JSUtil::toJSValueRef(context, false); } return JSUtil::toJSValueRef(context, true); } } else if (JSStringIsEqualToUTF8CString(propertyName, PLAINBODY)) { std::string body = msg->getBody(); return JSUtil::toJSValueRef(context, body); } else if (JSStringIsEqualToUTF8CString(propertyName, HTMLBODY)) { LoggerD("message Type : " << msg->getMessageType() ); std::string emailHtmlBody = ""; if (msg->getMessageType() == EMAIL) { IEmailPtr email = MessageFactory::convertToEmail(msg); emailHtmlBody = email->getHtmlBody(); } return JSUtil::toJSValueRef(context, emailHtmlBody); } else if (JSStringIsEqualToUTF8CString(propertyName, INLINEATT)) { switch(msg->getMessageType()) { case EMAIL: { std::vector inlineAttachments; std::vector::iterator it; int count = 0; AttachmentsPtr emailAttachments = DPL::StaticPointerCast(MessageFactory::convertToEmail(msg)); inlineAttachments = emailAttachments->getInlineAttachments(); count = inlineAttachments.size(); LoggerD( "attachments.size(): " << count); JSObjectRef jsMessageAttachmentObject[count]; //make for(int i = 0; i < count; i++) { LoggerD("Attachment ID: " << inlineAttachments[i]->getAttachmentID()); jsMessageAttachmentObject[i] = JSMessageAttachment::createJS(context, inlineAttachments[i]); } JSObjectRef result = JSObjectMakeArray(context, count, jsMessageAttachmentObject, NULL); return result; } case SMS: case MMS: { JSObjectRef arrayValue = JSObjectMakeArray(context, 0, NULL, NULL); if(NULL == arrayValue){ LoggerE("Could not create JS array object"); return JSValueMakeUndefined(context); } return arrayValue; } default: throw DeviceAPI::Common::UnknownException("Not supported message type."); } } else { LoggerD("Not supported property."); } } catch (const BasePlatformException& err) { LoggerE(err.getMessage().c_str()); return JSValueMakeUndefined(context); } catch (const WrtDeviceApis::Commons::Exception& err) { LoggerE(err.GetMessage().c_str()); return JSValueMakeUndefined(context); } return NULL; } bool JSMessageBody::setProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef value, JSValueRef* exception) { LoggerD("Entered"); try { if (JSStringIsEqualToUTF8CString(propertyName, MSGID)) { return true; } else if (JSStringIsEqualToUTF8CString(propertyName, LOADED)) { return true; } else if(JSStringIsEqualToUTF8CString(propertyName, PLAINBODY)) { IMessagePtr msg = getMessage(context, object); std::string body = JSUtil::JSValueToString(context, value); msg->setBody(body); return true; } else if (JSStringIsEqualToUTF8CString(propertyName, HTMLBODY)) { IMessagePtr msg = getMessage(context, object); if (msg->getMessageType() == EMAIL) { IEmailPtr email = MessageFactory::convertToEmail(msg); std::string body = JSUtil::JSValueToString(context, value); email->setHtmlBody(body); return true; } else { throw DeviceAPI::Common::NotSupportedException("HTML body is for Email only."); } return true; } else if (JSStringIsEqualToUTF8CString(propertyName, INLINEATT)) { ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context); std::vector inlineAttachments; if (JSIsArrayValue(context, value)) { JSObjectRef valueObj = converter->toJSObjectRef(value); unsigned int len = JSGetArrayLength(context, valueObj); LoggerD("Array Length = " << len); for (unsigned int i = 0; i < len; ++i) { JSValueRef att = JSGetArrayElement(context, valueObj, i); if (JSValueIsUndefined(context, att) || JSValueIsNull(context, att)) { LoggerW("Invalid array element. Skipping."); continue; } IAttachmentPtr attachment = converter->toIAttachment(att); if (attachment->getIsValidAttachment() == false) { std::string logMsg = "Invalid attachment : " + i; throw DeviceAPI::Common::InvalidValuesException(logMsg.c_str()); } LoggerD("Adding attachment , shortname: " << attachment->getShortName()); inlineAttachments.push_back(attachment); } } else { IAttachmentPtr attachment = converter->toIAttachment(value); LoggerD("Adding attachment , shortname: " << attachment->getShortName()); if (attachment->getIsValidAttachment() == false) { throw DeviceAPI::Common::InvalidValuesException("Invalid attachment."); } inlineAttachments.push_back(attachment); } LoggerD("inlineAttachments Size =" << inlineAttachments.size()); if ( inlineAttachments.size() > 0) { IMessagePtr msg = converter->toIMessage(object); if (msg) { MessageType msgType = msg->getMessageType(); switch (msgType) { case EMAIL: { IEmailPtr email = MessageFactory::convertToEmail(msg); email->setInlineAttachments(inlineAttachments); break; } default: throw DeviceAPI::Common::NotSupportedException("Not supported message type."); } } else { throw DeviceAPI::Common::UnknownException("Message converter failed."); } } return true; } else { LoggerD("Not supported property."); } } catch (const BasePlatformException& err) { LoggerE(err.getMessage().c_str()); return true; } catch (const WrtDeviceApis::Commons::Exception& err) { LoggerE(err.GetMessage().c_str()); return true; } return false; } void JSMessageBody::getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef propertyNames) { LoggerD("Entered"); JSStringRef propertyName = NULL; propertyName = JSStringCreateWithUTF8CString(MSGID); JSPropertyNameAccumulatorAddName(propertyNames, propertyName); JSStringRelease(propertyName); propertyName = JSStringCreateWithUTF8CString(LOADED); JSPropertyNameAccumulatorAddName(propertyNames, propertyName); JSStringRelease(propertyName); propertyName = JSStringCreateWithUTF8CString(PLAINBODY); JSPropertyNameAccumulatorAddName(propertyNames, propertyName); JSStringRelease(propertyName); propertyName = JSStringCreateWithUTF8CString(HTMLBODY); JSPropertyNameAccumulatorAddName(propertyNames, propertyName); JSStringRelease(propertyName); propertyName = JSStringCreateWithUTF8CString(INLINEATT); JSPropertyNameAccumulatorAddName(propertyNames, propertyName); JSStringRelease(propertyName); } } }