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);
223 TimeUtilConverter timeConverter(context);
226 RecurrenceRulePrivateObject* priv =
227 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
229 ThrowMsg(NullPointerException, "Private object is NULL.");
231 EventRecurrenceRulePtr rrule = priv->getObject();
233 ThrowMsg(NullPointerException, "recurrenceRule is NULL.");
236 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
237 return converter.toJSValueRef(converter.toTizenValue(rrule->getFrequency()));
238 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
239 return converter.toJSValueRef(rrule->getInterval());
240 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
241 if (UNDEFINED_TIME==rrule->getEndDate()) {
242 return JSValueMakeNull(context);
244 return timeConverter.toJSValueRefTZDate((double)(rrule->getEndDate()*1000.0), rrule->getTimeZone());
246 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
247 return converter.toJSValueRefLong(rrule->getOccurrenceCount());
248 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
249 //return converter.toJSValueRef(rrule->getDaysOfTheMonth());
250 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
251 return converter.toJSValueRef(rrule->getDaysOfTheWeek());
252 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
253 //return converter.toJSValueRef(rrule->getDaysOfTheYear());
254 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
255 //return converter.toJSValueRef(rrule->getWeeksOfTheYear());
256 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITION)) {
257 return converter.toJSValueRef(rrule->getSetPosition());
258 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
259 return converter.toJSValueRef(rrule->getExceptions());
264 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
266 return JSValueMakeUndefined(context);
269 bool JSCalendarRecurrenceRule::setProperty(JSContextRef context,
271 JSStringRef propertyName,
273 JSValueRef* exception)
276 CalendarConverter converter(context);
279 RecurrenceRulePrivateObject* priv =
280 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
282 ThrowMsg(NullPointerException, "Private object is NULL.");
284 EventRecurrenceRulePtr rrule = priv->getObject();
286 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
287 std::string frequency = converter.toString(value);
288 rrule->setFrequency(converter.toRecurrenceFrequency(frequency));
290 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
291 int interval = converter.toInt(value);
292 rrule->setInterval(interval);
294 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
295 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
296 ThrowMsg(ConversionException, "Wrong untilDate type.");
298 TimeUtilConverter timeConverter(context);
299 long long int untilDate = (long long int) (timeConverter.getTimeInMilliseconds(value)/1000);
300 rrule->setEndDate(untilDate);
302 if( rrule->getTimeZone().empty() ) {
303 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
304 rrule->setTimeZone(timeZone);
308 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
309 long occurrenceCount = converter.toLong(value);
310 rrule->setOccurrenceCount(occurrenceCount);
312 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
313 std::vector<int> daysOfTheMonth = converter.toVectorOfInts(value);
314 rrule->setDaysOfTheMonth(daysOfTheMonth);
316 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
317 rrule->setDaysOfTheWeek(converter.toStringArray(value));
319 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
320 std::vector<int> daysOfTheYear = converter.toVectorOfInts(value);
321 rrule->setDaysOfTheYear(daysOfTheYear);
323 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
324 std::vector<int> weekOfTheYear = converter.toVectorOfInts(value);
325 rrule->setWeeksOfTheYear(weekOfTheYear);
327 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITION)) {
328 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::NOT_SUPPORTED_ERROR);
330 bool setPosition = converter.toBool(value);
331 rrule->setSetPosition(setPosition);*/
333 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
334 rrule->setExceptions(converter.toNumberArray(value));
340 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
342 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);