Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContact.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        JSContact.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSContact class
22  */
23
24 #include <dpl/log/log.h>
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include "ContactConverter.h"
28 #include "JSContactProperties.h"
29 #include "JSContact.h"
30
31 #define FILTER_CLASS_NAME "Contact"
32
33 #define CONTACT_ATTR_ID "id"
34 #define CONTACT_ATTR_READ_ONLY "readOnly"
35 #define CONTACT_ATTR_LAST_UPDATED "lastUpdated"
36
37 namespace TizenApis {
38 namespace Tizen1_0 {
39 namespace Contact {
40
41 using namespace TizenApis::Api::Contact;
42
43 using TizenApis::Api::Contact::Contact;
44
45 JSClassRef JSContact::m_classRef = NULL;
46
47 JSClassDefinition JSContact::m_classInfo =
48 {
49         0,
50         kJSClassAttributeNone,
51         FILTER_CLASS_NAME,
52         JSContactProperties::getClassRef(),
53         m_property,
54         m_functions,
55         Initialize,
56         Finalize,
57         NULL, //hasProperty,
58         NULL, //GetProperty,
59         NULL, //SetProperty,
60         NULL, //DeleteProperty,
61         NULL, //getPropertyNames,
62         NULL,
63         NULL,
64         NULL,
65         NULL, //ConvertToType,
66 };
67
68 JSStaticValue JSContact::m_property[] = {
69         { CONTACT_ATTR_ID, getId, NULL, kJSPropertyAttributeReadOnly },
70         { CONTACT_ATTR_READ_ONLY, getReadOnly, NULL, kJSPropertyAttributeReadOnly },
71         { CONTACT_ATTR_LAST_UPDATED, getLastUpdated, NULL, kJSPropertyAttributeReadOnly },
72         { 0, 0, 0, 0 }
73 };
74
75 JSStaticFunction JSContact::m_functions[] =
76 {
77         { 0, 0, 0 }
78 };
79
80 JSClassRef JSContact::getClassRef() {
81         if (!m_classRef) {
82                 m_classRef = JSClassCreate(&m_classInfo);
83         }
84         return m_classRef;
85 }
86
87 JSValueRef JSContact::createJSObject(JSContextRef context
88                 // FIXME
89                 )
90 {
91         ContactPtr privateData = ContactPtr(new Contact());
92         JSContactPriv *priv = new JSContactPriv(context, privateData);
93         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
94         if (NULL == jsValueRef) {
95                 LogError("object creation error");
96                 return JSValueMakeUndefined(context);
97         }
98         return jsValueRef;
99 }
100
101 bool JSContact::isObjectOfClass(JSContextRef context, JSValueRef value)
102 {
103         return JSValueIsObjectOfClass(context, value, getClassRef());
104 }
105
106 ContactPtr JSContact::getContact(JSContextRef context, JSValueRef value)
107 {
108         if (!isObjectOfClass(context, value)) {
109                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
110         }
111         JSObjectRef object = JSValueToObject(context, value, NULL);
112         if (!object) {
113                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
114         }
115         JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
116         if (!priv) {
117                 Throw(WrtDeviceApis::Commons::NullPointerException);
118         }
119         return priv->getObject();
120 }
121
122 void JSContact::Initialize(JSContextRef context, JSObjectRef object)
123 {
124         assert(NULL != JSObjectGetPrivate(object));
125 }
126
127 void JSContact::Finalize(JSObjectRef object)
128 {
129         //delete (JSObjectGetPrivate(object));
130 }
131
132 ContactPtr JSContact::getPrivData(JSObjectRef object)
133 {
134         LogDebug("entered");
135         JSContactPriv *priv = static_cast<JSContactPriv*>(JSObjectGetPrivate(object));
136         if (!priv) {
137                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
138         }
139         ContactPtr result = priv->getObject();
140         if (!result) {
141                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
142         }
143         return result;
144 }
145
146 JSValueRef JSContact::getId(JSContextRef context,
147                 JSObjectRef object,
148                 JSStringRef propertyName,
149                 JSValueRef* exception)
150 {
151         LogDebug("entered");
152         Try
153         {
154                 ContactConverterFactory::ConverterType converter =
155                                 ContactConverterFactory::getConverter(context);
156                 ContactPtr contact = getPrivData(object);
157                 return converter->toJSValueRef(contact->getId());
158         }
159         Catch(WrtDeviceApis::Commons::Exception)
160         {
161                 LogWarning("trying to get incorrect value");
162         }
163         return JSValueMakeUndefined(context);
164 }
165
166 JSValueRef JSContact::getReadOnly(JSContextRef context,
167                 JSObjectRef object,
168                 JSStringRef propertyName,
169                 JSValueRef* exception)
170 {
171         LogDebug("entered");
172         Try
173         {
174                 ContactConverterFactory::ConverterType converter =
175                                 ContactConverterFactory::getConverter(context);
176                 ContactPtr contact = getPrivData(object);
177                 return converter->toJSValueRef(contact->getReadOnly());
178         }
179         Catch(WrtDeviceApis::Commons::Exception)
180         {
181                 LogWarning("trying to get incorrect value");
182         }
183         return JSValueMakeUndefined(context);
184 }
185
186
187 JSValueRef JSContact::getLastUpdated(JSContextRef context,
188                 JSObjectRef object,
189                 JSStringRef propertyName,
190                 JSValueRef* exception)
191 {
192         //LogDebug("entered");
193         Try
194         {
195                 ContactConverterFactory::ConverterType converter =
196                                 ContactConverterFactory::getConverter(context);
197                 ContactPtr contact = getPrivData(object);
198                 return converter->toJSValueRef(contact->getLastUpdated());
199         }
200         Catch(WrtDeviceApis::Commons::Exception)
201         {
202                 LogWarning("trying to get incorrect value");
203         }
204
205         return JSValueMakeUndefined(context);
206 }
207
208 } // Contact
209 } // Tizen1_0
210 } // TizenApis