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<2 || argumentCount>3) {
135 ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
138 EventAlarmPrivateObject* privateObject = static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(constructor));
139 JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
141 CalendarConverter converter(globalContext);
142 EventAlarmPtr alarm( new EventAlarm());
144 TimeUtilConverter timeConverter(context);
145 if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())){ // absoluteDate case
146 std::time_t absoluteDate = timeConverter.toTZDateTimeT(arguments[0]);
147 alarm->setAbsoluteDate(absoluteDate);
148 alarm->setTimeZone(timeConverter.getPropertiesInTZDate(arguments[0]).timezone);
149 if (JSValueIsString(context, arguments[1])) {
150 std::string method = converter.toString(arguments[1]);
151 std::vector<CalendarEvent::EventAlarmType> convertedMethods;
152 convertedMethods.push_back(converter.toEventAlarmType(method));
153 alarm->setMethods(convertedMethods);
155 ThrowMsg(ConversionException, "Second parameter conversion failed.");
157 if (argumentCount==3) {
158 if (JSValueIsString(context, arguments[2])) {
159 alarm->setDescription(converter.toString(arguments[2]));
161 ThrowMsg(ConversionException, "Third parameter conversion failed.");
164 } else { // if (JSValueIsObjectOfClass(context, arguments[0], JSTimeDuration::getClassRef())){ // before case
165 alarm->setDuration(timeConverter.getDurationPropertis(arguments[0]));
166 if (JSValueIsString(context, arguments[1])) {
167 std::string method = converter.toString(arguments[1]);
168 std::vector<CalendarEvent::EventAlarmType> convertedMethods;
169 convertedMethods.push_back(converter.toEventAlarmType(method));
170 alarm->setMethods(convertedMethods);
172 ThrowMsg(ConversionException, "Second parameter conversion failed.");
174 if (argumentCount==3) {
175 if (JSValueIsString(context, arguments[2])) {
176 alarm->setDescription(converter.toString(arguments[2]));
178 ThrowMsg(ConversionException, "Thrid parameter conversion failed.");
181 }/* else { // Workaround until plugin link issue is resolved.
182 ThrowMsg(ConversionException, "First parameter conversion failed.");
185 return createJSCalendarAlarm(globalContext, alarm);
187 Catch(UnsupportedException)
189 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
190 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
192 Catch(InvalidArgumentException)
194 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
195 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
197 Catch(ConversionException)
199 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
200 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
204 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
205 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
211 void JSCalendarAlarm::initialize(JSContextRef context,
217 void JSCalendarAlarm::finalize(JSObjectRef object)
220 EventAlarmPrivateObject* priv =
221 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
223 JSObjectSetPrivate(object, NULL);
226 JSValueRef JSCalendarAlarm::getProperty(JSContextRef context,
228 JSStringRef propertyName,
229 JSValueRef* exception)
232 WrtDeviceApis::CommonsJavaScript::Converter converter(context);
235 EventAlarmPrivateObject* priv =
236 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
238 Throw(WrtDeviceApis::Commons::NullPointerException);
240 EventAlarmPtr alarm = priv->getObject();
242 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
244 Throw(WrtDeviceApis::Commons::NullPointerException);
246 if (alarm->getAbsoluteDate() != 0) {
247 // Use the global context saved in the event struct.
248 return JSTZDate::createJSObject(priv->getContext(), alarm->getAbsoluteDate(), alarm->getTimeZone());
250 return JSValueMakeUndefined(context);
252 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
253 return JSTimeDuration::createJSObject(priv->getContext(), alarm->getDuration());
254 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
255 return converter.toJSValueRef(alarm->getDescription());
256 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
257 std::vector<JSValueRef> result;
258 std::vector<CalendarEvent::EventAlarmType> methods = alarm->getMethods();
259 for(std::vector<CalendarEvent::EventAlarmType>::iterator i = methods.begin(); i != methods.end(); i++)
260 result.push_back(converter.toJSValueRef(*i));
261 return converter.toJSValueRef(result);
264 Catch(WrtDeviceApis::Commons::Exception)
266 LogError("invalid property");
268 return JSValueMakeUndefined(context);
271 bool JSCalendarAlarm::setProperty(JSContextRef context,
273 JSStringRef propertyName,
275 JSValueRef* exception)
278 CalendarConverter converter(context);
281 EventAlarmPrivateObject* priv =
282 static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
284 Throw(WrtDeviceApis::Commons::NullPointerException);
286 EventAlarmPtr alarm = priv->getObject();
288 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
289 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
290 Throw(InvalidArgumentException);
292 TimeUtilConverter timeConverter(context);
293 std::time_t absoluteDate = timeConverter.toTZDateTimeT(value);
294 alarm->setAbsoluteDate(absoluteDate);
296 if( alarm->getTimeZone().empty() ) {
297 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
298 alarm->setTimeZone(timeZone);
301 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
302 if (!JSValueIsObjectOfClass(context, value, JSTimeDuration::getClassRef())) {
303 Throw(InvalidArgumentException);
305 TimeUtilConverter timeConverter(context);
306 alarm->setDuration(timeConverter.getDurationPropertis(value));
308 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
309 if (!JSValueIsString(context, value)) {
310 Throw(InvalidArgumentException);
312 alarm->setDescription(converter.toString(value));
314 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
315 std::string method = converter.toString(value);
316 std::vector<CalendarEvent::EventAlarmType> convertedMethods;
317 convertedMethods.push_back(converter.toEventAlarmType(method));
318 alarm->setMethods(convertedMethods);
322 Catch(WrtDeviceApis::Commons::Exception)
324 LogWarning("trying to set incorrect value");
326 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);