8e7d116cb9d41c7ae09e238568f2f600fd4b2a1c
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSContactAddress.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        JSContactAddress.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSContactAddress class
23  */
24
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include "ContactConverter.h"
29 #include "JSContactAddress.h"
30 #include <Logger.h>
31 #include <Export.h>
32
33 #define CONTACT_CLASS_NAME "ContactAddress"
34
35 #define CONTACT_ATTR_COUNTRY "country"
36 #define CONTACT_ATTR_REGION "region"
37 #define CONTACT_ATTR_CITY "city"
38 #define CONTACT_ATTR_STREET_ADDRESS "streetAddress"
39 #define CONTACT_ATTR_ADDITIONAL_INFORMATION "additionalInformation"
40 #define CONTACT_ATTR_POSTAL_CODE "postalCode"
41 #define CONTACT_ATTR_IS_DEFAULT "isDefault"
42 #define CONTACT_ATTR_TYPES "types"
43
44 namespace DeviceAPI {
45 namespace Contact {
46
47 using namespace DeviceAPI::Common;
48 using namespace WrtDeviceApis::Commons;
49 using namespace WrtDeviceApis::CommonsJavaScript;
50
51 JSClassDefinition JSContactAddress::m_classInfo =
52 {
53         0,
54         kJSClassAttributeNone,
55         CONTACT_CLASS_NAME,
56         NULL,
57         m_property,
58         m_functions,
59         Initialize,
60         Finalize,
61         NULL, //hasProperty,
62         NULL, //GetProperty,
63         NULL, //SetProperty,
64         NULL, //DeleteProperty,
65         NULL, //getPropertyNames,
66         NULL, //CallAsFunction,
67         constructor, //CallAsConstructor,
68         hasInstance, //HasInstance,
69         NULL, //ConvertToType,
70 };
71
72 JSStaticValue JSContactAddress::m_property[] = {
73         { CONTACT_ATTR_COUNTRY, getCountry, setCountry, kJSPropertyAttributeNone },
74         { CONTACT_ATTR_REGION, getRegion, setRegion, kJSPropertyAttributeNone },
75         { CONTACT_ATTR_CITY, getCity, setCity, kJSPropertyAttributeNone },
76         { CONTACT_ATTR_STREET_ADDRESS, getStreetAddress, setStreetAddress, kJSPropertyAttributeNone },
77         { CONTACT_ATTR_ADDITIONAL_INFORMATION, getAdditionalInformation, setAdditionalInformation, kJSPropertyAttributeNone },
78         { CONTACT_ATTR_POSTAL_CODE, getPostalCode, setPostalCode, kJSPropertyAttributeNone },
79         { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
80         { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
81         { 0, 0, 0, 0 }
82 };
83
84 JSStaticFunction JSContactAddress::m_functions[] =
85 {
86         { 0, 0, 0 }
87 };
88
89 JSClassRef JSContactAddress::m_classRef = JSClassCreate(&m_classInfo);
90
91 JSClassRef DLL_EXPORT JSContactAddress::getClassRef() {
92         if (!m_classRef) {
93                 m_classRef = JSClassCreate(&m_classInfo);
94         }
95         return m_classRef;
96 }
97
98 bool JSContactAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
99 {
100         return JSValueIsObjectOfClass(context, value, getClassRef());
101 }
102
103 ContactAddressPtr JSContactAddress::getContactAddress(JSContextRef context, JSValueRef value)
104 {
105         if (!isObjectOfClass(context, value)) {
106                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
107         }
108         JSObjectRef object = JSValueToObject(context, value, NULL);
109         if (!object) {
110                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
111         }
112         JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
113         if (!priv) {
114                 Throw(WrtDeviceApis::Commons::NullPointerException);
115         }
116         return priv->getObject();
117 }
118
119 void JSContactAddress::Initialize(JSContextRef context, JSObjectRef object)
120 {
121         if (!JSObjectGetPrivate(object))
122         {
123                 ContactAddressPtr address(new ContactAddress());
124                 JSContactAddressPriv *priv = new JSContactAddressPriv(context, ContactAddressPtr(address));
125                 if (!JSObjectSetPrivate(object, priv)) {
126                         delete priv;
127                 }
128         }
129 }
130
131 void JSContactAddress::Finalize(JSObjectRef object)
132 {
133         JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
134
135         if (priv != NULL)
136                 delete (priv);
137 }
138
139 JSObjectRef JSContactAddress::createJSObject(JSContextRef context, const ContactAddressPtr contactAddress)
140 {
141         JSContactAddressPriv *priv = new JSContactAddressPriv(context, contactAddress);
142         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
143         if (NULL == jsObjectRef) {
144                 LoggerE("object creation error");
145                 return NULL;
146         }
147         return jsObjectRef;
148 }
149
150 ContactAddressPtr JSContactAddress::getPrivData(JSObjectRef object)
151 {
152         JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
153         if (!priv) {
154                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155         }
156         ContactAddressPtr result = priv->getObject();
157         if (!result) {
158                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
159         }
160         return result;
161 }
162
163 JSObjectRef JSContactAddress::constructor(JSContextRef context,
164                 JSObjectRef constructor,
165                 size_t argumentCount,
166                 const JSValueRef arguments[],
167                 JSValueRef* exception)
168 {
169         bool js1stParamIsObject = false;
170
171         JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(constructor));
172         if (!priv) {
173                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
174         }
175         JSContextRef gContext = priv->getContext();
176
177         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
178         Try {
179                 if (argumentCount >= 1)
180                 {
181                         if (JSValueIsObject(gContext, arguments[0]))
182                                 js1stParamIsObject = true;
183
184                         if (!js1stParamIsObject &&
185                                         !JSValueIsNull(gContext, arguments[0]) &&
186                                         !JSValueIsUndefined(gContext, arguments[0]))
187                                 ThrowMsg(InvalidArgumentException, "1st argument must be a 'ContactAddressInit object'");
188                 }
189
190         } Catch(Exception ) {
191                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
192                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'ContactAddressInit object'");
193                 return NULL;
194         }
195
196         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
197
198         ContactAddressPtr contactEmailAddress(NULL);
199
200         Try {
201                 if(js1stParamIsObject)
202                         contactEmailAddress = converter->toContactAddressFromInit(arguments[0]);
203                 else
204                         contactEmailAddress = ContactAddressPtr(new ContactAddress());
205
206         } Catch(Exception) {
207                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
208                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'ContactAddressInit object'");
209                 return NULL;
210         }
211
212         JSObjectRef jsobject;
213
214         Try {
215                 jsobject = createJSObject(gContext, contactEmailAddress);
216         } Catch(Exception) {
217                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
218                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
219                 return NULL;
220         }
221
222         return jsobject;
223 }
224
225 bool JSContactAddress::hasInstance(JSContextRef context,
226                 JSObjectRef constructor,
227                 JSValueRef possibleInstance,
228                 JSValueRef* exception)
229 {
230         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
231 }
232
233 JSValueRef JSContactAddress::getCountry(JSContextRef context,
234                 JSObjectRef object,
235                 JSStringRef propertyName,
236                 JSValueRef* exception)
237 {
238         Try
239         {
240                 ContactConverterFactory::ConverterType converter =
241                                 ContactConverterFactory::getConverter(context);
242                 ContactAddressPtr contactAddress = getPrivData(object);
243                 if(!contactAddress->getCountryIsSet())
244                         return JSValueMakeNull(context);
245                 else
246                         return converter->toJSValueRef(contactAddress->getCountry());
247         }
248         Catch(WrtDeviceApis::Commons::Exception)
249         {
250                 LoggerW("trying to get incorrect value");
251         }
252         return JSValueMakeUndefined(context);
253 }
254
255 bool JSContactAddress::setCountry(JSContextRef context,
256                 JSObjectRef object,
257                 JSStringRef propertyName,
258                 JSValueRef value,
259                 JSValueRef* exception)
260 {
261         Try
262         {
263                 ContactAddressPtr contactAddress = getPrivData(object);
264                 ContactConverterFactory::ConverterType converter =
265                                 ContactConverterFactory::getConverter(context);
266                 BasicValidator validator =
267                                 BasicValidatorFactory::getValidator(context, exception);
268                 if(validator->isNullOrUndefined(value))
269                         contactAddress->unsetCountry();
270                 else
271                         contactAddress->setCountry(converter->toString(value));
272         }
273         Catch(WrtDeviceApis::Commons::Exception)
274         {
275                 LoggerW("trying to set incorrect value");
276                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
277         }
278         return true;
279 }
280
281
282 JSValueRef JSContactAddress::getRegion(JSContextRef context,
283                 JSObjectRef object,
284                 JSStringRef propertyName,
285                 JSValueRef* exception)
286 {
287         Try
288         {
289                 ContactConverterFactory::ConverterType converter =
290                                 ContactConverterFactory::getConverter(context);
291                 ContactAddressPtr contactAddress = getPrivData(object);
292                 if(!contactAddress->getRegionIsSet())
293                         return JSValueMakeNull(context);
294                 else
295                         return converter->toJSValueRef(contactAddress->getRegion());
296         }
297         Catch(WrtDeviceApis::Commons::Exception)
298         {
299                 LoggerW("trying to get incorrect value");
300         }
301         return JSValueMakeUndefined(context);
302 }
303
304 bool JSContactAddress::setRegion(JSContextRef context,
305                 JSObjectRef object,
306                 JSStringRef propertyName,
307                 JSValueRef value,
308                 JSValueRef* exception)
309 {
310         Try
311         {
312                 ContactAddressPtr contactAddress = getPrivData(object);
313                 ContactConverterFactory::ConverterType converter =
314                                 ContactConverterFactory::getConverter(context);
315                 BasicValidator validator =
316                                 BasicValidatorFactory::getValidator(context, exception);
317                 if(validator->isNullOrUndefined(value))
318                         contactAddress->unsetRegion();
319                 else
320                         contactAddress->setRegion(converter->toString(value));
321         }
322         Catch(WrtDeviceApis::Commons::Exception)
323         {
324                 LoggerW("trying to set incorrect value");
325                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
326         }
327         return true;
328 }
329
330 JSValueRef JSContactAddress::getCity(JSContextRef context,
331                 JSObjectRef object,
332                 JSStringRef propertyName,
333                 JSValueRef* exception)
334 {
335         Try
336         {
337                 ContactConverterFactory::ConverterType converter =
338                                 ContactConverterFactory::getConverter(context);
339                 ContactAddressPtr contactAddress = getPrivData(object);
340                 if(!contactAddress->getCityIsSet())
341                         return JSValueMakeNull(context);
342                 else
343                         return converter->toJSValueRef(contactAddress->getCity());
344         }
345         Catch(WrtDeviceApis::Commons::Exception)
346         {
347                 LoggerW("trying to get incorrect value");
348         }
349         return JSValueMakeUndefined(context);
350 }
351
352 bool JSContactAddress::setCity(JSContextRef context,
353                 JSObjectRef object,
354                 JSStringRef propertyName,
355                 JSValueRef value,
356                 JSValueRef* exception)
357 {
358         Try
359         {
360                 ContactAddressPtr contactAddress = getPrivData(object);
361                 ContactConverterFactory::ConverterType converter =
362                                 ContactConverterFactory::getConverter(context);
363                 BasicValidator validator =
364                                 BasicValidatorFactory::getValidator(context, exception);
365                 if(validator->isNullOrUndefined(value))
366                         contactAddress->unsetCity();
367                 else
368                         contactAddress->setCity(converter->toString(value));
369         }
370         Catch(WrtDeviceApis::Commons::Exception)
371         {
372                 LoggerW("trying to set incorrect value");
373                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
374         }
375         return true;
376 }
377
378
379 JSValueRef JSContactAddress::getStreetAddress(JSContextRef context,
380                 JSObjectRef object,
381                 JSStringRef propertyName,
382                 JSValueRef* exception)
383 {
384         Try
385         {
386                 ContactConverterFactory::ConverterType converter =
387                                 ContactConverterFactory::getConverter(context);
388                 ContactAddressPtr contactAddress = getPrivData(object);
389                 if(!contactAddress->getStreetAddressIsSet())
390                         return JSValueMakeNull(context);
391                 else
392                         return converter->toJSValueRef(contactAddress->getStreetAddress());
393         }
394         Catch(WrtDeviceApis::Commons::Exception)
395         {
396                 LoggerW("trying to get incorrect value");
397         }
398         return JSValueMakeUndefined(context);
399 }
400
401 bool JSContactAddress::setStreetAddress(JSContextRef context,
402                 JSObjectRef object,
403                 JSStringRef propertyName,
404                 JSValueRef value,
405                 JSValueRef* exception)
406 {
407         Try
408         {
409                 ContactAddressPtr contactAddress = getPrivData(object);
410                 ContactConverterFactory::ConverterType converter =
411                                 ContactConverterFactory::getConverter(context);
412                 BasicValidator validator =
413                                 BasicValidatorFactory::getValidator(context, exception);
414                 if(validator->isNullOrUndefined(value))
415                         contactAddress->unsetStreetAddress();
416                 else
417                         contactAddress->setStreetAddress(converter->toString(value));
418         }
419         Catch(WrtDeviceApis::Commons::Exception)
420         {
421                 LoggerW("trying to set incorrect value");
422                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
423         }
424         return true;
425 }
426
427
428 JSValueRef JSContactAddress::getAdditionalInformation(JSContextRef context,
429                 JSObjectRef object,
430                 JSStringRef propertyName,
431                 JSValueRef* exception)
432 {
433         Try
434         {
435                 ContactConverterFactory::ConverterType converter =
436                                 ContactConverterFactory::getConverter(context);
437                 ContactAddressPtr contactAddress = getPrivData(object);
438                 if(!contactAddress->getAdditionalInformationIsSet())
439                         return JSValueMakeNull(context);
440                 else
441                         return converter->toJSValueRef(contactAddress->getAdditionalInformation());
442         }
443         Catch(WrtDeviceApis::Commons::Exception)
444         {
445                 LoggerW("trying to get incorrect value");
446         }
447         return JSValueMakeUndefined(context);
448 }
449
450 bool JSContactAddress::setAdditionalInformation(JSContextRef context,
451                 JSObjectRef object,
452                 JSStringRef propertyName,
453                 JSValueRef value,
454                 JSValueRef* exception)
455 {
456         Try
457         {
458                 ContactAddressPtr contactAddress = getPrivData(object);
459                 ContactConverterFactory::ConverterType converter =
460                                 ContactConverterFactory::getConverter(context);
461                 BasicValidator validator =
462                                 BasicValidatorFactory::getValidator(context, exception);
463                 if(validator->isNullOrUndefined(value))
464                         contactAddress->unsetAdditionalInformation();
465                 else
466                         contactAddress->setAdditionalInformation(converter->toString(value));
467         }
468         Catch(WrtDeviceApis::Commons::Exception)
469         {
470                 LoggerW("trying to set incorrect value");
471                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
472         }
473         return true;
474 }
475
476
477 JSValueRef JSContactAddress::getPostalCode(JSContextRef context,
478                 JSObjectRef object,
479                 JSStringRef propertyName,
480                 JSValueRef* exception)
481 {
482         Try
483         {
484                 ContactConverterFactory::ConverterType converter =
485                                 ContactConverterFactory::getConverter(context);
486                 ContactAddressPtr contactAddress = getPrivData(object);
487                 if(!contactAddress->getPostalCodeIsSet())
488                         return JSValueMakeNull(context);
489                 else
490                         return converter->toJSValueRef(contactAddress->getPostalCode());
491         }
492         Catch(WrtDeviceApis::Commons::Exception)
493         {
494                 LoggerW("trying to get incorrect value");
495         }
496         return JSValueMakeUndefined(context);
497 }
498
499 bool JSContactAddress::setPostalCode(JSContextRef context,
500                 JSObjectRef object,
501                 JSStringRef propertyName,
502                 JSValueRef value,
503                 JSValueRef* exception)
504 {
505         Try
506         {
507                 ContactAddressPtr contactAddress = getPrivData(object);
508                 ContactConverterFactory::ConverterType converter =
509                                 ContactConverterFactory::getConverter(context);
510                 BasicValidator validator =
511                                 BasicValidatorFactory::getValidator(context, exception);
512                 if(validator->isNullOrUndefined(value))
513                         contactAddress->unsetPostalCode();
514                 else
515                         contactAddress->setPostalCode(converter->toString(value));
516         }
517         Catch(WrtDeviceApis::Commons::Exception)
518         {
519                 LoggerW("trying to set incorrect value");
520                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
521         }
522         return true;
523 }
524
525
526 JSValueRef JSContactAddress::getIsDefault(JSContextRef context,
527                 JSObjectRef object,
528                 JSStringRef propertyName,
529                 JSValueRef* exception)
530 {
531         Try
532         {
533                 ContactConverterFactory::ConverterType converter =
534                                 ContactConverterFactory::getConverter(context);
535                 ContactAddressPtr contactAddress = getPrivData(object);
536                 return converter->toJSValueRef(contactAddress->getIsDefault());
537         }
538         Catch(WrtDeviceApis::Commons::Exception)
539         {
540                 LoggerW("trying to get incorrect value");
541         }
542         return JSValueMakeUndefined(context);
543 }
544
545 bool JSContactAddress::setIsDefault(JSContextRef context,
546                 JSObjectRef object,
547                 JSStringRef propertyName,
548                 JSValueRef value,
549                 JSValueRef* exception)
550 {
551         Try
552         {
553                 ContactAddressPtr contactAddress = getPrivData(object);
554                 ContactConverterFactory::ConverterType converter =
555                                 ContactConverterFactory::getConverter(context);
556                 BasicValidator validator =
557                                 BasicValidatorFactory::getValidator(context, exception);
558                 contactAddress->setIsDefault(converter->toBool(value));
559         }
560         Catch(WrtDeviceApis::Commons::Exception)
561         {
562                 LoggerW("trying to set incorrect value");
563                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
564         }
565         return true;
566 }
567
568
569 JSValueRef JSContactAddress::getTypes(JSContextRef context,
570                 JSObjectRef object,
571                 JSStringRef propertyName,
572                 JSValueRef* exception)
573 {
574         Try
575         {
576                 JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
577                 if (!priv) {
578                         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
579                 }
580                 JSContextRef gContext = priv->getContext();
581                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
582                 ContactAddressPtr contactAddress = getPrivData(object);
583
584                 if(contactAddress->IsTypesSetJSArray()){
585                         return contactAddress->getTypesJSObj();
586                 }else{
587                         JSValueRef tempJSValue = contactAddress->getTypesJSArray();
588                         tempJSValue = converter->toJSValueRef(contactAddress->getTypes());
589
590                         JSObjectRef convertedJSObject = contactAddress->getTypesJSObj();
591                         convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
592                         contactAddress->setTypesJSArray(true, convertedJSObject);
593
594                         JSValueProtect(gContext, convertedJSObject);
595                         contactAddress->setContext(gContext);
596                         return tempJSValue;
597                 }
598         }
599         Catch(WrtDeviceApis::Commons::Exception)
600         {
601                 LoggerW("trying to get incorrect value");
602         }
603         return JSValueMakeUndefined(context);
604 }
605
606 bool JSContactAddress::setTypes(JSContextRef context,
607                 JSObjectRef object,
608                 JSStringRef propertyName,
609                 JSValueRef value,
610                 JSValueRef* exception)
611 {
612         Try
613         {
614                 ContactAddressPtr contactAddress = getPrivData(object);
615                 ContactConverterFactory::ConverterType converter =
616                                 ContactConverterFactory::getConverter(context);
617
618                 contactAddress->setTypes(converter->toContactAddressTypeArray(value));
619                 contactAddress->resetTypesJSObj();
620         }
621         Catch(WrtDeviceApis::Commons::Exception)
622         {
623                 LoggerW("trying to set incorrect value");
624                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
625         }
626         return true;
627 }
628
629 } // Contact
630 } // DeviceAPI