Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Messaging / JSMessageAttachment.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.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <CommonsJavaScript/JSUtils.h>
27 #include "ConverterMessage.h"
28 #include "JSMessageAttachment.h"
29 #include "JSAttachmentArray.h"          //attachment array
30 #include <Tizen/Filesystem/JSFile.h>
31 #include <Tizen/Filesystem/EventGetNodeData.h>
32 #include <API/Filesystem/IManager.h>
33 #include <Tizen/Common/JSTizenExceptionFactory.h>
34 #include <Tizen/Common/JSTizenException.h>
35
36 #include "MessagingErrorMsg.h"
37
38 namespace {
39 const char* MSG_ATTACHMENT_MIMETYPE             = "MIMEType";
40 const char* MSG_ATTACHMENT_MESSAGEID            = "messageId";
41 const char* MSG_ATTACHMENT_CONTENTLOCATION      = "contentLocation";
42 const char* MSG_ATTACHMENT_CONTENTID    = "contentId";
43 const char* MSG_ATTACHMENT_LOADED               = "loaded";
44 }
45
46 namespace TizenApis {
47 namespace Tizen1_0 {
48
49 using namespace TizenApis::Commons;
50 using namespace TizenApis::Api::Messaging;
51 using namespace WrtDeviceApis::Commons;
52 using namespace WrtDeviceApis::CommonsJavaScript;
53
54 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
55
56 JSClassDefinition JSMessageAttachment::m_classInfo = {
57     0,
58     kJSClassAttributeNone,
59     "MessageAttachment",
60     /*NULL,*/
61     /*JSFile::getClassRef(), parent*/
62     NULL,
63     m_property,
64     NULL,
65     initialize,
66     finalize,
67     NULL, //hasProperty,
68     NULL,   //getproperty
69     NULL,  //setProperty
70     NULL, //deleteProperty,
71     NULL, //getPropertyNames,
72     NULL, //callAsFunction,
73     NULL, //callAsConstructor,
74     NULL, //hasInstance,
75     NULL, //convertToType,
76 };
77
78 JSStaticValue JSMessageAttachment::m_property[] = {
79     //{ "MIMEType", JSMessageAttachment::getMIMEType, NULL, kJSPropertyAttributeReadOnly },
80     { "messageId", JSMessageAttachment::getMessageID, NULL, kJSPropertyAttributeReadOnly },
81     //{ "contentLocation", JSMessageAttachment::getContentLocation, NULL, kJSPropertyAttributeNone },
82     { "contentId", JSMessageAttachment::getContentID, NULL, kJSPropertyAttributeNone },
83     { "loaded", JSMessageAttachment::getLoaded, NULL, kJSPropertyAttributeReadOnly },
84     { "file", JSMessageAttachment::getFile, NULL, kJSPropertyAttributeReadOnly },
85     { 0, 0, 0, 0 }
86 };
87 const JSClassDefinition* JSMessageAttachment::getClassInfo()
88 {
89     return &(m_classInfo);
90 }
91
92 JSClassRef JSMessageAttachment::getClassRef()
93 {
94     if (!m_jsClassRef) {
95         m_jsClassRef = JSClassCreate(&m_classInfo);
96     }
97     return m_jsClassRef;
98 }
99
100 void JSMessageAttachment::initialize(JSContextRef context,
101         JSObjectRef object)
102 {
103     LogDebug("enter");
104 }
105
106 void JSMessageAttachment::finalize(JSObjectRef object)
107 {
108     LogDebug("enter");
109     JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
110     JSObjectSetPrivate(object, NULL);
111     delete priv;
112 }
113
114 JSObjectRef JSMessageAttachment::createJS( JSContextRef context, const Api::Messaging::IAttachmentPtr& attachment)
115 {
116
117     JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
118     return JSObjectMake(context, getClassRef(), priv);  //make JSObjectRef.
119 }
120
121 JSValueRef JSMessageAttachment::getMIMEType(JSContextRef context,
122            JSObjectRef object,
123            JSStringRef propertyName,
124            JSValueRef* exception)
125 {
126     LogDebug("enter");
127     Try
128     {
129           Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
130           
131         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
132            LogDebug("mine type :" << attachment->getMimeType());
133         return converter.toJSValueRef(attachment->getMimeType());
134     }
135     Catch(WrtDeviceApis::Commons::Exception)
136     {
137         LogError("invalid conversion");
138     }
139     return JSValueMakeUndefined(context);
140 }
141
142 JSValueRef JSMessageAttachment::getMessageID(JSContextRef context,
143            JSObjectRef object,
144            JSStringRef propertyName,
145            JSValueRef* exception)
146 {
147     LogDebug("enter");
148     Try
149     {
150           Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
151           
152           WrtDeviceApis::CommonsJavaScript::Converter converter(context);
153           LogDebug("Message ID :" << (attachment->getMessage()));
154           return converter.toJSValueRef(attachment->getMessage()->getId());
155     }
156     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
157     {
158            LogError("invalid value conversion");
159            return JSTizenExceptionFactory::postException(context, exception, 
160                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
161     }
162     Catch(WrtDeviceApis::Commons::Exception)
163     {
164         LogError("Unkown Exception");
165         return JSTizenExceptionFactory::postException(context, exception, 
166                                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
167     }
168     return JSValueMakeUndefined(context);
169 }
170
171
172 JSValueRef JSMessageAttachment::getContentLocation(JSContextRef context,
173            JSObjectRef object,
174            JSStringRef propertyName,
175            JSValueRef* exception)
176 {
177         return JSValueMakeNull(context);
178 }
179
180 JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
181            JSObjectRef object,
182            JSStringRef propertyName,
183            JSValueRef* exception)
184 {
185             LogDebug("enter");
186             Try
187             {
188                   Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
189                   
190                   WrtDeviceApis::CommonsJavaScript::Converter converter(context);
191                   LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
192                   return converter.toJSValueRef(attachment->getAttachmentID());
193             }
194             Catch(WrtDeviceApis::Commons::InvalidArgumentException)
195             {
196                    LogError("invalid value conversion");
197                    return JSTizenExceptionFactory::postException(context, exception, 
198                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
199
200             }
201             Catch(WrtDeviceApis::Commons::Exception)
202             {
203                 LogError("Unkown Exception");
204                    return JSTizenExceptionFactory::postException(context, exception, 
205                            JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
206             }
207                 
208             return JSValueMakeUndefined(context);
209
210 }
211
212 JSValueRef JSMessageAttachment::getLoaded(JSContextRef context,
213            JSObjectRef object,
214            JSStringRef propertyName,
215            JSValueRef* exception)
216 {
217         
218           LogDebug("enter");
219             Try
220             {
221                   Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
222                   
223         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
224                   LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
225                   return converter.toJSValueRef(attachment->getDownloaded());
226             }
227             Catch(WrtDeviceApis::Commons::InvalidArgumentException)
228             {
229                    LogError("invalid value conversion");
230                    return JSTizenExceptionFactory::postException(context, exception, 
231                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
232             }
233             Catch(WrtDeviceApis::Commons::Exception)
234             {
235                 LogError("Unkown Exception");
236                    return JSTizenExceptionFactory::postException(context, exception, 
237                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
238             }
239             return JSValueMakeUndefined(context);
240 }
241
242 JSValueRef JSMessageAttachment::getFile(JSContextRef context,
243            JSObjectRef object,
244            JSStringRef propertyName,
245            JSValueRef* exception)
246 {
247                 LogDebug("enter");
248                 Try
249                 {
250                   Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
251                    if (!attachment) {
252                         return JSValueMakeUndefined(context);
253                     }
254
255                     std::string fullPath = attachment->getFullPath();
256                     LogDebug("full Path = " << fullPath );      
257                     if (fullPath.empty()) {
258                         return JSValueMakeUndefined(context);
259                     }
260
261                     int permissions = Api::Filesystem::PERM_READ | Api::Filesystem::PERM_WRITE;
262                     EventGetNodeDataPtr data(
263                         new EventGetNodeData(permissions, WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr())
264                         );
265                     Api::Filesystem::EventResolvePtr event(new Api::Filesystem::EventResolve(
266                                                           Api::Filesystem::IPath::create(fullPath))
267                                                       );
268                     event->setPrivateData(
269                         DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(data)
270                         );
271                     event->setForSynchronousCall();
272                     Api::Filesystem::IManager::getInstance().getNode(event);
273
274                     WrtDeviceApis::Commons::ExceptionCodes::Enumeration code = event->getExceptionCode();
275                     if (code != WrtDeviceApis::Commons::ExceptionCodes::None) {
276                         LogError("Attachment location not resolved. Exception code: " << code);
277                            return JSTizenExceptionFactory::postException(context, exception, 
278                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
279
280                     }
281
282                     Api::Filesystem::INodePtr node = event->getResult();
283                     if (!node) {
284                         LogError("Resolved attachment location is empty.");
285                            return JSTizenExceptionFactory::postException(context, exception, 
286                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
287
288                     }
289
290                         Api::Filesystem::IPathPtr path = node->getPath();
291
292                         LogError(" Path = " << path->getPath()  << " Full Path = " <<  path->getFullPath());
293                         
294                     JSObjectRef result = WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(context,
295                                                                       JSFile::getClassRef(),
296                                                                       node);
297                     return result;
298                 
299                 }
300                 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
301                 {
302                    LogError("invalid value conversion");
303                    return JSTizenExceptionFactory::postException(context, exception, 
304                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
305
306                 }
307                 Catch(WrtDeviceApis::Commons::Exception)
308                 {
309                     LogError("Unkown Exception");
310                     return JSTizenExceptionFactory::postException(context, exception, 
311                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
312                 }
313                 return JSValueMakeUndefined(context);
314                 
315 }
316
317 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
318         JSValueRef value)
319 {
320     WrtDeviceApis::CommonsJavaScript::Converter converter(context);
321     return getAttachment(context, converter.toJSObjectRef(value));
322 }
323
324 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
325         JSObjectRef object)
326 {
327     // private object of thisObject
328     return getPrivate(object)->getObject();
329 }
330
331 JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObject)
332 {
333     JSMessageAttachmentPrivate* thisPrivate =
334         static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(thisObject));
335     if (!thisPrivate) {
336         LogError("no private");
337                Throw(WrtDeviceApis::Commons::NullPointerException);
338     }
339     return thisPrivate;
340 }
341
342 }
343 }
344
345