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)
25 #include <CommonsJavaScript/Converter.h>
26 #include <CommonsJavaScript/JSUtils.h>
27 #include "ConverterMessage.h"
28 #include "JSMessageAttachment.h"
29 #include "JSAttachmentArray.h" //attachment array
30 #include <Tizen/Filesystem/JSFile.h>
31 #include <Tizen/Filesystem/EventGetNodeData.h>
32 #include <API/Filesystem/IManager.h>
33 #include <Tizen/Common/JSTizenExceptionFactory.h>
34 #include <Tizen/Common/JSTizenException.h>
36 #include "MessagingErrorMsg.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;
54 JSClassRef JSMessageAttachment::m_jsClassRef = NULL;
56 JSClassDefinition JSMessageAttachment::m_classInfo = {
58 kJSClassAttributeNone,
61 /*JSFile::getClassRef(), parent*/
70 NULL, //deleteProperty,
71 NULL, //getPropertyNames,
72 NULL, //callAsFunction,
73 NULL, //callAsConstructor,
75 NULL, //convertToType,
78 JSStaticValue JSMessageAttachment::m_property[] = {
79 //{ "MIMEType", JSMessageAttachment::getMIMEType, NULL, kJSPropertyAttributeReadOnly },
80 { "messageId", JSMessageAttachment::getMessageID, NULL, kJSPropertyAttributeReadOnly },
81 //{ "contentLocation", JSMessageAttachment::getContentLocation, NULL, kJSPropertyAttributeNone },
82 { "contentId", JSMessageAttachment::getContentID, NULL, kJSPropertyAttributeNone },
83 { "loaded", JSMessageAttachment::getLoaded, NULL, kJSPropertyAttributeReadOnly },
84 { "file", JSMessageAttachment::getFile, NULL, kJSPropertyAttributeReadOnly },
87 const JSClassDefinition* JSMessageAttachment::getClassInfo()
89 return &(m_classInfo);
92 JSClassRef JSMessageAttachment::getClassRef()
95 m_jsClassRef = JSClassCreate(&m_classInfo);
100 void JSMessageAttachment::initialize(JSContextRef context,
106 void JSMessageAttachment::finalize(JSObjectRef object)
109 JSMessageAttachmentPrivate* priv = static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(object));
110 JSObjectSetPrivate(object, NULL);
114 JSObjectRef JSMessageAttachment::createJS( JSContextRef context, const Api::Messaging::IAttachmentPtr& attachment)
117 JSMessageAttachmentPrivate *priv = new JSMessageAttachmentPrivate(context, attachment);
118 return JSObjectMake(context, getClassRef(), priv); //make JSObjectRef.
121 JSValueRef JSMessageAttachment::getMIMEType(JSContextRef context,
123 JSStringRef propertyName,
124 JSValueRef* exception)
129 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
131 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
132 LogDebug("mine type :" << attachment->getMimeType());
133 return converter.toJSValueRef(attachment->getMimeType());
135 Catch(WrtDeviceApis::Commons::Exception)
137 LogError("invalid conversion");
139 return JSValueMakeUndefined(context);
142 JSValueRef JSMessageAttachment::getMessageID(JSContextRef context,
144 JSStringRef propertyName,
145 JSValueRef* exception)
150 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
152 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
153 LogDebug("Message ID :" << (attachment->getMessage()));
154 return converter.toJSValueRef(attachment->getMessage()->getId());
156 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
158 LogError("invalid value conversion");
159 return JSTizenExceptionFactory::postException(context, exception,
160 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
162 Catch(WrtDeviceApis::Commons::Exception)
164 LogError("Unkown Exception");
165 return JSTizenExceptionFactory::postException(context, exception,
166 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
168 return JSValueMakeUndefined(context);
172 JSValueRef JSMessageAttachment::getContentLocation(JSContextRef context,
174 JSStringRef propertyName,
175 JSValueRef* exception)
177 return JSValueMakeNull(context);
180 JSValueRef JSMessageAttachment::getContentID(JSContextRef context,
182 JSStringRef propertyName,
183 JSValueRef* exception)
188 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
190 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
191 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
192 return converter.toJSValueRef(attachment->getAttachmentID());
194 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
196 LogError("invalid value conversion");
197 return JSTizenExceptionFactory::postException(context, exception,
198 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
201 Catch(WrtDeviceApis::Commons::Exception)
203 LogError("Unkown Exception");
204 return JSTizenExceptionFactory::postException(context, exception,
205 JSTizenException::UNKNOWN_ERROR, JSMESSAGING_EXCEPTION_MSG_UNKNOWN);
208 return JSValueMakeUndefined(context);
212 JSValueRef JSMessageAttachment::getLoaded(JSContextRef context,
214 JSStringRef propertyName,
215 JSValueRef* exception)
221 Api::Messaging::IAttachmentPtr attachment = getAttachment(context, object);
223 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
224 LogDebug("Attachment Loaded :" << (attachment->getDownloaded() ));
225 return converter.toJSValueRef(attachment->getDownloaded());
227 Catch(WrtDeviceApis::Commons::InvalidArgumentException)
229 LogError("invalid value conversion");
230 return JSTizenExceptionFactory::postException(context, exception,
231 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);
239 return JSValueMakeUndefined(context);
242 JSValueRef JSMessageAttachment::getFile(JSContextRef context,
244 JSStringRef propertyName,
245 JSValueRef* exception)
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);
317 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
320 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
321 return getAttachment(context, converter.toJSObjectRef(value));
324 Api::Messaging::IAttachmentPtr JSMessageAttachment::getAttachment(JSContextRef context,
327 // private object of thisObject
328 return getPrivate(object)->getObject();
331 JSMessageAttachmentPrivate* JSMessageAttachment::getPrivate(JSObjectRef thisObject)
333 JSMessageAttachmentPrivate* thisPrivate =
334 static_cast<JSMessageAttachmentPrivate*>(JSObjectGetPrivate(thisObject));
336 LogError("no private");
337 Throw(WrtDeviceApis::Commons::NullPointerException);