781331dc48c3d31cc3bd2fcf38d35b94d1155464
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Messaging / JSConversation.cpp
1 /*
2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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. 
15 */
16
17
18
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"
27
28
29
30 using namespace std;
31 using namespace DPL;
32 using namespace TizenApis::Api::Messaging;
33 using namespace WrtDeviceApis::Commons;
34 using namespace WrtDeviceApis::CommonsJavaScript;
35
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39
40 JSClassDefinition JSConversation::m_classInfo =
41 {
42         0,
43         kJSClassAttributeNone,
44         "MessageConversation",
45         NULL,
46         m_properties,
47         NULL,
48         initialize,
49         finalize,
50         NULL, //hasProperty,
51         NULL, //getProperty,
52         NULL, //setProperty,
53         NULL, //deleteProperty,
54         NULL, //getPropertyNames,
55         NULL,
56         NULL,
57         hasInstance,
58         NULL
59 };
60
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";    
74
75 const std::string JSConversation::TYPE_SMS               = "tizen.sms";
76 const std::string JSConversation::TYPE_MMS               = "tizen.mms";
77 const std::string JSConversation::TYPE_EMAIL     = "tizen.email";
78
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 },
93         {0, 0, 0, 0}
94 };
95
96 const JSClassRef JSConversation::getClassRef() {
97         if (!m_jsClassRef) {
98                 m_jsClassRef = JSClassCreate(&m_classInfo);
99         }
100         return m_jsClassRef;
101 }
102
103 const JSClassDefinition* JSConversation::getClassInfo() {
104         return &m_classInfo;
105 }
106
107 JSClassRef JSConversation::m_jsClassRef = JSClassCreate(JSConversation::getClassInfo());
108
109 void JSConversation::initialize(JSContextRef context, JSObjectRef object) 
110 {
111         LogDebug("JSConversation::initialize ");
112         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
113 ;
114         if (priv == NULL)
115         {
116                 LogDebug("Private data is null ");              
117         }
118         else
119         {
120                 LogDebug("JSConversation::already exist ");             
121         }
122 }
123
124
125
126 JSObjectRef JSConversation::createJSObject(JSContextRef context, IConversationPtr object)
127 {
128         JSConversationPriv* priv = new JSConversationPriv( context, object);
129         return JSObjectMake(context, getClassRef(), priv);
130 }
131
132
133 void JSConversation::finalize(JSObjectRef object) {
134         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
135
136         LogDebug("JSConversation::Finalrize");
137
138         if (priv != NULL) 
139         {
140                 JSObjectSetPrivate(object, NULL);
141                 delete priv;
142         }
143 }
144
145 bool JSConversation::hasInstance(JSContextRef context, JSObjectRef constructor,
146                 JSValueRef possibleInstance, JSValueRef* exception) {
147         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
148 }
149
150 JSValueRef JSConversation::getProperty(JSContextRef context,
151                                                                                 JSObjectRef object,
152                                                                                 JSStringRef propertyName,
153                                                                                 JSValueRef* exception)
154 {
155         LogDebug("OK"); 
156
157         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
158         JSConversationPriv* priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));        
159         try 
160         {
161                 if (priv == NULL)
162                 {
163                         Throw(WrtDeviceApis::Commons::InvalidArgumentException);
164                 }
165                 
166                 IConversationPtr conversation(priv->getObject());
167
168                 if (conversation->getResult() == false)
169                 {
170                         Throw(WrtDeviceApis::Commons::PlatformException);
171                 }
172                 if(JSStringIsEqualToUTF8CString(propertyName, "id")) 
173                 {
174                         return converter.toJSValueRef(conversation->getId());
175                 }
176                 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE)) 
177                 {
178                         return converter.toJSValueRef(convertMessageType(conversation->getType()));
179                 }
180                 else if(JSStringIsEqualToUTF8CString(propertyName, TIMESTAMP)) 
181                 {
182                         return converter.toJSValueRef(conversation->getTime());                 
183                 }
184                 else if(JSStringIsEqualToUTF8CString(propertyName, MESSAGECOUNT)) 
185                 {
186                         return converter.toJSValueRef(conversation->getMessageCount()); 
187                 }
188                 else if(JSStringIsEqualToUTF8CString(propertyName, UNREADMESSAGES)) 
189                 {
190                         return converter.toJSValueRef(conversation->getUnreadMessages());       
191                 }
192                 else if(JSStringIsEqualToUTF8CString(propertyName, PREVIEW)) 
193                 {
194                         return converter.toJSValueRef(conversation->getPreview());      
195                 }
196                 else if(JSStringIsEqualToUTF8CString(propertyName, SUBJECT)) 
197                 {
198                         return converter.toJSValueRef(conversation->getSubject());      
199                 }
200                 else if(JSStringIsEqualToUTF8CString(propertyName, ISREAD)) 
201                 {
202                         return converter.toJSValueRef(conversation->getRead());                         
203                 }
204                 else if(JSStringIsEqualToUTF8CString(propertyName, FROM)) 
205                 {
206                         return converter.toJSValueRef(conversation->getFrom());         
207                 }
208                 else if(JSStringIsEqualToUTF8CString(propertyName, TO)) 
209                 {
210                         return converter.toJSValueRef(conversation->getTo());                                   
211                 }
212                 else if(JSStringIsEqualToUTF8CString(propertyName, CC)) 
213                 {
214                         return converter.toJSValueRef(conversation->getCC());                   
215                 }
216                 else if(JSStringIsEqualToUTF8CString(propertyName, BCC)) 
217                 {
218                         return converter.toJSValueRef(conversation->getBCC());                  
219                 }
220                 else if(JSStringIsEqualToUTF8CString(propertyName, LASTMESSAGEID)) 
221                 {
222                         return converter.toJSValueRef(conversation->getLastMessageId());                        
223                 }
224
225         }
226         catch (const WrtDeviceApis::Commons::Exception& ex) {
227                 LogError("Exception: " << ex.GetMessage());
228         }       
229         return JSValueMakeNull(context);
230 }
231
232 bool JSConversation::isObjectOfClass(JSContextRef context, JSValueRef value)
233 {
234         return JSValueIsObjectOfClass(context, value, getClassRef());
235 }
236
237 IConversationPtr JSConversation::getConversation(JSContextRef context, JSValueRef value)
238 {
239         JSObjectRef object = JSValueToObject(context, value, NULL);
240         if (!object) 
241         {
242                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
243         }
244         JSConversationPriv *priv = static_cast<JSConversationPriv*>(JSObjectGetPrivate(object));
245         
246         if (!priv) 
247         {
248                 Throw(WrtDeviceApis::Commons::NullPointerException);
249         }
250         return priv->getObject();
251 }
252
253 std::string JSConversation::convertMessageType(int msgType){
254
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;
261         }else{
262                 return NULL;
263         }
264  }
265
266
267 }// WrtPlugins
268
269