Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactName.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        JSContactName.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSContactName class
22  */
23
24 #include <dpl/log/log.h>
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <Tizen/Common/JSTizenExceptionFactory.h>
28 #include <Tizen/Common/JSTizenException.h>
29 #include "ContactConverter.h"
30 #include "JSContactName.h"
31
32 #define CONTACT_CLASS_NAME                                      "ContactName"
33 #define CONTACT_PROP_ATTR_PREFIX                        "prefix"
34 #define CONTACT_PROP_ATTR_FIRST_NAME            "firstName"
35 #define CONTACT_PROP_ATTR_MIDDLE_NAME           "middleName"
36 #define CONTACT_PROP_ATTR_LAST_NAME                     "lastName"
37 #define CONTACT_PROP_ATTR_NICKNAMES                     "nicknames"
38 #define CONTACT_PROP_ATTR_PHONETIC_NAME         "phoneticName"
39 #define CONTACT_PROP_ATTR_DISPLAY_NAME          "displayName"
40
41 namespace TizenApis {
42 namespace Tizen1_0 {
43 namespace Contact {
44
45 using namespace TizenApis::Commons;
46 using namespace TizenApis::Api::Contact;
47
48 JSClassRef JSContactName::m_classRef = NULL;
49
50 JSClassDefinition JSContactName::m_classInfo =
51 {
52         0,
53         kJSClassAttributeNone,
54         CONTACT_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,
66         NULL,
67         NULL,
68         NULL, //ConvertToType,
69 };
70
71 JSStaticValue JSContactName::m_property[] = {
72         { CONTACT_PROP_ATTR_PREFIX, getPrefix, setPrefix, kJSPropertyAttributeNone },
73         { CONTACT_PROP_ATTR_FIRST_NAME, getFirstName, setFirstName, kJSPropertyAttributeNone },
74         { CONTACT_PROP_ATTR_MIDDLE_NAME, getMiddleName, setMiddleName, kJSPropertyAttributeNone },
75         { CONTACT_PROP_ATTR_LAST_NAME, getLastName, setLastName, kJSPropertyAttributeNone },
76         { CONTACT_PROP_ATTR_NICKNAMES, getNicknames, setNicknames, kJSPropertyAttributeNone },
77         { CONTACT_PROP_ATTR_PHONETIC_NAME, getPhoneticName, setPhoneticName, kJSPropertyAttributeNone },
78         { CONTACT_PROP_ATTR_DISPLAY_NAME, getDisplayName, setDisplayName, kJSPropertyAttributeNone },
79         { 0, 0, 0, 0 }
80 };
81
82 JSStaticFunction JSContactName::m_functions[] =
83 {
84         { 0, 0, 0 }
85 };
86
87 JSClassRef JSContactName::getClassRef()
88 {
89         if (!m_classRef) {
90                 m_classRef = JSClassCreate(&m_classInfo);
91         }
92         return m_classRef;
93 }
94
95 JSValueRef JSContactName::createJSObject(JSContextRef context,
96                 const std::string& prefix,
97                 const std::string& firstName,
98                 const std::string& middleName,
99                 const std::string& lastName,
100                 const StringArrayPtr& nicknames,
101                 const std::string& phoneticName,
102                 const std::string& displayName)
103 {
104         ContactNamePtr privateData = ContactNamePtr(new ContactName());
105         privateData->setPrefix(prefix);
106         privateData->setFirstName(firstName);
107         privateData->setMiddleName(middleName);
108         privateData->setLastName(lastName);
109         privateData->setNicknames(nicknames);
110         privateData->setPhoneticName(phoneticName);
111         privateData->setDisplayName(displayName);
112         JSContactNamePriv *priv = new JSContactNamePriv(context, privateData);
113         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
114         if (NULL == jsValueRef)
115         {
116                 LogError("object creation error");
117                 return JSValueMakeUndefined(context);
118         }
119         return jsValueRef;
120 }
121
122 bool JSContactName::isObjectOfClass(JSContextRef context, JSValueRef value)
123 {
124         return JSValueIsObjectOfClass(context, value, getClassRef());
125 }
126
127 ContactNamePtr JSContactName::getContactName(JSContextRef context, JSValueRef value)
128 {
129         if (!isObjectOfClass(context, value)) {
130                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
131         }
132
133         JSObjectRef object = JSValueToObject(context, value, NULL);
134         if (!object) {
135                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
136         }
137
138         JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(object));
139         if (!priv) {
140                 Throw(WrtDeviceApis::Commons::NullPointerException);
141         }
142
143         return priv->getObject();
144 }
145
146 void JSContactName::Initialize(JSContextRef context, JSObjectRef object)
147 {
148         assert(NULL != JSObjectGetPrivate(object));
149 }
150
151 void JSContactName::Finalize(JSObjectRef object)
152 {
153         //delete (JSObjectGetPrivate(object));
154 }
155
156 ContactNamePtr JSContactName::getPrivData(JSObjectRef object)
157 {
158         //LogDebug("entered");
159         JSContactNamePriv *priv = static_cast<JSContactNamePriv*>(JSObjectGetPrivate(object));
160         if (!priv) {
161                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
162         }
163
164         ContactNamePtr result = priv->getObject();
165         if (!result) {
166                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
167         }
168
169         return result;
170 }
171
172 JSValueRef JSContactName::getPrefix(JSContextRef context,
173                 JSObjectRef object,
174                 JSStringRef propertyName,
175                 JSValueRef* exception)
176 {
177         //LogDebug("entered");
178         Try
179         {
180                 ContactConverterFactory::ConverterType converter =
181                                 ContactConverterFactory::getConverter(context);
182                 ContactNamePtr contactProperties = getPrivData(object);
183                 return converter->toJSValueRef(contactProperties->getPrefix());
184         }
185         Catch(WrtDeviceApis::Commons::Exception)
186         {
187                 LogWarning("trying to get incorrect value");
188         }
189
190         return JSValueMakeUndefined(context);
191 }
192
193 bool JSContactName::setPrefix(JSContextRef context,
194                 JSObjectRef object,
195                 JSStringRef propertyName,
196                 JSValueRef value,
197                 JSValueRef* exception)
198 {
199         Try
200         {
201                 ContactNamePtr contactProperties = getPrivData(object);
202                 ContactConverterFactory::ConverterType converter =
203                                 ContactConverterFactory::getConverter(context);
204                 contactProperties->setPrefix(converter->toString(value));
205                 return true;
206         }
207         Catch(WrtDeviceApis::Commons::Exception)
208         {
209                 LogWarning("trying to set incorrect value");
210         }
211
212         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
213         return false;
214 }
215
216 JSValueRef JSContactName::getFirstName(JSContextRef context,
217                 JSObjectRef object,
218                 JSStringRef propertyName,
219                 JSValueRef* exception)
220 {
221         //LogDebug("entered");
222         Try
223         {
224                 ContactConverterFactory::ConverterType converter =
225                                 ContactConverterFactory::getConverter(context);
226                 ContactNamePtr contactProperties = getPrivData(object);
227                 return converter->toJSValueRef(contactProperties->getFirstName());
228         }
229         Catch(WrtDeviceApis::Commons::Exception)
230         {
231                 LogWarning("trying to get incorrect value");
232         }
233
234         return JSValueMakeUndefined(context);
235 }
236
237 bool JSContactName::setFirstName(JSContextRef context,
238                 JSObjectRef object,
239                 JSStringRef propertyName,
240                 JSValueRef value,
241                 JSValueRef* exception)
242 {
243         Try
244         {
245                 ContactNamePtr contactProperties = getPrivData(object);
246                 ContactConverterFactory::ConverterType converter =
247                                 ContactConverterFactory::getConverter(context);
248                 contactProperties->setFirstName(converter->toString(value));
249                 return true;
250         }
251         Catch(WrtDeviceApis::Commons::Exception)
252         {
253                 LogWarning("trying to set incorrect value");
254         }
255
256         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
257         return false;
258 }
259
260 JSValueRef JSContactName::getMiddleName(JSContextRef context,
261                 JSObjectRef object,
262                 JSStringRef propertyName,
263                 JSValueRef* exception)
264 {
265         //LogDebug("entered");
266         Try
267         {
268                 ContactConverterFactory::ConverterType converter =
269                                 ContactConverterFactory::getConverter(context);
270                 ContactNamePtr contactProperties = getPrivData(object);
271                 return converter->toJSValueRef(contactProperties->getMiddleName());
272         }
273         Catch(WrtDeviceApis::Commons::Exception)
274         {
275                 LogWarning("trying to get incorrect value");
276         }
277
278         return JSValueMakeUndefined(context);
279 }
280
281 bool JSContactName::setMiddleName(JSContextRef context,
282                 JSObjectRef object,
283                 JSStringRef propertyName,
284                 JSValueRef value,
285                 JSValueRef* exception)
286 {
287         Try
288         {
289                 ContactNamePtr contactProperties = getPrivData(object);
290                 ContactConverterFactory::ConverterType converter =
291                                 ContactConverterFactory::getConverter(context);
292                 contactProperties->setMiddleName(converter->toString(value));
293                 return true;
294         }
295         Catch(WrtDeviceApis::Commons::Exception)
296         {
297                 LogWarning("trying to set incorrect value");
298         }
299
300         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
301         return false;
302 }
303
304 JSValueRef JSContactName::getLastName(JSContextRef context,
305                 JSObjectRef object,
306                 JSStringRef propertyName,
307                 JSValueRef* exception)
308 {
309         //LogDebug("entered");
310         Try
311         {
312                 ContactConverterFactory::ConverterType converter =
313                                 ContactConverterFactory::getConverter(context);
314                 ContactNamePtr contactProperties = getPrivData(object);
315                 return converter->toJSValueRef(contactProperties->getLastName());
316         }
317         Catch(WrtDeviceApis::Commons::Exception)
318         {
319                 LogWarning("trying to get incorrect value");
320         }
321
322         return JSValueMakeUndefined(context);
323 }
324
325 bool JSContactName::setLastName(JSContextRef context,
326                 JSObjectRef object,
327                 JSStringRef propertyName,
328                 JSValueRef value,
329                 JSValueRef* exception)
330 {
331         Try
332         {
333                 ContactNamePtr contactProperties = getPrivData(object);
334                 ContactConverterFactory::ConverterType converter =
335                                 ContactConverterFactory::getConverter(context);
336                 contactProperties->setLastName(converter->toString(value));
337                 return true;
338         }
339         Catch(WrtDeviceApis::Commons::Exception)
340         {
341                 LogWarning("trying to set incorrect value");
342         }
343
344         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
345         return false;
346 }
347
348 JSValueRef JSContactName::getNicknames(JSContextRef context,
349                 JSObjectRef object,
350                 JSStringRef propertyName,
351                 JSValueRef* exception)
352 {
353         //LogDebug("entered");
354         Try
355         {
356                 ContactConverterFactory::ConverterType converter =
357                                 ContactConverterFactory::getConverter(context);
358                 ContactNamePtr contactProperties = getPrivData(object);
359                 return converter->toJSValueRef(contactProperties->getNicknames());
360         }
361         Catch(WrtDeviceApis::Commons::Exception)
362         {
363                 LogWarning("trying to get incorrect value");
364         }
365
366         return JSValueMakeUndefined(context);
367 }
368
369 bool JSContactName::setNicknames(JSContextRef context,
370                 JSObjectRef object,
371                 JSStringRef propertyName,
372                 JSValueRef value,
373                 JSValueRef* exception)
374 {
375         Try
376         {
377                 ContactNamePtr contactProperties = getPrivData(object);
378                 ContactConverterFactory::ConverterType converter =
379                                 ContactConverterFactory::getConverter(context);
380                 contactProperties->setNicknames(converter->toStringArray(value));
381                 return true;
382         }
383         Catch(WrtDeviceApis::Commons::Exception)
384         {
385                 LogWarning("trying to set incorrect value");
386         }
387
388         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
389         return false;
390 }
391
392 JSValueRef JSContactName::getPhoneticName(JSContextRef context,
393                 JSObjectRef object,
394                 JSStringRef propertyName,
395                 JSValueRef* exception)
396 {
397         //LogDebug("entered");
398         Try
399         {
400                 ContactConverterFactory::ConverterType converter =
401                                 ContactConverterFactory::getConverter(context);
402                 ContactNamePtr contactProperties = getPrivData(object);
403                 return converter->toJSValueRef(contactProperties->getPhoneticName());
404         }
405         Catch(WrtDeviceApis::Commons::Exception)
406         {
407                 LogWarning("trying to get incorrect value");
408         }
409
410         return JSValueMakeUndefined(context);
411 }
412
413 bool JSContactName::setPhoneticName(JSContextRef context,
414                 JSObjectRef object,
415                 JSStringRef propertyName,
416                 JSValueRef value,
417                 JSValueRef* exception)
418 {
419         Try
420         {
421                 ContactNamePtr contactProperties = getPrivData(object);
422                 ContactConverterFactory::ConverterType converter =
423                                 ContactConverterFactory::getConverter(context);
424                 contactProperties->setPhoneticName(converter->toString(value));
425                 return true;
426         }
427         Catch(WrtDeviceApis::Commons::Exception)
428         {
429                 LogWarning("trying to set incorrect value");
430         }
431
432         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
433         return false;
434 }
435
436 JSValueRef JSContactName::getDisplayName(JSContextRef context,
437                 JSObjectRef object,
438                 JSStringRef propertyName,
439                 JSValueRef* exception)
440 {
441         //LogDebug("entered");
442         Try
443         {
444                 ContactConverterFactory::ConverterType converter =
445                                 ContactConverterFactory::getConverter(context);
446                 ContactNamePtr contactProperties = getPrivData(object);
447                 return converter->toJSValueRef(contactProperties->getDisplayName());
448         }
449         Catch(WrtDeviceApis::Commons::Exception){
450         LogWarning("trying to get incorrect value");
451 }
452         return JSValueMakeUndefined(context);
453 }
454
455 bool JSContactName::setDisplayName(JSContextRef context,
456                 JSObjectRef object,
457                 JSStringRef propertyName,
458                 JSValueRef value,
459                 JSValueRef* exception)
460 {
461         Try
462         {
463                 ContactNamePtr contactProperties = getPrivData(object);
464                 ContactConverterFactory::ConverterType converter =
465                                 ContactConverterFactory::getConverter(context);
466                 contactProperties->setDisplayName(converter->toString(value));
467                 return true;
468         }
469         Catch(WrtDeviceApis::Commons::Exception)
470         {
471                 LogWarning("trying to set incorrect value");
472         }
473
474         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
475         return false;
476 }
477
478 } // Contact
479 } // Tizen1_0
480 } // TizenApis