Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactAddress.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        JSContactAddress.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSContactAddress class
22  */
23
24 #include <dpl/shared_ptr.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "JSContactAddress.h"
30 #include "JSContactAddressTypeArray.h"
31
32 #define CONTACT_CLASS_NAME "ContactAddress"
33
34 #define CONTACT_ATTR_COUNTRY "country"
35 #define CONTACT_ATTR_REGION "region"
36 #define CONTACT_ATTR_CITY "city"
37 #define CONTACT_ATTR_STREET_ADDRESS "streetAddress"
38 #define CONTACT_ATTR_ADDITIONAL_INFORMATION "additionalInformation"
39 #define CONTACT_ATTR_POSTAL_CODE "postalCode"
40 #define CONTACT_ATTR_TYPES "types"
41
42 namespace TizenApis {
43 namespace Tizen1_0 {
44 namespace Contact {
45
46 using namespace TizenApis::Commons;
47 using namespace TizenApis::Api::Contact;
48
49 JSClassRef JSContactAddress::m_classRef = NULL;
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,
67         NULL,
68         NULL,
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_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
80         { 0, 0, 0, 0 }
81 };
82
83 JSStaticFunction JSContactAddress::m_functions[] =
84 {
85         { 0, 0, 0 }
86 };
87
88 JSClassRef JSContactAddress::getClassRef() {
89         if (!m_classRef) {
90                 m_classRef = JSClassCreate(&m_classInfo);
91         }
92         return m_classRef;
93 }
94
95 JSValueRef JSContactAddress::createJSObject(JSContextRef context,
96                 const std::string country,
97                 const std::string region,
98                 const std::string county,
99                 const std::string city,
100                 const std::string streetAddress,
101                 const std::string additionalInformation,
102                 const std::string postalCode,
103                 const ContactAddressTypeArrayPtr types)
104 {
105         ContactAddressPtr privateData = ContactAddressPtr(new ContactAddress());
106
107         privateData->setCountry(country);
108         privateData->setRegion(region);
109         privateData->setCity(city);
110         privateData->setStreetAddress(streetAddress);
111         privateData->setAdditionalInformation(additionalInformation);
112         privateData->setPostalCode(postalCode);
113         privateData->setTypes(types);
114
115         JSContactAddressPriv *priv = new JSContactAddressPriv(context, privateData);
116         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
117         if (NULL == jsValueRef) {
118                 LogError("object creation error");
119                 return JSValueMakeUndefined(context);
120         }
121         return jsValueRef;
122 }
123
124 bool JSContactAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
125 {
126         return JSValueIsObjectOfClass(context, value, getClassRef());
127 }
128
129 ContactAddressPtr JSContactAddress::getContactAddress(JSContextRef context, JSValueRef value)
130 {
131         if (!isObjectOfClass(context, value)) {
132                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
133         }
134         JSObjectRef object = JSValueToObject(context, value, NULL);
135         if (!object) {
136                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
137         }
138         JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
139         if (!priv) {
140                 Throw(WrtDeviceApis::Commons::NullPointerException);
141         }
142         return priv->getObject();
143 }
144
145 void JSContactAddress::Initialize(JSContextRef context, JSObjectRef object)
146 {
147         assert(NULL != JSObjectGetPrivate(object));
148 }
149
150 void JSContactAddress::Finalize(JSObjectRef object)
151 {
152         //delete (JSObjectGetPrivate(object));
153 }
154
155 ContactAddressPtr JSContactAddress::getPrivData(JSObjectRef object)
156 {
157         //LogDebug("entered");
158         JSContactAddressPriv *priv = static_cast<JSContactAddressPriv*>(JSObjectGetPrivate(object));
159         if (!priv) {
160                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
161         }
162         ContactAddressPtr result = priv->getObject();
163         if (!result) {
164                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165         }
166         return result;
167 }
168
169 JSValueRef JSContactAddress::getCountry(JSContextRef context,
170                 JSObjectRef object,
171                 JSStringRef propertyName,
172                 JSValueRef* exception)
173 {
174         //LogDebug("entered");
175         Try
176         {
177                 ContactConverterFactory::ConverterType converter =
178                                 ContactConverterFactory::getConverter(context);
179                 ContactAddressPtr contactAddress = getPrivData(object);
180                 return converter->toJSValueRef(contactAddress->getCountry());
181         }
182         Catch(WrtDeviceApis::Commons::Exception)
183         {
184                 LogWarning("trying to get incorrect value");
185         }
186         return JSValueMakeUndefined(context);
187 }
188
189 bool JSContactAddress::setCountry(JSContextRef context,
190                 JSObjectRef object,
191                 JSStringRef propertyName,
192                 JSValueRef value,
193                 JSValueRef* exception)
194 {
195         Try
196         {
197                 ContactAddressPtr contactAddress = getPrivData(object);
198                 ContactConverterFactory::ConverterType converter =
199                                 ContactConverterFactory::getConverter(context);
200                 contactAddress->setCountry(converter->toString(value));
201                 return true;
202         }
203         Catch(WrtDeviceApis::Commons::Exception)
204         {
205                 LogWarning("trying to set incorrect value");
206         }
207         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
208         return false;
209 }
210
211
212 JSValueRef JSContactAddress::getRegion(JSContextRef context,
213                 JSObjectRef object,
214                 JSStringRef propertyName,
215                 JSValueRef* exception)
216 {
217         //LogDebug("entered");
218         Try
219         {
220                 ContactConverterFactory::ConverterType converter =
221                                 ContactConverterFactory::getConverter(context);
222                 ContactAddressPtr contactAddress = getPrivData(object);
223                 return converter->toJSValueRef(contactAddress->getRegion());
224         }
225         Catch(WrtDeviceApis::Commons::Exception)
226         {
227                 LogWarning("trying to get incorrect value");
228         }
229         return JSValueMakeUndefined(context);
230 }
231
232 bool JSContactAddress::setRegion(JSContextRef context,
233                 JSObjectRef object,
234                 JSStringRef propertyName,
235                 JSValueRef value,
236                 JSValueRef* exception)
237 {
238         Try
239         {
240                 ContactAddressPtr contactAddress = getPrivData(object);
241                 ContactConverterFactory::ConverterType converter =
242                                 ContactConverterFactory::getConverter(context);
243                 contactAddress->setRegion(converter->toString(value));
244                 return true;
245         }
246         Catch(WrtDeviceApis::Commons::Exception)
247         {
248                 LogWarning("trying to set incorrect value");
249         }
250         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
251         return false;
252 }
253
254 JSValueRef JSContactAddress::getCity(JSContextRef context,
255                 JSObjectRef object,
256                 JSStringRef propertyName,
257                 JSValueRef* exception)
258 {
259         //LogDebug("entered");
260         Try
261         {
262                 ContactConverterFactory::ConverterType converter =
263                                 ContactConverterFactory::getConverter(context);
264                 ContactAddressPtr contactAddress = getPrivData(object);
265                 return converter->toJSValueRef(contactAddress->getCity());
266         }
267         Catch(WrtDeviceApis::Commons::Exception)
268         {
269                 LogWarning("trying to get incorrect value");
270         }
271         return JSValueMakeUndefined(context);
272 }
273
274 bool JSContactAddress::setCity(JSContextRef context,
275                 JSObjectRef object,
276                 JSStringRef propertyName,
277                 JSValueRef value,
278                 JSValueRef* exception)
279 {
280         Try
281         {
282                 ContactAddressPtr contactAddress = getPrivData(object);
283                 ContactConverterFactory::ConverterType converter =
284                                 ContactConverterFactory::getConverter(context);
285                 contactAddress->setCity(converter->toString(value));
286                 return true;
287         }
288         Catch(WrtDeviceApis::Commons::Exception)
289         {
290                 LogWarning("trying to set incorrect value");
291         }
292         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
293         return false;
294 }
295
296
297 JSValueRef JSContactAddress::getStreetAddress(JSContextRef context,
298                 JSObjectRef object,
299                 JSStringRef propertyName,
300                 JSValueRef* exception)
301 {
302         //LogDebug("entered");
303         Try
304         {
305                 ContactConverterFactory::ConverterType converter =
306                                 ContactConverterFactory::getConverter(context);
307                 ContactAddressPtr contactAddress = getPrivData(object);
308                 return converter->toJSValueRef(contactAddress->getStreetAddress());
309         }
310         Catch(WrtDeviceApis::Commons::Exception)
311         {
312                 LogWarning("trying to get incorrect value");
313         }
314         return JSValueMakeUndefined(context);
315 }
316
317 bool JSContactAddress::setStreetAddress(JSContextRef context,
318                 JSObjectRef object,
319                 JSStringRef propertyName,
320                 JSValueRef value,
321                 JSValueRef* exception)
322 {
323         Try
324         {
325                 ContactAddressPtr contactAddress = getPrivData(object);
326                 ContactConverterFactory::ConverterType converter =
327                                 ContactConverterFactory::getConverter(context);
328                 contactAddress->setStreetAddress(converter->toString(value));
329                 return true;
330         }
331         Catch(WrtDeviceApis::Commons::Exception)
332         {
333                 LogWarning("trying to set incorrect value");
334         }
335         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
336         return false;
337 }
338
339
340 JSValueRef JSContactAddress::getAdditionalInformation(JSContextRef context,
341                 JSObjectRef object,
342                 JSStringRef propertyName,
343                 JSValueRef* exception)
344 {
345         //LogDebug("entered");
346         Try
347         {
348                 ContactConverterFactory::ConverterType converter =
349                                 ContactConverterFactory::getConverter(context);
350                 ContactAddressPtr contactAddress = getPrivData(object);
351                 return converter->toJSValueRef(contactAddress->getAdditionalInformation());
352         }
353         Catch(WrtDeviceApis::Commons::Exception)
354         {
355                 LogWarning("trying to get incorrect value");
356         }
357         return JSValueMakeUndefined(context);
358 }
359
360 bool JSContactAddress::setAdditionalInformation(JSContextRef context,
361                 JSObjectRef object,
362                 JSStringRef propertyName,
363                 JSValueRef value,
364                 JSValueRef* exception)
365 {
366         Try
367         {
368                 ContactAddressPtr contactAddress = getPrivData(object);
369                 ContactConverterFactory::ConverterType converter =
370                                 ContactConverterFactory::getConverter(context);
371                 contactAddress->setAdditionalInformation(converter->toString(value));
372                 return true;
373         }
374         Catch(WrtDeviceApis::Commons::Exception)
375         {
376                 LogWarning("trying to set incorrect value");
377         }
378         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
379         return false;
380 }
381
382
383 JSValueRef JSContactAddress::getPostalCode(JSContextRef context,
384                 JSObjectRef object,
385                 JSStringRef propertyName,
386                 JSValueRef* exception)
387 {
388         //LogDebug("entered");
389         Try
390         {
391                 ContactConverterFactory::ConverterType converter =
392                                 ContactConverterFactory::getConverter(context);
393                 ContactAddressPtr contactAddress = getPrivData(object);
394                 return converter->toJSValueRef(contactAddress->getPostalCode());
395         }
396         Catch(WrtDeviceApis::Commons::Exception)
397         {
398                 LogWarning("trying to get incorrect value");
399         }
400         return JSValueMakeUndefined(context);
401 }
402
403 bool JSContactAddress::setPostalCode(JSContextRef context,
404                 JSObjectRef object,
405                 JSStringRef propertyName,
406                 JSValueRef value,
407                 JSValueRef* exception)
408 {
409         Try
410         {
411                 ContactAddressPtr contactAddress = getPrivData(object);
412                 ContactConverterFactory::ConverterType converter =
413                                 ContactConverterFactory::getConverter(context);
414                 contactAddress->setPostalCode(converter->toString(value));
415                 return true;
416         }
417         Catch(WrtDeviceApis::Commons::Exception)
418         {
419                 LogWarning("trying to set incorrect value");
420         }
421         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
422         return false;
423 }
424
425
426 JSValueRef JSContactAddress::getTypes(JSContextRef context,
427                 JSObjectRef object,
428                 JSStringRef propertyName,
429                 JSValueRef* exception)
430 {
431         //LogDebug("entered");
432         Try
433         {
434                 ContactConverterFactory::ConverterType converter =
435                                 ContactConverterFactory::getConverter(context);
436                 ContactAddressPtr contactAddress = getPrivData(object);
437                 return JSContactAddressTypeArray::createArray(context, contactAddress->getTypes());
438         }
439         Catch(WrtDeviceApis::Commons::Exception)
440         {
441                 LogWarning("trying to get incorrect value");
442         }
443         return JSValueMakeUndefined(context);
444 }
445
446 bool JSContactAddress::setTypes(JSContextRef context,
447                 JSObjectRef object,
448                 JSStringRef propertyName,
449                 JSValueRef value,
450                 JSValueRef* exception)
451 {
452         Try
453         {
454                 ContactAddressPtr contactAddress = getPrivData(object);
455                 ContactConverterFactory::ConverterType converter =
456                                 ContactConverterFactory::getConverter(context);
457
458
459                 if(JSContactAddressTypeArray::isObjectOfClass(context, value))
460                         contactAddress->setTypes(converter->toContactAddressTypeArray(value));  // TODO to implement this function on converter
461                 else
462                         contactAddress->setTypes(JSContactAddressTypeArray::getContactAddressTypeArray(context, value));
463
464                 return true;
465         }
466         Catch(WrtDeviceApis::Commons::Exception)
467         {
468                 LogWarning("trying to set incorrect value");
469         }
470         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
471         return false;
472 }
473
474 } // Contact
475 } // Tizen1_0
476 } // TizenApis