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