2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file JSContactAnniversary.cpp
20 * @author Kisub Song (kisubs.song@samsung.com)
22 * @brief Implementation of the JSContactAnniversary class
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"
33 #define FILTER_CLASS_NAME "Anniversary"
34 #define CONTACT_ATTR_DATE "date"
35 #define CONTACT_ATTR_LABEL "label"
40 using namespace DeviceAPI::Common;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
44 JSClassDefinition JSContactAnniversary::m_classInfo =
47 kJSClassAttributeNone,
57 NULL, //DeleteProperty,
58 NULL, //getPropertyNames,
59 NULL, //CallAsFunction,
60 constructor, //CallAsConstructor,
61 hasInstance, //HasInstance,
62 NULL, //ConvertToType,
65 JSStaticValue JSContactAnniversary::m_property[] = {
66 { CONTACT_ATTR_DATE, getDate, setDate, kJSPropertyAttributeNone },
67 { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
71 JSStaticFunction JSContactAnniversary::m_functions[] =
76 JSClassRef JSContactAnniversary::m_classRef = JSClassCreate(&m_classInfo);
78 JSClassRef JSContactAnniversary::getClassRef() {
80 m_classRef = JSClassCreate(&m_classInfo);
85 bool JSContactAnniversary::isObjectOfClass(JSContextRef context, JSValueRef value)
87 return JSValueIsObjectOfClass(context, value, getClassRef());
90 ContactAnniversaryPtr JSContactAnniversary::getContactAnniversary(JSContextRef context, JSValueRef value)
92 if (!isObjectOfClass(context, value)) {
93 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
95 JSObjectRef object = JSValueToObject(context, value, NULL);
97 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
101 Throw(WrtDeviceApis::Commons::NullPointerException);
103 return priv->getObject();
106 void JSContactAnniversary::Initialize(JSContextRef context, JSObjectRef object)
108 if (!JSObjectGetPrivate(object))
110 ContactAnniversaryPtr anniversary(new ContactAnniversary());
111 JSContactAnniversaryPriv *priv = new JSContactAnniversaryPriv(context, ContactAnniversaryPtr(anniversary));
112 if (!JSObjectSetPrivate(object, priv)) {
118 void JSContactAnniversary::Finalize(JSObjectRef object)
120 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
126 JSObjectRef JSContactAnniversary::createJSObject(JSContextRef context, ContactAnniversaryPtr contactAnniversary)
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");
137 ContactAnniversaryPtr JSContactAnniversary::getPrivData(JSObjectRef object)
139 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(object));
141 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
143 ContactAnniversaryPtr result = priv->getObject();
145 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
150 JSObjectRef JSContactAnniversary::constructor(JSContextRef context,
151 JSObjectRef constructor,
152 size_t argumentCount,
153 const JSValueRef arguments[],
154 JSValueRef* exception)
158 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(constructor));
160 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
162 JSContextRef gContext = priv->getContext();
164 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
168 ContactAnniversaryPtr contactAnniversary(new ContactAnniversary());
171 ArgumentValidator Argvalidator(context, argumentCount, arguments);
172 Argvalidator.toObject(0, false);
174 if (argumentCount >= 2){
175 label = Argvalidator.toString(1, true);
176 contactAnniversary->setLabel(label);
178 } catch (const TypeMismatchException& err ) {
179 JSWebAPIError::throwException(context, exception, err);
181 } catch(const BasePlatformException& err) {
182 JSWebAPIError::throwException(context, exception, err);
184 } catch(const ConversionException& err) {
185 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
187 } catch(const NullPointerException& err) {
188 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
194 date = converter->toDateTm(arguments[0]);
195 contactAnniversary->setDate(date);
197 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
198 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'Date object'");
202 JSObjectRef jsobject;
205 jsobject = createJSObject(gContext, contactAnniversary);
207 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
208 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
215 bool JSContactAnniversary::hasInstance(JSContextRef context,
216 JSObjectRef constructor,
217 JSValueRef possibleInstance,
218 JSValueRef* exception)
220 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
223 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
225 JSStringRef propertyName,
226 JSValueRef* exception)
230 ContactConverterFactory::ConverterType converter =
231 ContactConverterFactory::getConverter(context);
232 ContactAnniversaryPtr anniversary = getPrivData(object);
233 return converter->toJSValueRef(anniversary->getDate());
235 Catch(WrtDeviceApis::Commons::Exception)
237 LoggerW("trying to get incorrect value");
239 return JSValueMakeUndefined(context);
242 bool JSContactAnniversary::setDate(JSContextRef context,
244 JSStringRef propertyName,
246 JSValueRef* exception)
250 ContactAnniversaryPtr anniversary = getPrivData(object);
251 ContactConverterFactory::ConverterType converter =
252 ContactConverterFactory::getConverter(context);
253 anniversary->setDate(converter->toDateTm(value));
255 Catch(WrtDeviceApis::Commons::Exception)
257 LoggerW("trying to set incorrect value");
258 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
263 JSValueRef JSContactAnniversary::getLabel(JSContextRef context,
265 JSStringRef propertyName,
266 JSValueRef* exception)
270 ContactConverterFactory::ConverterType converter =
271 ContactConverterFactory::getConverter(context);
272 ContactAnniversaryPtr anniversary = getPrivData(object);
273 if(!anniversary->getLabelIsSet())
274 return JSValueMakeNull(context);
276 return converter->toJSValueRef(anniversary->getLabel());
278 Catch(WrtDeviceApis::Commons::Exception)
280 LoggerW("trying to get incorrect value");
282 return JSValueMakeUndefined(context);
285 bool JSContactAnniversary::setLabel(JSContextRef context,
287 JSStringRef propertyName,
289 JSValueRef* exception)
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();
301 anniversary->setLabel(converter->toString(value));
303 Catch(WrtDeviceApis::Commons::Exception)
305 LoggerW("trying to set incorrect value");
306 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");