Beta merge 2
[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 <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include <CommonsJavaScript/Converter.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
47 JSClassRef JSContactOrganization::m_classRef = NULL;
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,
65         NULL,
66         NULL,
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::getClassRef() {
86         if (!m_classRef) {
87                 m_classRef = JSClassCreate(&m_classInfo);
88         }
89         return m_classRef;
90 }
91
92 JSValueRef JSContactOrganization::createJSObject(JSContextRef context,
93                 const std::string& name,
94                 const std::string& department,
95                 const std::string& office,
96                 const std::string& title,
97                 const std::string& role,
98                 const std::string& logoURI)
99 {
100         ContactOrganizationPtr privateData = ContactOrganizationPtr(new ContactOrganization());
101         privateData->setName(name);
102         privateData->setDepartment(department);
103         privateData->setOffice(office);
104         privateData->setTitle(title);
105         privateData->setRole(role);
106         privateData->setLogoURI(logoURI);
107         JSContactOrganizationPriv *priv = new JSContactOrganizationPriv(context, privateData);
108         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
109         if (NULL == jsValueRef) {
110                 LogError("object creation error");
111                 return JSValueMakeUndefined(context);
112         }
113         return jsValueRef;
114 }
115
116 bool JSContactOrganization::isObjectOfClass(JSContextRef context, JSValueRef value)
117 {
118         return JSValueIsObjectOfClass(context, value, getClassRef());
119 }
120
121 ContactOrganizationPtr JSContactOrganization::getContactOrganization(JSContextRef context, JSValueRef value)
122 {
123         if (!isObjectOfClass(context, value)) {
124                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
125         }
126         JSObjectRef object = JSValueToObject(context, value, NULL);
127         if (!object) {
128                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
129         }
130         JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
131         if (!priv) {
132                 Throw(WrtDeviceApis::Commons::NullPointerException);
133         }
134         return priv->getObject();
135 }
136
137 void JSContactOrganization::Initialize(JSContextRef context, JSObjectRef object)
138 {
139         assert(NULL != JSObjectGetPrivate(object));
140 }
141
142 void JSContactOrganization::Finalize(JSObjectRef object)
143 {
144         //delete (JSObjectGetPrivate(object));
145 }
146
147 ContactOrganizationPtr JSContactOrganization::getPrivData(JSObjectRef object)
148 {
149         //LogDebug("entered");
150         JSContactOrganizationPriv *priv = static_cast<JSContactOrganizationPriv*>(JSObjectGetPrivate(object));
151         if (!priv) {
152                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
153         }
154         ContactOrganizationPtr result = priv->getObject();
155         if (!result) {
156                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
157         }
158         return result;
159 }
160
161 JSValueRef JSContactOrganization::getName(JSContextRef context,
162                 JSObjectRef object,
163                 JSStringRef propertyName,
164                 JSValueRef* exception)
165 {
166         //LogDebug("entered");
167         Try
168         {
169                 ContactConverterFactory::ConverterType converter =
170                                 ContactConverterFactory::getConverter(context);
171                 ContactOrganizationPtr organization = getPrivData(object);
172                 return converter->toJSValueRef(organization->getName());
173         }
174         Catch(WrtDeviceApis::Commons::Exception)
175         {
176                 LogWarning("trying to get incorrect value");
177         }
178         return JSValueMakeUndefined(context);
179 }
180
181 bool JSContactOrganization::setName(JSContextRef context,
182                 JSObjectRef object,
183                 JSStringRef propertyName,
184                 JSValueRef value,
185                 JSValueRef* exception)
186 {
187         Try
188         {
189                 ContactOrganizationPtr organization = getPrivData(object);
190                 ContactConverterFactory::ConverterType converter =
191                                 ContactConverterFactory::getConverter(context);
192                 organization->setName(converter->toString(value));
193                 return true;
194         }
195         Catch(WrtDeviceApis::Commons::Exception)
196         {
197                 LogWarning("trying to set incorrect value");
198         }
199         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
200         return false;
201 }
202
203
204 JSValueRef JSContactOrganization::getDepartment(JSContextRef context,
205                 JSObjectRef object,
206                 JSStringRef propertyName,
207                 JSValueRef* exception)
208 {
209         //LogDebug("entered");
210         Try
211         {
212                 ContactConverterFactory::ConverterType converter =
213                                 ContactConverterFactory::getConverter(context);
214                 ContactOrganizationPtr organization = getPrivData(object);
215                 return converter->toJSValueRef(organization->getDepartment());
216         }
217         Catch(WrtDeviceApis::Commons::Exception)
218         {
219                 LogWarning("trying to get incorrect value");
220         }
221         return JSValueMakeUndefined(context);
222 }
223
224 bool JSContactOrganization::setDepartment(JSContextRef context,
225                 JSObjectRef object,
226                 JSStringRef propertyName,
227                 JSValueRef value,
228                 JSValueRef* exception)
229 {
230         Try
231         {
232                 ContactOrganizationPtr organization = getPrivData(object);
233                 ContactConverterFactory::ConverterType converter =
234                                 ContactConverterFactory::getConverter(context);
235                 organization->setDepartment(converter->toString(value));
236                 return true;
237         }
238         Catch(WrtDeviceApis::Commons::Exception)
239         {
240                 LogWarning("trying to set incorrect value");
241         }
242         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
243         return false;
244 }
245
246 JSValueRef JSContactOrganization::getOffice(JSContextRef context,
247                 JSObjectRef object,
248                 JSStringRef propertyName,
249                 JSValueRef* exception)
250 {
251         //LogDebug("entered");
252         Try
253         {
254                 ContactConverterFactory::ConverterType converter =
255                                 ContactConverterFactory::getConverter(context);
256                 ContactOrganizationPtr organization = getPrivData(object);
257                 return converter->toJSValueRef(organization->getOffice());
258         }
259         Catch(WrtDeviceApis::Commons::Exception)
260         {
261                 LogWarning("trying to get incorrect value");
262         }
263         return JSValueMakeUndefined(context);
264 }
265
266 bool JSContactOrganization::setOffice(JSContextRef context,
267                 JSObjectRef object,
268                 JSStringRef propertyName,
269                 JSValueRef value,
270                 JSValueRef* exception)
271 {
272         Try
273         {
274                 ContactOrganizationPtr organization = getPrivData(object);
275                 ContactConverterFactory::ConverterType converter =
276                                 ContactConverterFactory::getConverter(context);
277                 organization->setOffice(converter->toString(value));
278                 return true;
279         }
280         Catch(WrtDeviceApis::Commons::Exception)
281         {
282                 LogWarning("trying to set incorrect value");
283         }
284         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
285         return false;
286 }
287
288 JSValueRef JSContactOrganization::getTitle(JSContextRef context,
289                 JSObjectRef object,
290                 JSStringRef propertyName,
291                 JSValueRef* exception)
292 {
293         //LogDebug("entered");
294         Try
295         {
296                 ContactConverterFactory::ConverterType converter =
297                                 ContactConverterFactory::getConverter(context);
298                 ContactOrganizationPtr organization = getPrivData(object);
299                 return converter->toJSValueRef(organization->getTitle());
300         }
301         Catch(WrtDeviceApis::Commons::Exception)
302         {
303                 LogWarning("trying to get incorrect value");
304         }
305         return JSValueMakeUndefined(context);
306 }
307
308 bool JSContactOrganization::setTitle(JSContextRef context,
309                 JSObjectRef object,
310                 JSStringRef propertyName,
311                 JSValueRef value,
312                 JSValueRef* exception)
313 {
314         Try
315         {
316                 ContactOrganizationPtr organization = getPrivData(object);
317                 ContactConverterFactory::ConverterType converter =
318                                 ContactConverterFactory::getConverter(context);
319                 organization->setTitle(converter->toString(value));
320                 return true;
321         }
322         Catch(WrtDeviceApis::Commons::Exception)
323         {
324                 LogWarning("trying to set incorrect value");
325         }
326         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
327         return false;
328 }
329
330 JSValueRef JSContactOrganization::getRole(JSContextRef context,
331                 JSObjectRef object,
332                 JSStringRef propertyName,
333                 JSValueRef* exception)
334 {
335         //LogDebug("entered");
336         Try
337         {
338                 ContactConverterFactory::ConverterType converter =
339                                 ContactConverterFactory::getConverter(context);
340                 ContactOrganizationPtr organization = getPrivData(object);
341                 return converter->toJSValueRef(organization->getRole());
342         }
343         Catch(WrtDeviceApis::Commons::Exception)
344         {
345                 LogWarning("trying to get incorrect value");
346         }
347         return JSValueMakeUndefined(context);
348 }
349
350 bool JSContactOrganization::setRole(JSContextRef context,
351                 JSObjectRef object,
352                 JSStringRef propertyName,
353                 JSValueRef value,
354                 JSValueRef* exception)
355 {
356         Try
357         {
358                 ContactOrganizationPtr organization = getPrivData(object);
359                 ContactConverterFactory::ConverterType converter =
360                                 ContactConverterFactory::getConverter(context);
361                 organization->setRole(converter->toString(value));
362                 return true;
363         }
364         Catch(WrtDeviceApis::Commons::Exception)
365         {
366                 LogWarning("trying to set incorrect value");
367         }
368         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
369         return false;
370 }
371
372 JSValueRef JSContactOrganization::getLogoURI(JSContextRef context,
373                 JSObjectRef object,
374                 JSStringRef propertyName,
375                 JSValueRef* exception)
376 {
377         //LogDebug("entered");
378         Try
379         {
380                 ContactConverterFactory::ConverterType converter =
381                                 ContactConverterFactory::getConverter(context);
382                 ContactOrganizationPtr organization = getPrivData(object);
383                 return converter->toJSValueRef(organization->getLogoURI());
384         }
385         Catch(WrtDeviceApis::Commons::Exception)
386         {
387                 LogWarning("trying to get incorrect value");
388         }
389         return JSValueMakeUndefined(context);
390 }
391
392 bool JSContactOrganization::setLogoURI(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->setLogoURI(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 } // Contact
415 } // Tizen1_0
416 } // TizenApis