wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSContactOrganization.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        JSContactOrganization.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSContactOrganization class
23  */
24
25 #include "JSContactOrganization.h"
26 #include <dpl/shared_ptr.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <JSTizenExceptionFactory.h>
29 #include <JSTizenException.h>
30 #include "ContactConverter.h"
31 #include <Logger.h>
32
33 #define ORGANIZATION_CLASS_NAME "ContactOrganization"
34
35 #define CONTACT_ATTR_NAME "name"
36 #define CONTACT_ATTR_DEPARTMENT "department"
37 #define CONTACT_ATTR_OFFICE "office"
38 #define CONTACT_ATTR_TITLE "title"
39 #define CONTACT_ATTR_ROLE "role"
40 #define CONTACT_ATTR_LOGO_URI "logoURI"
41
42 namespace DeviceAPI {
43 namespace Contact {
44
45 using namespace DeviceAPI::Common;
46 using namespace WrtDeviceApis::Commons;
47 using namespace WrtDeviceApis::CommonsJavaScript;
48
49 JSClassDefinition JSContactOrganization::m_classInfo =
50 {
51         0,
52         kJSClassAttributeNone,
53         ORGANIZATION_CLASS_NAME,
54         NULL,
55         m_property,
56         m_functions,
57         Initialize,
58         Finalize,
59         NULL, //hasProperty,
60         NULL, //GetProperty,
61         NULL, //SetProperty,
62         NULL, //DeleteProperty,
63         NULL, //getPropertyNames,
64         NULL, //CallAsFunction,
65         constructor, //CallAsConstructor,
66         hasInstance, //HasInstance,
67         NULL, //ConvertToType,
68 };
69
70 JSStaticValue JSContactOrganization::m_property[] = {
71         { CONTACT_ATTR_NAME, getName, setName, kJSPropertyAttributeNone },
72         { CONTACT_ATTR_DEPARTMENT, getDepartment, setDepartment, kJSPropertyAttributeNone },
73         { CONTACT_ATTR_OFFICE, getOffice, setOffice, kJSPropertyAttributeNone },
74         { CONTACT_ATTR_TITLE, getTitle, setTitle, kJSPropertyAttributeNone },
75         { CONTACT_ATTR_ROLE, getRole, setRole, kJSPropertyAttributeNone },
76         { CONTACT_ATTR_LOGO_URI, getLogoURI, setLogoURI, kJSPropertyAttributeNone },
77         { 0, 0, 0, 0 }
78 };
79
80 JSStaticFunction JSContactOrganization::m_functions[] =
81 {
82         { 0, 0, 0 }
83 };
84
85 JSClassRef JSContactOrganization::m_classRef = JSClassCreate(&m_classInfo);
86
87 JSClassRef JSContactOrganization::getClassRef() {
88         if (!m_classRef) {
89                 m_classRef = JSClassCreate(&m_classInfo);
90         }
91         return m_classRef;
92 }
93
94 bool JSContactOrganization::isObjectOfClass(JSContextRef context, JSValueRef value)
95 {
96         return JSValueIsObjectOfClass(context, value, getClassRef());
97 }
98
99 ContactOrganizationPtr JSContactOrganization::getContactOrganization(JSContextRef context, JSValueRef value)
100 {
101         if (!isObjectOfClass(context, value)) {
102                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
103         }
104         JSObjectRef object = JSValueToObject(context, value, NULL);
105         if (!object) {
106                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
107         }
108         JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
109         if (!priv) {
110                 Throw(WrtDeviceApis::Commons::NullPointerException);
111         }
112         return priv->getObject();
113 }
114
115 void JSContactOrganization::Initialize(JSContextRef context, JSObjectRef object)
116 {
117         if (!JSObjectGetPrivate(object))
118         {
119                 ContactOrganizationPtr organization(new ContactOrganization());
120                 JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, ContactOrganizationPtr(organization));
121                 if (!JSObjectSetPrivate(object, priv)) {
122                         delete priv;
123                 }
124         }
125 }
126
127 void JSContactOrganization::Finalize(JSObjectRef object)
128 {
129         JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
130
131         if (priv != NULL)
132                 delete (priv);
133 }
134
135 JSObjectRef JSContactOrganization::createJSObject(JSContextRef context, ContactOrganizationPtr contactOrganization)
136 {
137         JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, contactOrganization);
138         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
139         if (NULL == jsObjectRef) {
140                 LoggerE("object creation error");
141                 return NULL;
142         }
143         return jsObjectRef;
144 }
145
146 ContactOrganizationPtr JSContactOrganization::getPrivData(JSObjectRef object)
147 {
148         JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
149         if (!priv) {
150                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
151         }
152         ContactOrganizationPtr result = priv->getObject();
153         if (!result) {
154                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
155         }
156         return result;
157 }
158
159 JSObjectRef JSContactOrganization::constructor(JSContextRef context,
160                 JSObjectRef constructor,
161                 size_t argumentCount,
162                 const JSValueRef arguments[],
163                 JSValueRef* exception)
164 {
165         LoggerD("entered");
166
167         bool js1stParamIsObject = false;
168
169         JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(constructor));
170         if (!priv) {
171                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
172         }
173         JSContextRef gContext = priv->getContext();
174
175         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
176         Try {
177                 if (argumentCount >= 1)
178                 {
179                         if (JSValueIsObject(gContext, arguments[0]))
180                                 js1stParamIsObject = true;
181
182                         if (!js1stParamIsObject &&
183                                         !JSValueIsNull(gContext, arguments[0]) &&
184                                         !JSValueIsUndefined(gContext, arguments[0]))
185                                 ThrowMsg(InvalidArgumentException, "1st argument must be a 'object'");
186                 }
187
188         } Catch(Exception ) {
189                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
190                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'object'");
191                 return NULL;
192         }
193
194         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
195
196         ContactOrganizationPtr contactOrganization(NULL);
197         Try {
198                 if(js1stParamIsObject)
199                         contactOrganization = converter->toContactOrganizationFromInit(arguments[0]);
200                 else
201                         contactOrganization = ContactOrganizationPtr(new ContactOrganization());
202
203         } Catch(Exception) {
204                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
205                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
206                 return NULL;
207         }
208
209         JSObjectRef jsobject;
210
211         Try {
212                 jsobject = createJSObject(gContext, contactOrganization);
213         } Catch(Exception) {
214                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
215                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, "Internal error");
216                 return NULL;
217         }
218
219         return jsobject;
220 }
221
222 bool JSContactOrganization::hasInstance(JSContextRef context,
223                 JSObjectRef constructor,
224                 JSValueRef possibleInstance,
225                 JSValueRef* exception)
226 {
227         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
228 }
229
230 JSValueRef JSContactOrganization::getName(JSContextRef context,
231                 JSObjectRef object,
232                 JSStringRef propertyName,
233                 JSValueRef* exception)
234 {
235         Try
236         {
237                 ContactConverterFactory::ConverterType converter =
238                                 ContactConverterFactory::getConverter(context);
239                 ContactOrganizationPtr organization = getPrivData(object);
240                 if(!organization->getNameIsSet())
241                         return JSValueMakeNull(context);
242                 else
243                         return converter->toJSValueRef(organization->getName());
244         }
245         Catch(WrtDeviceApis::Commons::Exception)
246         {
247                 LoggerW("trying to get incorrect value");
248         }
249         return JSValueMakeUndefined(context);
250 }
251
252 bool JSContactOrganization::setName(JSContextRef context,
253                 JSObjectRef object,
254                 JSStringRef propertyName,
255                 JSValueRef value,
256                 JSValueRef* exception)
257 {
258         Try
259         {
260                 ContactOrganizationPtr organization = getPrivData(object);
261                 ContactConverterFactory::ConverterType converter =
262                                 ContactConverterFactory::getConverter(context);
263                 BasicValidator validator =
264                                 BasicValidatorFactory::getValidator(context, exception);
265                 if(validator->isNullOrUndefined(value))
266                         organization->unsetName();
267                 else
268                         organization->setName(converter->toString(value));
269         }
270         Catch(WrtDeviceApis::Commons::Exception)
271         {
272                 LoggerW("trying to set incorrect value");
273                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
274         }
275         return true;
276 }
277
278
279 JSValueRef JSContactOrganization::getDepartment(JSContextRef context,
280                 JSObjectRef object,
281                 JSStringRef propertyName,
282                 JSValueRef* exception)
283 {
284         Try
285         {
286                 ContactConverterFactory::ConverterType converter =
287                                 ContactConverterFactory::getConverter(context);
288                 ContactOrganizationPtr organization = getPrivData(object);
289                 if(!organization->getDepartmentIsSet())
290                         return JSValueMakeNull(context);
291                 else
292                         return converter->toJSValueRef(organization->getDepartment());
293         }
294         Catch(WrtDeviceApis::Commons::Exception)
295         {
296                 LoggerW("trying to get incorrect value");
297         }
298         return JSValueMakeUndefined(context);
299 }
300
301 bool JSContactOrganization::setDepartment(JSContextRef context,
302                 JSObjectRef object,
303                 JSStringRef propertyName,
304                 JSValueRef value,
305                 JSValueRef* exception)
306 {
307         Try
308         {
309                 ContactOrganizationPtr organization = getPrivData(object);
310                 ContactConverterFactory::ConverterType converter =
311                                 ContactConverterFactory::getConverter(context);
312                 BasicValidator validator =
313                                 BasicValidatorFactory::getValidator(context, exception);
314                 if(validator->isNullOrUndefined(value))
315                         organization->unsetDepartment();
316                 else
317                         organization->setDepartment(converter->toString(value));
318         }
319         Catch(WrtDeviceApis::Commons::Exception)
320         {
321                 LoggerW("trying to set incorrect value");
322                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
323         }
324         return true;
325 }
326
327 JSValueRef JSContactOrganization::getOffice(JSContextRef context,
328                 JSObjectRef object,
329                 JSStringRef propertyName,
330                 JSValueRef* exception)
331 {
332         // NOTE: Deprecated field.
333         return JSValueMakeNull(context);
334 }
335
336 bool JSContactOrganization::setOffice(JSContextRef context,
337                 JSObjectRef object,
338                 JSStringRef propertyName,
339                 JSValueRef value,
340                 JSValueRef* exception)
341 {
342         // NOTE: Deprecated field.
343         return true;
344 }
345
346 JSValueRef JSContactOrganization::getTitle(JSContextRef context,
347                 JSObjectRef object,
348                 JSStringRef propertyName,
349                 JSValueRef* exception)
350 {
351         Try
352         {
353                 ContactConverterFactory::ConverterType converter =
354                                 ContactConverterFactory::getConverter(context);
355                 ContactOrganizationPtr organization = getPrivData(object);
356                 if(!organization->getTitleIsSet())
357                         return JSValueMakeNull(context);
358                 else
359                         return converter->toJSValueRef(organization->getTitle());
360         }
361         Catch(WrtDeviceApis::Commons::Exception)
362         {
363                 LoggerW("trying to get incorrect value");
364         }
365         return JSValueMakeUndefined(context);
366 }
367
368 bool JSContactOrganization::setTitle(JSContextRef context,
369                 JSObjectRef object,
370                 JSStringRef propertyName,
371                 JSValueRef value,
372                 JSValueRef* exception)
373 {
374         Try
375         {
376                 ContactOrganizationPtr organization = getPrivData(object);
377                 ContactConverterFactory::ConverterType converter =
378                                 ContactConverterFactory::getConverter(context);
379                 BasicValidator validator =
380                                 BasicValidatorFactory::getValidator(context, exception);
381                 if(validator->isNullOrUndefined(value))
382                         organization->unsetTitle();
383                 else
384                         organization->setTitle(converter->toString(value));
385         }
386         Catch(WrtDeviceApis::Commons::Exception)
387         {
388                 LoggerW("trying to set incorrect value");
389                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
390         }
391         return true;
392 }
393
394 JSValueRef JSContactOrganization::getRole(JSContextRef context,
395                 JSObjectRef object,
396                 JSStringRef propertyName,
397                 JSValueRef* exception)
398 {
399         Try
400         {
401                 ContactConverterFactory::ConverterType converter =
402                                 ContactConverterFactory::getConverter(context);
403                 ContactOrganizationPtr organization = getPrivData(object);
404                 if(!organization->getRoleIsSet())
405                         return JSValueMakeNull(context);
406                 else
407                         return converter->toJSValueRef(organization->getRole());
408         }
409         Catch(WrtDeviceApis::Commons::Exception)
410         {
411                 LoggerW("trying to get incorrect value");
412         }
413         return JSValueMakeUndefined(context);
414 }
415
416 bool JSContactOrganization::setRole(JSContextRef context,
417                 JSObjectRef object,
418                 JSStringRef propertyName,
419                 JSValueRef value,
420                 JSValueRef* exception)
421 {
422         Try
423         {
424                 ContactOrganizationPtr organization = getPrivData(object);
425                 ContactConverterFactory::ConverterType converter =
426                                 ContactConverterFactory::getConverter(context);
427                 BasicValidator validator =
428                                 BasicValidatorFactory::getValidator(context, exception);
429                 if(validator->isNullOrUndefined(value))
430                         organization->unsetRole();
431                 else
432                         organization->setRole(converter->toString(value));
433         }
434         Catch(WrtDeviceApis::Commons::Exception)
435         {
436                 LoggerW("trying to set incorrect value");
437                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
438         }
439         return true;
440 }
441
442 JSValueRef JSContactOrganization::getLogoURI(JSContextRef context,
443                 JSObjectRef object,
444                 JSStringRef propertyName,
445                 JSValueRef* exception)
446 {
447         Try
448         {
449                 ContactConverterFactory::ConverterType converter =
450                                 ContactConverterFactory::getConverter(context);
451                 ContactOrganizationPtr organization = getPrivData(object);
452                 if(!organization->getLogoURIIsSet())
453                         return JSValueMakeNull(context);
454                 else
455                         return converter->toJSValueRef(organization->getLogoURI());
456         }
457         Catch(WrtDeviceApis::Commons::Exception)
458         {
459                 LoggerW("trying to get incorrect value");
460         }
461         return JSValueMakeUndefined(context);
462 }
463
464 bool JSContactOrganization::setLogoURI(JSContextRef context,
465                 JSObjectRef object,
466                 JSStringRef propertyName,
467                 JSValueRef value,
468                 JSValueRef* exception)
469 {
470         Try
471         {
472                 ContactOrganizationPtr organization = getPrivData(object);
473                 ContactConverterFactory::ConverterType converter =
474                                 ContactConverterFactory::getConverter(context);
475                 BasicValidator validator =
476                                 BasicValidatorFactory::getValidator(context, exception);
477                 if(validator->isNullOrUndefined(value))
478                         organization->unsetLogoURI();
479                 else
480                         organization->setLogoURI(converter->toString(value));
481         }
482         Catch(WrtDeviceApis::Commons::Exception)
483         {
484                 LoggerW("trying to set incorrect value");
485                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
486         }
487         return true;
488 }
489
490 } // Contact
491 } // DeviceAPI