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.
18 * @file JSContactAccount.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactAccount class
24 #include <dpl/shared_ptr.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "JSContactAccount.h"
31 #define ORGANIZATION_CLASS_NAME "ContactAccount"
33 #define CONTACT_ATTR_ACCOUNT_ID "accountId"
34 #define CONTACT_ATTR_CONTACT_URI "contactUri"
40 using namespace TizenApis::Commons;
41 using namespace TizenApis::Api::Contact;
43 JSClassRef JSContactAccount::m_classRef = NULL;
45 JSClassDefinition JSContactAccount::m_classInfo =
48 kJSClassAttributeNone,
49 ORGANIZATION_CLASS_NAME,
58 NULL, //DeleteProperty,
59 NULL, //getPropertyNames,
63 NULL, //ConvertToType,
66 JSStaticValue JSContactAccount::m_property[] = {
67 { CONTACT_ATTR_ACCOUNT_ID, getAccountId, setAccountId, kJSPropertyAttributeNone },
68 { CONTACT_ATTR_CONTACT_URI, getContactURI, setContactURI, kJSPropertyAttributeNone },
72 JSStaticFunction JSContactAccount::m_functions[] =
77 JSClassRef JSContactAccount::getClassRef() {
79 m_classRef = JSClassCreate(&m_classInfo);
84 JSValueRef JSContactAccount::createJSObject(JSContextRef context,
85 const std::string& accountId,
86 const std::string& contactURI)
88 ContactAccountPtr privateData = ContactAccountPtr(new ContactAccount());
89 privateData->setAccountId(accountId);
90 privateData->setContactURI(contactURI);
91 JSContactAccountPriv *priv = new JSContactAccountPriv(context, privateData);
92 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
93 if (NULL == jsValueRef) {
94 LogError("object creation error");
95 return JSValueMakeUndefined(context);
100 bool JSContactAccount::isObjectOfClass(JSContextRef context, JSValueRef value)
102 return JSValueIsObjectOfClass(context, value, getClassRef());
105 ContactAccountPtr JSContactAccount::getContactAccount(JSContextRef context, JSValueRef value)
107 if (!isObjectOfClass(context, value)) {
108 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
110 JSObjectRef object = JSValueToObject(context, value, NULL);
112 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
114 JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(object));
116 Throw(WrtDeviceApis::Commons::NullPointerException);
118 return priv->getObject();
121 void JSContactAccount::Initialize(JSContextRef context, JSObjectRef object)
123 assert(NULL != JSObjectGetPrivate(object));
126 void JSContactAccount::Finalize(JSObjectRef object)
128 //delete (JSObjectGetPrivate(object));
131 ContactAccountPtr JSContactAccount::getPrivData(JSObjectRef object)
133 //LogDebug("entered");
134 JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(object));
136 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
138 ContactAccountPtr result = priv->getObject();
140 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
145 JSValueRef JSContactAccount::getAccountId(JSContextRef context,
147 JSStringRef propertyAccountId,
148 JSValueRef* exception)
150 //LogDebug("entered");
153 ContactConverterFactory::ConverterType converter =
154 ContactConverterFactory::getConverter(context);
155 ContactAccountPtr organization = getPrivData(object);
156 return converter->toJSValueRef(organization->getAccountId());
158 Catch(WrtDeviceApis::Commons::Exception)
160 LogWarning("trying to get incorrect value");
162 return JSValueMakeUndefined(context);
165 bool JSContactAccount::setAccountId(JSContextRef context,
167 JSStringRef propertyAccountId,
169 JSValueRef* exception)
173 ContactAccountPtr organization = getPrivData(object);
174 ContactConverterFactory::ConverterType converter =
175 ContactConverterFactory::getConverter(context);
176 organization->setAccountId(converter->toString(value));
179 Catch(WrtDeviceApis::Commons::Exception)
181 LogWarning("trying to set incorrect value");
183 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
187 JSValueRef JSContactAccount::getContactURI(JSContextRef context,
189 JSStringRef propertyName,
190 JSValueRef* exception)
192 //LogDebug("entered");
195 ContactConverterFactory::ConverterType converter =
196 ContactConverterFactory::getConverter(context);
197 ContactAccountPtr organization = getPrivData(object);
198 return converter->toJSValueRef(organization->getContactURI());
200 Catch(WrtDeviceApis::Commons::Exception)
202 LogWarning("trying to get incorrect value");
204 return JSValueMakeUndefined(context);
207 bool JSContactAccount::setContactURI(JSContextRef context,
209 JSStringRef propertyName,
211 JSValueRef* exception)
215 ContactAccountPtr organization = getPrivData(object);
216 ContactConverterFactory::ConverterType converter =
217 ContactConverterFactory::getConverter(context);
218 organization->setContactURI(converter->toString(value));
221 Catch(WrtDeviceApis::Commons::Exception)
223 LogWarning("trying to set incorrect value");
225 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");