merge wrt-plugins-tizen_0.2.0-12
[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 <Tizen/Common/JSGlobalContextFactory.h>
29 #include "ContactConverter.h"
30 #include "JSContactEmailAddressTypeArray.h"
31 #include "JSContactEmailAddress.h"
32
33 #define CONTACT_CLASS_NAME "EmailAddress"
34
35 #define CONTACT_ATTR_EMAIL "email"
36 #define CONTACT_ATTR_TYPES "types"
37
38 namespace TizenApis {
39 namespace Tizen1_0 {
40 namespace Contact {
41
42 using namespace TizenApis::Commons;
43 using namespace TizenApis::Api::Contact;
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         { 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 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                 LogError("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         LogDebug("entered");
161
162 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
163 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
164
165         JSContactEmailAddressPriv *priv = static_cast<JSContactEmailAddressPriv*>(JSObjectGetPrivate(constructor));
166         if (!priv) {
167                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
168         }
169         JSContextRef gContext = priv->getContext();
170
171 //      JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
172
173         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
174         Try {
175                 if (argumentCount < 1 || argumentCount > 2)
176                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
177
178                 if (!JSValueIsString(gContext, arguments[0]))
179                         ThrowMsg(InvalidArgumentException, "1st argument is not string.");
180
181                 if (argumentCount >= 2)
182                 {
183                         if (!JSIsArrayValue(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
184                                 ThrowMsg(InvalidArgumentException, "2nd argument is not array.");
185                 }
186
187         } Catch(Exception ) {
188                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
189                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
190                 return NULL;
191         }
192
193         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
194
195         std::string email;
196         ContactEmailAddressTypeArrayPtr types(NULL);
197
198         Try {
199                 email = converter->toString(arguments[0]);
200         } Catch(Exception) {
201                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
202                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
203                 return NULL;
204         }
205
206         Try {
207                 if(argumentCount >= 2)
208                 {
209                         if(JSIsArrayValue(gContext, arguments[1]))
210                                 types = converter->toContactEmailAddressTypeArray(arguments[1]);
211                 }
212                 if(types == NULL)
213                         types = ContactEmailAddressTypeArrayPtr(new ContactEmailAddressTypeArray());
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