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 JSAccountServices.cpp
19 * @author Jihwa park (jh7979.park@samsung.com)
22 * @brief Implementation of the JSAccountServices class
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "AccountConverter.h"
30 #include "JSAccountServices.h"
33 #define TIZEN_ACCOUNT_CLASS_ACCOUNT_SERVICE "service"
35 #define ACCOUNT_SERVICES_ATTR_SERVICE_ID "id"
36 #define ACCOUNT_SERVICES_ATTR_SERVICE_NAME "name"
37 #define ACCOUNT_SERVICES_ATTR_SERVICE_APPLICATION_ID "applicationId"
38 #define ACCOUNT_SERVICES_ATTR_SERVICE_DISPLAY_NAME "displayName"
39 #define ACCOUNT_SERVICES_ATTR_SERVICE_ICON "icon"
40 #define ACCOUNT_SERVICES_ATTR_SERVICE_ACCOUNT_ID "accountId"
41 #define ACCOUNT_SERVICES_ATTR_SERVICE_SERVICE_TYPE_ID "serviceTypeId"
42 #define ACCOUNT_SERVICES_ATTR_SERVICE_PROVIDER_ID "providerId"
43 #define ACCOUNT_SERVICES_ATTR_SERVICE_TAGS "tags"
44 #define ACCOUNT_SERVICES_ATTR_SERVICE_SETTINGS "settings"
52 using namespace TizenApis::Commons;
53 using namespace TizenApis::Api::Account;
54 using namespace TizenApis::Tizen1_0::Account;
56 JSClassRef JSAccountServices::m_classRef = NULL;
58 JSClassDefinition JSAccountServices::m_classInfo =
61 kJSClassAttributeNone,
62 TIZEN_ACCOUNT_CLASS_ACCOUNT_SERVICE,
71 NULL, //DeleteProperty,
72 NULL, //getPropertyNames,
76 NULL, //ConvertToType,
79 JSStaticValue JSAccountServices::m_property[] = {
80 { ACCOUNT_SERVICES_ATTR_SERVICE_ID, getProperty, setProperty, kJSPropertyAttributeNone },
81 { ACCOUNT_SERVICES_ATTR_SERVICE_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
82 { ACCOUNT_SERVICES_ATTR_SERVICE_APPLICATION_ID, getProperty, setProperty, kJSPropertyAttributeNone },
83 { ACCOUNT_SERVICES_ATTR_SERVICE_DISPLAY_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
84 { ACCOUNT_SERVICES_ATTR_SERVICE_ICON, getProperty, setProperty, kJSPropertyAttributeNone },
85 { ACCOUNT_SERVICES_ATTR_SERVICE_ACCOUNT_ID, getProperty, setProperty, kJSPropertyAttributeNone },
86 { ACCOUNT_SERVICES_ATTR_SERVICE_SERVICE_TYPE_ID, getProperty, setProperty, kJSPropertyAttributeNone },
87 { ACCOUNT_SERVICES_ATTR_SERVICE_PROVIDER_ID, getProperty, setProperty, kJSPropertyAttributeNone },
88 { ACCOUNT_SERVICES_ATTR_SERVICE_TAGS, getProperty, setProperty, kJSPropertyAttributeNone },
89 { ACCOUNT_SERVICES_ATTR_SERVICE_SETTINGS, getProperty, setProperty, kJSPropertyAttributeNone },
94 JSClassRef JSAccountServices::getClassRef()
97 m_classRef = JSClassCreate(&m_classInfo);
102 JSObjectRef JSAccountServices::createJSObject(JSContextRef context, AccountServicesPtr service)
104 AccountServicesPrivObj *priv = new AccountServicesPrivObj(context, service);
105 return JSObjectMake(context, getClassRef(), priv);
108 bool JSAccountServices::isObjectOfClass(JSContextRef context, JSValueRef value)
110 return JSValueIsObjectOfClass(context, value, getClassRef());
113 AccountServicesPtr JSAccountServices::getAccountServices(JSContextRef context, JSValueRef value)
115 if (!isObjectOfClass(context, value)) {
116 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
119 JSObjectRef object = JSValueToObject(context, value, NULL);
121 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
124 AccountServicesPrivObj *priv = static_cast<AccountServicesPrivObj*>(JSObjectGetPrivate(object));
126 Throw(WrtDeviceApis::Commons::NullPointerException);
129 return priv->getObject();
132 void JSAccountServices::Initialize(JSContextRef context, JSObjectRef object)
137 void JSAccountServices::Finalize(JSObjectRef object)
139 AccountServicesPrivObj *privateObject = static_cast<AccountServicesPrivObj*>(JSObjectGetPrivate(object));
140 delete privateObject;
143 AccountServicesPtr JSAccountServices::getPrivData(JSObjectRef object)
146 AccountServicesPrivObj *priv = static_cast<AccountServicesPrivObj*>(JSObjectGetPrivate(object));
148 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
151 AccountServicesPtr result = priv->getObject();
153 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
159 JSValueRef JSAccountServices::getProperty(JSContextRef context,
161 JSStringRef propertyName,
162 JSValueRef* exception)
166 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
167 AccountServicesPtr accountServicePtr = getPrivData(object);
169 if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_ID)) {
170 return converter->toJSValueRef(accountServicePtr->getId());
171 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_NAME)) {
172 return converter->toJSValueRef(accountServicePtr->getName());
173 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_APPLICATION_ID)) {
174 return converter->toJSValueRef(accountServicePtr->getApplicationId());
175 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_DISPLAY_NAME)) {
176 return converter->toJSValueRef(accountServicePtr->getDisplayName());
177 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_ICON)) {
178 return converter->toJSValueRef(accountServicePtr->getIcon());
179 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_ACCOUNT_ID)) {
180 return converter->toJSValueRef(accountServicePtr->getAccountId());
181 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_SERVICE_TYPE_ID)) {
182 return converter->toJSValueRef(accountServicePtr->getServiceTypeId());
183 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_PROVIDER_ID)) {
184 return converter->toJSValueRef(accountServicePtr->getProviderId());
185 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_TAGS)) {
186 // return converter->toJSValueRef(accountServicePtr->getTags());
187 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_SETTINGS)) {
188 std::string settingsString = accountServicePtr->getSettings();
189 return converter->toJSONString(settingsString);
191 }Catch(WrtDeviceApis::Commons::Exception){
192 AccountLogWarning("trying to get incorrect value");
195 LogDebug(">>> return UNDEFINED");
196 return JSValueMakeUndefined(context);
199 bool JSAccountServices::setProperty(JSContextRef context,
201 JSStringRef propertyName,
203 JSValueRef* exception)
207 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
208 AccountServicesPtr accountServicePtr = getPrivData(object);
210 if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_APPLICATION_ID)) {
211 std::string applicationId = converter->toString(value);
212 accountServicePtr->setApplicationId(applicationId);
214 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_DISPLAY_NAME)) {
215 std::string displayName = converter->toString(value);
216 accountServicePtr->setDisplayName(displayName);
218 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_ICON)) {
219 std::string icon = converter->toString(value);
220 accountServicePtr->setIcon(icon);
222 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_SETTINGS)) {
223 JSStringRef jsonStringRef = JSValueCreateJSONString(context, value, 0, exception);
224 std::string settingString = converter->toString(jsonStringRef);
225 accountServicePtr->setSettings(settingString);
228 } Catch(WrtDeviceApis::Commons::Exception){
229 AccountLogWarning("trying to set incorrect value");
232 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");