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 <JSWebAPIErrorFactory.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 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
121 LoggerD("Private data is null ");
125 LoggerD("JSConversation::already exist ");
130 JSObjectRef JSConversation::createJSObject(JSContextRef context, IConversationPtr object)
132 JSConversationPriv* priv = new JSConversationPriv( context, object);
133 return JSObjectMake(context, getClassRef(), priv);
137 void JSConversation::finalize(JSObjectRef object) {
138 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
142 JSObjectSetPrivate(object, NULL);
147 bool JSConversation::hasInstance(JSContextRef context, JSObjectRef constructor,
148 JSValueRef possibleInstance, JSValueRef* exception) {
149 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
152 JSValueRef JSConversation::getProperty(JSContextRef context,
154 JSStringRef propertyName,
155 JSValueRef* exception)
158 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
159 // WrtDeviceApis::CommonsJavaScript::Converter converter(context);
161 JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
166 throw DeviceAPI::Common::UnknownException("Object is null");
169 IConversationPtr conversation(priv->getObject());
171 if (conversation->getResult() == false)
173 throw DeviceAPI::Common::UnknownException("Failed to get object");
175 if(JSStringIsEqualToUTF8CString(propertyName, CONVID))
177 return converter->toJSValueRef(converter->convertIntToString(conversation->getConvId()));
179 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
181 return converter->toJSValueRef(convertMessageType(conversation->getType()));
183 else if(JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP))
185 return converter->toJSValueRef(conversation->getTime());
187 else if(JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT))
189 return converter->toJSValueRef(conversation->getMessageCount());
191 else if(JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES))
193 return converter->toJSValueRef(conversation->getUnreadMessages());
195 else if(JSStringIsEqualToUTF8CString(propertyName, PREVIEW))
197 return converter->toJSValueRef(conversation->getPreview());
199 else if(JSStringIsEqualToUTF8CString(propertyName, SUBJECT))
201 return converter->toJSValueRef(conversation->getSubject());
203 else if(JSStringIsEqualToUTF8CString(propertyName, ISREAD))
205 return converter->toJSValueRef(conversation->getRead());
207 else if(JSStringIsEqualToUTF8CString(propertyName, FROM))
209 return converter->toJSValueRef(conversation->getFrom());
211 else if(JSStringIsEqualToUTF8CString(propertyName, TO))
213 return converter->toJSValueRef(conversation->getTo());
215 else if(JSStringIsEqualToUTF8CString(propertyName, CC))
217 return converter->toJSValueRef(conversation->getCC());
219 else if(JSStringIsEqualToUTF8CString(propertyName, BCC))
221 return converter->toJSValueRef(conversation->getBCC());
223 else if(JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID))
225 return converter->toJSValueRef(converter->convertIntToString(conversation->getLastMessageId()));
229 catch (const WrtDeviceApis::Commons::Exception& ex) {
230 LoggerE("Exception: " << ex.GetMessage());
231 return JSValueMakeUndefined(context);
236 bool JSConversation::setProperty(JSContextRef context,
238 JSStringRef propertyName,
240 JSValueRef* exception)
243 if (JSStringIsEqualToUTF8CString(propertyName, CONVID)) {
245 } else if (JSStringIsEqualToUTF8CString(propertyName, TYPE)) {
247 } else if (JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP)) {
249 } else if (JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT)) {
251 } else if (JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES)) {
253 } else if (JSStringIsEqualToUTF8CString(propertyName, PREVIEW)) {
255 } else if (JSStringIsEqualToUTF8CString(propertyName, SUBJECT)) {
257 } else if (JSStringIsEqualToUTF8CString(propertyName, ISREAD)) {
259 } else if (JSStringIsEqualToUTF8CString(propertyName, FROM)) {
261 } else if (JSStringIsEqualToUTF8CString(propertyName, TO)) {
263 } else if (JSStringIsEqualToUTF8CString(propertyName, CC)) {
265 } else if (JSStringIsEqualToUTF8CString(propertyName, BCC)) {
267 } else if (JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID)) {
273 void JSConversation::getPropertyNames(JSContextRef context,
275 JSPropertyNameAccumulatorRef propertyNames)
277 JSStringRef propertyName = NULL;
279 propertyName = JSStringCreateWithUTF8CString(CONVID);
280 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
281 JSStringRelease(propertyName);
283 propertyName = JSStringCreateWithUTF8CString(TYPE);
284 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
285 JSStringRelease(propertyName);
287 propertyName = JSStringCreateWithUTF8CString(TIMESTAMP);
288 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
289 JSStringRelease(propertyName);
291 propertyName = JSStringCreateWithUTF8CString(MESSAGECOUNT);
292 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
293 JSStringRelease(propertyName);
295 propertyName = JSStringCreateWithUTF8CString(UNREADMESSAGES);
296 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
297 JSStringRelease(propertyName);
299 propertyName = JSStringCreateWithUTF8CString(PREVIEW);
300 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
301 JSStringRelease(propertyName);
303 propertyName = JSStringCreateWithUTF8CString(SUBJECT);
304 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
305 JSStringRelease(propertyName);
307 propertyName = JSStringCreateWithUTF8CString(ISREAD);
308 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
309 JSStringRelease(propertyName);
311 propertyName = JSStringCreateWithUTF8CString(FROM);
312 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
313 JSStringRelease(propertyName);
315 propertyName = JSStringCreateWithUTF8CString(TO);
316 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
317 JSStringRelease(propertyName);
319 propertyName = JSStringCreateWithUTF8CString(CC);
320 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
321 JSStringRelease(propertyName);
323 propertyName = JSStringCreateWithUTF8CString(BCC);
324 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
325 JSStringRelease(propertyName);
327 propertyName = JSStringCreateWithUTF8CString(LASTMESSAGEID);
328 JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
329 JSStringRelease(propertyName);
332 bool JSConversation::isObjectOfClass(JSContextRef context, JSValueRef value)
334 return JSValueIsObjectOfClass(context, value, getClassRef());
337 IConversationPtr JSConversation::getConversation(JSContextRef context, JSValueRef value)
339 JSObjectRef object = JSValueToObject(context, value, NULL);
342 throw DeviceAPI::Common::TypeMismatchException("Failed to get object");
344 JSConversationPriv *priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
348 throw DeviceAPI::Common::TypeMismatchException("Object is null");
350 return priv->getObject();
353 std::string JSConversation::convertMessageType(int msgType){
356 return JSConversation::TYPE_SMS;
357 }else if(msgType == MMS){
358 return JSConversation::TYPE_MMS;
359 }else if(msgType == EMAIL){
360 return JSConversation::TYPE_EMAIL;