Beta merge 2
[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 #include <API/Messaging/AttachmentFactory.h>
35 #include "MessagingErrorMsg.h"
36 #include <Messaging/Attachment.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 using namespace TizenApis::Platform::Messaging;
54
55 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
56
57 JSClassDefinition JSMessageAttachment::m_classInfo = {
58     0,
59     kJSClassAttributeNone,
60     "MessageAttachment",
61     /*NULL,*/
62     /*JSFile::getClassRef(), parent*/
63     NULL,
64     m_property,
65     NULL,
66     initialize,
67     finalize,
68     NULL, //hasProperty,
69     NULL,   //getproperty
70     NULL,  //setProperty
71     NULL, //deleteProperty,
72     NULL, //getPropertyNames,
73     NULL, //callAsFunction,
74     constructor, //callAsConstructor,
75     NULL, //hasInstance,
76     NULL, //convertToType,
77 };
78
79 JSStaticValue JSMessageAttachment::m_property[] = {
80     //{ "MIMEType", JSMessageAttachment::getMIMEType, NULL, kJSPropertyAttributeReadOnly },
81     { "id", JSMessageAttachment::getContentID, NULL, kJSPropertyAttributeNone },
82     { "messageId", JSMessageAttachment::getMessageID, NULL, kJSPropertyAttributeReadOnly },
83     //{ "contentLocation", JSMessageAttachment::getContentLocation, NULL, kJSPropertyAttributeNone },
84     { "mimeType", JSMessageAttachment::getMIMEType, NULL, kJSPropertyAttributeReadOnly },
85     { "loaded", JSMessageAttachment::getLoaded, NULL, kJSPropertyAttributeReadOnly },
86     { "filePath", JSMessageAttachment::getFile, NULL, kJSPropertyAttributeReadOnly },
87     { 0, 0, 0, 0 }
88 };
89 const JSClassDefinition* JSMessageAttachment::getClassInfo()
90 {
91     return &(m_classInfo);
92 }
93
94 JSClassRef JSMessageAttachment::getClassRef()
95 {
96     if (!m_jsClassRef) {
97         m_jsClassRef = JSClassCreate(&m_classInfo);
98     }
99     return m_jsClassRef;
100 }
101
102 void JSMessageAttachment::initialize(JSContextRef context,
103         JSObjectRef object)
104 {
105         LogDebug("enter");
106         if (!JSObjectGetPrivate(object)) {
107                 LogDebug("Private object not set... setting it.");
108                 IAttachmentPtr attachment(new Attachment());
109                                                 
110                 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
111                 if (!JSObjectSetPrivate(object, priv)) {
112                         delete priv;
113                 }
114         }
115 }
116
117 void JSMessageAttachment::finalize(JSObjectRef object)
118 {
119     LogDebug("enter");
120     JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
121     JSObjectSetPrivate(object, NULL);
122     delete priv;
123 }
124
125 JSObjectRef JSMessageAttachment::createJS( JSContextRef context, const Api::Messaging::IAttachmentPtr& attachment)
126 {
127
128     JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
129     return JSObjectMake(context, getClassRef(), priv);  //make JSObjectRef.
130 }
131
132 JSValueRef JSMessageAttachment::getMIMEType(JSContextRef context,
133            JSObjectRef object,
134            JSStringRef propertyName,
135            JSValueRef* exception)
136 {
137     LogDebug("enter");
138     Try
139     {
140           Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
141           
142         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
143            LogDebug("mine type :" << attachment->getMimeType());
144         return converter.toJSValueRef(attachment->getMimeType());
145     }
146     Catch(WrtDeviceApis::Commons::Exception)
147     {
148         LogError("invalid conversion");
149     }
150     return JSValueMakeUndefined(context);
151 }
152
153 JSValueRef JSMessageAttachment::getMessageID(JSContextRef context,
154            JSObjectRef object,
155            JSStringRef propertyName,
156            JSValueRef* exception)
157 {
158     LogDebug("enter");
159     Try
160     {
161           Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
162           
163           WrtDeviceApis::CommonsJavaScript::Converter converter(context);
164           LogDebug("Message ID :" << (attachment->getMessage()));
165           return converter.toJSValueRef(attachment->getMessage()->getId());
166     }
167     Catch(WrtDeviceApis::Commons::InvalidArgumentException)
168     {
169            LogError("invalid value conversion");
170            return JSTizenExceptionFactory::postException(context, exception, 
171                    JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
172     }
173     Catch(WrtDeviceApis::Commons::Exception)
174     {
175         LogError("Unkown Exception");
176         return JSTizenExceptionFactory::postException(context, exception, 
177                                                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
178     }
179     return JSValueMakeUndefined(context);
180 }
181
182
183 JSValueRef JSMessageAttachment::getContentLocation(JSContextRef context,
184            JSObjectRef object,
185            JSStringRef propertyName,
186            JSValueRef* exception)
187 {
188         return JSValueMakeNull(context);
189 }
190
191 JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
192            JSObjectRef object,
193            JSStringRef propertyName,
194            JSValueRef* exception)
195 {
196             LogDebug("enter");
197             Try
198             {
199                   Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
200                   
201                   WrtDeviceApis::CommonsJavaScript::Converter converter(context);
202                   LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
203
204                   if ( attachment->getAttachmentID() < 0)
205                   {
206                          JSValueMakeUndefined(context);
207                   }
208                   
209                   return converter.toJSValueRef(attachment->getAttachmentID());
210             }
211             Catch(WrtDeviceApis::Commons::InvalidArgumentException)
212             {
213                    LogError("invalid value conversion");
214                    return JSTizenExceptionFactory::postException(context, exception, 
215                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
216
217             }
218             Catch(WrtDeviceApis::Commons::Exception)
219             {
220                 LogError("Unkown Exception");
221                    return JSTizenExceptionFactory::postException(context, exception, 
222                            JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
223             }
224                 
225             return JSValueMakeUndefined(context);
226
227 }
228
229 JSValueRef JSMessageAttachment::getLoaded(JSContextRef context,
230            JSObjectRef object,
231            JSStringRef propertyName,
232            JSValueRef* exception)
233 {
234         
235           LogDebug("enter");
236             Try
237             {
238                   Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
239                   
240         WrtDeviceApis::CommonsJavaScript::Converter converter(context);
241                   LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
242                   return converter.toJSValueRef(attachment->getDownloaded());
243             }
244             Catch(WrtDeviceApis::Commons::InvalidArgumentException)
245             {
246                    LogError("invalid value conversion");
247                    return JSTizenExceptionFactory::postException(context, exception, 
248                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
249             }
250             Catch(WrtDeviceApis::Commons::Exception)
251             {
252                 LogError("Unkown Exception");
253                    return JSTizenExceptionFactory::postException(context, exception, 
254                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
255             }
256             return JSValueMakeUndefined(context);
257 }
258
259 JSValueRef JSMessageAttachment::getFile(JSContextRef context,
260            JSObjectRef object,
261            JSStringRef propertyName,
262            JSValueRef* exception)
263 {
264         LogDebug("enter");
265
266         JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
267         if (priv)
268                 LogDebug("priv is vaild");
269         JSContextRef globalContext = priv ? priv->getContext() : context;
270         
271         Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
272         
273         if ( attachment )
274         {
275                 Try
276                 {
277                         std::string fullPath = attachment->getFullPath();
278                         LogDebug("fullPath=" << fullPath);
279
280                         if (fullPath.empty()) {
281                                 return JSValueMakeNull(context);
282                         }
283                         else
284                         {       
285                                 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
286                                 std::string virtualpath = AttachmentFactory::getVirtualPathFromRealPath(globalContext, fullPath);       
287                                 return converter.toJSValueRef(virtualpath);
288                         }
289                 }
290                 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
291                 {
292                    LogError("invalid value conversion");
293                    return JSTizenExceptionFactory::postException(context, exception, 
294                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
295                 }
296                 Catch(WrtDeviceApis::Commons::Exception)
297                 {
298                     LogError("Unkown Exception");
299                     return JSTizenExceptionFactory::postException(context, exception, 
300                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
301                 }
302                 return JSValueMakeUndefined(context);
303                 
304         }
305         else
306         {
307                 return JSTizenExceptionFactory::postException(context, exception, 
308                         JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
309         }
310         
311 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
312                 LogDebug("enter");
313                 Try
314                 {
315                   Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
316                    if (!attachment) {
317                         return JSValueMakeUndefined(context);
318                     }
319
320                     std::string fullPath = attachment->getFullPath();
321                     LogDebug("full Path = " << fullPath );      
322                     if (fullPath.empty()) {
323                         return JSValueMakeUndefined(context);
324                     }
325
326                     int permissions = Api::Filesystem::PERM_READ | Api::Filesystem::PERM_WRITE;
327                     EventGetNodeDataPtr data(
328                         new EventGetNodeData(permissions, WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr())
329                         );
330                     Api::Filesystem::EventResolvePtr event(new Api::Filesystem::EventResolve(
331                                                           Api::Filesystem::IPath::create(fullPath))
332                                                       );
333                     event->setPrivateData(
334                         DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(data)
335                         );
336                     event->setForSynchronousCall();
337                     Api::Filesystem::IManager::getInstance().getNode(event);
338
339                     WrtDeviceApis::Commons::ExceptionCodes::Enumeration code = event->getExceptionCode();
340                     if (code != WrtDeviceApis::Commons::ExceptionCodes::None) {
341                         LogError("Attachment location not resolved. Exception code: " << code);
342                            return JSTizenExceptionFactory::postException(context, exception, 
343                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
344
345                     }
346
347                     Api::Filesystem::INodePtr node = event->getResult();
348                     if (!node) {
349                         LogError("Resolved attachment location is empty.");
350                            return JSTizenExceptionFactory::postException(context, exception, 
351                                          JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
352
353                     }
354
355                         Api::Filesystem::IPathPtr path = node->getPath();
356
357                         LogError(" Path = " << path->getPath()  << " Full Path = " <<  path->getFullPath());
358                         
359                     JSObjectRef result = WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(context,
360                                                                       JSFile::getClassRef(),
361                                                                       node);
362                     return result;
363                 
364                 }
365                 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
366                 {
367                    LogError("invalid value conversion");
368                    return JSTizenExceptionFactory::postException(context, exception, 
369                            JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
370
371                 }
372                 Catch(WrtDeviceApis::Commons::Exception)
373                 {
374                     LogError("Unkown Exception");
375                     return JSTizenExceptionFactory::postException(context, exception, 
376                                 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
377                 }
378                 return JSValueMakeUndefined(context);
379 #endif
380 return JSValueMakeUndefined(context);
381
382 }
383
384 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
385         JSValueRef value)
386 {
387     WrtDeviceApis::CommonsJavaScript::Converter converter(context);
388     return getAttachment(context, converter.toJSObjectRef(value));
389 }
390
391 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
392         JSObjectRef object)
393 {
394     // private object of thisObject
395     return getPrivate(object)->getObject();
396 }
397
398 JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObject)
399 {
400     JSMessageAttachmentPrivate* thisPrivate =
401         static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(thisObject));
402     if (!thisPrivate) {
403         LogError("no private");
404                Throw(WrtDeviceApis::Commons::NullPointerException);
405     }
406     return thisPrivate;
407 }
408
409 JSObjectRef JSMessageAttachment::constructor(JSContextRef context, 
410                 JSObjectRef constructor, 
411                 size_t argumentCount, 
412                 const JSValueRef arguments[], 
413                 JSValueRef* exception)
414 {
415         LogDebug("entered");
416
417         JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(constructor));
418         JSContextRef globalContext = priv ? priv->getContext() : context;
419         
420         if (globalContext)
421         {
422                 try
423                 {
424                         LogDebug("argumentCount=" << argumentCount);
425                         if ( argumentCount < 1 || argumentCount > 2) {
426                                 LogError("Wrong argument count");
427                                 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Wrong Message argumentCount");
428                         }
429                         else
430                         {       
431                                 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
432                                 
433                                 std::string path = converter->toString(arguments[0]);   //filepath
434                                 std::string mimetype;
435                                 
436                                 if ( argumentCount == 2)
437                                         mimetype = converter->toString(arguments[1]);   //filepath
438
439                                 //IPathPtr src = Utils::fromVirtualPath(globalContext, path);
440                                 path = AttachmentFactory::getRealPathFromVirtualPath(globalContext,path);
441                                 LogDebug("real path = " << path);
442                                 Api::Messaging::IAttachmentPtr attachment = AttachmentFactory::createAttachment(
443                                                                                     path,
444                                                                                     mimetype);
445                                 if (attachment)
446                                 {
447                                         JSClassRef jsClassRef = JSClassCreate(getClassInfo());
448                                         LogDebug("jsClassRef success");
449                                         JSMessageAttachmentPrivate* priv = new JSMessageAttachmentPrivate(globalContext, attachment);
450                                         LogDebug("priv success");
451                                         
452                                         JSObjectRef jsObjRef = JSObjectMake(globalContext, jsClassRef, static_cast<void*>(priv));
453                                         LogDebug("JSObjectMake success");
454                                         JSClassRelease(jsClassRef);
455                                         if (NULL == jsObjRef) {
456                                                 LogError("object creation error");
457                                                 ThrowMsg(WrtDeviceApis::Commons::PlatformException, "JSObject make error"); 
458                                         }
459                                         return jsObjRef;
460                                 }
461                                 else
462                                 {
463                                         LogDebug("attachment can't create");
464                                         ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "attachment create failed..."); 
465                                 }
466                         }
467
468                 }       
469                 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
470                         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
471                         return NULL;
472                 }
473                 Catch(WrtDeviceApis::Commons::ConversionException) {
474                         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
475                         return NULL;
476                 }
477                 Catch(WrtDeviceApis::Commons::PlatformException) {
478                         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);     
479                         return NULL;
480                 }
481         }
482         else
483         {
484                 LogDebug(" globalContext is NULL ");
485                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);     
486                 return NULL;
487         }
488
489         return NULL;
490 }
491
492
493 }
494 }
495