wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Messaging / JSConversation.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
19
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"
29
30 #include <JSWebAPIError.h>
31 #include <Logger.h>
32
33
34 using namespace std;
35 using namespace DPL;
36 using namespace DeviceAPI::Messaging;
37 using namespace WrtDeviceApis::Commons;
38 using namespace WrtDeviceApis::CommonsJavaScript;
39
40
41 namespace DeviceAPI {
42 namespace Messaging {
43
44 JSClassDefinition JSConversation::m_classInfo =
45 {
46         0,
47         kJSClassAttributeNone,
48         "MessageConversation",
49         NULL,
50         NULL, //m_properties,
51         NULL,
52         initialize,
53         finalize,
54         NULL, //hasProperty,
55         getProperty,
56         setProperty,
57         NULL, //deleteProperty,
58         getPropertyNames,
59         NULL,
60         NULL,
61         hasInstance,
62         NULL
63 };
64
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";
78
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";
82
83 #if 0
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 },
98         {0, 0, 0, 0}
99 };
100 #endif
101
102 const JSClassRef JSConversation::getClassRef() {
103         if (!m_jsClassRef) {
104                 m_jsClassRef = JSClassCreate(&m_classInfo);
105         }
106         return m_jsClassRef;
107 }
108
109 const JSClassDefinition* JSConversation::getClassInfo() {
110         return &m_classInfo;
111 }
112
113 JSClassRef JSConversation::m_jsClassRef = JSClassCreate(JSConversation::getClassInfo());
114
115 void JSConversation::initialize(JSContextRef context, JSObjectRef object)
116 {
117         LoggerD("JSConversation::initialize ");
118         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
119 ;
120         if (priv == NULL)
121         {
122                 LoggerD("Private data is null ");
123         }
124         else
125         {
126                 LoggerD("JSConversation::already exist ");
127         }
128 }
129
130
131 JSObjectRef JSConversation::createJSObject(JSContextRef context, IConversationPtr object)
132 {
133         JSConversationPriv* priv = new JSConversationPriv( context, object);
134         return JSObjectMake(context, getClassRef(), priv);
135 }
136
137
138 void JSConversation::finalize(JSObjectRef object) {
139         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
140
141         LoggerD("JSConversation::Finalize");
142
143         if (priv != NULL)
144         {
145                 JSObjectSetPrivate(object, NULL);
146                 delete priv;
147         }
148 }
149
150 bool JSConversation::hasInstance(JSContextRef context, JSObjectRef constructor,
151                 JSValueRef possibleInstance, JSValueRef* exception) {
152         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
153 }
154
155 JSValueRef JSConversation::getProperty(JSContextRef context,
156                                                                                 JSObjectRef object,
157                                                                                 JSStringRef propertyName,
158                                                                                 JSValueRef* exception)
159 {
160         LoggerD("OK");
161
162         ConverterMessageFactory::ConverterType converter =  ConverterMessageFactory::getConverter(context);
163 //      WrtDeviceApis::CommonsJavaScript::Converter converter(context);
164
165         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
166         try
167         {
168                 if (priv == NULL)
169                 {
170                         throw DeviceAPI::Common::UnknownException("Object is null");
171                 }
172
173                 IConversationPtr conversation(priv->getObject());
174
175                 if (conversation->getResult() == false)
176                 {
177                         throw DeviceAPI::Common::UnknownException("Failed to get object");
178                 }
179                 if(JSStringIsEqualToUTF8CString(propertyName, CONVID))
180                 {
181                         return converter->toJSValueRef(converter->convertIntToString(conversation->getConvId()));
182                 }
183                 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
184                 {
185                         return converter->toJSValueRef(convertMessageType(conversation->getType()));
186                 }
187                 else if(JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP))
188                 {
189                         return converter->toJSValueRef(conversation->getTime());
190                 }
191                 else if(JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT))
192                 {
193                         return converter->toJSValueRef(conversation->getMessageCount());
194                 }
195                 else if(JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES))
196                 {
197                         return converter->toJSValueRef(conversation->getUnreadMessages());
198                 }
199                 else if(JSStringIsEqualToUTF8CString(propertyName, PREVIEW))
200                 {
201                         return converter->toJSValueRef(conversation->getPreview());
202                 }
203                 else if(JSStringIsEqualToUTF8CString(propertyName, SUBJECT))
204                 {
205                         return converter->toJSValueRef(conversation->getSubject());
206                 }
207                 else if(JSStringIsEqualToUTF8CString(propertyName, ISREAD))
208                 {
209                         return converter->toJSValueRef(conversation->getRead());
210                 }
211                 else if(JSStringIsEqualToUTF8CString(propertyName, FROM))
212                 {
213                         return converter->toJSValueRef(conversation->getFrom());
214                 }
215                 else if(JSStringIsEqualToUTF8CString(propertyName, TO))
216                 {
217                         return converter->toJSValueRef(conversation->getTo());
218                 }
219                 else if(JSStringIsEqualToUTF8CString(propertyName, CC))
220                 {
221                         return converter->toJSValueRef(conversation->getCC());
222                 }
223                 else if(JSStringIsEqualToUTF8CString(propertyName, BCC))
224                 {
225                         return converter->toJSValueRef(conversation->getBCC());
226                 }
227                 else if(JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID))
228                 {
229                         return converter->toJSValueRef(converter->convertIntToString(conversation->getLastMessageId()));
230                 }
231
232         }
233         catch (const WrtDeviceApis::Commons::Exception& ex) {
234                 LoggerE("Exception: " << ex.GetMessage());
235                 return JSValueMakeUndefined(context);
236         }
237         return NULL;
238 }
239
240 bool JSConversation::setProperty(JSContextRef context,
241         JSObjectRef object,
242         JSStringRef propertyName,
243         JSValueRef value,
244         JSValueRef* exception)
245 {
246     LoggerD("entered");
247
248     if (JSStringIsEqualToUTF8CString(propertyName, CONVID)) {
249         return true;
250     } else if (JSStringIsEqualToUTF8CString(propertyName, TYPE)) {
251         return true;
252     } else if (JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP)) {
253         return true;
254     } else if (JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT)) {
255         return true;
256     } else if (JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES)) {
257         return true;
258     } else if (JSStringIsEqualToUTF8CString(propertyName, PREVIEW)) {
259         return true;
260     } else if (JSStringIsEqualToUTF8CString(propertyName, SUBJECT)) {
261         return true;
262     } else if (JSStringIsEqualToUTF8CString(propertyName, ISREAD)) {
263         return true;
264     } else if (JSStringIsEqualToUTF8CString(propertyName, FROM)) {
265         return true;
266     } else if (JSStringIsEqualToUTF8CString(propertyName, TO)) {
267         return true;
268     } else if (JSStringIsEqualToUTF8CString(propertyName, CC)) {
269         return true;
270     } else if (JSStringIsEqualToUTF8CString(propertyName, BCC)) {
271         return true;
272     } else if (JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID)) {
273         return true;
274         }
275     return false;
276 }
277
278 void JSConversation::getPropertyNames(JSContextRef context,
279         JSObjectRef object,
280         JSPropertyNameAccumulatorRef propertyNames)
281 {
282     LoggerD("Entered");
283     JSStringRef propertyName = NULL;
284
285     propertyName = JSStringCreateWithUTF8CString(CONVID);
286     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
287     JSStringRelease(propertyName);
288
289     propertyName = JSStringCreateWithUTF8CString(TYPE);
290     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
291     JSStringRelease(propertyName);
292
293     propertyName = JSStringCreateWithUTF8CString(TIMESTAMP);
294     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
295     JSStringRelease(propertyName);
296
297     propertyName = JSStringCreateWithUTF8CString(MESSAGECOUNT);
298     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
299     JSStringRelease(propertyName);
300
301     propertyName = JSStringCreateWithUTF8CString(UNREADMESSAGES);
302     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
303     JSStringRelease(propertyName);
304
305     propertyName = JSStringCreateWithUTF8CString(PREVIEW);
306     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
307     JSStringRelease(propertyName);
308
309     propertyName = JSStringCreateWithUTF8CString(SUBJECT);
310     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
311     JSStringRelease(propertyName);
312
313     propertyName = JSStringCreateWithUTF8CString(ISREAD);
314     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
315     JSStringRelease(propertyName);
316
317     propertyName = JSStringCreateWithUTF8CString(FROM);
318     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
319     JSStringRelease(propertyName);
320
321     propertyName = JSStringCreateWithUTF8CString(TO);
322     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
323     JSStringRelease(propertyName);
324
325     propertyName = JSStringCreateWithUTF8CString(CC);
326     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
327     JSStringRelease(propertyName);
328
329     propertyName = JSStringCreateWithUTF8CString(BCC);
330     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
331     JSStringRelease(propertyName);
332
333     propertyName = JSStringCreateWithUTF8CString(LASTMESSAGEID);
334     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
335     JSStringRelease(propertyName);
336 }
337
338 bool JSConversation::isObjectOfClass(JSContextRef context, JSValueRef value)
339 {
340         return JSValueIsObjectOfClass(context, value, getClassRef());
341 }
342
343 IConversationPtr JSConversation::getConversation(JSContextRef context, JSValueRef value)
344 {
345         JSObjectRef object = JSValueToObject(context, value, NULL);
346         if (!object)
347         {
348                 throw DeviceAPI::Common::TypeMismatchException("Failed to get object");
349         }
350         JSConversationPriv *priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
351
352         if (!priv)
353         {
354                 throw DeviceAPI::Common::TypeMismatchException("Object is null");
355         }
356         return priv->getObject();
357 }
358
359 std::string JSConversation::convertMessageType(int msgType){
360
361         if(msgType == SMS){
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;
367         }else{
368                 return NULL;
369         }
370  }
371
372 }
373 }// WrtPlugins
374
375