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