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 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/Validator.h>
21 #include <CommonsJavaScript/JSUtils.h>
22 #include <CommonsJavaScript/JSCallbackManager.h>
23 #include <CommonsJavaScript/Utils.h>
24 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
25 #include <API/Messaging/IConversation.h>
26 #include "JSConversation.h"
32 using namespace TizenApis::Api::Messaging;
33 using namespace WrtDeviceApis::Commons;
34 using namespace WrtDeviceApis::CommonsJavaScript;
40 JSClassDefinition JSConversation::m_classInfo =
43 kJSClassAttributeNone,
44 "MessageConversation",
53 NULL, //deleteProperty,
54 NULL, //getPropertyNames,
61 const char* JSConversation::CONVID = "id";
62 const char* JSConversation::TYPE = "type";
63 const char* JSConversation::TIMESTAMP = "timestamp";
64 const char* JSConversation::MESSAGECOUNT = "messageCount";
65 const char* JSConversation::UNREADMESSAGES = "unreadMessages";
66 const char* JSConversation::PREVIEW = "preview";
67 const char* JSConversation::SUBJECT = "subject";
68 const char* JSConversation::ISREAD = "isRead";
69 const char* JSConversation::FROM = "from";
70 const char* JSConversation::TO = "to";
71 const char* JSConversation::CC = "cc";
72 const char* JSConversation::BCC = "bcc";
73 const char* JSConversation::LASTMESSAGEID = "lastMessageId";
75 const std::string JSConversation::TYPE_SMS = "messaging.sms";
76 const std::string JSConversation::TYPE_MMS = "messaging.mms";
77 const std::string JSConversation::TYPE_EMAIL = "messaging.email";
79 JSStaticValue JSConversation::m_properties[] = {
80 {"id", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
81 {"type", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
82 {"timestamp", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
83 {"messageCount", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
84 {"unreadMessages", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
85 {"preview", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
86 {"subject", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
87 {"isRead", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
88 {"from", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
89 {"to", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
90 {"cc", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
91 {"bcc", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
92 {"lastMessageId", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
96 const JSClassRef JSConversation::getClassRef() {
98 m_jsClassRef = JSClassCreate(&m_classInfo);
103 const JSClassDefinition* JSConversation::getClassInfo() {
107 JSClassRef JSConversation::m_jsClassRef = JSClassCreate(JSConversation::getClassInfo());
109 void JSConversation::initialize(JSContextRef context, JSObjectRef object)
111 LogDebug("JSConversation::initialize ");
112 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
116 LogDebug("Private data is null ");
120 LogDebug("JSConversation::already exist ");
126 JSObjectRef JSConversation::createJSObject(JSContextRef context, IConversationPtr object)
128 JSConversationPriv* priv = new JSConversationPriv( context, object);
129 return JSObjectMake(context, getClassRef(), priv);
133 void JSConversation::finalize(JSObjectRef object) {
134 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
136 LogDebug("JSConversation::Finalrize");
140 JSObjectSetPrivate(object, NULL);
145 bool JSConversation::hasInstance(JSContextRef context, JSObjectRef constructor,
146 JSValueRef possibleInstance, JSValueRef* exception) {
147 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
150 JSValueRef JSConversation::getProperty(JSContextRef context,
152 JSStringRef propertyName,
153 JSValueRef* exception)
157 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
158 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
163 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
166 IConversationPtr conversation(priv->getObject());
168 if (conversation->getResult() == false)
170 Throw(WrtDeviceApis::Commons::PlatformException);
172 if(JSStringIsEqualToUTF8CString(propertyName, "id"))
174 return converter.toJSValueRef(conversation->getId());
176 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
178 return converter.toJSValueRef(convertMessageType(conversation->getType()));
180 else if(JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP))
182 return converter.toJSValueRef(conversation->getTime());
184 else if(JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT))
186 return converter.toJSValueRef(conversation->getMessageCount());
188 else if(JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES))
190 return converter.toJSValueRef(conversation->getUnreadMessages());
192 else if(JSStringIsEqualToUTF8CString(propertyName, PREVIEW))
194 return converter.toJSValueRef(conversation->getPreview());
196 else if(JSStringIsEqualToUTF8CString(propertyName, SUBJECT))
198 return converter.toJSValueRef(conversation->getSubject());
200 else if(JSStringIsEqualToUTF8CString(propertyName, ISREAD))
202 return converter.toJSValueRef(conversation->getRead());
204 else if(JSStringIsEqualToUTF8CString(propertyName, FROM))
206 return converter.toJSValueRef(conversation->getFrom());
208 else if(JSStringIsEqualToUTF8CString(propertyName, TO))
210 return converter.toJSValueRef(conversation->getTo());
212 else if(JSStringIsEqualToUTF8CString(propertyName, CC))
214 return converter.toJSValueRef(conversation->getCC());
216 else if(JSStringIsEqualToUTF8CString(propertyName, BCC))
218 return converter.toJSValueRef(conversation->getBCC());
220 else if(JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID))
222 return converter.toJSValueRef(conversation->getLastMessageId());
226 catch (const WrtDeviceApis::Commons::Exception& ex) {
227 LogError("Exception: " << ex.GetMessage());
229 return JSValueMakeNull(context);
232 bool JSConversation::isObjectOfClass(JSContextRef context, JSValueRef value)
234 return JSValueIsObjectOfClass(context, value, getClassRef());
237 IConversationPtr JSConversation::getConversation(JSContextRef context, JSValueRef value)
239 JSObjectRef object = JSValueToObject(context, value, NULL);
242 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
244 JSConversationPriv *priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
248 Throw(WrtDeviceApis::Commons::NullPointerException);
250 return priv->getObject();
253 std::string JSConversation::convertMessageType(int msgType){
255 if(msgType == Api::Messaging::SMS){
256 return JSConversation::TYPE_SMS;
257 }else if(msgType == Api::Messaging::MMS){
258 return JSConversation::TYPE_MMS;
259 }else if(msgType == Api::Messaging::EMAIL){
260 return JSConversation::TYPE_EMAIL;