cc6b6cfd76e28b99800e76d14b5942640b297dc4
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Account / JSAccountServices.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 /**
18  * @file        JSAccountServices.cpp
19  * @author      Jihwa park (jh7979.park@samsung.com)
20  * @author      Sangtai Kim
21  * @version     0.1
22  * @brief       Implementation of the JSAccountServices class
23  */
24
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"
31
32 namespace {
33 #define TIZEN_ACCOUNT_CLASS_ACCOUNT_SERVICE     "service"
34
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"
45
46 }
47
48 namespace TizenApis {
49 namespace Api {
50 namespace Account {
51
52 using namespace TizenApis::Commons;
53 using namespace TizenApis::Api::Account;
54 using namespace TizenApis::Tizen1_0::Account;
55
56 JSClassRef JSAccountServices::m_classRef = NULL;
57
58 JSClassDefinition JSAccountServices::m_classInfo =
59 {
60         0,
61         kJSClassAttributeNone,
62         TIZEN_ACCOUNT_CLASS_ACCOUNT_SERVICE,
63         NULL,
64         m_property,
65         NULL,
66         Initialize,
67         Finalize,
68         NULL, //hasProperty,
69         NULL, //GetProperty,
70         NULL, //SetProperty,
71         NULL, //DeleteProperty,
72         NULL, //getPropertyNames,
73         NULL,
74         NULL,
75         NULL,
76         NULL, //ConvertToType,
77 };
78
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 },
90         { 0, 0, 0, 0 }
91 };
92
93
94 JSClassRef JSAccountServices::getClassRef()
95 {
96         if (!m_classRef) {
97                 m_classRef = JSClassCreate(&m_classInfo);
98         }
99         return m_classRef;
100 }
101
102 JSObjectRef JSAccountServices::createJSObject(JSContextRef context, AccountServicesPtr service)
103 {
104     AccountServicesPrivObj *priv = new AccountServicesPrivObj(context, service);
105     return JSObjectMake(context, getClassRef(), priv);
106 }
107
108 bool JSAccountServices::isObjectOfClass(JSContextRef context, JSValueRef value)
109 {
110         return JSValueIsObjectOfClass(context, value, getClassRef());
111 }
112
113 AccountServicesPtr JSAccountServices::getAccountServices(JSContextRef context, JSValueRef value)
114 {
115         if (!isObjectOfClass(context, value)) {
116                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
117         }
118
119         JSObjectRef object = JSValueToObject(context, value, NULL);
120         if (!object) {
121                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
122         }
123
124         AccountServicesPrivObj *priv = static_cast<AccountServicesPrivObj*>(JSObjectGetPrivate(object));
125         if (!priv) {
126                 Throw(WrtDeviceApis::Commons::NullPointerException);
127         }
128
129         return priv->getObject();
130 }
131
132 void JSAccountServices::Initialize(JSContextRef context, JSObjectRef object)
133 {
134         LogDebug("entered");
135 }
136
137 void JSAccountServices::Finalize(JSObjectRef object)
138 {
139     AccountServicesPrivObj *privateObject = static_cast<AccountServicesPrivObj*>(JSObjectGetPrivate(object));
140     delete privateObject;
141 }
142
143 AccountServicesPtr JSAccountServices::getPrivData(JSObjectRef object)
144 {
145         LogDebug("entered");
146         AccountServicesPrivObj *priv = static_cast<AccountServicesPrivObj*>(JSObjectGetPrivate(object));
147         if (!priv) {
148                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
149         }
150
151         AccountServicesPtr result = priv->getObject();
152         if (!result) {
153                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
154         }
155
156         return result;
157 }
158
159 JSValueRef JSAccountServices::getProperty(JSContextRef context,
160                 JSObjectRef object,
161                 JSStringRef propertyName,
162                 JSValueRef* exception)
163 {
164         LogDebug("<<<");
165         Try{
166                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
167                 AccountServicesPtr accountServicePtr = getPrivData(object);
168
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                         return converter->toJSValueRef(accountServicePtr->getSettings());
189                 }
190         }Catch(WrtDeviceApis::Commons::Exception){
191                 AccountLogWarning("trying to get incorrect value");
192         }
193
194         LogDebug(">>> return UNDEFINED");
195         return JSValueMakeUndefined(context);
196 }
197
198 bool JSAccountServices::setProperty(JSContextRef context,
199                 JSObjectRef object,
200                 JSStringRef propertyName,
201                 JSValueRef value,
202                 JSValueRef* exception)
203 {
204         LogDebug("entered");
205         Try
206         {
207                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
208                 AccountServicesPtr Properties = getPrivData(object);
209
210         if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_NAME)) {
211             std::string serviceName = converter->toString(value);
212             Properties->setName(serviceName);
213             return true;
214         } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_SERVICES_ATTR_SERVICE_SERVICE_TYPE_ID)) {
215             std::string serviceTypeId = converter->toString(value);
216             Properties->setServiceTypeId(serviceTypeId);
217             return true;
218         }
219
220                 return true;
221         }
222         Catch(WrtDeviceApis::Commons::Exception)
223         {
224                 LogWarning("trying to set incorrect value");
225         }
226
227         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
228         return false;
229 }
230
231 } // Account
232 } // Api
233 } // TizenApis
234