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.
18 #include "JSCalendarRecurrenceRule.h"
19 #include <JSTizenException.h>
20 #include <JSTizenExceptionFactory.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include "CalendarConverter.h"
23 #include <TimeUtilConverter.h>
25 #include <GlobalContextManager.h>
28 using namespace WrtDeviceApis::Commons;
29 using namespace WrtDeviceApis::CommonsJavaScript;
30 using namespace DeviceAPI::Common;
31 using namespace DeviceAPI::Time;
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_POSITIONS, 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 LoggerD("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 LoggerD("Private object already set.");
107 void JSCalendarRecurrenceRule::finalize(JSObjectRef object)
109 RecurrenceRulePrivateObject* priv = static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
112 JSObjectSetPrivate(object, NULL);
116 EventRecurrenceRulePtr JSCalendarRecurrenceRule::getPrivateObject(JSObjectRef object)
118 RecurrenceRulePrivateObject *priv =
119 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
121 ThrowMsg(NullPointerException, "Private object is null.");
123 EventRecurrenceRulePtr result = priv->getObject();
125 ThrowMsg(NullPointerException, "Private object is null.");
130 void JSCalendarRecurrenceRule::setPrivateObject(const EventRecurrenceRulePtr &rrule,
132 const JSObjectRef object)
136 RecurrenceRulePrivateObject *priv =
137 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
139 priv = new RecurrenceRulePrivateObject(ctx, rrule);
140 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
146 LoggerE("Error during replacing rrule object.");
150 JSObjectRef JSCalendarRecurrenceRule::constructor(JSContextRef context,
151 JSObjectRef constructor,
152 size_t argumentCount,
153 const JSValueRef arguments[],
154 JSValueRef* exception)
158 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
159 CalendarConverter converter(globalContext);
160 EventRecurrenceRulePtr rrule;
162 if (argumentCount==0) {
163 EventRecurrenceRulePtr result( new EventRecurrenceRule());
165 } else if (argumentCount==1) {
166 EventRecurrenceRulePtr result( new EventRecurrenceRule());
168 rrule->setFrequency(converter.toRecurrenceFrequency(converter.toString(arguments[0])));
169 } else if (argumentCount>=2) {
170 if (JSValueIsNull(context, arguments[1]) || JSValueIsUndefined(context, arguments[1])) {
171 EventRecurrenceRulePtr result( new EventRecurrenceRule());
174 if (!JSValueIsObject(context, arguments[1])) {
175 ThrowMsg(ConversionException, "Wrong second parameter type.");
178 rrule = converter.toEventRecurrenceRule(arguments[1]);
180 ThrowMsg(ConversionException, "Parameter conversion failed.");
183 rrule->setFrequency(converter.toRecurrenceFrequency(converter.toString(arguments[0])));
186 return createJSCalendarRecurrenceRule(context, rrule);
188 Catch(UnsupportedException)
190 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
191 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
193 Catch(InvalidArgumentException)
195 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
196 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
198 Catch(ConversionException)
200 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
201 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
205 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
206 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
212 JSValueRef JSCalendarRecurrenceRule::getProperty(JSContextRef context,
214 JSStringRef propertyName,
215 JSValueRef* exception)
219 EventRecurrenceRulePtr rrule = getPrivateObject(object);
220 JSContextRef globalContext = GlobalContextManager::getInstance()->getGlobalContext(context);
222 CalendarConverter converter(globalContext);
223 TimeUtilConverter timeConverter(globalContext);
225 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
226 return converter.toJSValueRef(converter.toTizenValue(rrule->getFrequency()));
227 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
228 return converter.toJSValueRef(rrule->getInterval());
229 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
230 if (UNDEFINED_TIME==rrule->getEndDate()) {
231 return JSValueMakeNull(context);
233 return timeConverter.toJSValueRefTZDate((double)(rrule->getEndDate()*1000.0), rrule->getTimeZone());
235 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
236 if(0<rrule->getOccurrenceCount()) {
237 return converter.toJSValueRefLong(rrule->getOccurrenceCount());
239 return converter.toJSValueRefLong(-1);
241 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
242 //return converter.toJSValueRef(rrule->getDaysOfTheMonth());
243 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
244 if(rrule->getDaysOfTheWeekJSRef()) {
245 return rrule->getDaysOfTheWeekJSRef();
247 LoggerD("Create a JS object for daysOfTheWeek.");
248 StringArrayPtr daysOfTheWeek = rrule->getDaysOfTheWeek();
249 JSValueRef jsDaysOfTheWeek = converter.toJSValueRefStringArray(daysOfTheWeek);
250 rrule->setDaysOfTheWeekJSRef(converter.toJSObjectRef(jsDaysOfTheWeek));
252 JSValueProtect(globalContext, jsDaysOfTheWeek);
253 rrule->setContext(globalContext);
254 return jsDaysOfTheWeek;
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_POSITIONS)) {
261 if(rrule->getSetPositionsJSRef()) {
262 return rrule->getSetPositionsJSRef();
264 LoggerD("Create a JS object for setPositions.");
265 NumberArrayPtr setPositions = rrule->getSetPositions();
266 JSValueRef jsSetPositions = converter.toJSValueRefNumberArray(setPositions);
267 rrule->setSetPositionsJSRef(converter.toJSObjectRef(jsSetPositions));
269 JSValueProtect(globalContext, jsSetPositions);
270 rrule->setContext(globalContext);
271 return jsSetPositions;
273 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
274 if(rrule->getExceptionsJSRef()) {
275 return rrule->getExceptionsJSRef();
277 LoggerD("Create a JS object for exceptions with time zone: "<<rrule->getTimeZone());
278 NumberArrayPtr exceptions = rrule->getExceptions();
279 JSValueRef jsExceptions = converter.toJSValueRefTZDateArray(exceptions, rrule->getTimeZone());
280 rrule->setExceptionsJSRef(converter.toJSObjectRef(jsExceptions));
282 JSValueProtect(globalContext, jsExceptions);
283 rrule->setContext(globalContext);
290 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
292 return JSValueMakeUndefined(context);
295 bool JSCalendarRecurrenceRule::setProperty(JSContextRef context,
297 JSStringRef propertyName,
299 JSValueRef* exception)
301 CalendarConverter converter(context);
304 RecurrenceRulePrivateObject* priv =
305 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
307 ThrowMsg(NullPointerException, "Private object is NULL.");
309 EventRecurrenceRulePtr rrule = priv->getObject();
311 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
312 std::string frequency = converter.toString(value);
313 rrule->setFrequency(converter.toRecurrenceFrequency(frequency));
315 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
316 int interval = converter.toInt(value);
317 rrule->setInterval(interval);
319 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
320 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
321 ThrowMsg(ConversionException, "Wrong untilDate type.");
323 TimeUtilConverter timeConverter(context);
324 long long int untilDate = (long long int) (timeConverter.getTimeInMilliseconds(value)/1000);
325 rrule->setEndDate(untilDate);
327 if( rrule->getTimeZone().empty() ) {
328 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
329 rrule->setTimeZone(timeZone);
333 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
334 long occurrenceCount = converter.toLong(value);
335 rrule->setOccurrenceCount(occurrenceCount);
337 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
338 std::vector<int> daysOfTheMonth = converter.toVectorOfInts(value);
339 rrule->setDaysOfTheMonth(daysOfTheMonth);
341 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
342 rrule->setDaysOfTheWeek(converter.toStringArray(value));
344 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
345 std::vector<int> daysOfTheYear = converter.toVectorOfInts(value);
346 rrule->setDaysOfTheYear(daysOfTheYear);
348 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
349 std::vector<int> weekOfTheYear = converter.toVectorOfInts(value);
350 rrule->setWeeksOfTheYear(weekOfTheYear);
352 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITIONS)) {
353 rrule->setSetPositions(converter.toNumberArray(value));
355 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
356 rrule->setExceptions(converter.toTZDateArray(value));
362 LoggerW("Exception: "<<_rethrown_exception.GetMessage());