upload tizen1.0 source
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactAccount.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        JSContactAccount.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSContactAccount class
22  */
23
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"
30
31 #define ORGANIZATION_CLASS_NAME "ContactAccount"
32
33 #define CONTACT_ATTR_ACCOUNT_SERVICE_ID "accountServiceId"
34 #define CONTACT_ATTR_CONTACT_URI "contactURI"
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38 namespace Contact {
39
40 using namespace TizenApis::Commons;
41 using namespace TizenApis::Api::Contact;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
44
45 JSClassDefinition JSContactAccount::m_classInfo =
46 {
47         0,
48         kJSClassAttributeNone,
49         ORGANIZATION_CLASS_NAME,
50         NULL,
51         m_property,
52         m_functions,
53         Initialize,
54         Finalize,
55         NULL, //hasProperty,
56         NULL, //GetProperty,
57         NULL, //SetProperty,
58         NULL, //DeleteProperty,
59         NULL, //getPropertyNames,
60         NULL, //CallAsFunction,
61         constructor, //CallAsConstructor,
62         hasInstance, //HasInstance,
63         NULL, //ConvertToType,
64 };
65
66 JSStaticValue JSContactAccount::m_property[] = {
67         { CONTACT_ATTR_ACCOUNT_SERVICE_ID, getAccountServiceId, setAccountServiceId, kJSPropertyAttributeNone },
68         { CONTACT_ATTR_CONTACT_URI, getContactURI, setContactURI, kJSPropertyAttributeNone },
69         { 0, 0, 0, 0 }
70 };
71
72 JSStaticFunction JSContactAccount::m_functions[] =
73 {
74         { 0, 0, 0 }
75 };
76
77 JSClassRef JSContactAccount::m_classRef = JSClassCreate(&m_classInfo);
78
79 JSClassRef JSContactAccount::getClassRef() {
80         if (!m_classRef) {
81                 m_classRef = JSClassCreate(&m_classInfo);
82         }
83         return m_classRef;
84 }
85
86 bool JSContactAccount::isObjectOfClass(JSContextRef context, JSValueRef value)
87 {
88         return JSValueIsObjectOfClass(context, value, getClassRef());
89 }
90
91 ContactAccountPtr JSContactAccount::getContactAccount(JSContextRef context, JSValueRef value)
92 {
93         if (!isObjectOfClass(context, value)) {
94                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
95         }
96         JSObjectRef object = JSValueToObject(context, value, NULL);
97         if (!object) {
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99         }
100         JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(object));
101         if (!priv) {
102                 Throw(WrtDeviceApis::Commons::NullPointerException);
103         }
104         return priv->getObject();
105 }
106
107 void JSContactAccount::Initialize(JSContextRef context, JSObjectRef object)
108 {
109         if (!JSObjectGetPrivate(object))
110         {
111                 ContactAccountPtr account(new ContactAccount());
112                 JSContactAccountPriv *priv = new JSContactAccountPriv(context, ContactAccountPtr(account));
113                 if (!JSObjectSetPrivate(object, priv)) {
114                         delete priv;
115                 }
116         }
117 }
118
119 void JSContactAccount::Finalize(JSObjectRef object)
120 {
121         JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(object));
122
123         if (priv != NULL)
124                 delete (priv);
125 }
126
127 JSObjectRef JSContactAccount::createJSObject(JSContextRef context,  const ContactAccountPtr contactAccount)
128 {
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");
133                 return NULL;
134         }
135         return jsObjectRef;
136 }
137
138 ContactAccountPtr JSContactAccount::getPrivData(JSObjectRef object)
139 {
140         JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(object));
141         if (!priv) {
142                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
143         }
144         ContactAccountPtr result = priv->getObject();
145         if (!result) {
146                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147         }
148         return result;
149 }
150
151 JSObjectRef JSContactAccount::constructor(JSContextRef context,
152                 JSObjectRef constructor,
153                 size_t argumentCount,
154                 const JSValueRef arguments[],
155                 JSValueRef* exception)
156 {
157         LogDebug("entered");
158
159         bool js2ndParamIsString = false;
160
161 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(CONTACT_FUNCTION_API_ADD);
162 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
163
164         JSContactAccountPriv *priv = static_cast<JSContactAccountPriv*>(JSObjectGetPrivate(constructor));
165         if (!priv) {
166                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
167         }
168         JSContextRef gContext = priv->getContext();
169
170         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
171         if (argumentCount >= 2)
172         {
173                 if (!JSValueIsNull(gContext, arguments[1]) && !JSValueIsUndefined(gContext, arguments[1]))
174                         js2ndParamIsString = true;
175         }
176
177         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
178
179         std::string accountServiceId;
180         std::string contactURI;
181
182         Try {
183                 if (argumentCount >= 1)
184                         accountServiceId = converter->toString(arguments[0]);
185                 else
186                         accountServiceId = converter->toString(JSValueMakeUndefined(context));
187
188         } Catch(Exception) {
189                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
190                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
191                 return NULL;
192         }
193
194         Try {
195                 if(js2ndParamIsString)
196                         contactURI = converter->toString(arguments[1]);
197
198         } Catch(Exception) {
199                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
200                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
201                 return NULL;
202         }
203
204         ContactAccountPtr contactAccount(new ContactAccount());
205         contactAccount->setAccountServiceId(accountServiceId);
206         contactAccount->setContactURI(contactURI);
207
208         JSObjectRef jsobject;
209
210         Try {
211                 jsobject = createJSObject(gContext, contactAccount);
212         } Catch(Exception) {
213                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
214                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
215                 return NULL;
216         }
217
218         return jsobject;
219 }
220
221 bool JSContactAccount::hasInstance(JSContextRef context,
222                 JSObjectRef constructor,
223                 JSValueRef possibleInstance,
224                 JSValueRef* exception)
225 {
226         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
227 }
228
229 JSValueRef JSContactAccount::getAccountServiceId(JSContextRef context,
230                 JSObjectRef object,
231                 JSStringRef propertyAccountId,
232                 JSValueRef* exception)
233 {
234         Try
235         {
236                 ContactConverterFactory::ConverterType converter =
237                                 ContactConverterFactory::getConverter(context);
238                 ContactAccountPtr organization = getPrivData(object);
239                 return converter->toJSValueRef(organization->getAccountServiceId());
240         }
241         Catch(WrtDeviceApis::Commons::Exception)
242         {
243                 LogWarning("trying to get incorrect value");
244         }
245         return JSValueMakeUndefined(context);
246 }
247
248 bool JSContactAccount::setAccountServiceId(JSContextRef context,
249                 JSObjectRef object,
250                 JSStringRef propertyAccountId,
251                 JSValueRef value,
252                 JSValueRef* exception)
253 {
254         Try
255         {
256                 ContactAccountPtr organization = getPrivData(object);
257                 ContactConverterFactory::ConverterType converter =
258                                 ContactConverterFactory::getConverter(context);
259                 organization->setAccountServiceId(converter->toString(value));
260                 return true;
261         }
262         Catch(WrtDeviceApis::Commons::Exception)
263         {
264                 LogWarning("trying to set incorrect value");
265         }
266         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
267         return false;
268 }
269
270 JSValueRef JSContactAccount::getContactURI(JSContextRef context,
271                 JSObjectRef object,
272                 JSStringRef propertyName,
273                 JSValueRef* exception)
274 {
275         Try
276         {
277                 ContactConverterFactory::ConverterType converter =
278                                 ContactConverterFactory::getConverter(context);
279                 ContactAccountPtr organization = getPrivData(object);
280                 return converter->toJSValueRef(organization->getContactURI());
281         }
282         Catch(WrtDeviceApis::Commons::Exception)
283         {
284                 LogWarning("trying to get incorrect value");
285         }
286         return JSValueMakeUndefined(context);
287 }
288
289 bool JSContactAccount::setContactURI(JSContextRef context,
290                 JSObjectRef object,
291                 JSStringRef propertyName,
292                 JSValueRef value,
293                 JSValueRef* exception)
294 {
295         Try
296         {
297                 ContactAccountPtr organization = getPrivData(object);
298                 ContactConverterFactory::ConverterType converter =
299                                 ContactConverterFactory::getConverter(context);
300                 organization->setContactURI(converter->toString(value));
301                 return true;
302         }
303         Catch(WrtDeviceApis::Commons::Exception)
304         {
305                 LogWarning("trying to set incorrect value");
306         }
307         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
308         return false;
309 }
310
311 } // Contact
312 } // Tizen1_0
313 } // TizenApis