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)
156 JSContactAnniversaryPriv *priv = static_cast<JSContactAnniversaryPriv*>(JSObjectGetPrivate(constructor));
158 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160 JSContextRef gContext = priv->getContext();
162 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
166 ContactAnniversaryPtr contactAnniversary(new ContactAnniversary());
169 ArgumentValidator Argvalidator(context, argumentCount, arguments);
170 Argvalidator.toObject(0, false);
172 if (argumentCount >= 2){
173 label = Argvalidator.toString(1, true);
174 contactAnniversary->setLabel(label);
176 } catch (const TypeMismatchException& err ) {
177 JSWebAPIError::throwException(context, exception, err);
179 } catch(const BasePlatformException& err) {
180 JSWebAPIError::throwException(context, exception, err);
182 } catch(const ConversionException& err) {
183 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
185 } catch(const NullPointerException& err) {
186 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "");
192 date = converter->toDateTm(arguments[0]);
193 contactAnniversary->setDate(date);
195 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
196 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "1st argument must be a 'Date object'");
200 JSObjectRef jsobject;
203 jsobject = createJSObject(gContext, contactAnniversary);
205 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
206 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, "Wrong arguments");
213 bool JSContactAnniversary::hasInstance(JSContextRef context,
214 JSObjectRef constructor,
215 JSValueRef possibleInstance,
216 JSValueRef* exception)
218 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
221 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
223 JSStringRef propertyName,
224 JSValueRef* exception)
228 ContactConverterFactory::ConverterType converter =
229 ContactConverterFactory::getConverter(context);
230 ContactAnniversaryPtr anniversary = getPrivData(object);
231 return converter->toJSValueRef(anniversary->getDate());
233 Catch(WrtDeviceApis::Commons::Exception)
235 LoggerW("trying to get incorrect value");
237 return JSValueMakeUndefined(context);
240 bool JSContactAnniversary::setDate(JSContextRef context,
242 JSStringRef propertyName,
244 JSValueRef* exception)
248 ContactAnniversaryPtr anniversary = getPrivData(object);
249 ContactConverterFactory::ConverterType converter =
250 ContactConverterFactory::getConverter(context);
251 anniversary->setDate(converter->toDateTm(value));
253 Catch(WrtDeviceApis::Commons::Exception)
255 LoggerW("trying to set incorrect value");
256 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
261 JSValueRef JSContactAnniversary::getLabel(JSContextRef context,
263 JSStringRef propertyName,
264 JSValueRef* exception)
268 ContactConverterFactory::ConverterType converter =
269 ContactConverterFactory::getConverter(context);
270 ContactAnniversaryPtr anniversary = getPrivData(object);
271 if(!anniversary->getLabelIsSet())
272 return JSValueMakeNull(context);
274 return converter->toJSValueRef(anniversary->getLabel());
276 Catch(WrtDeviceApis::Commons::Exception)
278 LoggerW("trying to get incorrect value");
280 return JSValueMakeUndefined(context);
283 bool JSContactAnniversary::setLabel(JSContextRef context,
285 JSStringRef propertyName,
287 JSValueRef* exception)
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();
299 anniversary->setLabel(converter->toString(value));
301 Catch(WrtDeviceApis::Commons::Exception)
303 LoggerW("trying to set incorrect value");
304 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");