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 <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>
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";
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;
55 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
57 JSClassDefinition JSMessageAttachment::m_classInfo = {
59 kJSClassAttributeNone,
62 /*JSFile::getClassRef(), parent*/
71 NULL, //deleteProperty,
72 NULL, //getPropertyNames,
73 NULL, //callAsFunction,
74 constructor, //callAsConstructor,
76 NULL, //convertToType,
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 },
89 const JSClassDefinition* JSMessageAttachment::getClassInfo()
91 return &(m_classInfo);
94 JSClassRef JSMessageAttachment::getClassRef()
97 m_jsClassRef = JSClassCreate(&m_classInfo);
102 void JSMessageAttachment::initialize(JSContextRef context,
106 if (!JSObjectGetPrivate(object)) {
107 LogDebug("Private object not set... setting it.");
108 IAttachmentPtr attachment(new Attachment());
110 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
111 if (!JSObjectSetPrivate(object, priv)) {
117 void JSMessageAttachment::finalize(JSObjectRef object)
120 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
121 JSObjectSetPrivate(object, NULL);
125 JSObjectRef JSMessageAttachment::createJS( JSContextRef context, const Api::Messaging::IAttachmentPtr& attachment)
128 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
129 return JSObjectMake(context, getClassRef(), priv); //make JSObjectRef.
132 JSValueRef JSMessageAttachment::getMIMEType(JSContextRef context,
134 JSStringRef propertyName,
135 JSValueRef* exception)
140 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
142 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
143 LogDebug("mine type :" << attachment->getMimeType());
144 return converter.toJSValueRef(attachment->getMimeType());
146 Catch(WrtDeviceApis::Commons::Exception)
148 LogError("invalid conversion");
150 return JSValueMakeUndefined(context);
153 JSValueRef JSMessageAttachment::getMessageID(JSContextRef context,
155 JSStringRef propertyName,
156 JSValueRef* exception)
161 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
163 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
164 LogDebug("Message ID :" << (attachment->getMessage()));
165 return converter.toJSValueRef(attachment->getMessage()->getId());
167 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
169 LogError("invalid value conversion");
170 return JSTizenExceptionFactory::postException(context, exception,
171 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
173 Catch(WrtDeviceApis::Commons::Exception)
175 LogError("Unkown Exception");
176 return JSTizenExceptionFactory::postException(context, exception,
177 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
179 return JSValueMakeUndefined(context);
183 JSValueRef JSMessageAttachment::getContentLocation(JSContextRef context,
185 JSStringRef propertyName,
186 JSValueRef* exception)
188 return JSValueMakeNull(context);
191 JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
193 JSStringRef propertyName,
194 JSValueRef* exception)
199 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
201 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
202 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
204 if ( attachment->getAttachmentID() < 0)
206 JSValueMakeUndefined(context);
209 return converter.toJSValueRef(attachment->getAttachmentID());
211 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
213 LogError("invalid value conversion");
214 return JSTizenExceptionFactory::postException(context, exception,
215 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
218 Catch(WrtDeviceApis::Commons::Exception)
220 LogError("Unkown Exception");
221 return JSTizenExceptionFactory::postException(context, exception,
222 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
225 return JSValueMakeUndefined(context);
229 JSValueRef JSMessageAttachment::getLoaded(JSContextRef context,
231 JSStringRef propertyName,
232 JSValueRef* exception)
238 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
240 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
241 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
242 return converter.toJSValueRef(attachment->getDownloaded());
244 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
246 LogError("invalid value conversion");
247 return JSTizenExceptionFactory::postException(context, exception,
248 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
250 Catch(WrtDeviceApis::Commons::Exception)
252 LogError("Unkown Exception");
253 return JSTizenExceptionFactory::postException(context, exception,
254 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
256 return JSValueMakeUndefined(context);
259 JSValueRef JSMessageAttachment::getFile(JSContextRef context,
261 JSStringRef propertyName,
262 JSValueRef* exception)
266 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
268 LogDebug("priv is vaild");
269 JSContextRef globalContext = priv ? priv->getContext() : context;
271 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
277 std::string fullPath = attachment->getFullPath();
278 LogDebug("fullPath=" << fullPath);
280 if (fullPath.empty()) {
281 return JSValueMakeNull(context);
285 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
286 std::string virtualpath = AttachmentFactory::getVirtualPathFromRealPath(globalContext, fullPath);
287 return converter.toJSValueRef(virtualpath);
290 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
292 LogError("invalid value conversion");
293 return JSTizenExceptionFactory::postException(context, exception,
294 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
296 Catch(WrtDeviceApis::Commons::Exception)
298 LogError("Unkown Exception");
299 return JSTizenExceptionFactory::postException(context, exception,
300 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
302 return JSValueMakeUndefined(context);
307 return JSTizenExceptionFactory::postException(context, exception,
308 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
311 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
315 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
317 return JSValueMakeUndefined(context);
320 std::string fullPath = attachment->getFullPath();
321 LogDebug("full Path = " << fullPath );
322 if (fullPath.empty()) {
323 return JSValueMakeUndefined(context);
326 int permissions = Api::Filesystem::PERM_READ | Api::Filesystem::PERM_WRITE;
327 EventGetNodeDataPtr data(
328 new EventGetNodeData(permissions, WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr())
330 Api::Filesystem::EventResolvePtr event(new Api::Filesystem::EventResolve(
331 Api::Filesystem::IPath::create(fullPath))
333 event->setPrivateData(
334 DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(data)
336 event->setForSynchronousCall();
337 Api::Filesystem::IManager::getInstance().getNode(event);
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);
347 Api::Filesystem::INodePtr node = event->getResult();
349 LogError("Resolved attachment location is empty.");
350 return JSTizenExceptionFactory::postException(context, exception,
351 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
355 Api::Filesystem::IPathPtr path = node->getPath();
357 LogError(" Path = " << path->getPath() << " Full Path = " << path->getFullPath());
359 JSObjectRef result = WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(context,
360 JSFile::getClassRef(),
365 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
367 LogError("invalid value conversion");
368 return JSTizenExceptionFactory::postException(context, exception,
369 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
372 Catch(WrtDeviceApis::Commons::Exception)
374 LogError("Unkown Exception");
375 return JSTizenExceptionFactory::postException(context, exception,
376 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
378 return JSValueMakeUndefined(context);
380 return JSValueMakeUndefined(context);
384 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
387 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
388 return getAttachment(context, converter.toJSObjectRef(value));
391 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
394 // private object of thisObject
395 return getPrivate(object)->getObject();
398 JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObject)
400 JSMessageAttachmentPrivate* thisPrivate =
401 static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(thisObject));
403 LogError("no private");
404 Throw(WrtDeviceApis::Commons::NullPointerException);
409 JSObjectRef JSMessageAttachment::constructor(JSContextRef context,
410 JSObjectRef constructor,
411 size_t argumentCount,
412 const JSValueRef arguments[],
413 JSValueRef* exception)
417 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(constructor));
418 JSContextRef globalContext = priv ? priv->getContext() : context;
424 LogDebug("argumentCount=" << argumentCount);
425 if ( argumentCount < 1 || argumentCount > 2) {
426 LogError("Wrong argument count");
427 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "Wrong Message argumentCount");
431 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
433 std::string path = converter->toString(arguments[0]); //filepath
434 std::string mimetype;
436 if ( argumentCount == 2)
437 mimetype = converter->toString(arguments[1]); //filepath
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(
447 JSClassRef jsClassRef = JSClassCreate(getClassInfo());
448 LogDebug("jsClassRef success");
449 JSMessageAttachmentPrivate* priv = new JSMessageAttachmentPrivate(globalContext, attachment);
450 LogDebug("priv success");
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");
463 LogDebug("attachment can't create");
464 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "attachment create failed...");
469 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
470 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
473 Catch(WrtDeviceApis::Commons::ConversionException) {
474 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
477 Catch(WrtDeviceApis::Commons::PlatformException) {
478 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
484 LogDebug(" globalContext is NULL ");
485 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);