tizen 2.3.1 release
[framework/web/mobile/wrt-plugins-tizen.git] / src / Contact / JSContactAnniversary.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 #include "JSContactAnniversary.h"
19
20 #include <Logger.h>
21 #include <Export.h>
22 #include <ArgumentValidator.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include <JSUtil.h>
25
26 namespace DeviceAPI {
27 namespace Contact {
28
29 using namespace DeviceAPI::Common;
30
31 struct ContactAnniversaryHolder {
32     ContactAnniversaryPtr ptr;
33 };
34
35 namespace {
36 const char* CONTACT_CONTACT_ANNIVERSARY = "ContactAnniversary";
37
38 const char* CONTACT_CONTACT_ANNIVERSARY_DATE = "date";
39 const char* CONTACT_CONTACT_ANNIVERSARY_LABEL = "label";
40 }
41
42 JSClassDefinition JSContactAnniversary::m_classInfo = {
43         0,
44         kJSClassAttributeNone,
45         CONTACT_CONTACT_ANNIVERSARY,
46         NULL,
47         JSContactAnniversary::m_property,
48         NULL, //m_function,
49         JSContactAnniversary::initialize,
50         JSContactAnniversary::finalize,
51         NULL, //hasProperty,
52         NULL, //getProperty,
53         NULL, //setProperty,
54         NULL, //deleteProperty,
55         NULL, //getPropertyNames,
56         NULL, //function,
57         NULL, //constructor,
58         NULL, //hasInstance
59         NULL, //convertToType,
60 };
61
62 JSStaticValue JSContactAnniversary::m_property[] = {
63         { CONTACT_CONTACT_ANNIVERSARY_DATE, getDate, setDate, kJSPropertyAttributeDontDelete },
64         { CONTACT_CONTACT_ANNIVERSARY_LABEL, getLabel, setLabel, kJSPropertyAttributeDontDelete },
65         { 0, 0, 0, 0 }
66 };
67
68 const JSClassDefinition* JSContactAnniversary::getClassInfo()
69 {
70     return &m_classInfo;
71 }
72
73 JSClassRef JSContactAnniversary::m_jsClassRef = JSClassCreate(JSContactAnniversary::getClassInfo());
74
75 JSClassRef DLL_EXPORT JSContactAnniversary::getClassRef()
76 {
77     if (!m_jsClassRef) {
78         m_jsClassRef = JSClassCreate(&m_classInfo);
79     }
80     return m_jsClassRef;
81 }
82
83 ContactAnniversaryPtr JSContactAnniversary::getPrivateObject(JSContextRef context, JSValueRef value)
84 {
85     if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
86         LOGE("Type mismatch");
87         throw TypeMismatchException("Type mismatch");
88     }
89
90     JSObjectRef object = JSUtil::JSValueToObject(context, value);
91     ContactAnniversaryHolder* priv = static_cast<ContactAnniversaryHolder*>(JSObjectGetPrivate(object));
92     if (!priv) {
93         LOGE("Priv is null");
94         throw UnknownException("Priv is null");
95     }
96     return priv->ptr;
97 }
98
99 void JSContactAnniversary::setPrivateObject(JSObjectRef object, ContactAnniversaryPtr native)
100 {
101     ContactAnniversaryHolder* priv = static_cast<ContactAnniversaryHolder*>(JSObjectGetPrivate(object));
102     if (!priv) {
103         LOGE("Priv is null");
104         throw UnknownException("Priv is null");
105     }
106     priv->ptr = native;
107 }
108
109 JSObjectRef JSContactAnniversary::makeJSObject(JSContextRef context,
110         ContactAnniversaryPtr native)
111 {
112     if (!native) {
113         LOGE("Native is null");
114         throw UnknownException("Native is null");
115     }
116
117     ContactAnniversaryHolder* priv = new(std::nothrow) ContactAnniversaryHolder();
118     if (!priv) {
119         LOGE("Priv is null");
120         throw UnknownException("Priv is null");
121     }
122     priv->ptr = native;
123
124     JSObjectRef obj = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
125     return obj;
126 }
127
128 void JSContactAnniversary::initialize(JSContextRef context, JSObjectRef object)
129 {
130     LOGD("Entered");
131 }
132
133 void JSContactAnniversary::finalize(JSObjectRef object)
134 {
135     LOGD("Entered");
136
137     ContactAnniversaryHolder* priv = static_cast<ContactAnniversaryHolder*>(JSObjectGetPrivate(object));
138     if (priv) {
139         JSObjectSetPrivate(object, NULL);
140         delete priv;
141         priv = NULL;
142     }
143 }
144
145 JSObjectRef DLL_EXPORT JSContactAnniversary::constructor(JSContextRef context,
146         JSObjectRef constructor,
147         size_t argumentCount,
148         const JSValueRef arguments[],
149         JSValueRef* exception)
150 {
151     LOGD("Entered");
152     ArgumentValidator validator(context, argumentCount, arguments);
153
154     JSObjectRef jsObjRef = JSObjectMake(context, getClassRef(), NULL);
155
156     JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
157     JSObjectSetProperty(context, jsObjRef, ctorName, constructor, kJSPropertyAttributeReadOnly
158             | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
159     JSStringRelease(ctorName);
160
161     try {
162         std::string label;
163
164         label = validator.toString(1, true, "");
165         ContactAnniversaryPtr priv = ContactAnniversaryPtr(new(std::nothrow) ContactAnniversary());
166         if (!priv) {
167             LOGE("Priv is null");
168             throw UnknownException("Priv is null");
169         }
170
171         tm tm_ = WrtDeviceApis::CommonsJavaScript::Converter(context).toDateTm(arguments[0]);
172         priv->setDate(tm_);
173
174         if (!validator.isNull(1) && !validator.isOmitted(1)) {
175             priv->setLabel(label);
176         }
177
178         ContactAnniversaryHolder* holder = new(std::nothrow) ContactAnniversaryHolder();
179         if (!holder) {
180             LOGE("Holder is null");
181             throw UnknownException("Holder is null");
182         }
183         holder->ptr = priv;
184         JSObjectSetPrivate(jsObjRef, static_cast<void*>(holder));
185     }
186     catch (const BasePlatformException &error) {
187         LOGE("ContactAnniversary creation failed: %s", error.getMessage().c_str());
188     }
189     catch (...) {
190         LOGE("ContactAnniversary creation failed");
191     }
192
193     return jsObjRef;
194 }
195
196 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
197         JSObjectRef object,
198         JSStringRef propertyName,
199         JSValueRef* exception)
200 {
201     LOGD("Entered");
202     try {
203         ContactAnniversaryPtr priv = JSContactAnniversary::getPrivateObject(context, object);
204         if (!priv) {
205             LOGE("Priv is null");
206             throw UnknownException("Priv is null");
207         }
208         return WrtDeviceApis::CommonsJavaScript::Converter(context).toJSValueRef(priv->getDate());
209     }
210     catch (const BasePlatformException &error) {
211         LOGE("Failed to get contact anniversary date. %s : %s", error.getName().c_str(), error.getMessage().c_str());
212     }
213     catch (...) {
214         LOGE("Unsupported error while getting contact anniversary date.");
215     }
216     return JSValueMakeUndefined(context);
217 }
218
219 JSValueRef JSContactAnniversary::getLabel(JSContextRef context,
220         JSObjectRef object,
221         JSStringRef propertyName,
222         JSValueRef* exception)
223 {
224     LOGD("Entered");
225     try {
226         ContactAnniversaryPtr priv = JSContactAnniversary::getPrivateObject(context, object);
227         if (!priv) {
228             LOGE("Priv is null");
229             throw UnknownException("Priv is null");
230         }
231         if (!priv->isLabelSet()) {
232             return JSValueMakeNull(context);
233         }
234         return JSUtil::toJSValueRef(context, priv->getLabel());
235     }
236     catch (const BasePlatformException &error) {
237         LOGE("Failed to get contact anniversary label. %s : %s", error.getName().c_str(), error.getMessage().c_str());
238     }
239     catch (...) {
240         LOGE("Unsupported error while getting contact anniversary label.");
241     }
242     return JSValueMakeUndefined(context);
243 }
244
245 bool JSContactAnniversary::setDate(JSContextRef context,
246         JSObjectRef object,
247         JSStringRef propertyName,
248         JSValueRef value,
249         JSValueRef* exception)
250 {
251     LOGD("Entered");
252     try {
253         ContactAnniversaryPtr priv = JSContactAnniversary::getPrivateObject(context, object);
254         if (!priv) {
255             LOGE("Priv is null");
256             throw UnknownException("Priv is null");
257         }
258         tm tm_ = WrtDeviceApis::CommonsJavaScript::Converter(context).toDateTm(value);
259         priv->setDate(tm_);
260     }
261     catch (const BasePlatformException &error) {
262         LOGE("Failed to set contact anniversary date. %s : %s", error.getName().c_str(), error.getMessage().c_str());
263     }
264     catch (...) {
265         LOGE("Unsupported error while setting contact anniversary date.");
266     }
267     return true;
268 }
269
270 bool JSContactAnniversary::setLabel(JSContextRef context,
271         JSObjectRef object,
272         JSStringRef propertyName,
273         JSValueRef value,
274         JSValueRef* exception)
275 {
276     LOGD("Entered");
277     try {
278         ContactAnniversaryPtr priv = JSContactAnniversary::getPrivateObject(context, object);
279         if (!priv) {
280             LOGE("Priv is null");
281             throw UnknownException("Priv is null");
282         }
283         if (JSValueIsNull(context, value)) {
284             priv->unsetLabel();
285         }
286         else {
287             std::string label = JSUtil::JSValueToString(context, value);
288             priv->setLabel(label);
289         }
290     }
291     catch (const BasePlatformException &error) {
292         LOGE("Failed to set contact anniversary label. %s : %s", error.getName().c_str(), error.getMessage().c_str());
293     }
294     catch (...) {
295         LOGE("Unsupported error while setting contact anniversary label.");
296     }
297     return true;
298 }
299
300 } // Contact
301 } // DeviceAPI