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 "JSCalendarRecurrenceRule.h"
19 #include <dpl/log/log.h>
20 #include <Tizen/Common/JSTizenException.h>
21 #include <Tizen/Common/JSTizenExceptionFactory.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include "CalendarConverter.h"
24 #include <Tizen/TimeUtil/TimeUtilConverter.h>
25 #include <Tizen/TimeUtil/JSTZDate.h>
27 using namespace TizenApis::Api::Calendar;
28 using namespace WrtDeviceApis::Commons;
29 using namespace WrtDeviceApis::CommonsJavaScript;
30 using namespace TizenApis::Commons;
36 JSClassDefinition JSCalendarRecurrenceRule::m_classInfo = {
38 kJSClassAttributeNone,
39 TIZEN_INTERFACE_CALENDAR_RECURRENCE_RULE,
48 NULL, //deleteProperty,
49 NULL, //getPropertyNames,
50 NULL, //callAsFunction,
53 NULL, //convertToType,
56 JSStaticValue JSCalendarRecurrenceRule::m_property[] = {
57 { TIZEN_RECURRENCE_RULE_FREQUENCY, getProperty, setProperty, kJSPropertyAttributeNone },
58 { TIZEN_RECURRENCE_RULE_INTERVAL, getProperty, setProperty, kJSPropertyAttributeNone },
59 { TIZEN_RECURRENCE_RULE_UNTIL_DATE, getProperty, setProperty, kJSPropertyAttributeNone },
60 { TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT, getProperty, setProperty, kJSPropertyAttributeNone },
61 // { TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH, getProperty, setProperty, kJSPropertyAttributeNone },
62 { TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK, getProperty, setProperty, kJSPropertyAttributeNone },
63 // { TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR, getProperty, setProperty, kJSPropertyAttributeNone },
64 // { TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR, getProperty, setProperty, kJSPropertyAttributeNone },
65 { TIZEN_RECURRENCE_RULE_SET_POSITION, getProperty, setProperty, kJSPropertyAttributeNone },
66 { TIZEN_RECURRENCE_RULE_EXCEPTIONS, getProperty, setProperty, kJSPropertyAttributeNone },
71 JSClassRef JSCalendarRecurrenceRule::m_jsClassRef = JSClassCreate(
72 JSCalendarRecurrenceRule::getClassInfo());
74 const JSClassDefinition* JSCalendarRecurrenceRule::getClassInfo()
76 return &(m_classInfo);
79 JSClassRef JSCalendarRecurrenceRule::getClassRef()
82 m_jsClassRef = JSClassCreate(&m_classInfo);
87 JSObjectRef JSCalendarRecurrenceRule::createJSCalendarRecurrenceRule(JSContextRef context, EventRecurrenceRulePtr rule)
89 RecurrenceRulePrivateObject *priv = new RecurrenceRulePrivateObject(context, rule);
90 return JSObjectMake(context, getClassRef(), priv);
93 void JSCalendarRecurrenceRule::initialize(JSContextRef context, JSObjectRef object)
95 if (!JSObjectGetPrivate(object)) {
96 LogDebug("Create calendar rrule private object.");
97 EventRecurrenceRulePtr rrule( new EventRecurrenceRule() );
98 RecurrenceRulePrivateObject *priv = new RecurrenceRulePrivateObject(context, rrule);
99 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
103 LogDebug("Private object alrerady set.");
107 void JSCalendarRecurrenceRule::finalize(JSObjectRef object)
110 RecurrenceRulePrivateObject* priv = static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
113 JSObjectSetPrivate(object, NULL);
117 EventRecurrenceRulePtr JSCalendarRecurrenceRule::getPrivateObject(JSObjectRef object)
120 RecurrenceRulePrivateObject *priv =
121 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
123 ThrowMsg(NullPointerException, "Private object is null.");
125 EventRecurrenceRulePtr result = priv->getObject();
127 ThrowMsg(NullPointerException, "Private object is null.");
132 void JSCalendarRecurrenceRule::setPrivateObject(const EventRecurrenceRulePtr &rrule,
134 const JSObjectRef object)
139 RecurrenceRulePrivateObject *priv =
140 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
142 priv = new RecurrenceRulePrivateObject(ctx, rrule);
143 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
149 LogError("Error during replacing rrule object.");
153 JSObjectRef JSCalendarRecurrenceRule::constructor(JSContextRef context,
154 JSObjectRef constructor,
155 size_t argumentCount,
156 const JSValueRef arguments[],
157 JSValueRef* exception)
163 CalendarConverter converter(context);
164 EventRecurrenceRulePtr rrule;
166 if (argumentCount==0) {
167 EventRecurrenceRulePtr result( new EventRecurrenceRule());
169 } else if (argumentCount==1) {
170 EventRecurrenceRulePtr result( new EventRecurrenceRule());
172 rrule->setFrequency(converter.toRecurrenceFrequency(converter.toString(arguments[0])));
173 } else if (argumentCount>=2) {
174 if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
175 EventRecurrenceRulePtr result( new EventRecurrenceRule());
178 if (!JSValueIsObject(context, arguments[1])) {
179 ThrowMsg(ConversionException, "Wrong second parameter type.");
182 rrule = converter.toEventRecurrenceRule(arguments[1]);
184 ThrowMsg(ConversionException, "Parameter conversion failed.");
187 rrule->setFrequency(converter.toRecurrenceFrequency(converter.toString(arguments[0])));
190 return createJSCalendarRecurrenceRule(context, rrule);
192 Catch(UnsupportedException)
194 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
195 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
197 Catch(InvalidArgumentException)
199 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
200 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
202 Catch(ConversionException)
204 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
205 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
209 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
210 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
216 JSValueRef JSCalendarRecurrenceRule::getProperty(JSContextRef context,
218 JSStringRef propertyName,
219 JSValueRef* exception)
222 CalendarConverter converter(context);
225 RecurrenceRulePrivateObject* priv =
226 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
228 Throw(NullPointerException);
230 EventRecurrenceRulePtr rrule = priv->getObject();
232 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
233 return converter.toJSValueRef(converter.toTizenValue(rrule->getFrequency()));
234 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
235 return converter.toJSValueRef(rrule->getInterval());
236 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
238 Throw(NullPointerException);
241 if (UNDEFINED_TIME==rrule->getEndDate()) {
242 return JSValueMakeNull(context);
244 //return JSTZDate::createJSObject(context, rrule->getEndDate(), rrule->getTimeZone());
245 LogDebug(" endDate in JSrrule" << rrule->getEndDate());
246 LogDebug(" timezone in JSrrule" << rrule->getTimeZone());
247 TimeUtilConverter timeConverter(context);
248 return timeConverter.FromUTCTimeTToTZDate(rrule->getEndDate(), rrule->getTimeZone());
250 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
251 return converter.toJSValueRefLong(rrule->getOccurrenceCount());
252 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
253 //return converter.toJSValueRef(rrule->getDaysOfTheMonth());
254 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
255 return converter.toJSValueRef(rrule->getDaysOfTheWeek());
256 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
257 //return converter.toJSValueRef(rrule->getDaysOfTheYear());
258 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
259 //return converter.toJSValueRef(rrule->getWeeksOfTheYear());
260 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITION)) {
261 return converter.toJSValueRef(rrule->getSetPosition());
262 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
263 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
264 if (NULL == jsResult) {
265 LogError("Could not create js array object.");
266 ThrowMsg(NullPointerException, "Could not create js array object");
269 for (std::size_t i = 0; i < rrule->getExceptions().size(); ++i) {
270 JSValueRef tmpVal = JSTZDate::createJSObject(context, rrule->getExceptions().at(i), rrule->getTimeZone());
271 if (!JSSetArrayElement(context, jsResult, i, tmpVal)) {
272 LogError("Could not insert value into js array.");
273 ThrowMsg(UnknownException, "Could not insert value into js array");
282 LogError("invalid property");
284 return JSValueMakeUndefined(context);
287 bool JSCalendarRecurrenceRule::setProperty(JSContextRef context,
289 JSStringRef propertyName,
291 JSValueRef* exception)
294 CalendarConverter converter(context);
297 RecurrenceRulePrivateObject* priv =
298 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
300 Throw(NullPointerException);
302 EventRecurrenceRulePtr rrule = priv->getObject();
304 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
305 if (!JSValueIsString(context, value)) {
306 Throw(InvalidArgumentException);
308 std::string frequency = converter.toString(value);
309 rrule->setFrequency(converter.toRecurrenceFrequency(frequency));
311 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
312 if (!JSValueIsNumber(context, value)) {
313 Throw(InvalidArgumentException);
315 int interval = converter.toInt(value);
316 rrule->setInterval(interval);
318 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
319 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
320 Throw(InvalidArgumentException);
322 TimeUtilConverter timeConverter(context);
323 std::time_t untilDate = timeConverter.toTZDateTimeT(value);
324 rrule->setEndDate(untilDate);
326 if( rrule->getTimeZone().empty() ) {
327 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
328 rrule->setTimeZone(timeZone);
332 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
333 if (!JSValueIsNumber(context, value)) {
334 Throw(InvalidArgumentException);
336 long occurrenceCount = converter.toLong(value);
337 rrule->setOccurrenceCount(occurrenceCount);
339 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
340 std::vector<int> daysOfTheMonth = converter.toVectorOfInts(value);
341 rrule->setDaysOfTheMonth(daysOfTheMonth);
343 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
344 std::vector<std::string> daysOfTheWeek = converter.toVectorOfStrings(value);
345 rrule->setDaysOfTheWeek(daysOfTheWeek);
347 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
348 std::vector<int> daysOfTheYear = converter.toVectorOfInts(value);
349 rrule->setDaysOfTheYear(daysOfTheYear);
351 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
352 std::vector<int> weekOfTheYear = converter.toVectorOfInts(value);
353 rrule->setWeeksOfTheYear(weekOfTheYear);
355 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITION)) {
356 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR);
358 bool setPosition = converter.toBool(value);
359 rrule->setSetPosition(setPosition);*/
361 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
362 // Only save the converted time info.
363 rrule->setExceptions(converter.toVectorOfTimeTFromTZDate(value));
369 LogWarning("trying to set incorrect value");
371 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);