BETA merge
[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/log.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <CommonsJavaScript/JSUtils.h>
27 #include "ConverterMessage.h"
28 #include "JSMessageAttachment.h"
29 #include <Tizen/Filesystem/JSFile.h>
30 #include <Tizen/Filesystem/EventGetNodeData.h>
31 #include <API/Filesystem/IManager.h>
32 #include <Tizen/Common/JSTizenExceptionFactory.h>
33 #include <Tizen/Common/JSTizenException.h>
34
35 #include "MessagingErrorMsg.h"
36
37 namespace {
38 const char* MSG_ATTACHMENT_MIMETYPE             = "MIMEType";
39 const char* MSG_ATTACHMENT_MESSAGEID            = "messageId";
40 const char* MSG_ATTACHMENT_CONTENTLOCATION      = "contentLocation";
41 const char* MSG_ATTACHMENT_CONTENTID    = "contentId";
42 const char* MSG_ATTACHMENT_LOADED               = "loaded";
43 }
44
45 namespace TizenApis {
46 namespace Tizen1_0 {
47
48 using namespace TizenApis::Commons;
49 using namespace TizenApis::Api::Messaging;
50 using namespace WrtDeviceApis::Commons;
51 using namespace WrtDeviceApis::CommonsJavaScript;
52
53 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
54
55 JSClassDefinition JSMessageAttachment::m_classInfo = {
56     0,
57     kJSClassAttributeNone,
58     "MessageAttachment",
59     /*NULL,*/
60     /*JSFile::getClassRef(), parent*/
61     NULL,
62     m_property,
63     NULL,
64     initialize,
65     finalize,
66     NULL, //hasProperty,
67     NULL,   //getproperty
68     NULL,  //setProperty
69     NULL, //deleteProperty,
70     NULL, //getPropertyNames,
71     NULL, //callAsFunction,
72     NULL, //callAsConstructor,
73     NULL, //hasInstance,
74     NULL, //convertToType,
75 };
76
77 JSStaticValue JSMessageAttachment::m_property[] = {
78     //{ "MIMEType", JSMessageAttachment::getMIMEType, NULL, kJSPropertyAttributeReadOnly },
79     { "messageId", JSMessageAttachment::getMessageID, NULL, kJSPropertyAttributeReadOnly },
80     //{ "contentLocation", JSMessageAttachment::getContentLocation, NULL, kJSPropertyAttributeNone },
81     { "contentId", JSMessageAttachment::getContentID, NULL, kJSPropertyAttributeNone },
82     { "loaded", JSMessageAttachment::getLoaded, NULL, kJSPropertyAttributeReadOnly },
83     { "file", JSMessageAttachment::getFile, NULL, kJSPropertyAttributeReadOnly },
84     { 0, 0, 0, 0 }
85 };
86 const JSClassDefinition* JSMessageAttachment::getClassInfo()
87 {
88     return &(m_classInfo);
89 }
90
91 JSClassRef JSMessageAttachment::getClassRef()
92 {
93     if (!m_jsClassRef) {
94         m_jsClassRef = JSClassCreate(&m_classInfo);
95     }
96     return m_jsClassRef;
97 }
98
99 void JSMessageAttachment::initialize(JSContextRef context,
100         JSObjectRef object)
101 {
102     LogDebug("enter");
103 }
104
105 void JSMessageAttachment::finalize(JSObjectRef object)
106 {
107     LogDebug("enter");
108     JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
109     JSObjectSetPrivate(object, NULL);
110     delete priv;
111 }
112
113 JSObjectRef JSMessageAttachment::createJS( JSContextRef context, const Api::Messaging::IAttachmentPtr& attachment)
114 {
115
116     JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
117     return JSObjectMake(context, getClassRef(), priv);  //make JSObjectRef.
118 }
119
120 JSValueRef JSMessageAttachment::getMIMEType(JSContextRef context,
121            JSObjectRef object,
122            JSStringRef propertyName,
123            JSValueRef* exception)
124 {
125     LogDebug("enter");
126     Try
127     {
128           Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
129           
130         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
131            LogDebug("mine type :" << attachment->getMimeType());
132         return converter.toJSValueRef(attachment->getMimeType());
133     }
134     Catch(WrtDeviceApis::Commons::Exception)
135     {
136         LogError("invalid conversion");
137     }
138     return JSValueMakeUndefined(context);
139 }
140
141 JSValueRef JSMessageAttachment::getMessageID(JSContextRef context,
142            JSObjectRef object,
143            JSStringRef propertyName,
144            JSValueRef* exception)
145 {
146     LogDebug("enter");
147     Try
148     {
149           Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
150           
151           WrtDeviceApis::CommonsJavaScript::Converter converter(context);
152           LogDebug("Message ID :" << (attachment->getMessage()));
153           return converter.toJSValueRef(attachment->getMessage()->getId());
154     }
155     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
156     {
157            LogError("invalid value conversion");
158            return JSTizenExceptionFactory::postException(context, exception, 
159                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
160     }
161     Catch(WrtDeviceApis::Commons::Exception)
162     {
163         LogError("Unkown Exception");
164         return JSTizenExceptionFactory::postException(context, exception, 
165                                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
166     }
167     return JSValueMakeUndefined(context);
168 }
169
170
171 JSValueRef JSMessageAttachment::getContentLocation(JSContextRef context,
172            JSObjectRef object,
173            JSStringRef propertyName,
174            JSValueRef* exception)
175 {
176         return JSValueMakeNull(context);
177 }
178
179 JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
180            JSObjectRef object,
181            JSStringRef propertyName,
182            JSValueRef* exception)
183 {
184             LogDebug("enter");
185             Try
186             {
187                   Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
188                   
189                   WrtDeviceApis::CommonsJavaScript::Converter converter(context);
190                   LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
191                   return converter.toJSValueRef(attachment->getAttachmentID());
192             }
193             Catch(WrtDeviceApis::Commons::InvalidArgumentException)
194             {
195                    LogError("invalid value conversion");
196                    return JSTizenExceptionFactory::postException(context, exception, 
197                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
198
199             }
200             Catch(WrtDeviceApis::Commons::Exception)
201             {
202                 LogError("Unkown Exception");
203                    return JSTizenExceptionFactory::postException(context, exception, 
204                            JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
205             }
206                 
207             return JSValueMakeUndefined(context);
208
209 }
210
211 JSValueRef JSMessageAttachment::getLoaded(JSContextRef context,
212            JSObjectRef object,
213            JSStringRef propertyName,
214            JSValueRef* exception)
215 {
216         
217           LogDebug("enter");
218             Try
219             {
220                   Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
221                   
222         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
223                   LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
224                   return converter.toJSValueRef(attachment->getDownloaded());
225             }
226             Catch(WrtDeviceApis::Commons::InvalidArgumentException)
227             {
228                    LogError("invalid value conversion");
229                    return JSTizenExceptionFactory::postException(context, exception, 
230                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
231             }
232             Catch(WrtDeviceApis::Commons::Exception)
233             {
234                 LogError("Unkown Exception");
235                    return JSTizenExceptionFactory::postException(context, exception, 
236                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
237             }
238             return JSValueMakeUndefined(context);
239 }
240
241 JSValueRef JSMessageAttachment::getFile(JSContextRef context,
242            JSObjectRef object,
243            JSStringRef propertyName,
244            JSValueRef* exception)
245 {
246 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
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 #endif
315 return JSValueMakeUndefined(context);
316
317 }
318
319 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
320         JSValueRef value)
321 {
322     WrtDeviceApis::CommonsJavaScript::Converter converter(context);
323     return getAttachment(context, converter.toJSObjectRef(value));
324 }
325
326 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
327         JSObjectRef object)
328 {
329     // private object of thisObject
330     return getPrivate(object)->getObject();
331 }
332
333 JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObject)
334 {
335     JSMessageAttachmentPrivate* thisPrivate =
336         static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(thisObject));
337     if (!thisPrivate) {
338         LogError("no private");
339                Throw(WrtDeviceApis::Commons::NullPointerException);
340     }
341     return thisPrivate;
342 }
343
344 }
345 }
346
347