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>
35 #include "MessagingErrorMsg.h"
38 const char* MSG_ATTACHMENT_MIMETYPE = "MIMEType";
39 const char* MSG_ATTACHMENT_MESSAGEID = "messageId";
40 const char* MSG_ATTACHMENT_CONTENTLOCATION = "contentLocation";
41 const char* MSG_ATTACHMENT_CONTENTID = "contentId";
42 const char* MSG_ATTACHMENT_LOADED = "loaded";
48 using namespace TizenApis::Commons;
49 using namespace TizenApis::Api::Messaging;
50 using namespace WrtDeviceApis::Commons;
51 using namespace WrtDeviceApis::CommonsJavaScript;
53 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
55 JSClassDefinition JSMessageAttachment::m_classInfo = {
57 kJSClassAttributeNone,
60 /*JSFile::getClassRef(), parent*/
69 NULL, //deleteProperty,
70 NULL, //getPropertyNames,
71 NULL, //callAsFunction,
72 NULL, //callAsConstructor,
74 NULL, //convertToType,
77 JSStaticValue JSMessageAttachment::m_property[] = {
78 //{ "MIMEType", JSMessageAttachment::getMIMEType, NULL, kJSPropertyAttributeReadOnly },
79 { "messageId", JSMessageAttachment::getMessageID, NULL, kJSPropertyAttributeReadOnly },
80 //{ "contentLocation", JSMessageAttachment::getContentLocation, NULL, kJSPropertyAttributeNone },
81 { "contentId", JSMessageAttachment::getContentID, NULL, kJSPropertyAttributeNone },
82 { "loaded", JSMessageAttachment::getLoaded, NULL, kJSPropertyAttributeReadOnly },
83 { "file", JSMessageAttachment::getFile, NULL, kJSPropertyAttributeReadOnly },
86 const JSClassDefinition* JSMessageAttachment::getClassInfo()
88 return &(m_classInfo);
91 JSClassRef JSMessageAttachment::getClassRef()
94 m_jsClassRef = JSClassCreate(&m_classInfo);
99 void JSMessageAttachment::initialize(JSContextRef context,
105 void JSMessageAttachment::finalize(JSObjectRef object)
108 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
109 JSObjectSetPrivate(object, NULL);
113 JSObjectRef JSMessageAttachment::createJS( JSContextRef context, const Api::Messaging::IAttachmentPtr& attachment)
116 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
117 return JSObjectMake(context, getClassRef(), priv); //make JSObjectRef.
120 JSValueRef JSMessageAttachment::getMIMEType(JSContextRef context,
122 JSStringRef propertyName,
123 JSValueRef* exception)
128 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
130 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
131 LogDebug("mine type :" << attachment->getMimeType());
132 return converter.toJSValueRef(attachment->getMimeType());
134 Catch(WrtDeviceApis::Commons::Exception)
136 LogError("invalid conversion");
138 return JSValueMakeUndefined(context);
141 JSValueRef JSMessageAttachment::getMessageID(JSContextRef context,
143 JSStringRef propertyName,
144 JSValueRef* exception)
149 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
151 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
152 LogDebug("Message ID :" << (attachment->getMessage()));
153 return converter.toJSValueRef(attachment->getMessage()->getId());
155 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
157 LogError("invalid value conversion");
158 return JSTizenExceptionFactory::postException(context, exception,
159 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
161 Catch(WrtDeviceApis::Commons::Exception)
163 LogError("Unkown Exception");
164 return JSTizenExceptionFactory::postException(context, exception,
165 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
167 return JSValueMakeUndefined(context);
171 JSValueRef JSMessageAttachment::getContentLocation(JSContextRef context,
173 JSStringRef propertyName,
174 JSValueRef* exception)
176 return JSValueMakeNull(context);
179 JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
181 JSStringRef propertyName,
182 JSValueRef* exception)
187 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
189 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
190 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
191 return converter.toJSValueRef(attachment->getAttachmentID());
193 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
195 LogError("invalid value conversion");
196 return JSTizenExceptionFactory::postException(context, exception,
197 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
200 Catch(WrtDeviceApis::Commons::Exception)
202 LogError("Unkown Exception");
203 return JSTizenExceptionFactory::postException(context, exception,
204 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
207 return JSValueMakeUndefined(context);
211 JSValueRef JSMessageAttachment::getLoaded(JSContextRef context,
213 JSStringRef propertyName,
214 JSValueRef* exception)
220 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
222 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
223 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
224 return converter.toJSValueRef(attachment->getDownloaded());
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);
232 Catch(WrtDeviceApis::Commons::Exception)
234 LogError("Unkown Exception");
235 return JSTizenExceptionFactory::postException(context, exception,
236 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
238 return JSValueMakeUndefined(context);
241 JSValueRef JSMessageAttachment::getFile(JSContextRef context,
243 JSStringRef propertyName,
244 JSValueRef* exception)
246 #if 0 // MESSAGING ATTACHMENT IS BLOCKED
250 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
252 return JSValueMakeUndefined(context);
255 std::string fullPath = attachment->getFullPath();
256 LogDebug("full Path = " << fullPath );
257 if (fullPath.empty()) {
258 return JSValueMakeUndefined(context);
261 int permissions = Api::Filesystem::PERM_READ | Api::Filesystem::PERM_WRITE;
262 EventGetNodeDataPtr data(
263 new EventGetNodeData(permissions, WrtDeviceApis::CommonsJavaScript::JSCallbackManagerPtr())
265 Api::Filesystem::EventResolvePtr event(new Api::Filesystem::EventResolve(
266 Api::Filesystem::IPath::create(fullPath))
268 event->setPrivateData(
269 DPL::StaticPointerCast<WrtDeviceApis::Commons::IEventPrivateData>(data)
271 event->setForSynchronousCall();
272 Api::Filesystem::IManager::getInstance().getNode(event);
274 WrtDeviceApis::Commons::ExceptionCodes::Enumeration code = event->getExceptionCode();
275 if (code != WrtDeviceApis::Commons::ExceptionCodes::None) {
276 LogError("Attachment location not resolved. Exception code: " << code);
277 return JSTizenExceptionFactory::postException(context, exception,
278 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
282 Api::Filesystem::INodePtr node = event->getResult();
284 LogError("Resolved attachment location is empty.");
285 return JSTizenExceptionFactory::postException(context, exception,
286 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
290 Api::Filesystem::IPathPtr path = node->getPath();
292 LogError(" Path = " << path->getPath() << " Full Path = " << path->getFullPath());
294 JSObjectRef result = WrtDeviceApis::CommonsJavaScript::JSUtils::makeObject(context,
295 JSFile::getClassRef(),
300 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
302 LogError("invalid value conversion");
303 return JSTizenExceptionFactory::postException(context, exception,
304 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
307 Catch(WrtDeviceApis::Commons::Exception)
309 LogError("Unkown Exception");
310 return JSTizenExceptionFactory::postException(context, exception,
311 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
313 return JSValueMakeUndefined(context);
315 return JSValueMakeUndefined(context);
319 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
322 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
323 return getAttachment(context, converter.toJSObjectRef(value));
326 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
329 // private object of thisObject
330 return getPrivate(object)->getObject();
333 JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObject)
335 JSMessageAttachmentPrivate* thisPrivate =
336 static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(thisObject));
338 LogError("no private");
339 Throw(WrtDeviceApis::Commons::NullPointerException);