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.
20 //#include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
26 #include "IConversation.h"
27 #include "JSConversation.h"
28 #include "ConverterMessage.h"
30 #include <JSWebAPIError.h>
36 using namespace DeviceAPI::Messaging;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
44 JSClassDefinition JSConversation::m_classInfo =
47 kJSClassAttributeNone,
48 "MessageConversation",
57 NULL, //deleteProperty,
65 const char* JSConversation::CONVID = "id";
66 const char* JSConversation::TYPE = "type";
67 const char* JSConversation::TIMESTAMP = "timestamp";
68 const char* JSConversation::MESSAGECOUNT = "messageCount";
69 const char* JSConversation::UNREADMESSAGES = "unreadMessages";
70 const char* JSConversation::PREVIEW = "preview";
71 const char* JSConversation::SUBJECT = "subject";
72 const char* JSConversation::ISREAD = "isRead";
73 const char* JSConversation::FROM = "from";
74 const char* JSConversation::TO = "to";
75 const char* JSConversation::CC = "cc";
76 const char* JSConversation::BCC = "bcc";
77 const char* JSConversation::LASTMESSAGEID = "lastMessageId";
79 const std::string JSConversation::TYPE_SMS = "messaging.sms";
80 const std::string JSConversation::TYPE_MMS = "messaging.mms";
81 const std::string JSConversation::TYPE_EMAIL = "messaging.email";
84 JSStaticValue JSConversation::m_properties[] = {
85 {"id", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
86 {"type", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
87 {"timestamp", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
88 {"messageCount", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
89 {"unreadMessages", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
90 {"preview", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
91 {"subject", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
92 {"isRead", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
93 {"from", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
94 {"to", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
95 {"cc", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
96 {"bcc", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
97 {"lastMessageId", JSConversation::getProperty, NULL, kJSPropertyAttributeReadOnly },
102 const JSClassRef JSConversation::getClassRef() {
104 m_jsClassRef = JSClassCreate(&m_classInfo);
109 const JSClassDefinition* JSConversation::getClassInfo() {
113 JSClassRef JSConversation::m_jsClassRef = JSClassCreate(JSConversation::getClassInfo());
115 void JSConversation::initialize(JSContextRef context, JSObjectRef object)
117 LoggerD("JSConversation::initialize ");
118 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
122 LoggerD("Private data is null ");
126 LoggerD("JSConversation::already exist ");
131 JSObjectRef JSConversation::createJSObject(JSContextRef context, IConversationPtr object)
133 JSConversationPriv* priv = new JSConversationPriv( context, object);
134 return JSObjectMake(context, getClassRef(), priv);
138 void JSConversation::finalize(JSObjectRef object) {
139 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
141 LoggerD("JSConversation::Finalize");
145 JSObjectSetPrivate(object, NULL);
150 bool JSConversation::hasInstance(JSContextRef context, JSObjectRef constructor,
151 JSValueRef possibleInstance, JSValueRef* exception) {
152 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
155 JSValueRef JSConversation::getProperty(JSContextRef context,
157 JSStringRef propertyName,
158 JSValueRef* exception)
162 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
163 // WrtDeviceApis::CommonsJavaScript::Converter converter(context);
165 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
170 throw DeviceAPI::Common::UnknownException("Object is null");
173 IConversationPtr conversation(priv->getObject());
175 if (conversation->getResult() == false)
177 throw DeviceAPI::Common::UnknownException("Failed to get object");
179 if(JSStringIsEqualToUTF8CString(propertyName, CONVID))
181 return converter->toJSValueRef(converter->convertIntToString(conversation->getConvId()));
183 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
185 return converter->toJSValueRef(convertMessageType(conversation->getType()));
187 else if(JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP))
189 return converter->toJSValueRef(conversation->getTime());
191 else if(JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT))
193 return converter->toJSValueRef(conversation->getMessageCount());
195 else if(JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES))
197 return converter->toJSValueRef(conversation->getUnreadMessages());
199 else if(JSStringIsEqualToUTF8CString(propertyName, PREVIEW))
201 return converter->toJSValueRef(conversation->getPreview());
203 else if(JSStringIsEqualToUTF8CString(propertyName, SUBJECT))
205 return converter->toJSValueRef(conversation->getSubject());
207 else if(JSStringIsEqualToUTF8CString(propertyName, ISREAD))
209 return converter->toJSValueRef(conversation->getRead());
211 else if(JSStringIsEqualToUTF8CString(propertyName, FROM))
213 return converter->toJSValueRef(conversation->getFrom());
215 else if(JSStringIsEqualToUTF8CString(propertyName, TO))
217 return converter->toJSValueRef(conversation->getTo());
219 else if(JSStringIsEqualToUTF8CString(propertyName, CC))
221 return converter->toJSValueRef(conversation->getCC());
223 else if(JSStringIsEqualToUTF8CString(propertyName, BCC))
225 return converter->toJSValueRef(conversation->getBCC());
227 else if(JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID))
229 return converter->toJSValueRef(converter->convertIntToString(conversation->getLastMessageId()));
233 catch (const WrtDeviceApis::Commons::Exception& ex) {
234 LoggerE("Exception: " << ex.GetMessage());
235 return JSValueMakeUndefined(context);
240 bool JSConversation::setProperty(JSContextRef context,
242 JSStringRef propertyName,
244 JSValueRef* exception)
248 if (JSStringIsEqualToUTF8CString(propertyName, CONVID)) {
250 } else if (JSStringIsEqualToUTF8CString(propertyName, TYPE)) {
252 } else if (JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP)) {
254 } else if (JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT)) {
256 } else if (JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES)) {
258 } else if (JSStringIsEqualToUTF8CString(propertyName, PREVIEW)) {
260 } else if (JSStringIsEqualToUTF8CString(propertyName, SUBJECT)) {
262 } else if (JSStringIsEqualToUTF8CString(propertyName, ISREAD)) {
264 } else if (JSStringIsEqualToUTF8CString(propertyName, FROM)) {
266 } else if (JSStringIsEqualToUTF8CString(propertyName, TO)) {
268 } else if (JSStringIsEqualToUTF8CString(propertyName, CC)) {
270 } else if (JSStringIsEqualToUTF8CString(propertyName, BCC)) {
272 } else if (JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID)) {
278 void JSConversation::getPropertyNames(JSContextRef context,
280 JSPropertyNameAccumulatorRef propertyNames)
283 JSStringRef propertyName = NULL;
285 propertyName = JSStringCreateWithUTF8CString(CONVID);
286 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
287 JSStringRelease(propertyName);
289 propertyName = JSStringCreateWithUTF8CString(TYPE);
290 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
291 JSStringRelease(propertyName);
293 propertyName = JSStringCreateWithUTF8CString(TIMESTAMP);
294 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
295 JSStringRelease(propertyName);
297 propertyName = JSStringCreateWithUTF8CString(MESSAGECOUNT);
298 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
299 JSStringRelease(propertyName);
301 propertyName = JSStringCreateWithUTF8CString(UNREADMESSAGES);
302 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
303 JSStringRelease(propertyName);
305 propertyName = JSStringCreateWithUTF8CString(PREVIEW);
306 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
307 JSStringRelease(propertyName);
309 propertyName = JSStringCreateWithUTF8CString(SUBJECT);
310 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
311 JSStringRelease(propertyName);
313 propertyName = JSStringCreateWithUTF8CString(ISREAD);
314 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
315 JSStringRelease(propertyName);
317 propertyName = JSStringCreateWithUTF8CString(FROM);
318 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
319 JSStringRelease(propertyName);
321 propertyName = JSStringCreateWithUTF8CString(TO);
322 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
323 JSStringRelease(propertyName);
325 propertyName = JSStringCreateWithUTF8CString(CC);
326 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
327 JSStringRelease(propertyName);
329 propertyName = JSStringCreateWithUTF8CString(BCC);
330 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
331 JSStringRelease(propertyName);
333 propertyName = JSStringCreateWithUTF8CString(LASTMESSAGEID);
334 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
335 JSStringRelease(propertyName);
338 bool JSConversation::isObjectOfClass(JSContextRef context, JSValueRef value)
340 return JSValueIsObjectOfClass(context, value, getClassRef());
343 IConversationPtr JSConversation::getConversation(JSContextRef context, JSValueRef value)
345 JSObjectRef object = JSValueToObject(context, value, NULL);
348 throw DeviceAPI::Common::TypeMismatchException("Failed to get object");
350 JSConversationPriv *priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
354 throw DeviceAPI::Common::TypeMismatchException("Object is null");
356 return priv->getObject();
359 std::string JSConversation::convertMessageType(int msgType){
362 return JSConversation::TYPE_SMS;
363 }else if(msgType == MMS){
364 return JSConversation::TYPE_MMS;
365 }else if(msgType == EMAIL){
366 return JSConversation::TYPE_EMAIL;