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>
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";
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;
57 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
59 JSClassDefinition JSMessageAttachment::m_classInfo = {
61 kJSClassAttributeNone,
64 /*JSFile::getClassRef(), parent*/
73 NULL, //deleteProperty,
74 NULL, //getPropertyNames,
75 NULL, //callAsFunction,
76 constructor, //callAsConstructor,
78 NULL, //convertToType,
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 },
91 const JSClassDefinition* JSMessageAttachment::getClassInfo()
93 return &(m_classInfo);
96 JSClassRef JSMessageAttachment::getClassRef()
99 m_jsClassRef = JSClassCreate(&m_classInfo);
104 void JSMessageAttachment::initialize(JSContextRef context,
108 if (!JSObjectGetPrivate(object)) {
109 LogDebug("Private object not set... setting it.");
110 IAttachmentPtr attachment(new Attachment());
112 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
113 if (!JSObjectSetPrivate(object, priv)) {
119 void JSMessageAttachment::finalize(JSObjectRef object)
122 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
123 JSObjectSetPrivate(object, NULL);
127 JSObjectRef JSMessageAttachment::createJS( JSContextRef context, const Api::Messaging::IAttachmentPtr& attachment)
130 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
131 return JSObjectMake(context, getClassRef(), priv); //make JSObjectRef.
134 JSValueRef JSMessageAttachment::getMIMEType(JSContextRef context,
136 JSStringRef propertyName,
137 JSValueRef* exception)
142 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
144 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
145 LogDebug("mine type :" << attachment->getMimeType());
146 return converter.toJSValueRef(attachment->getMimeType());
148 Catch(WrtDeviceApis::Commons::Exception)
150 LogError("invalid conversion");
152 return JSValueMakeUndefined(context);
155 JSValueRef JSMessageAttachment::getMessageID(JSContextRef context,
157 JSStringRef propertyName,
158 JSValueRef* exception)
163 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
165 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
166 LogDebug("Message ID :" << (attachment->getMessage()));
167 return converter.toJSValueRef(attachment->getMessage()->getId());
169 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
171 LogError("invalid value conversion");
172 return JSTizenExceptionFactory::postException(context, exception,
173 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
175 Catch(WrtDeviceApis::Commons::Exception)
177 LogError("Unkown Exception");
178 return JSTizenExceptionFactory::postException(context, exception,
179 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
181 return JSValueMakeUndefined(context);
185 JSValueRef JSMessageAttachment::getContentLocation(JSContextRef context,
187 JSStringRef propertyName,
188 JSValueRef* exception)
190 return JSValueMakeNull(context);
193 JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
195 JSStringRef propertyName,
196 JSValueRef* exception)
201 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
203 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
204 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
206 if ( attachment->getAttachmentID() < 0)
208 JSValueMakeUndefined(context);
211 return converter.toJSValueRef(attachment->getAttachmentID());
213 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
215 LogError("invalid value conversion");
216 return JSTizenExceptionFactory::postException(context, exception,
217 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
220 Catch(WrtDeviceApis::Commons::Exception)
222 LogError("Unkown Exception");
223 return JSTizenExceptionFactory::postException(context, exception,
224 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
227 return JSValueMakeUndefined(context);
231 JSValueRef JSMessageAttachment::getLoaded(JSContextRef context,
233 JSStringRef propertyName,
234 JSValueRef* exception)
240 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
242 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
243 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
244 return converter.toJSValueRef(attachment->getDownloaded());
246 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
248 LogError("invalid value conversion");
249 return JSTizenExceptionFactory::postException(context, exception,
250 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
252 Catch(WrtDeviceApis::Commons::Exception)
254 LogError("Unkown Exception");
255 return JSTizenExceptionFactory::postException(context, exception,
256 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
258 return JSValueMakeUndefined(context);
261 JSValueRef JSMessageAttachment::getFile(JSContextRef context,
263 JSStringRef propertyName,
264 JSValueRef* exception)
268 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
270 LogDebug("priv is vaild");
271 JSContextRef globalContext = priv ? priv->getContext() : context;
273 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
279 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
281 if ( (attachment->getVirtualPath()).length() > 0)
283 return converter.toJSValueRef(attachment->getVirtualPath());
287 std::string fullPath = attachment->getFullPath();
288 LogDebug("fullPath=" << fullPath);
289 if (fullPath.empty()) {
290 return JSValueMakeNull(context);
294 std::string virtualpath = AttachmentFactory::getVirtualPathFromRealPath(globalContext, fullPath);
295 if ( virtualpath.length() == 0 )
297 virtualpath = AttachmentFactory::getVirtualPathFromEmailServiceFolder(globalContext, fullPath);
298 attachment->setVirtualPath(virtualpath); //set virtual path
301 return converter.toJSValueRef(virtualpath);
306 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
308 LogError("invalid value conversion");
309 return JSTizenExceptionFactory::postException(context, exception,
310 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
312 Catch(WrtDeviceApis::Commons::Exception)
314 LogError("Unkown Exception");
315 return JSTizenExceptionFactory::postException(context, exception,
316 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
318 return JSValueMakeUndefined(context);
323 return JSTizenExceptionFactory::postException(context, exception,
324 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
327 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
331 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
333 return JSValueMakeUndefined(context);
336 std::string fullPath = attachment->getFullPath();
337 LogDebug("full Path = " << fullPath );
338 if (fullPath.empty()) {
339 return JSValueMakeUndefined(context);
342 int permissions = Api::Filesystem::PERM_READ | Api::Filesystem::PERM_WRITE;
343 EventGetNodeDataPtr data(
344 new EventGetNodeData(permissions, WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr())
346 Api::Filesystem::EventResolvePtr event(new Api::Filesystem::EventResolve(
347 Api::Filesystem::IPath::create(fullPath))
349 event->setPrivateData(
350 DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(data)
352 event->setForSynchronousCall();
353 Api::Filesystem::IManager::getInstance().getNode(event);
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);
363 Api::Filesystem::INodePtr node = event->getResult();
365 LogError("Resolved attachment location is empty.");
366 return JSTizenExceptionFactory::postException(context, exception,
367 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
371 Api::Filesystem::IPathPtr path = node->getPath();
373 LogError(" Path = " << path->getPath() << " Full Path = " << path->getFullPath());
375 JSObjectRef result = WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(context,
376 JSFile::getClassRef(),
381 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
383 LogError("invalid value conversion");
384 return JSTizenExceptionFactory::postException(context, exception,
385 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
388 Catch(WrtDeviceApis::Commons::Exception)
390 LogError("Unkown Exception");
391 return JSTizenExceptionFactory::postException(context, exception,
392 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
394 return JSValueMakeUndefined(context);
396 return JSValueMakeUndefined(context);
400 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
403 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
404 return getAttachment(context, converter.toJSObjectRef(value));
407 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
410 // private object of thisObject
411 return getPrivate(object)->getObject();
414 JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObject)
416 JSMessageAttachmentPrivate* thisPrivate =
417 static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(thisObject));
419 LogError("no private");
420 Throw(WrtDeviceApis::Commons::NullPointerException);
425 JSObjectRef JSMessageAttachment::constructor(JSContextRef context,
426 JSObjectRef constructor,
427 size_t argumentCount,
428 const JSValueRef arguments[],
429 JSValueRef* exception)
433 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(constructor));
434 JSContextRef globalContext = priv ? priv->getContext() : context;
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);
448 ConverterMessageFactory::ConverterType converter = ConverterMessageFactory::getConverter(context);
450 std::string path = converter->toString(arguments[0]); //filepath
451 std::string mimetype;
453 if ( argumentCount == 2)
454 mimetype = converter->toString(arguments[1]); //filepath
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(
464 JSClassRef jsClassRef = JSClassCreate(getClassInfo());
465 LogDebug("jsClassRef success");
466 JSMessageAttachmentPrivate* priv = new JSMessageAttachmentPrivate(globalContext, attachment);
467 LogDebug("priv success");
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");
480 LogDebug("attachment can't create");
481 ThrowMsg(WrtDeviceApis::Commons::InvalidArgumentException, "attachment create failed...");
486 Catch(WrtDeviceApis::Commons::InvalidArgumentException) {
487 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
490 Catch(WrtDeviceApis::Commons::ConversionException) {
491 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_TYPE_MISMATCH);
494 Catch(WrtDeviceApis::Commons::PlatformException) {
495 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
501 LogDebug(" globalContext is NULL ");
502 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);