Update change log and spec for wrt-plugins-tizen_0.4.70
[platform/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 <JSWebAPIErrorFactory.h>
31 #include <Logger.h>
32
33
34 using namespace std;
35
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         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
118
119         if (priv == NULL)
120         {
121                 LoggerD("Private data is null ");
122         }
123         else
124         {
125                 LoggerD("JSConversation::already exist ");
126         }
127 }
128
129
130 JSObjectRef JSConversation::createJSObject(JSContextRef context, IConversationPtr object)
131 {
132         JSConversationPriv* priv = new JSConversationPriv( context, object);
133         return JSObjectMake(context, getClassRef(), priv);
134 }
135
136
137 void JSConversation::finalize(JSObjectRef object) {
138         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
139
140         if (priv != NULL)
141         {
142                 JSObjectSetPrivate(object, NULL);
143                 delete priv;
144         }
145 }
146
147 bool JSConversation::hasInstance(JSContextRef context, JSObjectRef constructor,
148                 JSValueRef possibleInstance, JSValueRef* exception) {
149         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
150 }
151
152 JSValueRef JSConversation::getProperty(JSContextRef context,
153                                                                                 JSObjectRef object,
154                                                                                 JSStringRef propertyName,
155                                                                                 JSValueRef* exception)
156 {
157
158         ConverterMessageFactory::ConverterType converter =  ConverterMessageFactory::getConverter(context);
159 //      WrtDeviceApis::CommonsJavaScript::Converter converter(context);
160
161         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
162         try
163         {
164                 if (priv == NULL)
165                 {
166                         throw DeviceAPI::Common::UnknownException("Object is null");
167                 }
168
169                 IConversationPtr conversation(priv->getObject());
170
171                 if (conversation->getResult() == false)
172                 {
173                         throw DeviceAPI::Common::UnknownException("Failed to get object");
174                 }
175                 if(JSStringIsEqualToUTF8CString(propertyName, CONVID))
176                 {
177                         return converter->toJSValueRef(converter->convertIntToString(conversation->getConvId()));
178                 }
179                 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
180                 {
181                         return converter->toJSValueRef(convertMessageType(conversation->getType()));
182                 }
183                 else if(JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP))
184                 {
185                         return converter->toJSValueRef(conversation->getTime());
186                 }
187                 else if(JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT))
188                 {
189                         return converter->toJSValueRef(conversation->getMessageCount());
190                 }
191                 else if(JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES))
192                 {
193                         return converter->toJSValueRef(conversation->getUnreadMessages());
194                 }
195                 else if(JSStringIsEqualToUTF8CString(propertyName, PREVIEW))
196                 {
197                         return converter->toJSValueRef(conversation->getPreview());
198                 }
199                 else if(JSStringIsEqualToUTF8CString(propertyName, SUBJECT))
200                 {
201                         return converter->toJSValueRef(conversation->getSubject());
202                 }
203                 else if(JSStringIsEqualToUTF8CString(propertyName, ISREAD))
204                 {
205                         return converter->toJSValueRef(conversation->getRead());
206                 }
207                 else if(JSStringIsEqualToUTF8CString(propertyName, FROM))
208                 {
209                         return converter->toJSValueRef(conversation->getFrom());
210                 }
211                 else if(JSStringIsEqualToUTF8CString(propertyName, TO))
212                 {
213                         return converter->toJSValueRef(conversation->getTo());
214                 }
215                 else if(JSStringIsEqualToUTF8CString(propertyName, CC))
216                 {
217                         return converter->toJSValueRef(conversation->getCC());
218                 }
219                 else if(JSStringIsEqualToUTF8CString(propertyName, BCC))
220                 {
221                         return converter->toJSValueRef(conversation->getBCC());
222                 }
223                 else if(JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID))
224                 {
225                         return converter->toJSValueRef(converter->convertIntToString(conversation->getLastMessageId()));
226                 }
227
228         }
229         catch (const WrtDeviceApis::Commons::Exception& ex) {
230                 LoggerE("Exception: " << ex.GetMessage());
231                 return JSValueMakeUndefined(context);
232         }
233         return NULL;
234 }
235
236 bool JSConversation::setProperty(JSContextRef context,
237         JSObjectRef object,
238         JSStringRef propertyName,
239         JSValueRef value,
240         JSValueRef* exception)
241 {
242
243     if (JSStringIsEqualToUTF8CString(propertyName, CONVID)) {
244         return true;
245     } else if (JSStringIsEqualToUTF8CString(propertyName, TYPE)) {
246         return true;
247     } else if (JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP)) {
248         return true;
249     } else if (JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT)) {
250         return true;
251     } else if (JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES)) {
252         return true;
253     } else if (JSStringIsEqualToUTF8CString(propertyName, PREVIEW)) {
254         return true;
255     } else if (JSStringIsEqualToUTF8CString(propertyName, SUBJECT)) {
256         return true;
257     } else if (JSStringIsEqualToUTF8CString(propertyName, ISREAD)) {
258         return true;
259     } else if (JSStringIsEqualToUTF8CString(propertyName, FROM)) {
260         return true;
261     } else if (JSStringIsEqualToUTF8CString(propertyName, TO)) {
262         return true;
263     } else if (JSStringIsEqualToUTF8CString(propertyName, CC)) {
264         return true;
265     } else if (JSStringIsEqualToUTF8CString(propertyName, BCC)) {
266         return true;
267     } else if (JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID)) {
268         return true;
269         }
270     return false;
271 }
272
273 void JSConversation::getPropertyNames(JSContextRef context,
274         JSObjectRef object,
275         JSPropertyNameAccumulatorRef propertyNames)
276 {
277     JSStringRef propertyName = NULL;
278
279     propertyName = JSStringCreateWithUTF8CString(CONVID);
280     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
281     JSStringRelease(propertyName);
282
283     propertyName = JSStringCreateWithUTF8CString(TYPE);
284     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
285     JSStringRelease(propertyName);
286
287     propertyName = JSStringCreateWithUTF8CString(TIMESTAMP);
288     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
289     JSStringRelease(propertyName);
290
291     propertyName = JSStringCreateWithUTF8CString(MESSAGECOUNT);
292     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
293     JSStringRelease(propertyName);
294
295     propertyName = JSStringCreateWithUTF8CString(UNREADMESSAGES);
296     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
297     JSStringRelease(propertyName);
298
299     propertyName = JSStringCreateWithUTF8CString(PREVIEW);
300     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
301     JSStringRelease(propertyName);
302
303     propertyName = JSStringCreateWithUTF8CString(SUBJECT);
304     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
305     JSStringRelease(propertyName);
306
307     propertyName = JSStringCreateWithUTF8CString(ISREAD);
308     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
309     JSStringRelease(propertyName);
310
311     propertyName = JSStringCreateWithUTF8CString(FROM);
312     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
313     JSStringRelease(propertyName);
314
315     propertyName = JSStringCreateWithUTF8CString(TO);
316     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
317     JSStringRelease(propertyName);
318
319     propertyName = JSStringCreateWithUTF8CString(CC);
320     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
321     JSStringRelease(propertyName);
322
323     propertyName = JSStringCreateWithUTF8CString(BCC);
324     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
325     JSStringRelease(propertyName);
326
327     propertyName = JSStringCreateWithUTF8CString(LASTMESSAGEID);
328     JSPropertyNameAccumulatorAddName(propertyNames, propertyName);
329     JSStringRelease(propertyName);
330 }
331
332 bool JSConversation::isObjectOfClass(JSContextRef context, JSValueRef value)
333 {
334         return JSValueIsObjectOfClass(context, value, getClassRef());
335 }
336
337 IConversationPtr JSConversation::getConversation(JSContextRef context, JSValueRef value)
338 {
339         JSObjectRef object = JSValueToObject(context, value, NULL);
340         if (!object)
341         {
342                 throw DeviceAPI::Common::TypeMismatchException("Failed to get object");
343         }
344         JSConversationPriv *priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
345
346         if (!priv)
347         {
348                 throw DeviceAPI::Common::TypeMismatchException("Object is null");
349         }
350         return priv->getObject();
351 }
352
353 std::string JSConversation::convertMessageType(int msgType){
354
355         if(msgType == SMS){
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;
361         }else{
362                 return NULL;
363         }
364  }
365
366 }
367 }// WrtPlugins
368
369