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.
20 #include <CommonsJavaScript/Converter.h>
21 #include <CommonsJavaScript/Validator.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSCallbackManager.h>
24 #include <CommonsJavaScript/Utils.h>
25 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
26 #include <API/Messaging/IMessageFolder.h>
27 #include <API/Messaging/IMessagingTypes.h>
30 #include <Tizen/Common/JSTizenExceptionFactory.h>
31 #include <Tizen/Common/JSTizenException.h>
32 #include <Tizen/Common/SecurityExceptions.h>
34 #include "MessagingErrorMsg.h"
35 #include "JSMessageFolder.h"
39 using namespace TizenApis::Api::Messaging;
40 using namespace TizenApis::Commons;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
47 JSClassDefinition JSMessageFolder::m_classInfo =
50 kJSClassAttributeNone,
60 NULL, //deleteProperty,
61 NULL, //getPropertyNames,
68 const char* JSMessageFolder::FOLDERID = "id";
69 const char* JSMessageFolder::PARENTID = "parentId";
70 const char* JSMessageFolder::ACCOUNTID = "serviceId";
71 const char* JSMessageFolder::CONTENTTYPE = "contentType";
72 const char* JSMessageFolder::NAME = "name";
73 const char* JSMessageFolder::PATH = "path";
74 const char* JSMessageFolder::TYPE = "type";
75 const char* JSMessageFolder::SYNCHRONIZABLE = "synchronizable";
77 const std::string JSMessageFolder::TYPE_SMS = "tizen.sms";
78 const std::string JSMessageFolder::TYPE_MMS = "tizen.mms";
79 const std::string JSMessageFolder::TYPE_EMAIL = "tizen.email";
81 const std::string JSMessageFolder::FOLDER_INBOX = "INBOX";
82 const std::string JSMessageFolder::FOLDER_OUTBOX = "OUTBOX";
83 const std::string JSMessageFolder::FOLDER_DRAFTBOX = "DRAFTS";
84 const std::string JSMessageFolder::FOLDER_SENTBOX = "SENTBOX";
85 const std::string JSMessageFolder::FOLDER_NOTSTANDARD = "";
89 JSStaticValue JSMessageFolder::m_properties[] = {
90 {"id", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
91 {"parentId", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
92 {"serviceId", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
93 {"contentType", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
94 {"name", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
95 {"path", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
96 {"type", JSMessageFolder::getProperty, NULL, kJSPropertyAttributeReadOnly },
97 {"synchronizable", JSMessageFolder::getProperty, JSMessageFolder::setProperty, kJSPropertyAttributeNone },
101 const JSClassRef JSMessageFolder::getClassRef() {
103 m_jsClassRef = JSClassCreate(&m_classInfo);
108 const JSClassDefinition* JSMessageFolder::getClassInfo() {
112 JSClassRef JSMessageFolder::m_jsClassRef = JSClassCreate(JSMessageFolder::getClassInfo());
114 void JSMessageFolder::initialize(JSContextRef context, JSObjectRef object)
116 LogDebug("JSMessageFolder::initialize ");
117 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
125 LogDebug("JSMessageFolder::already exist ");
131 JSObjectRef JSMessageFolder::createJSObject(JSContextRef context, IMessageFolderPtr object)
133 JSMessageFolderPriv* priv = new JSMessageFolderPriv(context, object);
134 return JSObjectMake(context, getClassRef(), priv);
138 void JSMessageFolder::finalize(JSObjectRef object) {
139 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
141 LogDebug("JSMessageFolder::Finalrize");
145 JSObjectSetPrivate(object, NULL);
150 bool JSMessageFolder::hasInstance(JSContextRef context, JSObjectRef constructor,
151 JSValueRef possibleInstance, JSValueRef* exception) {
152 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
155 JSValueRef JSMessageFolder::getProperty(JSContextRef context,
157 JSStringRef propertyName,
158 JSValueRef* exception)
162 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
163 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
168 return JSTizenExceptionFactory::postException(context, exception,
169 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
172 IMessageFolderPtr MessageFolder(priv->getObject());
173 if(JSStringIsEqualToUTF8CString(propertyName, FOLDERID))
175 return converter.toJSValueRef(MessageFolder->getId());
177 else if(JSStringIsEqualToUTF8CString(propertyName, PARENTID))
179 return converter.toJSValueRef(MessageFolder->getParentId());
181 else if(JSStringIsEqualToUTF8CString(propertyName, ACCOUNTID))
183 return converter.toJSValueRef(MessageFolder->getAccountId());
185 else if(JSStringIsEqualToUTF8CString(propertyName, CONTENTTYPE))
187 return converter.toJSValueRef(convertMessageType(MessageFolder->getContentType()));
189 else if(JSStringIsEqualToUTF8CString(propertyName, NAME))
191 return converter.toJSValueRef(MessageFolder->getName());
193 else if(JSStringIsEqualToUTF8CString(propertyName, PATH))
195 return converter.toJSValueRef(MessageFolder->getPath());
197 else if(JSStringIsEqualToUTF8CString(propertyName, TYPE))
199 return converter.toJSValueRef(convertFolderType(MessageFolder->getType()));
201 else if(JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE))
203 return converter.toJSValueRef(MessageFolder->getSynchronizable());
207 catch (const WrtDeviceApis::Commons::Exception& ex) {
208 LogError("Exception: " << ex.GetMessage());
210 return JSValueMakeNull(context);
214 bool JSMessageFolder::setProperty(JSContextRef context,
216 JSStringRef propertyName,
218 JSValueRef* exception)
221 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
225 JSMessageFolderPriv* priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
227 Throw(WrtDeviceApis::Commons::NullPointerException);
229 Api::Messaging::IMessageFolderPtr MessageFolder(priv->getObject());
231 if (JSStringIsEqualToUTF8CString(propertyName, NAME)) {
232 std::string name = converter.toString(value);
233 MessageFolder->setName(name);
235 } else if (JSStringIsEqualToUTF8CString(propertyName, SYNCHRONIZABLE)) {
236 bool synchronizable = converter.toBool(value);
237 MessageFolder->setSynchronizable(synchronizable);
241 Catch(WrtDeviceApis::Commons::Exception)
243 LogWarning("trying to set incorrect value");
244 return JSTizenExceptionFactory::postException(context, exception,
245 JSTizenException::TYPE_MISMATCH_ERROR, JSMESSAGING_EXCEPTION_MSG_INVALID_ARGUMENT);
251 bool JSMessageFolder::isObjectOfClass(JSContextRef context, JSValueRef value)
253 return JSValueIsObjectOfClass(context, value, getClassRef());
256 IMessageFolderPtr JSMessageFolder::getMessageFolder(JSContextRef context, JSValueRef value)
258 JSObjectRef object = JSValueToObject(context, value, NULL);
261 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
263 JSMessageFolderPriv *priv = static_cast<JSMessageFolderPriv*>(JSObjectGetPrivate(object));
267 Throw(WrtDeviceApis::Commons::NullPointerException);
269 return priv->getObject();
272 std::string JSMessageFolder::convertMessageType(int msgType){
274 if(msgType == Api::Messaging::SMS){
275 return JSMessageFolder::TYPE_SMS;
276 }else if(msgType == Api::Messaging::MMS){
277 return JSMessageFolder::TYPE_MMS;
278 }else if(msgType == Api::Messaging::EMAIL){
279 return JSMessageFolder::TYPE_EMAIL;
285 std::string JSMessageFolder::convertFolderType(int msgType){
287 if(msgType == Api::Messaging::INBOX){
288 return JSMessageFolder::FOLDER_INBOX;
289 }else if(msgType == Api::Messaging::OUTBOX){
290 return JSMessageFolder::FOLDER_OUTBOX;
291 }else if(msgType == Api::Messaging::DRAFTBOX){
292 return JSMessageFolder::FOLDER_DRAFTBOX;
293 }else if(msgType == Api::Messaging::SENTBOX){
294 return JSMessageFolder::FOLDER_SENTBOX;
295 }else if(msgType == Api::Messaging::USERDEFINED_FOLDER){
296 return JSMessageFolder::FOLDER_NOTSTANDARD;