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 #include "JSCalendarAlarm.h"
19 #include "CalendarConverter.h"
20 #include <dpl/log/log.h>
21 #include <Tizen/Common/JSTizenException.h>
22 #include <Tizen/Common/JSTizenExceptionFactory.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include <Tizen/TimeUtil/TimeUtilConverter.h>
25 #include <Tizen/TimeUtil/JSTZDate.h>
26 #include <Tizen/TimeUtil/JSTimeDuration.h>
28 using namespace TizenApis::Api::Calendar;
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace TizenApis::Commons;
37 JSClassDefinition JSCalendarAlarm::m_classInfo = {
39 kJSClassAttributeNone,
40 TIZEN_INTERFACE_CALENDAR_ALARM,
49 NULL, //deleteProperty,
50 NULL, //getPropertyNames,
51 NULL, //callAsFunction,
54 NULL, //convertToType,
57 JSStaticValue JSCalendarAlarm::m_property[] = {
58 { TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE, getProperty, setProperty, kJSPropertyAttributeNone },
59 { TIZEN_CALENDAR_ALARM_BEFORE, getProperty, setProperty, kJSPropertyAttributeNone },
60 { TIZEN_CALENDAR_ALARM_METHOD, getProperty, setProperty, kJSPropertyAttributeNone },
61 { TIZEN_CALENDAR_ALARM_DESCRIPTION, getProperty, setProperty, kJSPropertyAttributeNone },
66 JSClassRef JSCalendarAlarm::m_jsClassRef = JSClassCreate(
67 JSCalendarAlarm::getClassInfo());
69 const JSClassDefinition* JSCalendarAlarm::getClassInfo()
71 return &(m_classInfo);
74 JSClassRef JSCalendarAlarm::getClassRef()
77 m_jsClassRef = JSClassCreate(&m_classInfo);
82 JSObjectRef JSCalendarAlarm::createJSCalendarAlarm(JSContextRef context, EventAlarmPtr alarm)
84 EventAlarmPrivateObject *priv = new EventAlarmPrivateObject(context, alarm);
85 return JSObjectMake(context, getClassRef(), priv);
88 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)
110 EventAlarmPrivateObject *priv =
111 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
113 priv = new EventAlarmPrivateObject(ctx, alarm);
114 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
120 LogError("Error during replacing alarm object.");
124 JSObjectRef JSCalendarAlarm::constructor(JSContextRef context,
125 JSObjectRef constructor,
126 size_t argumentCount,
127 const JSValueRef arguments[],
128 JSValueRef* exception)
134 if (argumentCount<1) {
135 ThrowMsg(ConversionException, "Wrong parameter type.");
138 CalendarConverter converter(context);
139 TimeUtilConverter timeConverter(context);
141 EventAlarmPtr alarm( new EventAlarm() );
143 if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())){
144 LogDebug("absoluteDate case");
145 long long int absoluteDate = (long long int) (timeConverter.getTimeInMilliseconds(arguments[0])/1000);
146 alarm->setAbsoluteDate(absoluteDate);
147 alarm->setTimeZone(timeConverter.getPropertiesInTZDate(arguments[0]).timezone);
149 if (argumentCount>=2) {
150 std::string method = converter.toString(arguments[1]);
151 std::vector<EventAlarm::EventAlarmType> convertedMethods;
152 convertedMethods.push_back(converter.toEventAlarmType(method));
153 alarm->setMethods(convertedMethods);
155 if (argumentCount>=3) {
156 alarm->setDescription(converter.toString(arguments[2]));
158 } else if (JSValueIsObjectOfClass(context, arguments[0], JSTimeDuration::getClassRef())){
159 LogDebug("before case");
160 alarm->setDuration(timeConverter.getDurationPropertis(arguments[0]));
162 if (argumentCount>=2) {
163 std::string method = converter.toString(arguments[1]);
164 std::vector<EventAlarm::EventAlarmType> convertedMethods;
165 convertedMethods.push_back(converter.toEventAlarmType(method));
166 alarm->setMethods(convertedMethods);
168 if (argumentCount>=3) {
169 alarm->setDescription(converter.toString(arguments[2]));
172 ThrowMsg(ConversionException, "Wrong first parameter type.");
175 return createJSCalendarAlarm(context, alarm);
177 Catch(UnsupportedException)
179 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
180 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
182 Catch(InvalidArgumentException)
184 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
185 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
187 Catch(ConversionException)
189 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
190 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
194 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
195 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
201 void JSCalendarAlarm::initialize(JSContextRef context,
204 if (!JSObjectGetPrivate(object)) {
205 LogDebug("Create calendar alarm private object.");
206 EventAlarmPtr alarm( new EventAlarm() );
207 EventAlarmPrivateObject *priv = new EventAlarmPrivateObject(context, alarm);
208 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
212 LogDebug("Private object alrerady set.");
216 void JSCalendarAlarm::finalize(JSObjectRef object)
219 EventAlarmPrivateObject* priv = static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
222 JSObjectSetPrivate(object, NULL);
226 JSValueRef JSCalendarAlarm::getProperty(JSContextRef context,
228 JSStringRef propertyName,
229 JSValueRef* exception)
232 CalendarConverter converter(context);
235 EventAlarmPrivateObject* priv =
236 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
238 ThrowMsg(NullPointerException, "Private object is NULL.");
240 EventAlarmPtr alarm = priv->getObject();
242 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
244 ThrowMsg(NullPointerException, "Alarm object is NULL.");
247 if (UNDEFINED_TIME==alarm->getAbsoluteDate()) {
248 return JSValueMakeNull(context);
250 TimeUtilConverter timeConverter(context);
251 return timeConverter.toJSValueRefTZDate((double)(alarm->getAbsoluteDate()*1000.0), alarm->getTimeZone());
253 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
254 return JSTimeDuration::createJSObject(context, alarm->getDuration());
255 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
256 return converter.toJSValueRef(alarm->getDescription());
257 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
258 return converter.toJSValueRef(converter.toTizenValue(alarm->getMethods().at(0)));
261 Catch(WrtDeviceApis::Commons::Exception)
263 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
265 return JSValueMakeUndefined(context);
268 bool JSCalendarAlarm::setProperty(JSContextRef context,
270 JSStringRef propertyName,
272 JSValueRef* exception)
275 CalendarConverter converter(context);
278 EventAlarmPrivateObject* priv =
279 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
281 ThrowMsg(NullPointerException, "Private object is NULL.");
283 EventAlarmPtr alarm = priv->getObject();
285 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
286 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
287 ThrowMsg(ConversionException, "Wrong absoluteDate type.");
289 TimeUtilConverter timeConverter(context);
290 long long int absoluteDate = (long long int) (timeConverter.getTimeInMilliseconds(value)/1000);
291 alarm->setAbsoluteDate(absoluteDate);
293 if( alarm->getTimeZone().empty() ) {
294 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
295 alarm->setTimeZone(timeZone);
298 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
299 if (!JSValueIsObjectOfClass(context, value, JSTimeDuration::getClassRef())) {
300 ThrowMsg(ConversionException, "Wrong before type.");
302 TimeUtilConverter timeConverter(context);
303 alarm->setDuration(timeConverter.getDurationPropertis(value));
305 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
306 alarm->setDescription(converter.toString(value));
308 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
309 std::string method = converter.toString(value);
310 std::vector<EventAlarm::EventAlarmType> convertedMethods;
311 convertedMethods.push_back(converter.toEventAlarmType(method));
312 alarm->setMethods(convertedMethods);
316 Catch(WrtDeviceApis::Commons::Exception)
318 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
320 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);