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/Validator.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_SERVICE_ID "accountServiceId"
34 #define CONTACT_ATTR_CONTACT_URI "contactURI"
40 using namespace TizenApis::Commons;
41 using namespace TizenApis::Api::Contact;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
45 JSClassDefinition JSContactAccount::m_classInfo =
48 kJSClassAttributeNone,
49 ORGANIZATION_CLASS_NAME,
58 NULL, //DeleteProperty,
59 NULL, //getPropertyNames,
60 NULL, //CallAsFunction,
61 constructor, //CallAsConstructor,
62 hasInstance, //HasInstance,
63 NULL, //ConvertToType,
66 JSStaticValue JSContactAccount::m_property[] = {
67 { CONTACT_ATTR_ACCOUNT_SERVICE_ID, getAccountServiceId, setAccountServiceId, kJSPropertyAttributeNone },
68 { CONTACT_ATTR_CONTACT_URI, getContactURI, setContactURI, kJSPropertyAttributeNone },
72 JSStaticFunction JSContactAccount::m_functions[] =
77 JSClassRef JSContactAccount::m_classRef = JSClassCreate(&m_classInfo);
79 JSClassRef JSContactAccount::getClassRef() {
81 m_classRef = JSClassCreate(&m_classInfo);
86 bool JSContactAccount::isObjectOfClass(JSContextRef context, JSValueRef value)
88 return JSValueIsObjectOfClass(context, value, getClassRef());
91 ContactAccountPtr JSContactAccount::getContactAccount(JSContextRef context, JSValueRef value)
93 if (!isObjectOfClass(context, value)) {
94 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
96 JSObjectRef object = JSValueToObject(context, value, NULL);
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(object));
102 Throw(WrtDeviceApis::Commons::NullPointerException);
104 return priv->getObject();
107 void JSContactAccount::Initialize(JSContextRef context, JSObjectRef object)
109 if (!JSObjectGetPrivate(object))
111 ContactAccountPtr account(new ContactAccount());
112 JSContactAccountPriv *priv = new JSContactAccountPriv(context, ContactAccountPtr(account));
113 if (!JSObjectSetPrivate(object, priv)) {
119 void JSContactAccount::Finalize(JSObjectRef object)
121 JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(object));
127 JSObjectRef JSContactAccount::createJSObject(JSContextRef context, const ContactAccountPtr contactAccount)
129 JSContactAccountPriv *priv = new JSContactAccountPriv(context, contactAccount);
130 JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
131 if (NULL == jsObjectRef) {
132 LogError("object creation error");
138 ContactAccountPtr JSContactAccount::getPrivData(JSObjectRef object)
140 JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(object));
142 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
144 ContactAccountPtr result = priv->getObject();
146 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
151 JSObjectRef JSContactAccount::constructor(JSContextRef context,
152 JSObjectRef constructor,
153 size_t argumentCount,
154 const JSValueRef arguments[],
155 JSValueRef* exception)
159 bool js2ndParamIsString = false;
161 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
162 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
164 JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(constructor));
166 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
168 JSContextRef gContext = priv->getContext();
170 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
171 if (argumentCount >= 2)
173 if (!JSValueIsNull(gContext, arguments[1]) && !JSValueIsUndefined(gContext, arguments[1]))
174 js2ndParamIsString = true;
177 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
179 std::string accountServiceId;
180 std::string contactURI;
183 if (argumentCount >= 1)
184 accountServiceId = converter->toString(arguments[0]);
186 accountServiceId = converter->toString(JSValueMakeUndefined(context));
189 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
190 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
195 if(js2ndParamIsString)
196 contactURI = converter->toString(arguments[1]);
199 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
200 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
204 ContactAccountPtr contactAccount(new ContactAccount());
205 contactAccount->setAccountServiceId(accountServiceId);
206 contactAccount->setContactURI(contactURI);
208 JSObjectRef jsobject;
211 jsobject = createJSObject(gContext, contactAccount);
213 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
214 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
221 bool JSContactAccount::hasInstance(JSContextRef context,
222 JSObjectRef constructor,
223 JSValueRef possibleInstance,
224 JSValueRef* exception)
226 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
229 JSValueRef JSContactAccount::getAccountServiceId(JSContextRef context,
231 JSStringRef propertyAccountId,
232 JSValueRef* exception)
236 ContactConverterFactory::ConverterType converter =
237 ContactConverterFactory::getConverter(context);
238 ContactAccountPtr organization = getPrivData(object);
239 return converter->toJSValueRef(organization->getAccountServiceId());
241 Catch(WrtDeviceApis::Commons::Exception)
243 LogWarning("trying to get incorrect value");
245 return JSValueMakeUndefined(context);
248 bool JSContactAccount::setAccountServiceId(JSContextRef context,
250 JSStringRef propertyAccountId,
252 JSValueRef* exception)
256 ContactAccountPtr organization = getPrivData(object);
257 ContactConverterFactory::ConverterType converter =
258 ContactConverterFactory::getConverter(context);
259 organization->setAccountServiceId(converter->toString(value));
262 Catch(WrtDeviceApis::Commons::Exception)
264 LogWarning("trying to set incorrect value");
266 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
270 JSValueRef JSContactAccount::getContactURI(JSContextRef context,
272 JSStringRef propertyName,
273 JSValueRef* exception)
277 ContactConverterFactory::ConverterType converter =
278 ContactConverterFactory::getConverter(context);
279 ContactAccountPtr organization = getPrivData(object);
280 return converter->toJSValueRef(organization->getContactURI());
282 Catch(WrtDeviceApis::Commons::Exception)
284 LogWarning("trying to get incorrect value");
286 return JSValueMakeUndefined(context);
289 bool JSContactAccount::setContactURI(JSContextRef context,
291 JSStringRef propertyName,
293 JSValueRef* exception)
297 ContactAccountPtr organization = getPrivData(object);
298 ContactConverterFactory::ConverterType converter =
299 ContactConverterFactory::getConverter(context);
300 organization->setContactURI(converter->toString(value));
303 Catch(WrtDeviceApis::Commons::Exception)
305 LogWarning("trying to set incorrect value");
307 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");