merge wrt-plugins-tizen_0.2.0-12
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Messaging / JSMessageBody.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  * @file        JSAttachmentArray.cpp
20  * @author      Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
21  * @version     0.1
22  */
23
24 #include <dpl/log/log.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <CommonsJavaScript/JSUtils.h>
27 #include "ConverterMessage.h"
28 #include "JSMessageBody.h"
29 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
30 #include "JSAttachmentArray.h"
31 #endif
32 #include <Tizen/Common/JSTizenExceptionFactory.h>
33 #include <Tizen/Common/JSTizenException.h>
34 #include "MessagingErrorMsg.h"
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38
39         using namespace TizenApis::Commons;
40         using namespace TizenApis::Api::Messaging;
41         using namespace WrtDeviceApis::Commons; 
42         using namespace WrtDeviceApis::CommonsJavaScript;
43
44
45 JSClassRef JSMessageBody::m_jsClassRef = NULL;
46
47 JSClassDefinition JSMessageBody::m_classInfo = {
48     0,
49     kJSClassAttributeNone,
50     "MessageBody",
51     0,
52     m_property,
53     NULL,
54     initialize,
55     finalize,
56     hasProperty,
57     NULL,   //getproperty
58     NULL,  //setProperty
59     NULL, //deleteProperty,
60     NULL, //getPropertyNames,
61     NULL, //callAsFunction,
62     NULL, //callAsConstructor,
63     NULL, //hasInstance,
64     NULL, //convertToType,
65 };
66
67 JSStaticValue JSMessageBody::m_property[] = {
68     { "messageId", JSMessageBody::getMessageID, NULL, kJSPropertyAttributeReadOnly },
69     { "loaded", JSMessageBody::getLoadedStatus, NULL, kJSPropertyAttributeReadOnly },
70     { "plainBody", JSMessageBody::getPlainBody, JSMessageBody::setPlainBody, kJSPropertyAttributeNone },
71     { "htmlBody", JSMessageBody::getHtmlBody, JSMessageBody::setHtmlBody, kJSPropertyAttributeNone },
72 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
73     { "inlineAttachments", NULL, NULL, kJSPropertyAttributeReadOnly },
74 #endif
75     { 0, 0, 0, 0 }
76 };
77
78 const JSClassDefinition* JSMessageBody::getClassInfo()
79 {
80     return &(m_classInfo);
81 }
82
83 JSClassRef JSMessageBody::getClassRef()
84 {
85     if (!m_jsClassRef) {
86         m_jsClassRef = JSClassCreate(&m_classInfo);
87     }
88     return m_jsClassRef;
89 }
90
91 bool JSMessageBody::hasProperty(JSContextRef context,
92         JSObjectRef object,
93         JSStringRef propertyName)
94 {
95     LogDebug("enter");
96     WrtDeviceApis::CommonsJavaScript::Converter converter(context);
97     Try
98     {
99         Api::Messaging::IMessagePtr msg = getMessage(context, object);
100
101         if (!msg) {
102             return true;
103         }
104     }
105     Catch(WrtDeviceApis::Commons::Exception)
106     {
107         //not reporting error is intended
108     }
109     return false;
110 }
111
112 void JSMessageBody::initialize(JSContextRef context,
113         JSObjectRef object)
114 {
115     LogDebug("enter");
116 }
117
118 void JSMessageBody::finalize(JSObjectRef object)
119 {
120     LogDebug("enter");
121     JSMessageBodyPrivate* priv = static_cast<JSMessageBodyPrivate*>(JSObjectGetPrivate(object));
122     JSObjectSetPrivate(object, NULL);
123     delete priv;
124 }
125
126 JSObjectRef JSMessageBody::createJS( JSContextRef context, const Api::Messaging::IMessagePtr &msg)
127 {
128     JSMessageBodyPrivate *priv = new JSMessageBodyPrivate(context, msg);
129     return JSObjectMake(context, getClassRef(), priv);  //make JSObjectRef.
130 }
131
132 JSValueRef JSMessageBody::getMessageID(JSContextRef context,
133         JSObjectRef object,
134         JSStringRef propertyName,
135         JSValueRef* exception)
136 {
137     LogDebug("enter");
138     Try
139     {
140           Api::Messaging::IMessagePtr msg = getMessage(context, object);
141           
142         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
143         return converter.toJSValueRef(msg->getId());
144     }
145     Catch(WrtDeviceApis::Commons::Exception)
146     {
147         LogError("invalid conversion");
148     }
149     return JSValueMakeUndefined(context);
150 }
151
152 JSValueRef JSMessageBody::getLoadedStatus(JSContextRef context,
153         JSObjectRef object,
154         JSStringRef propertyName,
155         JSValueRef* exception)
156 {
157     LogDebug("enter");
158     Try
159     {
160           WrtDeviceApis::CommonsJavaScript::Converter converter(context);
161           Api::Messaging::IMessagePtr msg = getMessage(context, object);
162           
163           if (msg->getMessageType() == Api::Messaging::EMAIL)
164           {
165                 //Api::Messaging::IEmailPtr email = DPL::StaticPointerCast<Api::IEmailPtr>(Api::Messaging::MessageFactory::convertToEmail(msg));
166                 Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
167                 return converter.toJSValueRef(email->isBodyDownloaded() > 0 ? true : false);
168           }
169           else
170           {
171                 return converter.toJSValueRef(true);
172           }
173           
174     }
175     Catch(WrtDeviceApis::Commons::Exception)
176     {
177         LogError("invalid conversion");
178     }
179     return JSValueMakeUndefined(context);
180 }
181
182
183 JSValueRef JSMessageBody::getPlainBody(JSContextRef context,
184         JSObjectRef object,
185         JSStringRef propertyName,
186         JSValueRef* exception)
187 {       
188     LogDebug("getPlainBody");
189
190     Try
191     {           
192                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
193                 Api::Messaging::IMessagePtr msg = getMessage(context, object);
194                 std::string body = msg->getBody();
195                 LogDebug("getPlainBody plainBody" << body );
196                 return converter.toJSValueRef(body);
197     }
198     Catch(WrtDeviceApis::Commons::ConversionException) {
199         LogError("Error on conversion");
200           return JSTizenExceptionFactory::postException(context, exception, 
201                         JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
202
203     }
204     Catch(WrtDeviceApis::Commons::NullPointerException) {
205         LogError("Error on conversion");
206         return JSTizenExceptionFactory::postException(context, exception, 
207                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
208
209     }
210     return JSValueMakeUndefined(context);
211 }
212
213 bool JSMessageBody::setPlainBody(JSContextRef context,
214         JSObjectRef object,
215         JSStringRef propertyName,
216         JSValueRef value,
217         JSValueRef* exception)
218 {
219
220     LogDebug("setPlainBody");
221
222     Try
223     {
224            WrtDeviceApis::CommonsJavaScript::Converter converter(context);
225         Api::Messaging::IMessagePtr msg = getMessage(context, object);
226         std::string body = converter.toString(value);
227         msg->setBody(body);
228           LogDebug("setPlainBody plainBody" << body );
229         return true;
230     }
231     Catch(WrtDeviceApis::Commons::ConversionException) {
232         LogError("Error on conversion");
233            return JSTizenExceptionFactory::postException(context, exception, 
234                           JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
235     }
236     Catch(WrtDeviceApis::Commons::NullPointerException) {
237         LogError("Error on pointer, null value");
238         return JSTizenExceptionFactory::postException(context, exception, 
239                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
240     }
241     Catch(WrtDeviceApis::Commons::PlatformException) {
242         LogError("Platform execution");
243         return JSTizenExceptionFactory::postException(context, exception, 
244                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
245     }
246     return false;
247 }
248
249 JSValueRef JSMessageBody::getHtmlBody(JSContextRef context,
250         JSObjectRef object,
251         JSStringRef propertyName,
252         JSValueRef* exception)
253 {
254     
255         LogDebug("getHtmlBody");
256     Try
257     {           
258                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
259                 Api::Messaging::IMessagePtr msg = getMessage(context, object);
260
261               LogDebug("message Type : " << msg->getMessageType() );
262                 if (msg->getMessageType() == Api::Messaging::EMAIL)
263                   {
264                         Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
265
266                  LogDebug("Html Body : " << email->getHtmlBody() );
267                         return converter.toJSValueRef(email->getHtmlBody());
268                   }
269                   else
270                   {
271                          return JSValueMakeUndefined(context);
272                   }
273     }
274     Catch(WrtDeviceApis::Commons::ConversionException) {
275         LogError("Error on conversion");
276            return JSTizenExceptionFactory::postException(context, exception, 
277                           JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
278     }
279     Catch(WrtDeviceApis::Commons::NullPointerException) {
280         LogError("Error on pointer, null value");
281         return JSTizenExceptionFactory::postException(context, exception, 
282                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
283     }
284     Catch(WrtDeviceApis::Commons::PlatformException) {
285         LogError("Platform execution");
286         return JSTizenExceptionFactory::postException(context, exception, 
287                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
288     }
289
290         
291     return JSValueMakeUndefined(context);
292 }
293
294 bool JSMessageBody::setHtmlBody(JSContextRef context,
295         JSObjectRef object,
296         JSStringRef propertyName,
297         JSValueRef value,
298         JSValueRef* exception)
299 {
300
301         LogDebug("setHtmlBody");
302
303         Try
304            {
305                   WrtDeviceApis::CommonsJavaScript::Converter converter(context);
306                   Api::Messaging::IMessagePtr msg = getMessage(context, object);
307                   
308                   if (msg->getMessageType() == Api::Messaging::EMAIL)
309                   {
310                         Api::Messaging::IEmailPtr email = Api::Messaging::MessageFactory::convertToEmail(msg);
311                         std::string body = converter.toString(value);
312                         LogDebug("input String : " << body);
313                         email->setHtmlBody(body);
314                         return true;
315                   }
316                   else
317                   {
318                          LogError("html body is for Email only.");
319                          return JSTizenExceptionFactory::postException(context, exception, 
320                                   JSTizenException::NOT_SUPPORTED_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_SUPPORTED);
321                   }
322            }
323             Catch(WrtDeviceApis::Commons::ConversionException) {
324                 LogError("Error on conversion");
325                    return JSTizenExceptionFactory::postException(context, exception, 
326                                   JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
327             }
328             Catch(WrtDeviceApis::Commons::NullPointerException) {
329                 LogError("Error on pointer, null value");
330                 return JSTizenExceptionFactory::postException(context, exception, 
331                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
332             }
333             Catch(WrtDeviceApis::Commons::PlatformException) {
334                 LogError("Platform execution");
335                 return JSTizenExceptionFactory::postException(context, exception, 
336                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
337             }
338
339            return false;
340 }
341
342 Api::Messaging::IMessagePtr JSMessageBody::getMessage(JSContextRef context,
343         JSValueRef value)
344 {
345     WrtDeviceApis::CommonsJavaScript::Converter converter(context);
346     return getMessage(context, converter.toJSObjectRef(value));
347 }
348
349 Api::Messaging::IMessagePtr JSMessageBody::getMessage(JSContextRef context,
350         JSObjectRef object)
351 {
352     // private object of thisObject
353     return getPrivate(object)->getObject();
354 }
355
356 JSMessageBodyPrivate* JSMessageBody::getPrivate(JSObjectRef thisObject)
357 {
358     JSMessageBodyPrivate* thisPrivate =
359         static_cast<JSMessageBodyPrivate*>(JSObjectGetPrivate(thisObject));
360     if (!thisPrivate) {
361         LogError("no private");
362         Throw(WrtDeviceApis::Commons::NullPointerException);
363     }
364     return thisPrivate;
365 }
366
367 }
368 }
369
370