2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include "ConverterMessage.h"
22 #include "JSMessageBody.h"
23 #include <JSTizenExceptionFactory.h>
24 #include <JSTizenException.h>
25 #include "MessagingErrorMsg.h"
26 #include "JSMessageAttachment.h"
27 #include <JSWebAPIError.h>
28 #include <ArgumentValidator.h>
35 using namespace DeviceAPI::Common;
36 using namespace DeviceAPI::Messaging;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
40 const char* JSMessageBody::MSGID = "messageId";
41 const char* JSMessageBody::LOADED = "loaded";
42 const char* JSMessageBody::PLAINBODY = "plainBody";
43 const char* JSMessageBody::HTMLBODY = "htmlBody";
44 const char* JSMessageBody::INLINEATT = "inlineAttachments";
46 JSClassRef JSMessageBody::m_jsClassRef = NULL;
48 JSClassDefinition JSMessageBody::m_classInfo = {
50 kJSClassAttributeNone,
60 NULL, //deleteProperty,
62 NULL, //callAsFunction,
63 NULL, //callAsConstructor,
65 NULL, //convertToType,
68 JSStaticValue JSMessageBody::m_property[] = {
72 const JSClassDefinition* JSMessageBody::getClassInfo()
74 return &(m_classInfo);
77 JSClassRef JSMessageBody::getClassRef()
80 m_jsClassRef = JSClassCreate(&m_classInfo);
85 void JSMessageBody::initialize(JSContextRef context,
91 void JSMessageBody::finalize(JSObjectRef object)
94 JSMessageBodyPrivate* priv = static_cast<JSMessageBodyPrivate*>(JSObjectGetPrivate(object));
95 JSObjectSetPrivate(object, NULL);
99 JSObjectRef JSMessageBody::createJS( JSContextRef context, const IMessagePtr &msg)
101 JSMessageBodyPrivate *priv = new JSMessageBodyPrivate(context, msg);
102 return JSObjectMake(context, getClassRef(), priv); //make JSObjectRef.
105 IMessagePtr JSMessageBody::getMessage(JSContextRef context,
108 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
109 return getMessage(context, converter.toJSObjectRef(value));
112 IMessagePtr JSMessageBody::getMessage(JSContextRef context,
115 return getPrivate(object)->getObject();
118 JSMessageBodyPrivate* JSMessageBody::getPrivate(JSObjectRef thisObject)
120 JSMessageBodyPrivate* thisPrivate = static_cast<JSMessageBodyPrivate*>(JSObjectGetPrivate(thisObject));
122 LoggerE("no private");
123 Throw(WrtDeviceApis::Commons::NullPointerException);
128 JSValueRef JSMessageBody::getProperty(JSContextRef context,
130 JSStringRef propertyName,
131 JSValueRef* exception)
136 IMessagePtr msg = getMessage(context, object);
138 throw DeviceAPI::Common::UnknownException("Object is null");
141 if (JSStringIsEqualToUTF8CString(propertyName, MSGID)) {
142 std::string id = msg->getId();
143 LoggerD("msgId=" << id);
144 LoggerD("msg->getMessageStatus()=" << msg->getMessageStatus());
145 if((id.size() == 0) && (msg->getMessageStatus() == MESSAGE_STATUS_CREATED)) {
148 return JSUtil::toJSValueRef(context, id);
150 else if (JSStringIsEqualToUTF8CString(propertyName, LOADED)) {
151 if (msg->getMessageType() == EMAIL) {
152 IEmailPtr email = MessageFactory::convertToEmail(msg);
153 return JSUtil::toJSValueRef(context, email->isBodyDownloaded() > 0 ? true : false);
156 LoggerD("msgId=" << msg->getId());
157 LoggerD("msg->getMessageStatus()=" << msg->getMessageStatus());
158 if ((msg->getId().size() == 0) && msg->getMessageStatus() == MESSAGE_STATUS_CREATED) {
159 return JSUtil::toJSValueRef(context, false);
161 return JSUtil::toJSValueRef(context, true);
164 else if (JSStringIsEqualToUTF8CString(propertyName, PLAINBODY)) {
165 std::string body = msg->getBody();
166 LoggerD("getPlainBody plainBody" << body );
167 return JSUtil::toJSValueRef(context, body);
169 else if (JSStringIsEqualToUTF8CString(propertyName, HTMLBODY)) {
170 LoggerD("message Type : " << msg->getMessageType() );
171 std::string emailHtmlBody = "";
172 if (msg->getMessageType() == EMAIL) {
173 IEmailPtr email = MessageFactory::convertToEmail(msg);
174 emailHtmlBody = email->getHtmlBody();
175 LoggerD("Html Body : " << emailHtmlBody );
177 return JSUtil::toJSValueRef(context, emailHtmlBody);
179 else if (JSStringIsEqualToUTF8CString(propertyName, INLINEATT)) {
180 switch(msg->getMessageType()) {
183 std::vector<IAttachmentPtr> inlineAttachments;
184 std::vector<IAttachmentPtr>::iterator it;
187 AttachmentsPtr emailAttachments =
188 DPL::StaticPointerCast<Attachments>(MessageFactory::convertToEmail(msg));
189 inlineAttachments = emailAttachments->getInlineAttachments();
190 count = inlineAttachments.size();
192 LoggerD( "attachments.size(): " << count);
194 JSObjectRef jsMessageAttachmentObject[count]; //make
195 for(int i = 0; i < count; i++) {
196 LoggerD("Attachment ID: " << inlineAttachments[i]->getAttachmentID());
197 jsMessageAttachmentObject[i] = JSMessageAttachment::createJS(context, inlineAttachments[i]);
199 JSObjectRef result = JSObjectMakeArray(context, count, jsMessageAttachmentObject, NULL);
205 JSObjectRef arrayValue = JSObjectMakeArray(context, 0, NULL, NULL);
206 if(NULL == arrayValue){
207 LoggerE("Could not create JS array object");
208 return JSValueMakeUndefined(context);
214 throw DeviceAPI::Common::UnknownException("Not supported message type.");
218 LoggerD("Not supported property.");
221 catch (const BasePlatformException& err) {
222 LoggerE(err.getMessage().c_str());
223 return JSValueMakeUndefined(context);
225 catch (const WrtDeviceApis::Commons::Exception& err) {
226 LoggerE(err.GetMessage().c_str());
227 return JSValueMakeUndefined(context);
233 bool JSMessageBody::setProperty(JSContextRef context,
235 JSStringRef propertyName,
237 JSValueRef* exception)
242 if (JSStringIsEqualToUTF8CString(propertyName, MSGID)) {
245 else if (JSStringIsEqualToUTF8CString(propertyName, LOADED)) {
248 else if(JSStringIsEqualToUTF8CString(propertyName, PLAINBODY)) {
249 IMessagePtr msg = getMessage(context, object);
250 std::string body = JSUtil::JSValueToString(context, value);
252 LoggerD("setPlainBody plainBody" << body );
255 else if (JSStringIsEqualToUTF8CString(propertyName, HTMLBODY)) {
256 IMessagePtr msg = getMessage(context, object);
258 if (msg->getMessageType() == EMAIL)
260 IEmailPtr email = MessageFactory::convertToEmail(msg);
261 std::string body = JSUtil::JSValueToString(context, value);
262 LoggerD("input String : " << body);
263 email->setHtmlBody(body);
267 throw DeviceAPI::Common::NotSupportedException("HTML body is for Email only.");
271 else if (JSStringIsEqualToUTF8CString(propertyName, INLINEATT)) {
272 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
274 std::vector<IAttachmentPtr> inlineAttachments;
275 if (JSIsArrayValue(context, value)) {
276 JSObjectRef valueObj = converter->toJSObjectRef(value);
277 unsigned int len = JSGetArrayLength(context, valueObj);
278 LoggerD("Array Length = " << len);
279 for (unsigned int i = 0; i < len; ++i) {
281 JSValueRef att = JSGetArrayElement(context, valueObj, i);
282 if (JSValueIsUndefined(context, att) || JSValueIsNull(context, att)) {
283 LoggerW("Invalid array element. Skipping.");
286 IAttachmentPtr attachment = converter->toIAttachment(att);
288 if (attachment->getIsValidAttachment() == false) {
289 std::string logMsg = "Invalid attachment : " + i;
290 throw DeviceAPI::Common::InvalidValuesException(logMsg.c_str());
293 LoggerD("Adding attachment , shortname: " << attachment->getShortName());
294 inlineAttachments.push_back(attachment);
298 IAttachmentPtr attachment = converter->toIAttachment(value);
299 LoggerD("Adding attachment , shortname: " << attachment->getShortName());
300 if (attachment->getIsValidAttachment() == false) {
301 throw DeviceAPI::Common::InvalidValuesException("Invalid attachment.");
303 inlineAttachments.push_back(attachment);
306 LoggerD("inlineAttachments Size =" << inlineAttachments.size());
307 if ( inlineAttachments.size() > 0) {
308 IMessagePtr msg = converter->toIMessage(object);
310 MessageType msgType = msg->getMessageType();
314 IEmailPtr email = MessageFactory::convertToEmail(msg);
315 email->setInlineAttachments(inlineAttachments);
319 throw DeviceAPI::Common::NotSupportedException("Not supported message type.");
323 throw DeviceAPI::Common::UnknownException("Message converter failed.");
329 LoggerD("Not supported property.");
332 catch (const BasePlatformException& err) {
333 LoggerE(err.getMessage().c_str());
336 catch (const WrtDeviceApis::Commons::Exception& err) {
337 LoggerE(err.GetMessage().c_str());
344 void JSMessageBody::getPropertyNames(JSContextRef context,
346 JSPropertyNameAccumulatorRef propertyNames)
349 JSStringRef propertyName = NULL;
351 propertyName = JSStringCreateWithUTF8CString(MSGID);
352 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
353 JSStringRelease(propertyName);
355 propertyName = JSStringCreateWithUTF8CString(LOADED);
356 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
357 JSStringRelease(propertyName);
359 propertyName = JSStringCreateWithUTF8CString(PLAINBODY);
360 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
361 JSStringRelease(propertyName);
363 propertyName = JSStringCreateWithUTF8CString(HTMLBODY);
364 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
365 JSStringRelease(propertyName);
367 propertyName = JSStringCreateWithUTF8CString(INLINEATT);
368 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
369 JSStringRelease(propertyName);