Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Account / JSAccount.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 /*
19  * @file        JSAccount.cpp
20  *
21  * @version     0.1
22  */
23
24 #include "JSAccount.h"
25 #include "JSFeatureArray.h"
26 #include "JSAccountServices.h"
27 #include <CommonsJavaScript/Converter.h>
28 #include <CommonsJavaScript/JSUtils.h>
29
30 #include "AccountConverter.h"
31 #include "plugin_config.h"
32
33 #include <Tizen/Common/JSTizenException.h>
34 #include <Tizen/Common/JSTizenExceptionFactory.h>
35 #include <Tizen/Common/SecurityExceptions.h>
36
37 using namespace TizenApis::Api::Account;
38 using namespace WrtDeviceApis::Commons;
39 using namespace WrtDeviceApis::CommonsJavaScript;
40 using namespace TizenApis::Commons;
41
42 namespace TizenApis {
43 namespace Tizen1_0 {
44 namespace Account{
45
46 namespace {
47
48 #define ACCOUNT_ATTRIBUTE_NAME  "Account"
49
50 #define ACCOUNT_ATTRIBUTE_ACCOUNT_ID  "id"
51 #define ACCOUNT_ATTRIBUTE_DISPLAY_NAME  "displayName"
52 #define ACCOUNT_ATTRIBUTE_ICON_PATH  "icon"
53 #define ACCOUNT_ATTRIBUTE_ENABLED  "enabled"
54 #define ACCOUNT_ATTRIBUTE_PROVIDER_ID  "providerId"
55 #define ACCOUNT_ATTRIBUTE_CREDENTIAL_ID  "credentialId"
56 #define ACCOUNT_ATTRIBUTE_SERVICES  "services"
57 #define ACCOUNT_ATTRIBUTE_SETTINGS  "settings"
58
59 }
60
61 JSClassDefinition JSAccount::m_classInfo = {
62     0,
63     kJSClassAttributeNone,
64     ACCOUNT_ATTRIBUTE_NAME,
65     0,
66     m_property,
67     NULL, //m_function,
68     initialize,
69     finalize,
70     NULL, //hasProperty,
71     NULL, //getProperty,
72     NULL, //setProperty,
73     NULL, //deleteProperty,
74     NULL, //getPropertyNames,
75     NULL, //callAsFunction,
76     constructor, //callAsConstructor,
77     hasInstance, //hasInstance,
78     NULL, //convertToType,
79 };
80
81 JSStaticValue JSAccount::m_property[] = {
82     { ACCOUNT_ATTRIBUTE_ACCOUNT_ID,     getProperty, setProperty, kJSPropertyAttributeNone },
83     { ACCOUNT_ATTRIBUTE_DISPLAY_NAME,   getProperty, setProperty, kJSPropertyAttributeNone },
84     { ACCOUNT_ATTRIBUTE_ICON_PATH,      getProperty, setProperty, kJSPropertyAttributeNone },
85     { ACCOUNT_ATTRIBUTE_ENABLED,        getProperty, setProperty, kJSPropertyAttributeNone },
86     { ACCOUNT_ATTRIBUTE_PROVIDER_ID,    getProperty, setProperty, kJSPropertyAttributeNone },
87     { ACCOUNT_ATTRIBUTE_CREDENTIAL_ID,  getProperty, setProperty, kJSPropertyAttributeNone },
88     { ACCOUNT_ATTRIBUTE_SERVICES,       getServices, setServices, kJSPropertyAttributeNone },
89     { ACCOUNT_ATTRIBUTE_SETTINGS,       getProperty, setProperty, kJSPropertyAttributeNone },
90     { 0, 0, 0, 0 }
91 };
92
93 JSClassRef JSAccount::m_jsClassRef = JSClassCreate(
94         JSAccount::getClassInfo());
95
96 const JSClassDefinition* JSAccount::getClassInfo()
97 {
98     return &(m_classInfo);
99 }
100
101 JSClassRef JSAccount::getClassRef()
102 {
103     if (!m_jsClassRef) {
104         m_jsClassRef = JSClassCreate(&m_classInfo);
105     }
106     return m_jsClassRef;
107 }
108
109 EventAccountPtr JSAccount::getIEvent(JSObjectRef object)
110 {
111     LogDebug("entered");
112     AccountPrivateObject *priv =
113         static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
114     if (!priv) {
115         ThrowMsg(NullPointerException, "Private object is null");
116     }
117     EventAccountPtr result = priv->getObject();
118     if (!result) {
119         ThrowMsg(NullPointerException, "Private object is null");
120     }
121     return result;
122 }
123
124 void JSAccount::setIEvent(const EventAccountPtr &event,
125         JSContextRef ctx,
126         const JSObjectRef object)
127 {
128     LogDebug("entered");
129     Try
130     {
131         AccountPrivateObject *priv =
132             static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
133         delete priv;
134         priv = new AccountPrivateObject(ctx, event);
135         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
136             delete priv;
137         }
138     }
139     Catch(Exception)
140     {
141         AccountLogError("Error during replacing account object");
142     }
143 }
144
145
146 bool JSAccount::validate(JSContextRef ctx,
147         const JSObjectRef object,
148         JSValueRef* exception)
149 {
150     LogDebug("entered");
151     AccountPrivateObject *priv =
152         static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
153     if (priv == NULL) {
154         return false;
155     }
156     EventAccountPtr account = priv->getObject();
157     if (!account) {
158         return false;
159     }
160     return account->validate();
161 }
162
163
164 JSObjectRef JSAccount::createJSAccount(JSContextRef context, EventAccountPtr account)
165 {
166         LogDebug("<<< ");
167         AccountPrivateObject *priv = new AccountPrivateObject(context, account);
168         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
169         return jsValueRef;
170 }
171
172 void JSAccount::initialize(JSContextRef context, JSObjectRef object)
173 {
174         LogDebug("<<< ");
175
176         AccountPrivateObject *privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
177         if (NULL == privateObject) {
178                 AccountLogWarning("privateObject is NULL");
179                 EventAccountPtr accountPtr(new EventAccount());
180                 AccountPrivateObject *priv = new AccountPrivateObject(context, accountPtr);
181                 if (!JSObjectSetPrivate(object, priv)) {
182                         delete priv;
183                 }
184         }
185 }
186
187 void JSAccount::finalize(JSObjectRef object)
188 {
189     LogDebug("enter");
190     AccountPrivateObject* priv =
191         static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
192     delete priv;
193     JSObjectSetPrivate(object, NULL);
194 }
195
196
197 JSValueRef JSAccount::getProperty(JSContextRef context,
198         JSObjectRef object,
199         JSStringRef propertyName,
200         JSValueRef* exception)
201 {
202         LogDebug("<<<");
203         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
204         Try{
205                 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
206                 if (!priv) {
207                         Throw(NullPointerException);
208                 }
209                 EventAccountPtr account = priv->getObject();
210
211                 if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ACCOUNT_ID)) {
212                         return converter->toJSValueRef(account->getAccountId());
213                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_DISPLAY_NAME)) {
214                         return converter->toJSValueRef(account->getDisplayName());
215                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ICON_PATH)) {
216                         return converter->toJSValueRef(account->getIconPath());
217                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ENABLED)) {
218                         return converter->toJSValueRef(account->getEnabled());
219                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_PROVIDER_ID)) {
220                         return converter->toJSValueRef(account->getProviderId());
221                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_CREDENTIAL_ID)) {
222                         return converter->toJSValueRef(account->getCredentailId());
223                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_SETTINGS)) {
224                         std::string settingsString = account->getSettings();
225                         LogDebug("settingsString:[" << settingsString << "]");
226                         return converter->toJSONString(settingsString);
227                 }
228         }Catch(Exception){
229                 AccountLogError("invalid property");
230         }
231
232         return JSValueMakeUndefined(context);
233 }
234
235 bool JSAccount::setProperty(JSContextRef context,
236         JSObjectRef object,
237         JSStringRef propertyName,
238         JSValueRef value,
239         JSValueRef* exception)
240 {
241         LogDebug("<<<");
242         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
243         Try{
244                 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
245                 if (!priv) {
246                         Throw(NullPointerException);
247                 }
248                 EventAccountPtr account = priv->getObject();
249                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
250
251                 if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_DISPLAY_NAME)) {
252                         std::string displayname = converter->toString(value);
253                         account->setDisplayName(displayname);
254                         return true;
255                 } else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ICON_PATH)) {
256                         std::string iconPath = converter->toString(value);
257                         account->setIconPath(iconPath);
258                         return true;
259                 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_ENABLED)) {
260                         bool enabled = converter->toBool(value);
261                         account->setEnabled(enabled);
262                         return true;
263                 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_CREDENTIAL_ID)) {
264                         std::string credentialId = converter->toString(value);
265                         account->setCredentailId(credentialId);
266                         return true;
267                 }else if (JSStringIsEqualToUTF8CString(propertyName, ACCOUNT_ATTRIBUTE_SETTINGS)) {
268                         JSStringRef jsonStringRef = JSValueCreateJSONString(context, value, 0, exception);
269
270                         std::string settingString = converter->toString(jsonStringRef);
271                         account->setSettings(settingString);
272                         return true;
273                 }
274         }Catch(Exception){
275         AccountLogWarning("trying to set incorrect value");
276 }
277         JSTizenExceptionFactory::postException(context, exception,
278                        JSTizenException::TYPE_MISMATCH_ERROR, "Trying to set incorrect value");
279         return false;
280 }
281
282 JSValueRef JSAccount::getServices(JSContextRef context,
283         JSObjectRef object,
284         JSStringRef propertyName,
285         JSValueRef* exception)
286 {
287         LogDebug("<<<");
288
289         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
290         Try{
291                 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
292                 if (!priv) {
293                         Throw(NullPointerException);
294                 }
295                 EventAccountPtr account = priv->getObject();
296
297                 AccountServicesArrayPtr services = account->getService();
298                 if (services) {
299                         JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
300                         if (NULL == jsResult) {
301                                 ThrowMsg(NullPointerException, "Could not create js array object");
302                         }
303                         for(unsigned int i = 0; i < services->size(); i++) {
304                                 if (!JSSetArrayElement(context, jsResult, i, JSAccountServices::createJSObject(context, services->at(i)))) {
305                                         ThrowMsg(UnknownException, "Could not insert value into js array");
306                                 }
307                         }
308
309                         LogDebug(">>> jsResult");
310                         return jsResult;
311                 }
312         }       Catch(Exception)        {
313                 AccountLogWarning("trying to get incorrect value");
314         }
315
316         LogDebug(">>> undefined");
317         return JSValueMakeUndefined(context);
318 }
319
320 bool JSAccount::setServices(JSContextRef context,
321         JSObjectRef object,
322         JSStringRef propertyName,
323         JSValueRef value,
324         JSValueRef* exception)
325 {
326         LogDebug("<<<");
327
328         AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
329         Try{
330                 AccountPrivateObject* priv = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
331                 if (!priv) {
332                         Throw(NullPointerException);
333                 }
334                 EventAccountPtr account = priv->getObject();
335                 account->setService(converter->toAccountServiceArray(value));
336                 return true;
337         }Catch(Exception){
338                 AccountLogWarning("trying to set incorrect value");
339         }
340
341         JSTizenExceptionFactory::postException(context, exception,
342                JSTizenException::TYPE_MISMATCH_ERROR, "Trying to set incorrect value");
343         return false;
344 }
345
346 bool JSAccount::hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleInstance, JSValueRef* exception) {
347         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
348 }
349
350 JSObjectRef JSAccount::constructor(JSContextRef context,
351                 JSObjectRef object,
352                 size_t argumentCount,
353                 const JSValueRef arguments[],
354                 JSValueRef* exception)
355 {
356         LogDebug("<<<");
357         AccountPrivateObject* privateObject = static_cast<AccountPrivateObject*>(JSObjectGetPrivate(object));
358         if (!privateObject) {
359                 AccountLogError(">>> NULL POINTER EXCEPTION !");
360                 Throw(NullPointerException);
361         }
362
363         if (argumentCount > 2) {
364                 AccountLogError("Wrong argument count:" << argumentCount);
365                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "Wrong argument count");
366         }
367
368         Try{
369                 AccountConverterFactory::ConverterType converter = AccountConverterFactory::getConverter(context);
370
371                 std::string accountServiceProviderId;
372
373                 if (JSValueIsUndefined(context, arguments[0]) || JSValueIsNull(context, arguments[0]) || !JSValueIsString(context, arguments[0])){
374                         AccountLogError(">>> invalid value argument 0");
375                         JSTizenExceptionFactory::postException(context, exception, JSTizenException::INVALID_VALUES_ERROR, "invalid value argument 0");
376                 }
377
378                 accountServiceProviderId = converter->toString(arguments[0]);
379                 LogDebug("accountServiceProviderId:[" << accountServiceProviderId << "]");
380
381                 EventAccountPtr account = converter->toAccount(accountServiceProviderId);
382
383                 if(argumentCount == 2){
384                         JSValueRef displayNameRef = JSUtils::getJSProperty(context, arguments[1], "displayName");
385                         if(displayNameRef != NULL){
386                                 std::string displayName = converter->toString(displayNameRef);
387                                 LogDebug("displayName:[" << displayName << "]");
388                                 account->setDisplayName(displayName);
389                         }
390
391                         JSValueRef iconPathRef = JSUtils::getJSProperty(context, arguments[1], "iconPath");
392                         if(iconPathRef != NULL){
393                                 std::string iconPath = converter->toString(iconPathRef);
394                                 LogDebug("iconPath:[" << iconPath << "]");
395                                 account->setIconPath(iconPath);
396                         }
397                 }
398
399                 LogDebug(">>>");
400                 return JSAccount::createJSAccount(context, account);
401         }Catch(Exception){
402                 AccountLogError("UNKNOWN EXCEPTION");
403                 JSTizenExceptionFactory::postException(context, exception,
404                                 JSTizenException::UNKNOWN_ERROR, "UNKNOWN_ERROR");
405         }
406
407         AccountLogWarning(">>> return NULL");
408         return NULL;
409 }
410
411 }
412 }
413 }