2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file JSAttachmentArray.cpp
20 * @author Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
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>
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";
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;
58 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
60 JSClassDefinition JSMessageAttachment::m_classInfo = {
62 kJSClassAttributeNone,
65 /*JSFile::getClassRef(), parent*/
74 NULL, //deleteProperty,
75 NULL, //getPropertyNames,
76 NULL, //callAsFunction,
77 constructor, //callAsConstructor,
79 NULL, //convertToType,
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 },
92 const JSClassDefinition* JSMessageAttachment::getClassInfo()
94 return &(m_classInfo);
97 JSClassRef JSMessageAttachment::getClassRef()
100 m_jsClassRef = JSClassCreate(&m_classInfo);
105 void JSMessageAttachment::initialize(JSContextRef context,
109 if (!JSObjectGetPrivate(object)) {
110 LogDebug("Private object not set... setting it.");
111 IAttachmentPtr attachment(new Attachment());
113 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
114 if (!JSObjectSetPrivate(object, priv)) {
120 void JSMessageAttachment::finalize(JSObjectRef object)
123 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
124 JSContextRef gContext = priv->getContext();
126 Api::Messaging::IAttachmentPtr attachment = getAttachment(gContext, object);
130 if (attachment->hasDummyFile())
132 AttachmentFactory::removeDummyFile(attachment->getDummyFilePath());
136 JSObjectSetPrivate(object, NULL);
140 JSObjectRef JSMessageAttachment::createJS( JSContextRef context, const Api::Messaging::IAttachmentPtr& attachment)
143 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
144 return JSObjectMake(context, getClassRef(), priv); //make JSObjectRef.
147 JSValueRef JSMessageAttachment::getMIMEType(JSContextRef context,
149 JSStringRef propertyName,
150 JSValueRef* exception)
155 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
157 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
158 LogDebug("mine type :" << attachment->getMimeType());
159 return converter.toJSValueRef(attachment->getMimeType());
161 Catch(WrtDeviceApis::Commons::Exception)
163 LogError("invalid conversion");
165 return JSValueMakeUndefined(context);
168 JSValueRef JSMessageAttachment::getMessageID(JSContextRef context,
170 JSStringRef propertyName,
171 JSValueRef* exception)
176 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
178 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
179 LogDebug("Message ID :" << (attachment->getMessage()));
180 return converter.toJSValueRef(attachment->getMessage()->getId());
182 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
184 LogError("invalid value conversion");
185 return JSTizenExceptionFactory::postException(context, exception,
186 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
188 Catch(WrtDeviceApis::Commons::Exception)
190 LogError("Unkown Exception");
191 return JSTizenExceptionFactory::postException(context, exception,
192 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
194 return JSValueMakeUndefined(context);
198 JSValueRef JSMessageAttachment::getContentLocation(JSContextRef context,
200 JSStringRef propertyName,
201 JSValueRef* exception)
203 return JSValueMakeNull(context);
206 JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
208 JSStringRef propertyName,
209 JSValueRef* exception)
214 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
216 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
217 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
219 if ( attachment->getAttachmentID() < 0)
221 JSValueMakeUndefined(context);
224 return converter.toJSValueRef(attachment->getAttachmentID());
226 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
228 LogError("invalid value conversion");
229 return JSTizenExceptionFactory::postException(context, exception,
230 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
233 Catch(WrtDeviceApis::Commons::Exception)
235 LogError("Unkown Exception");
236 return JSTizenExceptionFactory::postException(context, exception,
237 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
240 return JSValueMakeUndefined(context);
244 JSValueRef JSMessageAttachment::getLoaded(JSContextRef context,
246 JSStringRef propertyName,
247 JSValueRef* exception)
253 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
255 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
256 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
257 return converter.toJSValueRef(attachment->getDownloaded());
259 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
261 LogError("invalid value conversion");
262 return JSTizenExceptionFactory::postException(context, exception,
263 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
265 Catch(WrtDeviceApis::Commons::Exception)
267 LogError("Unkown Exception");
268 return JSTizenExceptionFactory::postException(context, exception,
269 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
271 return JSValueMakeUndefined(context);
274 JSValueRef JSMessageAttachment::getFile(JSContextRef context,
276 JSStringRef propertyName,
277 JSValueRef* exception)
281 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
283 LogDebug("priv is vaild");
284 JSContextRef globalContext = priv ? priv->getContext() : context;
286 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
292 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
294 std::string fullPath = attachment->getFullPath();
295 LogDebug("fullPath=" << fullPath);
297 if (fullPath.empty()) {
298 return JSValueMakeNull(context);
302 std::string filePath;
304 filePath = Utils::toVirtualPath(globalContext, fullPath);
306 catch (WrtDeviceApis::Commons::ConversionException)
308 LogDebug("It is not in the the list of root locations ");
309 if (attachment->hasDummyFile())
311 LogDebug("Get Dummy File Path " << attachment->getDummyFilePath());
312 filePath = attachment->getDummyFilePath();
316 LogDebug("Create DummyFile");
317 std::string tmpPath = AttachmentFactory::getDummyFileFromRealPath(globalContext, fullPath);
318 LogDebug("tmp Path : " << tmpPath);
319 if (tmpPath.length())
321 attachment->setDummyFilePath(tmpPath);
322 if ( !attachment->hasDummyFile())
324 LogDebug("dummy File Path is invalided But Can't access this file.");
325 return JSValueMakeNull(context);
334 return JSValueMakeNull(context);
341 return converter.toJSValueRef(Utils::toVirtualPath(globalContext, filePath));
346 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
348 LogError("invalid value conversion");
349 return JSTizenExceptionFactory::postException(context, exception,
350 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
352 Catch(WrtDeviceApis::Commons::Exception)
354 LogError("Unkown Exception");
355 return JSTizenExceptionFactory::postException(context, exception,
356 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
358 return JSValueMakeUndefined(context);
363 return JSTizenExceptionFactory::postException(context, exception,
364 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
367 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
371 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
373 return JSValueMakeUndefined(context);
376 std::string fullPath = attachment->getFullPath();
377 LogDebug("full Path = " << fullPath );
378 if (fullPath.empty()) {
379 return JSValueMakeUndefined(context);
382 int permissions = Api::Filesystem::PERM_READ | Api::Filesystem::PERM_WRITE;
383 EventGetNodeDataPtr data(
384 new EventGetNodeData(permissions, WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr())
386 Api::Filesystem::EventResolvePtr event(new Api::Filesystem::EventResolve(
387 Api::Filesystem::IPath::create(fullPath))
389 event->setPrivateData(
390 DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(data)
392 event->setForSynchronousCall();
393 Api::Filesystem::IManager::getInstance().getNode(event);
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);
403 Api::Filesystem::INodePtr node = event->getResult();
405 LogError("Resolved attachment location is empty.");
406 return JSTizenExceptionFactory::postException(context, exception,
407 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
411 Api::Filesystem::IPathPtr path = node->getPath();
413 LogError(" Path = " << path->getPath() << " Full Path = " << path->getFullPath());
415 JSObjectRef result = WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(context,
416 JSFile::getClassRef(),
421 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
423 LogError("invalid value conversion");
424 return JSTizenExceptionFactory::postException(context, exception,
425 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
428 Catch(WrtDeviceApis::Commons::Exception)
430 LogError("Unkown Exception");
431 return JSTizenExceptionFactory::postException(context, exception,
432 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
434 return JSValueMakeUndefined(context);
436 return JSValueMakeUndefined(context);
440 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
443 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
444 return getAttachment(context, converter.toJSObjectRef(value));
447 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
450 // private object of thisObject
451 return getPrivate(object)->getObject();
454 JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObject)
456 JSMessageAttachmentPrivate* thisPrivate =
457 static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(thisObject));
459 LogError("no private");
460 Throw(WrtDeviceApis::Commons::NullPointerException);
465 JSObjectRef JSMessageAttachment::constructor(JSContextRef context,
466 JSObjectRef constructor,
467 size_t argumentCount,
468 const JSValueRef arguments[],
469 JSValueRef* exception)
473 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(constructor));
474 JSContextRef globalContext = priv ? priv->getContext() : context;
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);
488 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
490 std::string path = converter->toString(arguments[0]); //filepath
491 std::string mimetype;
493 if ( argumentCount == 2)
494 mimetype = converter->toString(arguments[1]); //filepath
496 Api::Filesystem::IPathPtr src = Utils::fromVirtualPath(globalContext, path);
499 path = src->getFullPath();
501 path = ""; //set empty string.
503 LogDebug("real path = " << path);
504 Api::Messaging::IAttachmentPtr attachment = AttachmentFactory::createAttachment(
509 JSClassRef jsClassRef = JSClassCreate(getClassInfo());
510 LogDebug("jsClassRef success");
511 JSMessageAttachmentPrivate* priv = new JSMessageAttachmentPrivate(globalContext, attachment);
512 LogDebug("priv success");
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");
525 LogDebug("attachment can't create");
526 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "attachment create failed...");
531 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
532 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
535 Catch(WrtDeviceApis::Commons::ConversionException) {
536 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
539 Catch(WrtDeviceApis::Commons::PlatformException) {
540 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
543 Catch(WrtDeviceApis::Commons::NotFoundException) {
544 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_FOUND_ERROR, JSMESSAGING_EXCEPTION_MSG_NOT_FOUND);
550 LogDebug(" globalContext is NULL ");
551 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);