2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 * @file JSAttachmentArray.cpp
20 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
24 #include <dpl/log/log.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <CommonsJavaScript/JSUtils.h>
27 #include "ConverterMessage.h"
28 #include "JSMessageBody.h"
29 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
30 #include "JSAttachmentArray.h"
32 #include <Tizen/Common/JSTizenExceptionFactory.h>
33 #include <Tizen/Common/JSTizenException.h>
34 #include "MessagingErrorMsg.h"
39 using namespace TizenApis::Commons;
40 using namespace TizenApis::Api::Messaging;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
45 JSClassRef JSMessageBody::m_jsClassRef = NULL;
47 JSClassDefinition JSMessageBody::m_classInfo = {
49 kJSClassAttributeNone,
59 NULL, //deleteProperty,
60 NULL, //getPropertyNames,
61 NULL, //callAsFunction,
62 NULL, //callAsConstructor,
64 NULL, //convertToType,
67 JSStaticValue JSMessageBody::m_property[] = {
68 { "messageId", JSMessageBody::getMessageID, NULL, kJSPropertyAttributeReadOnly },
69 { "loaded", JSMessageBody::getLoadedStatus, NULL, kJSPropertyAttributeReadOnly },
70 { "plainBody", JSMessageBody::getPlainBody, JSMessageBody::setPlainBody, kJSPropertyAttributeNone },
71 { "htmlBody", JSMessageBody::getHtmlBody, JSMessageBody::setHtmlBody, kJSPropertyAttributeNone },
72 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
73 { "inlineAttachments", NULL, NULL, kJSPropertyAttributeReadOnly },
78 const JSClassDefinition* JSMessageBody::getClassInfo()
80 return &(m_classInfo);
83 JSClassRef JSMessageBody::getClassRef()
86 m_jsClassRef = JSClassCreate(&m_classInfo);
91 bool JSMessageBody::hasProperty(JSContextRef context,
93 JSStringRef propertyName)
96 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
99 Api::Messaging::IMessagePtr msg = getMessage(context, object);
105 Catch(WrtDeviceApis::Commons::Exception)
107 //not reporting error is intended
112 void JSMessageBody::initialize(JSContextRef context,
118 void JSMessageBody::finalize(JSObjectRef object)
121 JSMessageBodyPrivate* priv = static_cast<JSMessageBodyPrivate*>(JSObjectGetPrivate(object));
122 JSObjectSetPrivate(object, NULL);
126 JSObjectRef JSMessageBody::createJS( JSContextRef context, const Api::Messaging::IMessagePtr &msg)
128 JSMessageBodyPrivate *priv = new JSMessageBodyPrivate(context, msg);
129 return JSObjectMake(context, getClassRef(), priv); //make JSObjectRef.
132 JSValueRef JSMessageBody::getMessageID(JSContextRef context,
134 JSStringRef propertyName,
135 JSValueRef* exception)
140 Api::Messaging::IMessagePtr msg = getMessage(context, object);
142 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
143 return converter.toJSValueRef(msg->getId());
145 Catch(WrtDeviceApis::Commons::Exception)
147 LogError("invalid conversion");
149 return JSValueMakeUndefined(context);
152 JSValueRef JSMessageBody::getLoadedStatus(JSContextRef context,
154 JSStringRef propertyName,
155 JSValueRef* exception)
160 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
161 Api::Messaging::IMessagePtr msg = getMessage(context, object);
163 if (msg->getMessageType() == Api::Messaging::EMAIL)
165 //Api::Messaging::IEmailPtr email = DPL::StaticPointerCast<Api::IEmailPtr>(Api::Messaging::MessageFactory::convertToEmail(msg));
166 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
167 return converter.toJSValueRef(email->isBodyDownloaded() > 0 ? true : false);
171 return converter.toJSValueRef(true);
175 Catch(WrtDeviceApis::Commons::Exception)
177 LogError("invalid conversion");
179 return JSValueMakeUndefined(context);
183 JSValueRef JSMessageBody::getPlainBody(JSContextRef context,
185 JSStringRef propertyName,
186 JSValueRef* exception)
188 LogDebug("getPlainBody");
192 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
193 Api::Messaging::IMessagePtr msg = getMessage(context, object);
194 std::string body = msg->getBody();
195 LogDebug("getPlainBody plainBody" << body );
196 return converter.toJSValueRef(body);
198 Catch(WrtDeviceApis::Commons::ConversionException) {
199 LogError("Error on conversion");
200 return JSTizenExceptionFactory::postException(context, exception,
201 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
204 Catch(WrtDeviceApis::Commons::NullPointerException) {
205 LogError("Error on conversion");
206 return JSTizenExceptionFactory::postException(context, exception,
207 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
210 return JSValueMakeUndefined(context);
213 bool JSMessageBody::setPlainBody(JSContextRef context,
215 JSStringRef propertyName,
217 JSValueRef* exception)
220 LogDebug("setPlainBody");
224 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
225 Api::Messaging::IMessagePtr msg = getMessage(context, object);
226 std::string body = converter.toString(value);
228 LogDebug("setPlainBody plainBody" << body );
231 Catch(WrtDeviceApis::Commons::ConversionException) {
232 LogError("Error on conversion");
233 return JSTizenExceptionFactory::postException(context, exception,
234 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
236 Catch(WrtDeviceApis::Commons::NullPointerException) {
237 LogError("Error on pointer, null value");
238 return JSTizenExceptionFactory::postException(context, exception,
239 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
241 Catch(WrtDeviceApis::Commons::PlatformException) {
242 LogError("Platform execution");
243 return JSTizenExceptionFactory::postException(context, exception,
244 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
249 JSValueRef JSMessageBody::getHtmlBody(JSContextRef context,
251 JSStringRef propertyName,
252 JSValueRef* exception)
255 LogDebug("getHtmlBody");
258 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
259 Api::Messaging::IMessagePtr msg = getMessage(context, object);
261 LogDebug("message Type : " << msg->getMessageType() );
262 if (msg->getMessageType() == Api::Messaging::EMAIL)
264 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
266 LogDebug("Html Body : " << email->getHtmlBody() );
267 return converter.toJSValueRef(email->getHtmlBody());
271 return JSValueMakeUndefined(context);
274 Catch(WrtDeviceApis::Commons::ConversionException) {
275 LogError("Error on conversion");
276 return JSTizenExceptionFactory::postException(context, exception,
277 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
279 Catch(WrtDeviceApis::Commons::NullPointerException) {
280 LogError("Error on pointer, null value");
281 return JSTizenExceptionFactory::postException(context, exception,
282 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
284 Catch(WrtDeviceApis::Commons::PlatformException) {
285 LogError("Platform execution");
286 return JSTizenExceptionFactory::postException(context, exception,
287 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
291 return JSValueMakeUndefined(context);
294 bool JSMessageBody::setHtmlBody(JSContextRef context,
296 JSStringRef propertyName,
298 JSValueRef* exception)
301 LogDebug("setHtmlBody");
305 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
306 Api::Messaging::IMessagePtr msg = getMessage(context, object);
308 if (msg->getMessageType() == Api::Messaging::EMAIL)
310 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
311 std::string body = converter.toString(value);
312 LogDebug("input String : " << body);
313 email->setHtmlBody(body);
318 LogError("html body is for Email only.");
319 return JSTizenExceptionFactory::postException(context, exception,
320 JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);
323 Catch(WrtDeviceApis::Commons::ConversionException) {
324 LogError("Error on conversion");
325 return JSTizenExceptionFactory::postException(context, exception,
326 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
328 Catch(WrtDeviceApis::Commons::NullPointerException) {
329 LogError("Error on pointer, null value");
330 return JSTizenExceptionFactory::postException(context, exception,
331 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
333 Catch(WrtDeviceApis::Commons::PlatformException) {
334 LogError("Platform execution");
335 return JSTizenExceptionFactory::postException(context, exception,
336 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
342 Api::Messaging::IMessagePtr JSMessageBody::getMessage(JSContextRef context,
345 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
346 return getMessage(context, converter.toJSObjectRef(value));
349 Api::Messaging::IMessagePtr JSMessageBody::getMessage(JSContextRef context,
352 // private object of thisObject
353 return getPrivate(object)->getObject();
356 JSMessageBodyPrivate* JSMessageBody::getPrivate(JSObjectRef thisObject)
358 JSMessageBodyPrivate* thisPrivate =
359 static_cast<JSMessageBodyPrivate*>(JSObjectGetPrivate(thisObject));
361 LogError("no private");
362 Throw(WrtDeviceApis::Commons::NullPointerException);