2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file JSContactAnniversary.cpp
19 * @author Kisub Song (kisubs.song@samsung.com)
21 * @brief Implementation of the JSContactAnniversary class
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"
32 #define FILTER_CLASS_NAME "Anniversary"
33 #define CONTACT_ATTR_DATE "date"
34 #define CONTACT_ATTR_LABEL "label"
40 using namespace TizenApis::Commons;
41 using namespace TizenApis::Api::Contact;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
45 JSClassDefinition JSContactAnniversary::m_classInfo =
48 kJSClassAttributeNone,
58 NULL, //DeleteProperty,
59 NULL, //getPropertyNames,
60 NULL, //CallAsFunction,
61 constructor, //CallAsConstructor,
62 hasInstance, //HasInstance,
63 NULL, //ConvertToType,
66 JSStaticValue JSContactAnniversary::m_property[] = {
67 { CONTACT_ATTR_DATE, getDate, setDate, kJSPropertyAttributeNone },
68 { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
72 JSStaticFunction JSContactAnniversary::m_functions[] =
77 JSClassRef JSContactAnniversary::m_classRef = JSClassCreate(&m_classInfo);
79 JSClassRef JSContactAnniversary::getClassRef() {
81 m_classRef = JSClassCreate(&m_classInfo);
86 bool JSContactAnniversary::isObjectOfClass(JSContextRef context, JSValueRef value)
88 return JSValueIsObjectOfClass(context, value, getClassRef());
91 ContactAnniversaryPtr JSContactAnniversary::getContactAnniversary(JSContextRef context, JSValueRef value)
93 if (!isObjectOfClass(context, value)) {
94 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
96 JSObjectRef object = JSValueToObject(context, value, NULL);
98 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
100 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
102 Throw(WrtDeviceApis::Commons::NullPointerException);
104 return priv->getObject();
107 void JSContactAnniversary::Initialize(JSContextRef context, JSObjectRef object)
109 if (!JSObjectGetPrivate(object))
111 ContactAnniversaryPtr anniversary(new ContactAnniversary());
112 JSContactAnniversaryPriv *priv = new JSContactAnniversaryPriv(context, ContactAnniversaryPtr(anniversary));
113 if (!JSObjectSetPrivate(object, priv)) {
119 void JSContactAnniversary::Finalize(JSObjectRef object)
121 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
127 JSObjectRef JSContactAnniversary::createJSObject(JSContextRef context, ContactAnniversaryPtr contactAnniversary)
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");
138 ContactAnniversaryPtr JSContactAnniversary::getPrivData(JSObjectRef object)
140 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
142 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
144 ContactAnniversaryPtr result = priv->getObject();
146 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
151 JSObjectRef JSContactAnniversary::constructor(JSContextRef context,
152 JSObjectRef constructor,
153 size_t argumentCount,
154 const JSValueRef arguments[],
155 JSValueRef* exception)
159 // AceSecurityStatus status = CONTACT_CHECK_ACCESS(controller->getContext(), CONTACT_FUNCTION_API_ADD);
160 // TIZEN_SYNC_ACCESS_HANDLER(status, context, exception);
162 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(constructor));
164 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
166 JSContextRef gContext = priv->getContext();
168 // JSContextRef gContext = JSGlobalContextFactory::getInstance()->get();
170 BasicValidator validator = BasicValidatorFactory::getValidator(context, exception);
172 if (argumentCount < 1 || argumentCount > 2)
173 ThrowMsg(InvalidArgumentException, "Wrong arguments count.");
175 if (!JSValueIsObject(gContext, arguments[0]))
176 ThrowMsg(InvalidArgumentException, "1st argument is not object.");
178 if (argumentCount >= 2)
180 if (!JSValueIsString(gContext, arguments[1]) && !JSValueIsNull(gContext, arguments[1]))
181 ThrowMsg(InvalidArgumentException, "2nd argument is not string.");
184 } Catch(Exception ) {
185 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
186 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
190 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
194 bool labelIsSet = false;
197 date = converter->toDateTm(arguments[0]);
199 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
200 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
205 if(argumentCount >= 2)
207 if(!JSValueIsNull(gContext, arguments[1]))
209 label = converter->toString(arguments[1]);
214 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
215 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
219 ContactAnniversaryPtr contactAnniversary(new ContactAnniversary());
220 contactAnniversary->setDate(date);
222 // NOTE: Currently not support label field
224 // contactAnniversary->setLabel(label);
226 JSObjectRef jsobject;
229 jsobject = createJSObject(gContext, contactAnniversary);
231 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
232 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
239 bool JSContactAnniversary::hasInstance(JSContextRef context,
240 JSObjectRef constructor,
241 JSValueRef possibleInstance,
242 JSValueRef* exception)
244 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
247 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
249 JSStringRef propertyName,
250 JSValueRef* exception)
254 ContactConverterFactory::ConverterType converter =
255 ContactConverterFactory::getConverter(context);
256 ContactAnniversaryPtr anniversary = getPrivData(object);
257 return converter->toJSValueRef(anniversary->getDate());
259 Catch(WrtDeviceApis::Commons::Exception)
261 LogWarning("trying to get incorrect value");
263 return JSValueMakeUndefined(context);
266 bool JSContactAnniversary::setDate(JSContextRef context,
268 JSStringRef propertyName,
270 JSValueRef* exception)
274 ContactAnniversaryPtr anniversary = getPrivData(object);
275 ContactConverterFactory::ConverterType converter =
276 ContactConverterFactory::getConverter(context);
277 anniversary->setDate(converter->toDateTm(value));
280 Catch(WrtDeviceApis::Commons::Exception)
282 LogWarning("trying to set incorrect value");
284 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
288 JSValueRef JSContactAnniversary::getLabel(JSContextRef context,
290 JSStringRef propertyName,
291 JSValueRef* exception)
295 ContactConverterFactory::ConverterType converter =
296 ContactConverterFactory::getConverter(context);
297 ContactAnniversaryPtr anniversary = getPrivData(object);
298 if(!anniversary->getLabelIsSet())
299 return JSValueMakeNull(context);
301 return converter->toJSValueRef(anniversary->getLabel());
303 Catch(WrtDeviceApis::Commons::Exception)
305 LogWarning("trying to get incorrect value");
307 return JSValueMakeUndefined(context);
310 bool JSContactAnniversary::setLabel(JSContextRef context,
312 JSStringRef propertyName,
314 JSValueRef* exception)
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));
325 Catch(WrtDeviceApis::Commons::Exception)
327 LogWarning("trying to set incorrect value");
329 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");