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