Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSRecurrenceRule.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
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>
26
27 using namespace TizenApis::Api::Calendar;
28 using namespace WrtDeviceApis::Commons;
29 using namespace WrtDeviceApis::CommonsJavaScript;
30
31 namespace TizenApis {
32 namespace Tizen1_0 {
33 namespace Calendar {
34
35 #define TIZEN_RECURRENCE_RULE_ATTRIBUTENAME "RecurrenceRule"
36
37 JSClassDefinition JSRecurrenceRule::m_classInfo = {
38     0,
39     kJSClassAttributeNone,
40     TIZEN_RECURRENCE_RULE_ATTRIBUTENAME,
41     0,
42     m_property,
43     NULL, //m_function,
44     initialize,
45     finalize,
46     NULL, //hasProperty,
47     NULL, //getProperty,
48     NULL, //setProperty,
49     NULL, //deleteProperty,
50     NULL, //getPropertyNames,
51     NULL, //callAsFunction,
52     NULL, //callAsConstructor,
53     NULL, //hasInstance,
54     NULL, //convertToType,
55 };
56
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 },
68
69     { 0, 0, 0, 0 }
70 };
71
72 JSClassRef JSRecurrenceRule::m_jsClassRef = JSClassCreate(
73         JSRecurrenceRule::getClassInfo());
74
75 const JSClassDefinition* JSRecurrenceRule::getClassInfo()
76 {
77     return &(m_classInfo);
78 }
79
80 JSClassRef JSRecurrenceRule::getClassRef()
81 {
82     if (!m_jsClassRef) {
83         m_jsClassRef = JSClassCreate(&m_classInfo);
84     }
85     return m_jsClassRef;
86 }
87
88 JSObjectRef JSRecurrenceRule::createJSRecurrenceRule(JSContextRef context, EventRecurrenceRulePtr rule)
89 {
90     RecurrenceRulePrivateObject *priv = new RecurrenceRulePrivateObject(context, rule);
91     return JSObjectMake(context, getClassRef(), priv);
92 }
93
94 void JSRecurrenceRule::initialize(JSContextRef context, JSObjectRef object)
95 {
96     LogDebug("enter");
97 }
98
99 void JSRecurrenceRule::finalize(JSObjectRef object)
100 {
101     LogDebug("enter");
102     RecurrenceRulePrivateObject* priv =
103         static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
104     delete priv;
105     JSObjectSetPrivate(object, NULL);
106 }
107
108 JSValueRef JSRecurrenceRule::getProperty(JSContextRef context,
109         JSObjectRef object,
110         JSStringRef propertyName,
111         JSValueRef* exception)
112 {
113     LogDebug("enter");
114     CalendarConverterFactory::ConverterType converter =
115         CalendarConverterFactory::getConverter(context);
116     Try
117     {
118         RecurrenceRulePrivateObject* priv =
119             static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
120         if (!priv) {
121             Throw(NullPointerException);
122         }
123         EventRecurrenceRulePtr rrule = priv->getObject();
124
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)) {
130             if (!rrule) {
131                 Throw(NullPointerException);
132             }
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());
136             } else {
137                 return JSValueMakeUndefined(context);
138             }
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");
156             }
157
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");
163                 }
164             }
165
166             return jsResult;
167         }
168     }
169     Catch(Exception)
170     {
171         LogError("invalid property");
172     }
173     return JSValueMakeUndefined(context);
174 }
175
176 bool JSRecurrenceRule::setProperty(JSContextRef context,
177         JSObjectRef object,
178         JSStringRef propertyName,
179         JSValueRef value,
180         JSValueRef* exception)
181 {
182     LogDebug("entered");
183     CalendarConverterFactory::ConverterType converter =
184         CalendarConverterFactory::getConverter(context);
185     Try
186     {
187         RecurrenceRulePrivateObject* priv =
188             static_cast<RecurrenceRulePrivateObject*>(JSObjectGetPrivate(object));
189         if (!priv) {
190             Throw(NullPointerException);
191         }
192         EventRecurrenceRulePtr rrule = priv->getObject();
193
194         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_FREQUENCY)) {
195             if (!JSValueIsString(context, value)) {
196                 Throw(InvalidArgumentException);
197             }
198             std::string frequency = converter->toString(value);
199             rrule->setFrequency(converter->toRecurrenceFrequency(frequency));
200             return true;
201         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_INTERVAL)) {
202             if (!JSValueIsNumber(context, value)) {
203                 Throw(InvalidArgumentException);
204             }
205             int interval = converter->toInt(value);
206             rrule->setInterval(interval);
207             return true;
208         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_UNTIL_DATE)) {
209             if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
210                 Throw(InvalidArgumentException);
211             }
212             TimeUtilConverter timeConverter(context);
213             std::time_t untilDate = timeConverter.toTZDateTimeT(value);
214             rrule->setEndDate(untilDate);
215
216             if( rrule->getTimeZone().empty() ) {
217                 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
218                 rrule->setTimeZone(timeZone);
219             }
220             return true;
221         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_OCCURRENCE_COUNT)) {
222             if (!JSValueIsNumber(context, value)) {
223                 Throw(InvalidArgumentException);
224             }
225             long occurrenceCount = converter->toLong(value);
226             rrule->setOccurrenceCount(occurrenceCount);
227             return true;
228         /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_MONTH)) {
229             std::vector<int> daysOfTheMonth = converter->toVectorOfInts(value);
230             rrule->setDaysOfTheMonth(daysOfTheMonth);
231             return true;*/
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);
235             return true;
236         /*} else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_DAYS_OF_THE_YEAR)) {
237             std::vector<int> daysOfTheYear = converter->toVectorOfInts(value);
238             rrule->setDaysOfTheYear(daysOfTheYear);
239             return true;
240         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_WEEKS_OF_THE_YEAR)) {
241             std::vector<int> weekOfTheYear = converter->toVectorOfInts(value);
242             rrule->setWeeksOfTheYear(weekOfTheYear);
243             return true;*/
244         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_SET_POSITION)) {
245             return TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::NOT_SUPPORTED_ERROR);
246             /*
247             bool setPosition = converter->toBool(value);
248             rrule->setSetPosition(setPosition);*/
249             return true;
250         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_RECURRENCE_RULE_EXCEPTIONS)) {
251             // Only save the converted time info.
252             rrule->setExceptions(converter->toVectorOfTimeTFromTZDate(value));
253             return true;
254         }
255     }
256     Catch(Exception)
257     {
258         LogWarning("trying to set incorrect value");
259     }
260     TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
261     return false;
262 }
263
264 }
265 }
266 }