Update change log and spec for wrt-plugins-tizen_0.4.25-1
[platform/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         JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(constructor));
157         if (!priv) {
158                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
159         }
160         JSContextRef gContext = priv->getContext();
161
162         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
163
164         std::tm date;
165         std::string label;
166         ContactAnniversaryPtr contactAnniversary(new ContactAnniversary());
167
168         try {
169                 ArgumentValidator Argvalidator(context, argumentCount, arguments);
170                 Argvalidator.toObject(0, false);
171
172                 if (argumentCount >= 2){
173                         label = Argvalidator.toString(1, true);
174                         contactAnniversary->setLabel(label);
175                 }
176         } catch (const TypeMismatchException& err ) {
177                 JSWebAPIError::throwException(context, exception, err);
178                 return NULL;
179         } catch(const BasePlatformException& err) {
180                 JSWebAPIError::throwException(context, exception, err);
181                 return NULL;
182         } catch(const ConversionException& err) {
183                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
184                 return NULL;
185         } catch(const NullPointerException& err) {
186                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
187                 return NULL;
188         }
189
190
191         Try {
192                 date = converter->toDateTm(arguments[0]);
193                 contactAnniversary->setDate(date);
194         } Catch(Exception) {
195                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
196                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'Date object'");
197                 return NULL;
198         }
199
200         JSObjectRef jsobject;
201
202         Try {
203                 jsobject = createJSObject(gContext, contactAnniversary);
204         } Catch(Exception) {
205                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
206                 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
207                 return NULL;
208         }
209
210         return jsobject;
211 }
212
213 bool JSContactAnniversary::hasInstance(JSContextRef context,
214                 JSObjectRef constructor,
215                 JSValueRef possibleInstance,
216                 JSValueRef* exception)
217 {
218         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
219 }
220
221 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
222                 JSObjectRef object,
223                 JSStringRef propertyName,
224                 JSValueRef* exception)
225 {
226         Try
227         {
228                 ContactConverterFactory::ConverterType converter =
229                                 ContactConverterFactory::getConverter(context);
230                 ContactAnniversaryPtr anniversary = getPrivData(object);
231                 return converter->toJSValueRef(anniversary->getDate());
232         }
233         Catch(WrtDeviceApis::Commons::Exception)
234         {
235                 LoggerW("trying to get incorrect value");
236         }
237         return JSValueMakeUndefined(context);
238 }
239
240 bool JSContactAnniversary::setDate(JSContextRef context,
241                 JSObjectRef object,
242                 JSStringRef propertyName,
243                 JSValueRef value,
244                 JSValueRef* exception)
245 {
246         Try
247         {
248                 ContactAnniversaryPtr anniversary = getPrivData(object);
249                 ContactConverterFactory::ConverterType converter =
250                                 ContactConverterFactory::getConverter(context);
251                 anniversary->setDate(converter->toDateTm(value));
252         }
253         Catch(WrtDeviceApis::Commons::Exception)
254         {
255                 LoggerW("trying to set incorrect value");
256                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
257         }
258         return true;
259 }
260
261 JSValueRef JSContactAnniversary::getLabel(JSContextRef context,
262                 JSObjectRef object,
263                 JSStringRef propertyName,
264                 JSValueRef* exception)
265 {
266         Try
267         {
268                 ContactConverterFactory::ConverterType converter =
269                                 ContactConverterFactory::getConverter(context);
270                 ContactAnniversaryPtr anniversary = getPrivData(object);
271                 if(!anniversary->getLabelIsSet())
272                         return JSValueMakeNull(context);
273                 else
274                         return converter->toJSValueRef(anniversary->getLabel());
275         }
276         Catch(WrtDeviceApis::Commons::Exception)
277         {
278                 LoggerW("trying to get incorrect value");
279         }
280         return JSValueMakeUndefined(context);
281 }
282
283 bool JSContactAnniversary::setLabel(JSContextRef context,
284                 JSObjectRef object,
285                 JSStringRef propertyName,
286                 JSValueRef value,
287                 JSValueRef* exception)
288 {
289         Try
290         {
291                 ContactAnniversaryPtr anniversary = getPrivData(object);
292                 ContactConverterFactory::ConverterType converter =
293                                 ContactConverterFactory::getConverter(context);
294                 BasicValidator validator =
295                                 BasicValidatorFactory::getValidator(context, exception);
296                 if(validator->isNullOrUndefined(value))
297                         anniversary->unsetLabel();
298                 else
299                         anniversary->setLabel(converter->toString(value));
300         }
301         Catch(WrtDeviceApis::Commons::Exception)
302         {
303                 LoggerW("trying to set incorrect value");
304                 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
305         }
306         return true;
307 }
308
309 } // Contact
310 } // DeviceAPI