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 contactAnniversary->setLabel(label);
224 JSObjectRef jsobject;
227 jsobject = createJSObject(gContext, contactAnniversary);
229 LogError("Argument type mismatch : " << _rethrown_exception.GetMessage());
230 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
237 bool JSContactAnniversary::hasInstance(JSContextRef context,
238 JSObjectRef constructor,
239 JSValueRef possibleInstance,
240 JSValueRef* exception)
242 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
245 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
247 JSStringRef propertyName,
248 JSValueRef* exception)
252 ContactConverterFactory::ConverterType converter =
253 ContactConverterFactory::getConverter(context);
254 ContactAnniversaryPtr anniversary = getPrivData(object);
255 return converter->toJSValueRef(anniversary->getDate());
257 Catch(WrtDeviceApis::Commons::Exception)
259 LogWarning("trying to get incorrect value");
261 return JSValueMakeUndefined(context);
264 bool JSContactAnniversary::setDate(JSContextRef context,
266 JSStringRef propertyName,
268 JSValueRef* exception)
272 ContactAnniversaryPtr anniversary = getPrivData(object);
273 ContactConverterFactory::ConverterType converter =
274 ContactConverterFactory::getConverter(context);
275 anniversary->setDate(converter->toDateTm(value));
278 Catch(WrtDeviceApis::Commons::Exception)
280 LogWarning("trying to set incorrect value");
282 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
286 JSValueRef JSContactAnniversary::getLabel(JSContextRef context,
288 JSStringRef propertyName,
289 JSValueRef* exception)
293 ContactConverterFactory::ConverterType converter =
294 ContactConverterFactory::getConverter(context);
295 ContactAnniversaryPtr anniversary = getPrivData(object);
296 if(!anniversary->getLabelIsSet())
297 return JSValueMakeNull(context);
299 return converter->toJSValueRef(anniversary->getLabel());
301 Catch(WrtDeviceApis::Commons::Exception)
303 LogWarning("trying to get incorrect value");
305 return JSValueMakeUndefined(context);
308 bool JSContactAnniversary::setLabel(JSContextRef context,
310 JSStringRef propertyName,
312 JSValueRef* exception)
316 ContactAnniversaryPtr anniversary = getPrivData(object);
317 ContactConverterFactory::ConverterType converter =
318 ContactConverterFactory::getConverter(context);
319 anniversary->setLabel(converter->toString(value));
322 Catch(WrtDeviceApis::Commons::Exception)
324 LogWarning("trying to set incorrect value");
326 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");