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 JSMessageFolder.cpp
20 * @author Oy Kwon (sirot.kwon@samsung.com)
25 #include <CommonsJavaScript/Converter.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <CommonsJavaScript/JSUtils.h>
28 #include <CommonsJavaScript/JSCallbackManager.h>
29 #include <CommonsJavaScript/Utils.h>
30 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
31 #include <API/Messaging/IMessageFolder.h>
32 #include <API/Messaging/IMessagingTypes.h>
35 #include <Tizen/Common/JSTizenExceptionFactory.h>
36 #include <Tizen/Common/JSTizenException.h>
37 #include <Tizen/Common/SecurityExceptions.h>
39 #include "MessagingErrorMsg.h"
40 #include "JSMessageFolder.h"
44 using namespace TizenApis::Api::Messaging;
45 using namespace TizenApis::Commons;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
52 JSClassDefinition JSMessageFolder::m_classInfo =
55 kJSClassAttributeNone,
65 NULL, //deleteProperty,
66 NULL, //getPropertyNames,
73 const char* JSMessageFolder::FOLDERID = "id";
74 const char* JSMessageFolder::PARENTID = "parentid";
75 const char* JSMessageFolder::ACCOUNTID = "accountid";
76 const char* JSMessageFolder::CONTENTTYPE = "contentType";
77 const char* JSMessageFolder::NAME = "name";
78 const char* JSMessageFolder::PATH = "path";
79 const char* JSMessageFolder::TYPE = "type";
80 const char* JSMessageFolder::SYNCHRONIZABLE = "synchronizable";
82 const std::string JSMessageFolder::TYPE_SMS = "SMS";
83 const std::string JSMessageFolder::TYPE_MMS = "MMS";
84 const std::string JSMessageFolder::TYPE_EMAIL = "EMAIL";
86 const std::string JSMessageFolder::FOLDER_INBOX = "INBOX";
87 const std::string JSMessageFolder::FOLDER_OUTBOX = "OUTBOX";
88 const std::string JSMessageFolder::FOLDER_DRAFTBOX = "DRAFTS";
89 const std::string JSMessageFolder::FOLDER_SENTBOX = "SENTBOX";
90 const std::string JSMessageFolder::FOLDER_NOTSTANDARD = "";
94 JSStaticValue JSMessageFolder::m_properties[] = {
95 {"id", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
96 {"parentid", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
97 {"accountid", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
98 {"contentType", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
99 {"name", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
100 {"path", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
101 {"type", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
102 {"synchronizable", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
106 const JSClassRef JSMessageFolder::getClassRef() {
108 m_jsClassRef = JSClassCreate(&m_classInfo);
113 const JSClassDefinition* JSMessageFolder::getClassInfo() {
117 JSClassRef JSMessageFolder::m_jsClassRef = JSClassCreate(JSMessageFolder::getClassInfo());
119 void JSMessageFolder::initialize(JSContextRef context, JSObjectRef object)
121 LogDebug("JSMessageFolder::initialize ");
122 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
130 LogDebug("JSMessageFolder::already exist ");
136 JSObjectRef JSMessageFolder::createJSObject(JSContextRef context, IMessageFolderPtr object)
138 JSMessageFolderPriv* priv = new JSMessageFolderPriv(context, object);
139 return JSObjectMake(context, getClassRef(), priv);
143 void JSMessageFolder::finalize(JSObjectRef object) {
144 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
146 LogDebug("JSMessageFolder::Finalrize");
150 JSObjectSetPrivate(object, NULL);
155 bool JSMessageFolder::hasInstance(JSContextRef context, JSObjectRef constructor,
156 JSValueRef possibleInstance, JSValueRef* exception) {
157 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
160 JSValueRef JSMessageFolder::getProperty(JSContextRef context,
162 JSStringRef propertyName,
163 JSValueRef* exception)
167 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
168 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
173 return JSTizenExceptionFactory::postException(context, exception,
174 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
177 IMessageFolderPtr MessageFolder(priv->getObject());
178 if(JSStringIsEqualToUTF8CString(propertyName, FOLDERID))
180 return converter.toJSValueRef(MessageFolder->getId());
182 else if(JSStringIsEqualToUTF8CString(propertyName, PARENTID))
184 return converter.toJSValueRef(MessageFolder->getParentId());
186 else if(JSStringIsEqualToUTF8CString(propertyName, ACCOUNTID))
188 return converter.toJSValueRef(MessageFolder->getAccountId());
190 else if(JSStringIsEqualToUTF8CString(propertyName, CONTENTTYPE))
192 return converter.toJSValueRef(convertMessageType(MessageFolder->getContentType()));
194 else if(JSStringIsEqualToUTF8CString(propertyName, NAME))
196 return converter.toJSValueRef(MessageFolder->getName());
198 else if(JSStringIsEqualToUTF8CString(propertyName, PATH))
200 return converter.toJSValueRef(MessageFolder->getPath());
202 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
204 return converter.toJSValueRef(convertFolderType(MessageFolder->getType()));
206 else if(JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE))
208 return converter.toJSValueRef(MessageFolder->getSynchronizable());
212 catch (const WrtDeviceApis::Commons::Exception& ex) {
213 LogError("Exception: " << ex.GetMessage());
215 return JSValueMakeNull(context);
219 bool JSMessageFolder::setProperty(JSContextRef context,
221 JSStringRef propertyName,
223 JSValueRef* exception)
226 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
230 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
232 Throw(WrtDeviceApis::Commons::NullPointerException);
234 Api::Messaging::IMessageFolderPtr MessageFolder(priv->getObject());
236 if (JSStringIsEqualToUTF8CString(propertyName, NAME)) {
237 std::string name = converter.toString(value);
238 MessageFolder->setName(name);
240 } else if (JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE)) {
241 bool synchronizable = converter.toBool(value);
242 MessageFolder->setSynchronizable(synchronizable);
246 Catch(WrtDeviceApis::Commons::Exception)
248 LogWarning("trying to set incorrect value");
249 return JSTizenExceptionFactory::postException(context, exception,
250 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
256 bool JSMessageFolder::isObjectOfClass(JSContextRef context, JSValueRef value)
258 return JSValueIsObjectOfClass(context, value, getClassRef());
261 IMessageFolderPtr JSMessageFolder::getMessageFolder(JSContextRef context, JSValueRef value)
263 JSObjectRef object = JSValueToObject(context, value, NULL);
266 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
268 JSMessageFolderPriv *priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
272 Throw(WrtDeviceApis::Commons::NullPointerException);
274 return priv->getObject();
277 std::string JSMessageFolder::convertMessageType(int msgType){
279 if(msgType == Api::Messaging::SMS){
280 return JSMessageFolder::TYPE_SMS;
281 }else if(msgType == Api::Messaging::MMS){
282 return JSMessageFolder::TYPE_MMS;
283 }else if(msgType == Api::Messaging::EMAIL){
284 return JSMessageFolder::TYPE_EMAIL;
290 std::string JSMessageFolder::convertFolderType(int msgType){
292 if(msgType == Api::Messaging::INBOX){
293 return JSMessageFolder::FOLDER_INBOX;
294 }else if(msgType == Api::Messaging::OUTBOX){
295 return JSMessageFolder::FOLDER_OUTBOX;
296 }else if(msgType == Api::Messaging::DRAFTBOX){
297 return JSMessageFolder::FOLDER_DRAFTBOX;
298 }else if(msgType == Api::Messaging::SENTBOX){
299 return JSMessageFolder::FOLDER_SENTBOX;
300 }else if(msgType == Api::Messaging::USERDEFINED_FOLDER){
301 return JSMessageFolder::FOLDER_NOTSTANDARD;