Update change log and spec for wrt-plugins-tizen_0.4.49
[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 #define CONTACT_ATTR_LABEL "label"
39
40 namespace DeviceAPI {
41 namespace Contact {
42
43 using namespace DeviceAPI::Common;
44 using namespace WrtDeviceApis::Commons;
45 using namespace WrtDeviceApis::CommonsJavaScript;
46
47 JSClassDefinition JSContactEmailAddress::m_classInfo =
48 {
49         0,
50         kJSClassAttributeNone,
51         CONTACT_CLASS_NAME,
52         NULL,
53         m_property,
54         m_functions,
55         Initialize,
56         Finalize,
57         NULL, //hasProperty,
58         NULL, //GetProperty,
59         NULL, //SetProperty,
60         NULL, //DeleteProperty,
61         NULL, //getPropertyNames,
62         NULL, //CallAsFunction,
63         constructor, //CallAsConstructor,
64         hasInstance, //HasInstance,
65         NULL, //ConvertToType,
66 };
67
68 JSStaticValue JSContactEmailAddress::m_property[] = {
69         { CONTACT_ATTR_EMAIL, getEmail, setEmail, kJSPropertyAttributeNone },
70         { CONTACT_ATTR_TYPES, getTypes, setTypes, kJSPropertyAttributeNone },
71         { CONTACT_ATTR_IS_DEFAULT, getIsDefault, setIsDefault, kJSPropertyAttributeNone },
72         { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
73         { 0, 0, 0, 0 }
74 };
75
76 JSStaticFunction JSContactEmailAddress::m_functions[] =
77 {
78         { 0, 0, 0 }
79 };
80
81 JSClassRef JSContactEmailAddress::m_classRef = JSClassCreate(&m_classInfo);
82
83 JSClassRef DLL_EXPORT JSContactEmailAddress::getClassRef() {
84         if (!m_classRef) {
85                 m_classRef = JSClassCreate(&m_classInfo);
86         }
87         return m_classRef;
88 }
89
90 bool JSContactEmailAddress::isObjectOfClass(JSContextRef context, JSValueRef value)
91 {
92         return JSValueIsObjectOfClass(context, value, getClassRef());
93 }
94
95 ContactEmailAddressPtr JSContactEmailAddress::getContactEmailAddress(JSContextRef context, JSValueRef value)
96 {
97         if (!isObjectOfClass(context, value)) {
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99         }
100         JSObjectRef object = JSValueToObject(context, value, NULL);
101         if (!object) {
102                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103         }
104         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
105         if (!priv) {
106                 Throw(WrtDeviceApis::Commons::NullPointerException);
107         }
108         return priv->getObject();
109 }
110
111 void JSContactEmailAddress::Initialize(JSContextRef context, JSObjectRef object)
112 {
113         if (!JSObjectGetPrivate(object))
114         {
115                 ContactEmailAddressPtr emailAddress(new ContactEmailAddress());
116                 JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, ContactEmailAddressPtr(emailAddress));
117                 if (!JSObjectSetPrivate(object, priv)) {
118                         delete priv;
119                 }
120         }
121 }
122
123 void JSContactEmailAddress::Finalize(JSObjectRef object)
124 {
125         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
126
127         if (priv != NULL)
128                 delete (priv);
129 }
130
131 JSObjectRef JSContactEmailAddress::createJSObject(JSContextRef context,
132                 const ContactEmailAddressPtr emailAddress)
133 {
134         JSContactEmailAddressPriv *priv = new JSContactEmailAddressPriv(context, emailAddress);
135         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
136         if (NULL == jsObjectRef) {
137                 LoggerE("object creation error");
138                 return NULL;
139         }
140         return jsObjectRef;
141 }
142
143 ContactEmailAddressPtr JSContactEmailAddress::getPrivData(JSObjectRef object)
144 {
145         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
146         if (!priv) {
147                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
148         }
149         ContactEmailAddressPtr result = priv->getObject();
150         if (!result) {
151                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
152         }
153         return result;
154 }
155
156 JSObjectRef JSContactEmailAddress::constructor(JSContextRef context,
157                 JSObjectRef constructor,
158                 size_t argumentCount,
159                 const JSValueRef arguments[],
160                 JSValueRef* exception)
161 {
162         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(constructor));
163         if (!priv) {
164                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165         }
166         JSContextRef gContext = priv->getContext();
167
168         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
169
170         std::string email;
171         ContactEmailAddressTypeArrayPtr types(NULL);
172         bool isDefault = false;
173         std::string label;
174
175         try {
176                 ArgumentValidator Argvalidator(context, argumentCount, arguments);
177                 email = Argvalidator.toString(0, false);
178
179                 if (argumentCount >= 2){
180                         Argvalidator.toArrayObject(1, true);
181                         types = converter->toContactEmailAddressTypeArray(arguments[1]);
182                 }else
183                         types = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
184
185                 if(types->size() == 0)
186                         types->push_back(CONTACT_EMAIL_TYPE_WORK);
187
188                 if (argumentCount >= 3)
189                         isDefault = Argvalidator.toBool(2, false);
190
191                 if (argumentCount >= 4)
192                         label = Argvalidator.toString(3, false);
193
194         } catch (const TypeMismatchException& err ) {
195                 JSWebAPIErrorFactory::postException(context, exception, err);
196                 return NULL;
197         } catch(const BasePlatformException& err) {
198                 JSWebAPIErrorFactory::postException(context, exception, err);
199                 return NULL;
200         } catch(const ConversionException& err) {
201                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
202                 return NULL;
203         } catch(const NullPointerException& err) {
204                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
205                 return NULL;
206         }
207
208         ContactEmailAddressPtr contactEmailAddress(new ContactEmailAddress());
209         contactEmailAddress->setEmail(email);
210         contactEmailAddress->setTypes(types);
211         contactEmailAddress->setIsDefault(isDefault);
212         contactEmailAddress->setLabel(label);
213
214         JSObjectRef jsobject;
215
216         Try {
217                 jsobject = createJSObject(gContext, contactEmailAddress);
218         } Catch(Exception) {
219                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
220                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
221                 return NULL;
222         }
223
224         return jsobject;
225 }
226
227 bool JSContactEmailAddress::hasInstance(JSContextRef context,
228                 JSObjectRef constructor,
229                 JSValueRef possibleInstance,
230                 JSValueRef* exception)
231 {
232         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
233 }
234
235 JSValueRef JSContactEmailAddress::getEmail(JSContextRef context,
236                 JSObjectRef object,
237                 JSStringRef propertyName,
238                 JSValueRef* exception)
239 {
240         Try
241         {
242                 ContactConverterFactory::ConverterType converter =
243                                 ContactConverterFactory::getConverter(context);
244                 ContactEmailAddressPtr emailAddress = getPrivData(object);
245                 return converter->toJSValueRef(emailAddress->getEmail());
246         }
247         Catch(WrtDeviceApis::Commons::Exception)
248         {
249                 LoggerW("trying to get incorrect value");
250         }
251         return JSValueMakeUndefined(context);
252 }
253
254 bool JSContactEmailAddress::setEmail(JSContextRef context,
255                 JSObjectRef object,
256                 JSStringRef propertyName,
257                 JSValueRef value,
258                 JSValueRef* exception)
259 {
260         Try
261         {
262                 ContactEmailAddressPtr emailAddress = getPrivData(object);
263                 ContactConverterFactory::ConverterType converter =
264                                 ContactConverterFactory::getConverter(context);
265                 emailAddress->setEmail(converter->toString(value));
266         }
267         Catch(WrtDeviceApis::Commons::Exception)
268         {
269                 LoggerW("trying to set incorrect value");
270                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
271         }
272         return true;
273 }
274
275
276 JSValueRef JSContactEmailAddress::getIsDefault(JSContextRef context,
277                 JSObjectRef object,
278                 JSStringRef propertyName,
279                 JSValueRef* exception)
280 {
281         Try
282         {
283                 ContactConverterFactory::ConverterType converter =
284                                 ContactConverterFactory::getConverter(context);
285                 ContactEmailAddressPtr emailAddress = getPrivData(object);
286                 return converter->toJSValueRef(emailAddress->getIsDefault());
287         }
288         Catch(WrtDeviceApis::Commons::Exception)
289         {
290                 LoggerW("trying to get incorrect value");
291         }
292         return JSValueMakeUndefined(context);
293 }
294
295 bool JSContactEmailAddress::setIsDefault(JSContextRef context,
296                 JSObjectRef object,
297                 JSStringRef propertyName,
298                 JSValueRef value,
299                 JSValueRef* exception)
300 {
301         Try
302         {
303                 ContactEmailAddressPtr emailAddress = getPrivData(object);
304                 ContactConverterFactory::ConverterType converter =
305                                 ContactConverterFactory::getConverter(context);
306                 BasicValidator validator =
307                                 BasicValidatorFactory::getValidator(context, exception);
308                 emailAddress->setIsDefault(converter->toBool(value));
309         }
310         Catch(WrtDeviceApis::Commons::Exception)
311         {
312                 LoggerW("trying to set incorrect value");
313                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
314         }
315         return true;
316 }
317
318
319 JSValueRef JSContactEmailAddress::getTypes(JSContextRef context,
320                 JSObjectRef object,
321                 JSStringRef propertyName,
322                 JSValueRef* exception)
323 {
324         Try
325         {
326                 JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(object));
327                 if (!priv) {
328                         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
329                 }
330                 JSContextRef gContext = priv->getContext();
331
332                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
333                 ContactEmailAddressPtr emailAddress = getPrivData(object);
334
335                 if(emailAddress->IsTypesSetJSArray()){
336                         return emailAddress->getTypesJSObj();
337                 }else{
338                         JSValueRef tempJSValue = emailAddress->getTypesJSArray();
339                         tempJSValue = converter->toJSValueRef(emailAddress->getTypes());
340
341                         JSObjectRef convertedJSObject = emailAddress->getTypesJSObj();
342                         convertedJSObject = JSValueToObject( gContext, tempJSValue, NULL );
343                         emailAddress->setTypesJSArray(true, convertedJSObject);
344
345                         JSValueProtect(gContext, convertedJSObject);
346                         emailAddress->setContext(gContext);
347                         return tempJSValue;
348                 }
349         }
350         Catch(WrtDeviceApis::Commons::Exception)
351         {
352                 LoggerW("trying to get incorrect value");
353         }
354         return JSValueMakeUndefined(context);
355 }
356
357 bool JSContactEmailAddress::setTypes(JSContextRef context,
358                 JSObjectRef object,
359                 JSStringRef propertyName,
360                 JSValueRef value,
361                 JSValueRef* exception)
362 {
363         Try
364         {
365                 ContactEmailAddressPtr emailAddress = getPrivData(object);
366                 ContactConverterFactory::ConverterType converter =
367                                 ContactConverterFactory::getConverter(context);
368
369                 emailAddress->setTypes(converter->toContactEmailAddressTypeArray(value));
370                 emailAddress->resetTypesJSObj();
371         }
372         Catch(WrtDeviceApis::Commons::Exception)
373         {
374                 LoggerW("trying to set incorrect value");
375                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
376         }
377         return true;
378 }
379
380 JSValueRef JSContactEmailAddress::getLabel(JSContextRef context,
381                 JSObjectRef object,
382                 JSStringRef propertyName,
383                 JSValueRef* exception)
384 {
385         Try
386         {
387                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
388                 ContactEmailAddressPtr emailAddress = getPrivData(object);
389                 return converter->toJSValueRef(emailAddress->getLabel());
390         }
391         Catch(WrtDeviceApis::Commons::Exception)
392         {
393                 LoggerW("trying to get incorrect value");
394         }
395         return JSValueMakeUndefined(context);
396 }
397
398 bool JSContactEmailAddress::setLabel(JSContextRef context,
399                 JSObjectRef object,
400                 JSStringRef propertyName,
401                 JSValueRef value,
402                 JSValueRef* exception)
403 {
404         Try
405         {
406                 ContactEmailAddressPtr emailAddress = getPrivData(object);
407                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
408                 emailAddress->setLabel(converter->toString(value));
409         }
410         Catch(WrtDeviceApis::Commons::Exception)
411         {
412                 LoggerW("trying to set incorrect value");
413                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
414         }
415
416         return true;
417 }
418
419 } // Contact
420 } // DeviceAPI