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.
19 #include "JSCalendarRecurrenceRule.h"
20 #include <dpl/log/log.h>
21 #include <JSTizenException.h>
22 #include <JSTizenExceptionFactory.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include "CalendarConverter.h"
25 #include <TimeUtilConverter.h>
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace DeviceAPI::Common;
32 using namespace DeviceAPI::Time;
37 JSClassDefinition JSCalendarRecurrenceRule::m_classInfo = {
39 kJSClassAttributeNone,
40 TIZEN_INTERFACE_CALENDAR_RECURRENCE_RULE,
49 NULL, //deleteProperty,
50 NULL, //getPropertyNames,
51 NULL, //callAsFunction,
54 NULL, //convertToType,
57 JSStaticValue JSCalendarRecurrenceRule::m_property[] = {
58 { TIZEN_RECURRENCE_RULE_FREQUENCY, getProperty, setProperty, kJSPropertyAttributeNone },
59 { TIZEN_RECURRENCE_RULE_INTERVAL, getProperty, setProperty, kJSPropertyAttributeNone },
60 { TIZEN_RECURRENCE_RULE_UNTIL_DATE, getProperty, setProperty, kJSPropertyAttributeNone },
61 { TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT, getProperty, setProperty, kJSPropertyAttributeNone },
62 // { TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH, getProperty, setProperty, kJSPropertyAttributeNone },
63 { TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK, getProperty, setProperty, kJSPropertyAttributeNone },
64 // { TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR, getProperty, setProperty, kJSPropertyAttributeNone },
65 // { TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR, getProperty, setProperty, kJSPropertyAttributeNone },
66 { TIZEN_RECURRENCE_RULE_SET_POSITIONS, getProperty, setProperty, kJSPropertyAttributeNone },
67 { TIZEN_RECURRENCE_RULE_EXCEPTIONS, getProperty, setProperty, kJSPropertyAttributeNone },
72 JSClassRef JSCalendarRecurrenceRule::m_jsClassRef = JSClassCreate(
73 JSCalendarRecurrenceRule::getClassInfo());
75 const JSClassDefinition* JSCalendarRecurrenceRule::getClassInfo()
77 return &(m_classInfo);
80 JSClassRef JSCalendarRecurrenceRule::getClassRef()
83 m_jsClassRef = JSClassCreate(&m_classInfo);
88 JSObjectRef JSCalendarRecurrenceRule::createJSCalendarRecurrenceRule(JSContextRef context, EventRecurrenceRulePtr rule)
90 RecurrenceRulePrivateObject *priv = new RecurrenceRulePrivateObject(context, rule);
91 return JSObjectMake(context, getClassRef(), priv);
94 void JSCalendarRecurrenceRule::initialize(JSContextRef context, JSObjectRef object)
96 if (!JSObjectGetPrivate(object)) {
97 LogDebug("Create calendar rrule private object.");
98 EventRecurrenceRulePtr rrule( new EventRecurrenceRule() );
99 RecurrenceRulePrivateObject *priv = new RecurrenceRulePrivateObject(context, rrule);
100 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
104 LogDebug("Private object already set.");
108 void JSCalendarRecurrenceRule::finalize(JSObjectRef object)
110 RecurrenceRulePrivateObject* priv = static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
113 JSObjectSetPrivate(object, NULL);
117 EventRecurrenceRulePtr JSCalendarRecurrenceRule::getPrivateObject(JSObjectRef object)
119 RecurrenceRulePrivateObject *priv =
120 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
122 ThrowMsg(NullPointerException, "Private object is null.");
124 EventRecurrenceRulePtr result = priv->getObject();
126 ThrowMsg(NullPointerException, "Private object is null.");
131 void JSCalendarRecurrenceRule::setPrivateObject(const EventRecurrenceRulePtr &rrule,
133 const JSObjectRef object)
137 RecurrenceRulePrivateObject *priv =
138 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
140 priv = new RecurrenceRulePrivateObject(ctx, rrule);
141 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
147 LogError("Error during replacing rrule object.");
151 JSObjectRef JSCalendarRecurrenceRule::constructor(JSContextRef context,
152 JSObjectRef constructor,
153 size_t argumentCount,
154 const JSValueRef arguments[],
155 JSValueRef* exception)
159 CalendarConverter converter(context);
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 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
191 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
193 Catch(InvalidArgumentException)
195 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
196 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
198 Catch(ConversionException)
200 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
201 *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
205 LogWarning("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)
217 CalendarConverter converter(context);
218 TimeUtilConverter timeConverter(context);
221 RecurrenceRulePrivateObject* priv =
222 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
224 ThrowMsg(NullPointerException, "Private object is NULL.");
226 EventRecurrenceRulePtr rrule = priv->getObject();
228 ThrowMsg(NullPointerException, "recurrenceRule is NULL.");
231 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
232 return converter.toJSValueRef(converter.toTizenValue(rrule->getFrequency()));
233 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
234 return converter.toJSValueRef(rrule->getInterval());
235 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
236 if (UNDEFINED_TIME==rrule->getEndDate()) {
237 return JSValueMakeNull(context);
239 return timeConverter.toJSValueRefTZDate((double)(rrule->getEndDate()*1000.0), rrule->getTimeZone());
241 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
242 return converter.toJSValueRefLong(rrule->getOccurrenceCount());
243 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
244 //return converter.toJSValueRef(rrule->getDaysOfTheMonth());
245 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
246 return converter.toJSValueRef(rrule->getDaysOfTheWeek());
247 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
248 //return converter.toJSValueRef(rrule->getDaysOfTheYear());
249 //} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
250 //return converter.toJSValueRef(rrule->getWeeksOfTheYear());
251 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITIONS)) {
252 return converter.toJSValueRefNumberArray(rrule->getSetPositions());
253 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
254 return converter.toJSValueRefTZDateArray(rrule->getExceptions());
259 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
261 return JSValueMakeUndefined(context);
264 bool JSCalendarRecurrenceRule::setProperty(JSContextRef context,
266 JSStringRef propertyName,
268 JSValueRef* exception)
270 CalendarConverter converter(context);
273 RecurrenceRulePrivateObject* priv =
274 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
276 ThrowMsg(NullPointerException, "Private object is NULL.");
278 EventRecurrenceRulePtr rrule = priv->getObject();
280 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
281 std::string frequency = converter.toString(value);
282 rrule->setFrequency(converter.toRecurrenceFrequency(frequency));
284 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
285 int interval = converter.toInt(value);
286 rrule->setInterval(interval);
288 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
289 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
290 ThrowMsg(ConversionException, "Wrong untilDate type.");
292 TimeUtilConverter timeConverter(context);
293 long long int untilDate = (long long int) (timeConverter.getTimeInMilliseconds(value)/1000);
294 rrule->setEndDate(untilDate);
296 if( rrule->getTimeZone().empty() ) {
297 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
298 rrule->setTimeZone(timeZone);
302 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
303 long occurrenceCount = converter.toLong(value);
304 rrule->setOccurrenceCount(occurrenceCount);
306 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
307 std::vector<int> daysOfTheMonth = converter.toVectorOfInts(value);
308 rrule->setDaysOfTheMonth(daysOfTheMonth);
310 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
311 rrule->setDaysOfTheWeek(converter.toStringArray(value));
313 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
314 std::vector<int> daysOfTheYear = converter.toVectorOfInts(value);
315 rrule->setDaysOfTheYear(daysOfTheYear);
317 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
318 std::vector<int> weekOfTheYear = converter.toVectorOfInts(value);
319 rrule->setWeeksOfTheYear(weekOfTheYear);
321 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITIONS)) {
322 rrule->setSetPositions(converter.toNumberArray(value));
324 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
325 rrule->setExceptions(converter.toTZDateArray(value));
331 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
333 JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);