362b56676ef42db06c637b23d21445fdaeb0ec96
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSContactEmailAddress.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        JSContactEmailAddress.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSContactEmailAddress class
23  */
24
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include "ContactConverter.h"
29 #include "JSContactEmailAddress.h"
30 #include <Logger.h>
31 #include <Export.h>
32
33 #define CONTACT_CLASS_NAME "EmailAddress"
34
35 #define CONTACT_ATTR_EMAIL "email"
36 #define CONTACT_ATTR_TYPES "types"
37 #define CONTACT_ATTR_IS_DEFAULT "isDefault"
38
39 namespace DeviceAPI {
40 namespace Contact {
41
42 using namespace DeviceAPI::Common;
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         { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
71         { 0, 0, 0, 0 }
72 };
73
74 JSStaticFunction JSContactEmailAddress::m_functions[] =
75 {
76         { 0, 0, 0 }
77 };
78
79 JSClassRef JSContactEmailAddress::m_classRef = JSClassCreate(&m_classInfo);
80
81 JSClassRef DLL_EXPORT JSContactEmailAddress::getClassRef() {
82         if (!m_classRef) {
83                 m_classRef = JSClassCreate(&m_classInfo);
84         }
85         return m_classRef;
86 }
87
88 bool JSContactEmailAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
89 {
90         return JSValueIsObjectOfClass(context, value, getClassRef());
91 }
92
93 ContactEmailAddressPtr JSContactEmailAddress::getContactEmailAddress(JSContextRef context, JSValueRef value)
94 {
95         if (!isObjectOfClass(context, value)) {
96                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
97         }
98         JSObjectRef object = JSValueToObject(context, value, NULL);
99         if (!object) {
100                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101         }
102         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
103         if (!priv) {
104                 Throw(WrtDeviceApis::Commons::NullPointerException);
105         }
106         return priv->getObject();
107 }
108
109 void JSContactEmailAddress::Initialize(JSContextRef context, JSObjectRef object)
110 {
111         if (!JSObjectGetPrivate(object))
112         {
113                 ContactEmailAddressPtr emailAddress(new ContactEmailAddress());
114                 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, ContactEmailAddressPtr(emailAddress));
115                 if (!JSObjectSetPrivate(object, priv)) {
116                         delete priv;
117                 }
118         }
119 }
120
121 void JSContactEmailAddress::Finalize(JSObjectRef object)
122 {
123         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
124
125         if (priv != NULL)
126                 delete (priv);
127 }
128
129 JSObjectRef JSContactEmailAddress::createJSObject(JSContextRef context,
130                 const ContactEmailAddressPtr emailAddress)
131 {
132         JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, emailAddress);
133         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
134         if (NULL == jsObjectRef) {
135                 LoggerE("object creation error");
136                 return NULL;
137         }
138         return jsObjectRef;
139 }
140
141 ContactEmailAddressPtr JSContactEmailAddress::getPrivData(JSObjectRef object)
142 {
143         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
144         if (!priv) {
145                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
146         }
147         ContactEmailAddressPtr result = priv->getObject();
148         if (!result) {
149                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
150         }
151         return result;
152 }
153
154 JSObjectRef JSContactEmailAddress::constructor(JSContextRef context,
155                 JSObjectRef constructor,
156                 size_t argumentCount,
157                 const JSValueRef arguments[],
158                 JSValueRef* exception)
159 {
160         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(constructor));
161         if (!priv) {
162                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
163         }
164         JSContextRef gContext = priv->getContext();
165
166         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
167
168         std::string email;
169         ContactEmailAddressTypeArrayPtr types(NULL);
170         bool isDefault = false;
171
172         try {
173                 ArgumentValidator Argvalidator(context, argumentCount, arguments);
174                 email = Argvalidator.toString(0, false);
175
176                 if (argumentCount >= 2){
177                         Argvalidator.toArrayObject(1, true);
178                         types = converter->toContactEmailAddressTypeArray(arguments[1]);
179                 }else
180                         types = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
181
182                 if(types->size() == 0)
183                         types->push_back(CONTACT_EMAIL_TYPE_WORK);
184
185                 if (argumentCount >= 3)
186                         isDefault = Argvalidator.toBool(2, false);
187
188         } catch (const TypeMismatchException& err ) {
189                 JSWebAPIErrorFactory::postException(context, exception, err);
190                 return NULL;
191         } catch(const BasePlatformException& err) {
192                 JSWebAPIErrorFactory::postException(context, exception, err);
193                 return NULL;
194         } catch(const ConversionException& err) {
195                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
196                 return NULL;
197         } catch(const NullPointerException& err) {
198                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
199                 return NULL;
200         }
201
202         ContactEmailAddressPtr contactEmailAddress(new ContactEmailAddress());
203         contactEmailAddress->setEmail(email);
204         contactEmailAddress->setTypes(types);
205         contactEmailAddress->setIsDefault(isDefault);
206
207         JSObjectRef jsobject;
208
209         Try {
210                 jsobject = createJSObject(gContext, contactEmailAddress);
211         } Catch(Exception) {
212                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
213                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
214                 return NULL;
215         }
216
217         return jsobject;
218 }
219
220 bool JSContactEmailAddress::hasInstance(JSContextRef context,
221                 JSObjectRef constructor,
222                 JSValueRef possibleInstance,
223                 JSValueRef* exception)
224 {
225         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
226 }
227
228 JSValueRef JSContactEmailAddress::getEmail(JSContextRef context,
229                 JSObjectRef object,
230                 JSStringRef propertyName,
231                 JSValueRef* exception)
232 {
233         Try
234         {
235                 ContactConverterFactory::ConverterType converter =
236                                 ContactConverterFactory::getConverter(context);
237                 ContactEmailAddressPtr emailAddress = getPrivData(object);
238                 return converter->toJSValueRef(emailAddress->getEmail());
239         }
240         Catch(WrtDeviceApis::Commons::Exception)
241         {
242                 LoggerW("trying to get incorrect value");
243         }
244         return JSValueMakeUndefined(context);
245 }
246
247 bool JSContactEmailAddress::setEmail(JSContextRef context,
248                 JSObjectRef object,
249                 JSStringRef propertyName,
250                 JSValueRef value,
251                 JSValueRef* exception)
252 {
253         Try
254         {
255                 ContactEmailAddressPtr emailAddress = getPrivData(object);
256                 ContactConverterFactory::ConverterType converter =
257                                 ContactConverterFactory::getConverter(context);
258                 emailAddress->setEmail(converter->toString(value));
259         }
260         Catch(WrtDeviceApis::Commons::Exception)
261         {
262                 LoggerW("trying to set incorrect value");
263                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
264         }
265         return true;
266 }
267
268
269 JSValueRef JSContactEmailAddress::getIsDefault(JSContextRef context,
270                 JSObjectRef object,
271                 JSStringRef propertyName,
272                 JSValueRef* exception)
273 {
274         Try
275         {
276                 ContactConverterFactory::ConverterType converter =
277                                 ContactConverterFactory::getConverter(context);
278                 ContactEmailAddressPtr emailAddress = getPrivData(object);
279                 return converter->toJSValueRef(emailAddress->getIsDefault());
280         }
281         Catch(WrtDeviceApis::Commons::Exception)
282         {
283                 LoggerW("trying to get incorrect value");
284         }
285         return JSValueMakeUndefined(context);
286 }
287
288 bool JSContactEmailAddress::setIsDefault(JSContextRef context,
289                 JSObjectRef object,
290                 JSStringRef propertyName,
291                 JSValueRef value,
292                 JSValueRef* exception)
293 {
294         Try
295         {
296                 ContactEmailAddressPtr emailAddress = getPrivData(object);
297                 ContactConverterFactory::ConverterType converter =
298                                 ContactConverterFactory::getConverter(context);
299                 BasicValidator validator =
300                                 BasicValidatorFactory::getValidator(context, exception);
301                 emailAddress->setIsDefault(converter->toBool(value));
302         }
303         Catch(WrtDeviceApis::Commons::Exception)
304         {
305                 LoggerW("trying to set incorrect value");
306                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
307         }
308         return true;
309 }
310
311
312 JSValueRef JSContactEmailAddress::getTypes(JSContextRef context,
313                 JSObjectRef object,
314                 JSStringRef propertyName,
315                 JSValueRef* exception)
316 {
317         Try
318         {
319                 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
320                 if (!priv) {
321                         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
322                 }
323                 JSContextRef gContext = priv->getContext();
324
325                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
326                 ContactEmailAddressPtr emailAddress = getPrivData(object);
327
328                 if(emailAddress->IsTypesSetJSArray()){
329                         return emailAddress->getTypesJSObj();
330                 }else{
331                         JSValueRef tempJSValue = emailAddress->getTypesJSArray();
332                         tempJSValue = converter->toJSValueRef(emailAddress->getTypes());
333
334                         JSObjectRef convertedJSObject = emailAddress->getTypesJSObj();
335                         convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
336                         emailAddress->setTypesJSArray(true, convertedJSObject);
337
338                         JSValueProtect(gContext, convertedJSObject);
339                         emailAddress->setContext(gContext);
340                         return tempJSValue;
341                 }
342         }
343         Catch(WrtDeviceApis::Commons::Exception)
344         {
345                 LoggerW("trying to get incorrect value");
346         }
347         return JSValueMakeUndefined(context);
348 }
349
350 bool JSContactEmailAddress::setTypes(JSContextRef context,
351                 JSObjectRef object,
352                 JSStringRef propertyName,
353                 JSValueRef value,
354                 JSValueRef* exception)
355 {
356         Try
357         {
358                 ContactEmailAddressPtr emailAddress = getPrivData(object);
359                 ContactConverterFactory::ConverterType converter =
360                                 ContactConverterFactory::getConverter(context);
361
362                 emailAddress->setTypes(converter->toContactEmailAddressTypeArray(value));
363                 emailAddress->resetTypesJSObj();
364         }
365         Catch(WrtDeviceApis::Commons::Exception)
366         {
367                 LoggerW("trying to set incorrect value");
368                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
369         }
370         return true;
371 }
372
373 } // Contact
374 } // DeviceAPI