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