merge wrt-plugins-tizen_0.2.0-3
[platform/framework/web/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactAnniversary.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        JSContactAnniversary.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSContactAnniversary class
22  */
23
24 #include <dpl/shared_ptr.h>
25 #include <CommonsJavaScript/Validator.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 //#include <Tizen/Common/JSGlobalContextFactory.h>
29 #include "ContactConverter.h"
30 #include "JSContactAnniversary.h"
31
32 #define FILTER_CLASS_NAME "Anniversary"
33 #define CONTACT_ATTR_DATE "date"
34 #define CONTACT_ATTR_LABEL "label"
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38 namespace Contact {
39
40 using namespace TizenApis::Commons;
41 using namespace TizenApis::Api::Contact;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
44
45 JSClassDefinition JSContactAnniversary::m_classInfo =
46 {
47         0,
48         kJSClassAttributeNone,
49         FILTER_CLASS_NAME,
50         NULL,
51         m_property,
52         m_functions,
53         Initialize,
54         Finalize,
55         NULL, //hasProperty,
56         NULL, //GetProperty,
57         NULL, //SetProperty,
58         NULL, //DeleteProperty,
59         NULL, //getPropertyNames,
60         NULL, //CallAsFunction,
61         constructor, //CallAsConstructor,
62         hasInstance, //HasInstance,
63         NULL, //ConvertToType,
64 };
65
66 JSStaticValue JSContactAnniversary::m_property[] = {
67         { CONTACT_ATTR_DATE, getDate, setDate, kJSPropertyAttributeNone },
68         { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
69         { 0, 0, 0, 0 }
70 };
71
72 JSStaticFunction JSContactAnniversary::m_functions[] =
73 {
74         { 0, 0, 0 }
75 };
76
77 JSClassRef JSContactAnniversary::m_classRef = JSClassCreate(&m_classInfo);
78
79 JSClassRef JSContactAnniversary::getClassRef() {
80         if (!m_classRef) {
81                 m_classRef = JSClassCreate(&m_classInfo);
82         }
83         return m_classRef;
84 }
85
86 bool JSContactAnniversary::isObjectOfClass(JSContextRef context, JSValueRef value)
87 {
88         return JSValueIsObjectOfClass(context, value, getClassRef());
89 }
90
91 ContactAnniversaryPtr JSContactAnniversary::getContactAnniversary(JSContextRef context, JSValueRef value)
92 {
93         if (!isObjectOfClass(context, value)) {
94                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
95         }
96         JSObjectRef object = JSValueToObject(context, value, NULL);
97         if (!object) {
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99         }
100         JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
101         if (!priv) {
102                 Throw(WrtDeviceApis::Commons::NullPointerException);
103         }
104         return priv->getObject();
105 }
106
107 void JSContactAnniversary::Initialize(JSContextRef context, JSObjectRef object)
108 {
109         if (!JSObjectGetPrivate(object))
110         {
111                 ContactAnniversaryPtr anniversary(new ContactAnniversary());
112                 JSContactAnniversaryPriv *priv = new JSContactAnniversaryPriv(context, ContactAnniversaryPtr(anniversary));
113                 if (!JSObjectSetPrivate(object, priv)) {
114                         delete priv;
115                 }
116         }
117 }
118
119 void JSContactAnniversary::Finalize(JSObjectRef object)
120 {
121         JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
122
123         if (priv != NULL)
124                 delete (priv);
125 }
126
127 JSObjectRef JSContactAnniversary::createJSObject(JSContextRef context, ContactAnniversaryPtr contactAnniversary)
128 {
129         JSContactAnniversaryPriv *priv = new JSContactAnniversaryPriv(context, contactAnniversary);
130         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
131         if (NULL == jsObjectRef) {
132                 LogError("object creation error");
133                 return NULL;
134         }
135         return jsObjectRef;
136 }
137
138 ContactAnniversaryPtr JSContactAnniversary::getPrivData(JSObjectRef object)
139 {
140         JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
141         if (!priv) {
142                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
143         }
144         ContactAnniversaryPtr result = priv->getObject();
145         if (!result) {
146                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147         }
148         return result;
149 }
150
151 JSObjectRef JSContactAnniversary::constructor(JSContextRef context,
152                 JSObjectRef constructor,
153                 size_t argumentCount,
154                 const JSValueRef arguments[],
155                 JSValueRef* exception)
156 {
157         LogDebug("entered");
158
159 //      AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
160 //      TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
161
162         JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(constructor));
163         if (!priv) {
164                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
165         }
166         JSContextRef gContext = priv->getContext();
167
168 //      JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
169
170         BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
171         Try {
172                 if (argumentCount < 1 || argumentCount > 2)
173                         ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
174
175                 if (!JSValueIsObject(gContext, arguments[0]))
176                         ThrowMsg(InvalidArgumentException, "1st argument is not object.");
177
178                 if (argumentCount >= 2)
179                 {
180                         if (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
181                                 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
182                 }
183
184         } Catch(Exception ) {
185                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
186                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
187                 return NULL;
188         }
189
190         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
191
192         std::tm date;
193         std::string label;
194         bool labelIsSet = false;
195
196         Try {
197                 date = converter->toDateTm(arguments[0]);
198         } Catch(Exception) {
199                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
200                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
201                 return NULL;
202         }
203
204         Try {
205                 if(argumentCount >= 2)
206                 {
207                         if(!JSValueIsNull(gContext, arguments[1]))
208                         {
209                                 label = converter->toString(arguments[1]);
210                                 labelIsSet = true;
211                         }
212                 }
213         } Catch(Exception) {
214                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
215                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
216                 return NULL;
217         }
218
219         ContactAnniversaryPtr contactAnniversary(new ContactAnniversary());
220         contactAnniversary->setDate(date);
221
222         // NOTE: Currently not support label field
223         //if(labelIsSet)
224         //      contactAnniversary->setLabel(label);
225
226         JSObjectRef jsobject;
227
228         Try {
229                 jsobject = createJSObject(gContext, contactAnniversary);
230         } Catch(Exception) {
231                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
232                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
233                 return NULL;
234         }
235
236         return jsobject;
237 }
238
239 bool JSContactAnniversary::hasInstance(JSContextRef context,
240                 JSObjectRef constructor,
241                 JSValueRef possibleInstance,
242                 JSValueRef* exception)
243 {
244         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
245 }
246
247 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
248                 JSObjectRef object,
249                 JSStringRef propertyName,
250                 JSValueRef* exception)
251 {
252         Try
253         {
254                 ContactConverterFactory::ConverterType converter =
255                                 ContactConverterFactory::getConverter(context);
256                 ContactAnniversaryPtr anniversary = getPrivData(object);
257                 return converter->toJSValueRef(anniversary->getDate());
258         }
259         Catch(WrtDeviceApis::Commons::Exception)
260         {
261                 LogWarning("trying to get incorrect value");
262         }
263         return JSValueMakeUndefined(context);
264 }
265
266 bool JSContactAnniversary::setDate(JSContextRef context,
267                 JSObjectRef object,
268                 JSStringRef propertyName,
269                 JSValueRef value,
270                 JSValueRef* exception)
271 {
272         Try
273         {
274                 ContactAnniversaryPtr anniversary = getPrivData(object);
275                 ContactConverterFactory::ConverterType converter =
276                                 ContactConverterFactory::getConverter(context);
277                 anniversary->setDate(converter->toDateTm(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 JSContactAnniversary::getLabel(JSContextRef context,
289                 JSObjectRef object,
290                 JSStringRef propertyName,
291                 JSValueRef* exception)
292 {
293         Try
294         {
295                 ContactConverterFactory::ConverterType converter =
296                                 ContactConverterFactory::getConverter(context);
297                 ContactAnniversaryPtr anniversary = getPrivData(object);
298                 if(!anniversary->getLabelIsSet())
299                         return JSValueMakeNull(context);
300                 else
301                         return converter->toJSValueRef(anniversary->getLabel());
302         }
303         Catch(WrtDeviceApis::Commons::Exception)
304         {
305                 LogWarning("trying to get incorrect value");
306         }
307         return JSValueMakeUndefined(context);
308 }
309
310 bool JSContactAnniversary::setLabel(JSContextRef context,
311                 JSObjectRef object,
312                 JSStringRef propertyName,
313                 JSValueRef value,
314                 JSValueRef* exception)
315 {
316         Try
317         {
318                 // NOTE: Currently not support this field
319                 //ContactAnniversaryPtr anniversary = getPrivData(object);
320                 //ContactConverterFactory::ConverterType converter =
321                 //              ContactConverterFactory::getConverter(context);
322                 //anniversary->setLabel(converter->toString(value));
323                 return true;
324         }
325         Catch(WrtDeviceApis::Commons::Exception)
326         {
327                 LogWarning("trying to set incorrect value");
328         }
329         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
330         return false;
331 }
332
333 } // Contact
334 } // Tizen1_0
335 } // TizenApis