403c52f97455ddbbf7310c28200d18ed264c3790
[profile/ivi/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         if(labelIsSet)
222                 contactAnniversary->setLabel(label);
223
224         JSObjectRef jsobject;
225
226         Try {
227                 jsobject = createJSObject(gContext, contactAnniversary);
228         } Catch(Exception) {
229                 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
230                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
231                 return NULL;
232         }
233
234         return jsobject;
235 }
236
237 bool JSContactAnniversary::hasInstance(JSContextRef context,
238                 JSObjectRef constructor,
239                 JSValueRef possibleInstance,
240                 JSValueRef* exception)
241 {
242         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
243 }
244
245 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
246                 JSObjectRef object,
247                 JSStringRef propertyName,
248                 JSValueRef* exception)
249 {
250         Try
251         {
252                 ContactConverterFactory::ConverterType converter =
253                                 ContactConverterFactory::getConverter(context);
254                 ContactAnniversaryPtr anniversary = getPrivData(object);
255                 return converter->toJSValueRef(anniversary->getDate());
256         }
257         Catch(WrtDeviceApis::Commons::Exception)
258         {
259                 LogWarning("trying to get incorrect value");
260         }
261         return JSValueMakeUndefined(context);
262 }
263
264 bool JSContactAnniversary::setDate(JSContextRef context,
265                 JSObjectRef object,
266                 JSStringRef propertyName,
267                 JSValueRef value,
268                 JSValueRef* exception)
269 {
270         Try
271         {
272                 ContactAnniversaryPtr anniversary = getPrivData(object);
273                 ContactConverterFactory::ConverterType converter =
274                                 ContactConverterFactory::getConverter(context);
275                 anniversary->setDate(converter->toDateTm(value));
276                 return true;
277         }
278         Catch(WrtDeviceApis::Commons::Exception)
279         {
280                 LogWarning("trying to set incorrect value");
281         }
282         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
283         return false;
284 }
285
286 JSValueRef JSContactAnniversary::getLabel(JSContextRef context,
287                 JSObjectRef object,
288                 JSStringRef propertyName,
289                 JSValueRef* exception)
290 {
291         Try
292         {
293                 ContactConverterFactory::ConverterType converter =
294                                 ContactConverterFactory::getConverter(context);
295                 ContactAnniversaryPtr anniversary = getPrivData(object);
296                 if(!anniversary->getLabelIsSet())
297                         return JSValueMakeNull(context);
298                 else
299                         return converter->toJSValueRef(anniversary->getLabel());
300         }
301         Catch(WrtDeviceApis::Commons::Exception)
302         {
303                 LogWarning("trying to get incorrect value");
304         }
305         return JSValueMakeUndefined(context);
306 }
307
308 bool JSContactAnniversary::setLabel(JSContextRef context,
309                 JSObjectRef object,
310                 JSStringRef propertyName,
311                 JSValueRef value,
312                 JSValueRef* exception)
313 {
314         Try
315         {
316                 ContactAnniversaryPtr anniversary = getPrivData(object);
317                 ContactConverterFactory::ConverterType converter =
318                                 ContactConverterFactory::getConverter(context);
319                 anniversary->setLabel(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 } // Contact
331 } // Tizen1_0
332 } // TizenApis