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