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 "JSRecurrenceRule.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;
35 #define TIZEN_RECURRENCE_RULE_ATTRIBUTENAME "RecurrenceRule"
37 JSClassDefinition JSRecurrenceRule::m_classInfo = {
39 kJSClassAttributeNone,
40 TIZEN_RECURRENCE_RULE_ATTRIBUTENAME,
49 NULL, //deleteProperty,
50 NULL, //getPropertyNames,
51 NULL, //callAsFunction,
52 NULL, //callAsConstructor,
54 NULL, //convertToType,
57 JSStaticValue JSRecurrenceRule::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_POSITION, getProperty, setProperty, kJSPropertyAttributeNone },
67 { TIZEN_RECURRENCE_RULE_EXCEPTIONS, getProperty, setProperty, kJSPropertyAttributeNone },
72 JSClassRef JSRecurrenceRule::m_jsClassRef = JSClassCreate(
73 JSRecurrenceRule::getClassInfo());
75 const JSClassDefinition* JSRecurrenceRule::getClassInfo()
77 return &(m_classInfo);
80 JSClassRef JSRecurrenceRule::getClassRef()
83 m_jsClassRef = JSClassCreate(&m_classInfo);
88 JSObjectRef JSRecurrenceRule::createJSRecurrenceRule(JSContextRef context, EventRecurrenceRulePtr rule)
90 RecurrenceRulePrivateObject *priv = new RecurrenceRulePrivateObject(context, rule);
91 return JSObjectMake(context, getClassRef(), priv);
94 void JSRecurrenceRule::initialize(JSContextRef context, JSObjectRef object)
99 void JSRecurrenceRule::finalize(JSObjectRef object)
102 RecurrenceRulePrivateObject* priv =
103 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
105 JSObjectSetPrivate(object, NULL);
108 JSValueRef JSRecurrenceRule::getProperty(JSContextRef context,
110 JSStringRef propertyName,
111 JSValueRef* exception)
114 CalendarConverterFactory::ConverterType converter =
115 CalendarConverterFactory::getConverter(context);
118 RecurrenceRulePrivateObject* priv =
119 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
121 Throw(NullPointerException);
123 EventRecurrenceRulePtr rrule = priv->getObject();
125 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
126 return converter->toJSValueRef(converter->toTizenValue(rrule->getFrequency()));
127 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
128 return converter->toJSValueRef(rrule->getInterval());
129 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
131 Throw(NullPointerException);
133 if (rrule->getEndDate() != 0) {
134 // Use the global context saved in the event struct.
135 return JSTZDate::createJSObject(priv->getContext(), rrule->getEndDate(), rrule->getTimeZone());
137 return JSValueMakeUndefined(context);
139 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
140 return converter->toJSValueRefLong(rrule->getOccurrenceCount());
141 // } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
142 // return converter->toJSValueRef(rrule->getDaysOfTheMonth());
143 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
144 return converter->toJSValueRef(rrule->getDaysOfTheWeek());
145 // } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
146 // return converter->toJSValueRef(rrule->getDaysOfTheYear());
147 // } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
148 // return converter->toJSValueRef(rrule->getWeeksOfTheYear());
149 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITION)) {
150 return converter->toJSValueRef(rrule->getSetPosition());
151 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
152 JSObjectRef jsResult = JSCreateArrayObject(context, 0, NULL);
153 if (NULL == jsResult) {
154 LogError("Could not create js array object.");
155 ThrowMsg(NullPointerException, "Could not create js array object");
158 for (std::size_t i = 0; i < rrule->getExceptions().size(); ++i) {
159 JSValueRef tmpVal = JSTZDate::createJSObject(priv->getContext(), rrule->getExceptions().at(i), rrule->getTimeZone());
160 if (!JSSetArrayElement(context, jsResult, i, tmpVal)) {
161 LogError("Could not insert value into js array.");
162 ThrowMsg(UnknownException, "Could not insert value into js array");
171 LogError("invalid property");
173 return JSValueMakeUndefined(context);
176 bool JSRecurrenceRule::setProperty(JSContextRef context,
178 JSStringRef propertyName,
180 JSValueRef* exception)
183 CalendarConverterFactory::ConverterType converter =
184 CalendarConverterFactory::getConverter(context);
187 RecurrenceRulePrivateObject* priv =
188 static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
190 Throw(NullPointerException);
192 EventRecurrenceRulePtr rrule = priv->getObject();
194 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
195 if (!JSValueIsString(context, value)) {
196 Throw(InvalidArgumentException);
198 std::string frequency = converter->toString(value);
199 rrule->setFrequency(converter->toRecurrenceFrequency(frequency));
201 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
202 if (!JSValueIsNumber(context, value)) {
203 Throw(InvalidArgumentException);
205 int interval = converter->toInt(value);
206 rrule->setInterval(interval);
208 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
209 if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
210 Throw(InvalidArgumentException);
212 TimeUtilConverter timeConverter(context);
213 std::time_t untilDate = timeConverter.toTZDateTimeT(value);
214 rrule->setEndDate(untilDate);
216 if( rrule->getTimeZone().empty() ) {
217 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
218 rrule->setTimeZone(timeZone);
221 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
222 if (!JSValueIsNumber(context, value)) {
223 Throw(InvalidArgumentException);
225 long occurrenceCount = converter->toLong(value);
226 rrule->setOccurrenceCount(occurrenceCount);
228 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
229 std::vector<int> daysOfTheMonth = converter->toVectorOfInts(value);
230 rrule->setDaysOfTheMonth(daysOfTheMonth);
232 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_WEEK)) {
233 std::vector<std::string> daysOfTheWeek = converter->toVectorOfStrings(value);
234 rrule->setDaysOfTheWeek(daysOfTheWeek);
236 /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
237 std::vector<int> daysOfTheYear = converter->toVectorOfInts(value);
238 rrule->setDaysOfTheYear(daysOfTheYear);
240 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
241 std::vector<int> weekOfTheYear = converter->toVectorOfInts(value);
242 rrule->setWeeksOfTheYear(weekOfTheYear);
244 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITION)) {
245 return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::NOT_SUPPORTED_ERROR);
247 bool setPosition = converter->toBool(value);
248 rrule->setSetPosition(setPosition);*/
250 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
251 // Only save the converted time info.
252 rrule->setExceptions(converter->toVectorOfTimeTFromTZDate(value));
258 LogWarning("trying to set incorrect value");
260 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);