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