17df015ca1ab1615362482d2e3116657d3aab610
[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 <JSWebAPIErrorFactory.h>
29 #include "ContactConverter.h"
30 #include <Logger.h>
31 #include <Export.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 DLL_EXPORT 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         bool js1stParamIsObject = false;
166
167         JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(constructor));
168         if (!priv) {
169                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
170         }
171         JSContextRef gContext = priv->getContext();
172
173         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
174         Try {
175                 if (argumentCount >= 1)
176                 {
177                         if (JSValueIsObject(gContext, arguments[0]))
178                                 js1stParamIsObject = true;
179
180                         if (!js1stParamIsObject &&
181                                         !JSValueIsNull(gContext, arguments[0]) &&
182                                         !JSValueIsUndefined(gContext, arguments[0]))
183                                 ThrowMsg(InvalidArgumentException, "1st argument must be a 'object'");
184                 }
185
186         } Catch(Exception ) {
187                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
188                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "1st argument must be a 'object'");
189                 return NULL;
190         }
191
192         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
193
194         ContactOrganizationPtr contactOrganization(NULL);
195         Try {
196                 if(js1stParamIsObject)
197                         contactOrganization = converter->toContactOrganizationFromInit(arguments[0]);
198                 else
199                         contactOrganization = ContactOrganizationPtr(new ContactOrganization());
200
201         } Catch(Exception) {
202                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
203                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
204                 return NULL;
205         }
206
207         JSObjectRef jsobject;
208
209         Try {
210                 jsobject = createJSObject(gContext, contactOrganization);
211         } Catch(Exception) {
212                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
213                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::UNKNOWN_ERROR, "Internal error");
214                 return NULL;
215         }
216
217         return jsobject;
218 }
219
220 bool JSContactOrganization::hasInstance(JSContextRef context,
221                 JSObjectRef constructor,
222                 JSValueRef possibleInstance,
223                 JSValueRef* exception)
224 {
225         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
226 }
227
228 JSValueRef JSContactOrganization::getName(JSContextRef context,
229                 JSObjectRef object,
230                 JSStringRef propertyName,
231                 JSValueRef* exception)
232 {
233         Try
234         {
235                 ContactConverterFactory::ConverterType converter =
236                                 ContactConverterFactory::getConverter(context);
237                 ContactOrganizationPtr organization = getPrivData(object);
238                 if(!organization->getNameIsSet())
239                         return JSValueMakeNull(context);
240                 else
241                         return converter->toJSValueRef(organization->getName());
242         }
243         Catch(WrtDeviceApis::Commons::Exception)
244         {
245                 LoggerW("trying to get incorrect value");
246         }
247         return JSValueMakeUndefined(context);
248 }
249
250 bool JSContactOrganization::setName(JSContextRef context,
251                 JSObjectRef object,
252                 JSStringRef propertyName,
253                 JSValueRef value,
254                 JSValueRef* exception)
255 {
256         Try
257         {
258                 ContactOrganizationPtr organization = getPrivData(object);
259                 ContactConverterFactory::ConverterType converter =
260                                 ContactConverterFactory::getConverter(context);
261                 BasicValidator validator =
262                                 BasicValidatorFactory::getValidator(context, exception);
263                 if(validator->isNullOrUndefined(value))
264                         organization->unsetName();
265                 else
266                         organization->setName(converter->toString(value));
267         }
268         Catch(WrtDeviceApis::Commons::Exception)
269         {
270                 LoggerW("trying to set incorrect value");
271                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
272         }
273         return true;
274 }
275
276
277 JSValueRef JSContactOrganization::getDepartment(JSContextRef context,
278                 JSObjectRef object,
279                 JSStringRef propertyName,
280                 JSValueRef* exception)
281 {
282         Try
283         {
284                 ContactConverterFactory::ConverterType converter =
285                                 ContactConverterFactory::getConverter(context);
286                 ContactOrganizationPtr organization = getPrivData(object);
287                 if(!organization->getDepartmentIsSet())
288                         return JSValueMakeNull(context);
289                 else
290                         return converter->toJSValueRef(organization->getDepartment());
291         }
292         Catch(WrtDeviceApis::Commons::Exception)
293         {
294                 LoggerW("trying to get incorrect value");
295         }
296         return JSValueMakeUndefined(context);
297 }
298
299 bool JSContactOrganization::setDepartment(JSContextRef context,
300                 JSObjectRef object,
301                 JSStringRef propertyName,
302                 JSValueRef value,
303                 JSValueRef* exception)
304 {
305         Try
306         {
307                 ContactOrganizationPtr organization = getPrivData(object);
308                 ContactConverterFactory::ConverterType converter =
309                                 ContactConverterFactory::getConverter(context);
310                 BasicValidator validator =
311                                 BasicValidatorFactory::getValidator(context, exception);
312                 if(validator->isNullOrUndefined(value))
313                         organization->unsetDepartment();
314                 else
315                         organization->setDepartment(converter->toString(value));
316         }
317         Catch(WrtDeviceApis::Commons::Exception)
318         {
319                 LoggerW("trying to set incorrect value");
320                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
321         }
322         return true;
323 }
324
325 JSValueRef JSContactOrganization::getOffice(JSContextRef context,
326                 JSObjectRef object,
327                 JSStringRef propertyName,
328                 JSValueRef* exception)
329 {
330         // NOTE: Deprecated field.
331         return JSValueMakeNull(context);
332 }
333
334 bool JSContactOrganization::setOffice(JSContextRef context,
335                 JSObjectRef object,
336                 JSStringRef propertyName,
337                 JSValueRef value,
338                 JSValueRef* exception)
339 {
340         // NOTE: Deprecated field.
341         return true;
342 }
343
344 JSValueRef JSContactOrganization::getTitle(JSContextRef context,
345                 JSObjectRef object,
346                 JSStringRef propertyName,
347                 JSValueRef* exception)
348 {
349         Try
350         {
351                 ContactConverterFactory::ConverterType converter =
352                                 ContactConverterFactory::getConverter(context);
353                 ContactOrganizationPtr organization = getPrivData(object);
354                 if(!organization->getTitleIsSet())
355                         return JSValueMakeNull(context);
356                 else
357                         return converter->toJSValueRef(organization->getTitle());
358         }
359         Catch(WrtDeviceApis::Commons::Exception)
360         {
361                 LoggerW("trying to get incorrect value");
362         }
363         return JSValueMakeUndefined(context);
364 }
365
366 bool JSContactOrganization::setTitle(JSContextRef context,
367                 JSObjectRef object,
368                 JSStringRef propertyName,
369                 JSValueRef value,
370                 JSValueRef* exception)
371 {
372         Try
373         {
374                 ContactOrganizationPtr organization = getPrivData(object);
375                 ContactConverterFactory::ConverterType converter =
376                                 ContactConverterFactory::getConverter(context);
377                 BasicValidator validator =
378                                 BasicValidatorFactory::getValidator(context, exception);
379                 if(validator->isNullOrUndefined(value))
380                         organization->unsetTitle();
381                 else
382                         organization->setTitle(converter->toString(value));
383         }
384         Catch(WrtDeviceApis::Commons::Exception)
385         {
386                 LoggerW("trying to set incorrect value");
387                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
388         }
389         return true;
390 }
391
392 JSValueRef JSContactOrganization::getRole(JSContextRef context,
393                 JSObjectRef object,
394                 JSStringRef propertyName,
395                 JSValueRef* exception)
396 {
397         Try
398         {
399                 ContactConverterFactory::ConverterType converter =
400                                 ContactConverterFactory::getConverter(context);
401                 ContactOrganizationPtr organization = getPrivData(object);
402                 if(!organization->getRoleIsSet())
403                         return JSValueMakeNull(context);
404                 else
405                         return converter->toJSValueRef(organization->getRole());
406         }
407         Catch(WrtDeviceApis::Commons::Exception)
408         {
409                 LoggerW("trying to get incorrect value");
410         }
411         return JSValueMakeUndefined(context);
412 }
413
414 bool JSContactOrganization::setRole(JSContextRef context,
415                 JSObjectRef object,
416                 JSStringRef propertyName,
417                 JSValueRef value,
418                 JSValueRef* exception)
419 {
420         Try
421         {
422                 ContactOrganizationPtr organization = getPrivData(object);
423                 ContactConverterFactory::ConverterType converter =
424                                 ContactConverterFactory::getConverter(context);
425                 BasicValidator validator =
426                                 BasicValidatorFactory::getValidator(context, exception);
427                 if(validator->isNullOrUndefined(value))
428                         organization->unsetRole();
429                 else
430                         organization->setRole(converter->toString(value));
431         }
432         Catch(WrtDeviceApis::Commons::Exception)
433         {
434                 LoggerW("trying to set incorrect value");
435                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
436         }
437         return true;
438 }
439
440 JSValueRef JSContactOrganization::getLogoURI(JSContextRef context,
441                 JSObjectRef object,
442                 JSStringRef propertyName,
443                 JSValueRef* exception)
444 {
445         Try
446         {
447                 ContactConverterFactory::ConverterType converter =
448                                 ContactConverterFactory::getConverter(context);
449                 ContactOrganizationPtr organization = getPrivData(object);
450                 if(!organization->getLogoURIIsSet())
451                         return JSValueMakeNull(context);
452                 else
453                         return converter->toJSValueRef(organization->getLogoURI());
454         }
455         Catch(WrtDeviceApis::Commons::Exception)
456         {
457                 LoggerW("trying to get incorrect value");
458         }
459         return JSValueMakeUndefined(context);
460 }
461
462 bool JSContactOrganization::setLogoURI(JSContextRef context,
463                 JSObjectRef object,
464                 JSStringRef propertyName,
465                 JSValueRef value,
466                 JSValueRef* exception)
467 {
468         Try
469         {
470                 ContactOrganizationPtr organization = getPrivData(object);
471                 ContactConverterFactory::ConverterType converter =
472                                 ContactConverterFactory::getConverter(context);
473                 BasicValidator validator =
474                                 BasicValidatorFactory::getValidator(context, exception);
475                 if(validator->isNullOrUndefined(value))
476                         organization->unsetLogoURI();
477                 else
478                         organization->setLogoURI(converter->toString(value));
479         }
480         Catch(WrtDeviceApis::Commons::Exception)
481         {
482                 LoggerW("trying to set incorrect value");
483                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
484         }
485         return true;
486 }
487
488 } // Contact
489 } // DeviceAPI