2 // Tizen Web Device API
3 // Copyright (c) 2012-2013 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.
18 #include "JSContactAnniversary.h"
22 #include <ArgumentValidator.h>
23 #include <CommonsJavaScript/Converter.h>
29 using namespace DeviceAPI::Common;
31 struct ContactAnniversaryHolder {
32 ContactAnniversaryPtr ptr;
36 const char* CONTACT_CONTACT_ANNIVERSARY = "ContactAnniversary";
38 const char* CONTACT_CONTACT_ANNIVERSARY_DATE = "date";
39 const char* CONTACT_CONTACT_ANNIVERSARY_LABEL = "label";
42 JSClassDefinition JSContactAnniversary::m_classInfo = {
44 kJSClassAttributeNone,
45 CONTACT_CONTACT_ANNIVERSARY,
47 JSContactAnniversary::m_property,
49 JSContactAnniversary::initialize,
50 JSContactAnniversary::finalize,
54 NULL, //deleteProperty,
55 NULL, //getPropertyNames,
59 NULL, //convertToType,
62 JSStaticValue JSContactAnniversary::m_property[] = {
63 { CONTACT_CONTACT_ANNIVERSARY_DATE, getDate, setDate, kJSPropertyAttributeDontDelete },
64 { CONTACT_CONTACT_ANNIVERSARY_LABEL, getLabel, setLabel, kJSPropertyAttributeDontDelete },
68 const JSClassDefinition* JSContactAnniversary::getClassInfo()
73 JSClassRef JSContactAnniversary::m_jsClassRef = JSClassCreate(JSContactAnniversary::getClassInfo());
75 JSClassRef DLL_EXPORT JSContactAnniversary::getClassRef()
78 m_jsClassRef = JSClassCreate(&m_classInfo);
83 ContactAnniversaryPtr JSContactAnniversary::getPrivateObject(JSContextRef context, JSValueRef value)
85 if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
86 LOGE("Type mismatch");
87 throw TypeMismatchException("Type mismatch");
90 JSObjectRef object = JSUtil::JSValueToObject(context, value);
91 ContactAnniversaryHolder* priv = static_cast<ContactAnniversaryHolder*>(JSObjectGetPrivate(object));
94 throw UnknownException("Priv is null");
99 void JSContactAnniversary::setPrivateObject(JSObjectRef object, ContactAnniversaryPtr native)
101 ContactAnniversaryHolder* priv = static_cast<ContactAnniversaryHolder*>(JSObjectGetPrivate(object));
103 LOGE("Priv is null");
104 throw UnknownException("Priv is null");
109 JSObjectRef JSContactAnniversary::makeJSObject(JSContextRef context,
110 ContactAnniversaryPtr native)
113 LOGE("Native is null");
114 throw UnknownException("Native is null");
117 ContactAnniversaryHolder* priv = new(std::nothrow) ContactAnniversaryHolder();
119 LOGE("Priv is null");
120 throw UnknownException("Priv is null");
124 JSObjectRef obj = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
128 void JSContactAnniversary::initialize(JSContextRef context, JSObjectRef object)
133 void JSContactAnniversary::finalize(JSObjectRef object)
137 ContactAnniversaryHolder* priv = static_cast<ContactAnniversaryHolder*>(JSObjectGetPrivate(object));
139 JSObjectSetPrivate(object, NULL);
145 JSObjectRef DLL_EXPORT JSContactAnniversary::constructor(JSContextRef context,
146 JSObjectRef constructor,
147 size_t argumentCount,
148 const JSValueRef arguments[],
149 JSValueRef* exception)
152 ArgumentValidator validator(context, argumentCount, arguments);
154 JSObjectRef jsObjRef = JSObjectMake(context, getClassRef(), NULL);
156 JSStringRef ctorName = JSStringCreateWithUTF8CString("constructor");
157 JSObjectSetProperty(context, jsObjRef, ctorName, constructor, kJSPropertyAttributeReadOnly
158 | kJSPropertyAttributeDontDelete | kJSPropertyAttributeDontEnum, NULL);
159 JSStringRelease(ctorName);
164 label = validator.toString(1, true, "");
165 ContactAnniversaryPtr priv = ContactAnniversaryPtr(new(std::nothrow) ContactAnniversary());
167 LOGE("Priv is null");
168 throw UnknownException("Priv is null");
171 tm tm_ = WrtDeviceApis::CommonsJavaScript::Converter(context).toDateTm(arguments[0]);
174 if (!validator.isNull(1) && !validator.isOmitted(1)) {
175 priv->setLabel(label);
178 ContactAnniversaryHolder* holder = new(std::nothrow) ContactAnniversaryHolder();
180 LOGE("Holder is null");
181 throw UnknownException("Holder is null");
184 JSObjectSetPrivate(jsObjRef, static_cast<void*>(holder));
186 catch (const BasePlatformException &error) {
187 LOGE("ContactAnniversary creation failed: %s", error.getMessage().c_str());
190 LOGE("ContactAnniversary creation failed");
196 JSValueRef JSContactAnniversary::getDate(JSContextRef context,
198 JSStringRef propertyName,
199 JSValueRef* exception)
203 ContactAnniversaryPtr priv = JSContactAnniversary::getPrivateObject(context, object);
205 LOGE("Priv is null");
206 throw UnknownException("Priv is null");
208 return WrtDeviceApis::CommonsJavaScript::Converter(context).toJSValueRef(priv->getDate());
210 catch (const BasePlatformException &error) {
211 LOGE("Failed to get contact anniversary date. %s : %s", error.getName().c_str(), error.getMessage().c_str());
214 LOGE("Unsupported error while getting contact anniversary date.");
216 return JSValueMakeUndefined(context);
219 JSValueRef JSContactAnniversary::getLabel(JSContextRef context,
221 JSStringRef propertyName,
222 JSValueRef* exception)
226 ContactAnniversaryPtr priv = JSContactAnniversary::getPrivateObject(context, object);
228 LOGE("Priv is null");
229 throw UnknownException("Priv is null");
231 if (!priv->isLabelSet()) {
232 return JSValueMakeNull(context);
234 return JSUtil::toJSValueRef(context, priv->getLabel());
236 catch (const BasePlatformException &error) {
237 LOGE("Failed to get contact anniversary label. %s : %s", error.getName().c_str(), error.getMessage().c_str());
240 LOGE("Unsupported error while getting contact anniversary label.");
242 return JSValueMakeUndefined(context);
245 bool JSContactAnniversary::setDate(JSContextRef context,
247 JSStringRef propertyName,
249 JSValueRef* exception)
253 ContactAnniversaryPtr priv = JSContactAnniversary::getPrivateObject(context, object);
255 LOGE("Priv is null");
256 throw UnknownException("Priv is null");
258 tm tm_ = WrtDeviceApis::CommonsJavaScript::Converter(context).toDateTm(value);
261 catch (const BasePlatformException &error) {
262 LOGE("Failed to set contact anniversary date. %s : %s", error.getName().c_str(), error.getMessage().c_str());
265 LOGE("Unsupported error while setting contact anniversary date.");
270 bool JSContactAnniversary::setLabel(JSContextRef context,
272 JSStringRef propertyName,
274 JSValueRef* exception)
278 ContactAnniversaryPtr priv = JSContactAnniversary::getPrivateObject(context, object);
280 LOGE("Priv is null");
281 throw UnknownException("Priv is null");
283 if (JSValueIsNull(context, value)) {
287 std::string label = JSUtil::JSValueToString(context, value);
288 priv->setLabel(label);
291 catch (const BasePlatformException &error) {
292 LOGE("Failed to set contact anniversary label. %s : %s", error.getName().c_str(), error.getMessage().c_str());
295 LOGE("Unsupported error while setting contact anniversary label.");