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 #include "JSCalendarAlarm.h"
20 #include "CalendarConverter.h"
21 #include <JSWebAPIErrorFactory.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <TimeUtilConverter.h>
25 #include <JSTimeDuration.h>
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
33 using namespace DeviceAPI::Time;
38 JSClassDefinition JSCalendarAlarm::m_classInfo = {
40 kJSClassAttributeNone,
41 TIZEN_INTERFACE_CALENDAR_ALARM,
50 NULL, //deleteProperty,
51 NULL, //getPropertyNames,
52 NULL, //callAsFunction,
55 NULL, //convertToType,
58 JSStaticValue JSCalendarAlarm::m_property[] = {
59 { TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE, getProperty, setProperty, kJSPropertyAttributeNone },
60 { TIZEN_CALENDAR_ALARM_BEFORE, getProperty, setProperty, kJSPropertyAttributeNone },
61 { TIZEN_CALENDAR_ALARM_METHOD, getProperty, setProperty, kJSPropertyAttributeNone },
62 { TIZEN_CALENDAR_ALARM_DESCRIPTION, getProperty, setProperty, kJSPropertyAttributeNone },
67 JSClassRef JSCalendarAlarm::m_jsClassRef = JSClassCreate(
68 JSCalendarAlarm::getClassInfo());
70 const JSClassDefinition* JSCalendarAlarm::getClassInfo()
72 return &(m_classInfo);
75 JSClassRef DLL_EXPORT JSCalendarAlarm::getClassRef()
78 m_jsClassRef = JSClassCreate(&m_classInfo);
83 JSObjectRef JSCalendarAlarm::createJSCalendarAlarm(JSContextRef context, EventAlarmPtr alarm)
85 EventAlarmPrivateObject *priv = new EventAlarmPrivateObject(context, alarm);
86 return JSObjectMake(context, getClassRef(), priv);
89 EventAlarmPtr JSCalendarAlarm::getPrivateObject(JSObjectRef object)
91 EventAlarmPrivateObject *priv =
92 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
94 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
96 EventAlarmPtr result = priv->getObject();
98 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
103 void JSCalendarAlarm::setPrivateObject(const EventAlarmPtr &alarm,
105 const JSObjectRef object)
109 EventAlarmPrivateObject *priv =
110 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
112 priv = new EventAlarmPrivateObject(ctx, alarm);
113 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
119 LoggerE("Error during replacing alarm object.");
123 JSObjectRef DLL_EXPORT JSCalendarAlarm::constructor(JSContextRef context,
124 JSObjectRef constructor,
125 size_t argumentCount,
126 const JSValueRef arguments[],
127 JSValueRef* exception)
131 if (argumentCount<1) {
132 ThrowMsg(ConversionException, "Wrong parameter type.");
135 CalendarConverter converter(context);
136 TimeUtilConverter timeConverter(context);
138 EventAlarmPtr alarm( new EventAlarm() );
140 if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())){
141 LoggerD("absoluteDate case");
142 long long int absoluteDate = (long long int) (timeConverter.getTimeInMilliseconds(arguments[0])/1000);
143 alarm->setAbsoluteDate(absoluteDate);
144 alarm->setTimeZone(timeConverter.getPropertiesInTZDate(arguments[0]).timezone);
146 if (argumentCount>=2) {
147 alarm->setMethod(converter.toEventAlarmType(converter.toString(arguments[1])));
149 if (argumentCount>=3) {
150 alarm->setDescription(converter.toString(arguments[2]));
152 } else if (JSValueIsObjectOfClass(context, arguments[0], JSTimeDuration::getClassRef())){
153 LoggerD("before case");
154 alarm->setDuration(timeConverter.getDurationProperties(arguments[0]));
156 if (argumentCount>=2) {
157 alarm->setMethod(converter.toEventAlarmType(converter.toString(arguments[1])));
159 if (argumentCount>=3) {
160 alarm->setDescription(converter.toString(arguments[2]));
163 ThrowMsg(ConversionException, "Wrong first parameter type.");
166 return createJSCalendarAlarm(context, alarm);
168 Catch(UnsupportedException)
170 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
171 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
173 Catch(InvalidArgumentException)
175 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
176 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
178 Catch(ConversionException)
180 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
181 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
185 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
186 return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
190 void JSCalendarAlarm::initialize(JSContextRef context,
193 if (!JSObjectGetPrivate(object)) {
194 LoggerD("Create calendar alarm private object.");
195 EventAlarmPtr alarm( new EventAlarm() );
196 EventAlarmPrivateObject *priv = new EventAlarmPrivateObject(context, alarm);
197 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
201 LoggerD("Private object already set.");
205 void JSCalendarAlarm::finalize(JSObjectRef object)
207 EventAlarmPrivateObject* priv = static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
210 JSObjectSetPrivate(object, NULL);
214 JSValueRef JSCalendarAlarm::getProperty(JSContextRef context,
216 JSStringRef propertyName,
217 JSValueRef* exception)
219 CalendarConverter converter(context);
222 EventAlarmPrivateObject* priv =
223 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
225 ThrowMsg(NullPointerException, "Private object is NULL.");
227 EventAlarmPtr alarm = priv->getObject();
229 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
231 ThrowMsg(NullPointerException, "Alarm object is NULL.");
234 if (UNDEFINED_TIME==alarm->getAbsoluteDate()) {
235 return JSValueMakeNull(context);
237 TimeUtilConverter timeConverter(context);
238 return timeConverter.toJSValueRefTZDate((double)(alarm->getAbsoluteDate()*1000.0), alarm->getTimeZone());
240 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
241 if (UNDEFINED_TIME==alarm->getDuration().length) {
242 return JSValueMakeNull(context);
244 return JSTimeDuration::createJSObject(context, alarm->getDuration());
246 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
247 return converter.toJSValueRef(alarm->getDescription());
248 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
249 LoggerD("Alarm method: "<<converter.toTizenValue(alarm->getMethod()));
250 return converter.toJSValueRef(converter.toTizenValue(alarm->getMethod()));
253 Catch(WrtDeviceApis::Commons::Exception)
255 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
257 return JSValueMakeUndefined(context);
260 bool JSCalendarAlarm::setProperty(JSContextRef context,
262 JSStringRef propertyName,
264 JSValueRef* exception)
266 CalendarConverter converter(context);
269 EventAlarmPrivateObject* priv =
270 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
272 ThrowMsg(NullPointerException, "Private object is NULL.");
274 EventAlarmPtr alarm = priv->getObject();
276 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
277 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
278 ThrowMsg(ConversionException, "Wrong absoluteDate type.");
280 TimeUtilConverter timeConverter(context);
281 long long int absoluteDate = (long long int) (timeConverter.getTimeInMilliseconds(value)/1000);
282 alarm->setAbsoluteDate(absoluteDate);
284 if( alarm->getTimeZone().empty() ) {
285 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
286 alarm->setTimeZone(timeZone);
289 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
290 if (!JSValueIsObjectOfClass(context, value, JSTimeDuration::getClassRef())) {
291 ThrowMsg(ConversionException, "Wrong before type.");
293 TimeUtilConverter timeConverter(context);
294 alarm->setDuration(timeConverter.getDurationProperties(value));
296 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
297 alarm->setDescription(converter.toString(value));
299 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
300 alarm->setMethod(converter.toEventAlarmType(converter.toString(value)));
304 Catch(WrtDeviceApis::Commons::Exception)
306 LoggerW("Exception: "<<_rethrown_exception.GetMessage());