Update change log and spec for wrt-plugins-tizen_0.2.84
[framework/web/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarAlarm.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 "JSCalendarAlarm.h"
19 #include "CalendarConverter.h"
20 #include <dpl/log/log.h>
21 #include <Tizen/Common/JSTizenException.h>
22 #include <Tizen/Common/JSTizenExceptionFactory.h>
23 #include <CommonsJavaScript/Converter.h>
24 #include <Tizen/TimeUtil/TimeUtilConverter.h>
25 #include <Tizen/TimeUtil/JSTZDate.h>
26 #include <Tizen/TimeUtil/JSTimeDuration.h>
27
28 using namespace TizenApis::Api::Calendar;
29 using namespace WrtDeviceApis::Commons;
30 using namespace WrtDeviceApis::CommonsJavaScript;
31 using namespace TizenApis::Commons;
32
33 namespace TizenApis {
34 namespace Tizen1_0 {
35 namespace Calendar {
36
37 JSClassDefinition JSCalendarAlarm::m_classInfo = {
38     0,
39     kJSClassAttributeNone,
40     TIZEN_INTERFACE_CALENDAR_ALARM,
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     constructor,
53     NULL, //hasInstance,
54     NULL, //convertToType,
55 };
56
57 JSStaticValue JSCalendarAlarm::m_property[] = {
58     { TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE, getProperty, setProperty, kJSPropertyAttributeNone },
59     { TIZEN_CALENDAR_ALARM_BEFORE, getProperty, setProperty, kJSPropertyAttributeNone },
60     { TIZEN_CALENDAR_ALARM_METHOD, getProperty, setProperty, kJSPropertyAttributeNone },
61     { TIZEN_CALENDAR_ALARM_DESCRIPTION, getProperty, setProperty, kJSPropertyAttributeNone },
62
63     { 0, 0, 0, 0 }
64 };
65
66 JSClassRef JSCalendarAlarm::m_jsClassRef = JSClassCreate(
67         JSCalendarAlarm::getClassInfo());
68
69 const JSClassDefinition* JSCalendarAlarm::getClassInfo()
70 {
71     return &(m_classInfo);
72 }
73
74 JSClassRef JSCalendarAlarm::getClassRef()
75 {
76     if (!m_jsClassRef) {
77         m_jsClassRef = JSClassCreate(&m_classInfo);
78     }
79     return m_jsClassRef;
80 }
81
82 JSObjectRef JSCalendarAlarm::createJSCalendarAlarm(JSContextRef context, EventAlarmPtr alarm)
83 {
84     EventAlarmPrivateObject *priv = new EventAlarmPrivateObject(context, alarm);
85     return JSObjectMake(context, getClassRef(), priv);
86 }
87
88 EventAlarmPtr JSCalendarAlarm::getPrivateObject(JSObjectRef object)
89 {
90     LogDebug("entered");
91     EventAlarmPrivateObject *priv =
92         static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
93     if (!priv) {
94         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
95     }
96     EventAlarmPtr result = priv->getObject();
97     if (!result) {
98         ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
99     }
100     return result;
101 }
102
103 void JSCalendarAlarm::setPrivateObject(const EventAlarmPtr &alarm,
104         JSContextRef ctx,
105         const JSObjectRef object)
106 {
107     LogDebug("entered");
108     Try
109     {
110         EventAlarmPrivateObject *priv =
111             static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
112         delete priv;
113         priv = new EventAlarmPrivateObject(ctx, alarm);
114         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
115             delete priv;
116         }
117     }
118     Catch(Exception)
119     {
120         LogError("Error during replacing alarm object.");
121     }
122 }
123
124 JSObjectRef JSCalendarAlarm::constructor(JSContextRef context,
125     JSObjectRef constructor,
126     size_t argumentCount,
127     const JSValueRef arguments[],
128     JSValueRef* exception)
129 {
130     LogDebug("entered");
131
132     Try
133     {
134         if (argumentCount<1) {
135             ThrowMsg(ConversionException, "Wrong parameter type.");
136         }
137
138         CalendarConverter converter(context);
139         TimeUtilConverter timeConverter(context);
140
141         EventAlarmPtr alarm( new EventAlarm() );
142
143         if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())){
144             LogDebug("absoluteDate case");
145             long long int absoluteDate = (long long int) (timeConverter.getTimeInMilliseconds(arguments[0])/1000);
146             alarm->setAbsoluteDate(absoluteDate);
147             alarm->setTimeZone(timeConverter.getPropertiesInTZDate(arguments[0]).timezone);
148
149             if (argumentCount>=2) {
150                 std::string method = converter.toString(arguments[1]);
151                 std::vector<EventAlarm::EventAlarmType> convertedMethods;
152                 convertedMethods.push_back(converter.toEventAlarmType(method));
153                 alarm->setMethods(convertedMethods);
154             }
155             if (argumentCount>=3) {
156                 alarm->setDescription(converter.toString(arguments[2]));
157             }
158         } else if (JSValueIsObjectOfClass(context, arguments[0], JSTimeDuration::getClassRef())){
159             LogDebug("before case");
160             alarm->setDuration(timeConverter.getDurationPropertis(arguments[0]));
161
162             if (argumentCount>=2) {
163                 std::string method = converter.toString(arguments[1]);
164                 std::vector<EventAlarm::EventAlarmType> convertedMethods;
165                 convertedMethods.push_back(converter.toEventAlarmType(method));
166                 alarm->setMethods(convertedMethods);
167             }
168             if (argumentCount>=3) {
169                 alarm->setDescription(converter.toString(arguments[2]));
170             }
171         } else {
172             ThrowMsg(ConversionException, "Wrong first parameter type.");
173         }
174
175         return createJSCalendarAlarm(context, alarm);
176     }
177     Catch(UnsupportedException)
178     {
179         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
180         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
181     }
182     Catch(InvalidArgumentException)
183     {
184         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
185         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
186     }
187     Catch(ConversionException)
188     {
189         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
190         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
191     }
192     Catch(Exception)
193     {
194         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
195         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
196     }
197
198     return NULL;
199 }
200
201 void JSCalendarAlarm::initialize(JSContextRef context,
202         JSObjectRef object)
203 {
204     if (!JSObjectGetPrivate(object)) {
205         LogDebug("Create calendar alarm private object.");
206         EventAlarmPtr alarm( new EventAlarm() );
207         EventAlarmPrivateObject *priv = new EventAlarmPrivateObject(context, alarm);
208         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
209             delete priv;
210         }
211     } else {
212         LogDebug("Private object alrerady set.");
213     }
214 }
215
216 void JSCalendarAlarm::finalize(JSObjectRef object)
217 {
218     LogDebug("entered");
219     EventAlarmPrivateObject* priv = static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
220     if (priv) {
221         delete priv;
222         JSObjectSetPrivate(object, NULL);
223     }
224 }
225
226 JSValueRef JSCalendarAlarm::getProperty(JSContextRef context,
227         JSObjectRef object,
228         JSStringRef propertyName,
229         JSValueRef* exception)
230 {
231     LogDebug("enter");
232     CalendarConverter converter(context);
233     Try
234     {
235         EventAlarmPrivateObject* priv =
236             static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
237         if (!priv) {
238             ThrowMsg(NullPointerException, "Private object is NULL.");
239         }
240         EventAlarmPtr alarm = priv->getObject();
241
242         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
243             if (!alarm) {
244                 ThrowMsg(NullPointerException, "Alarm object is NULL.");
245             }
246
247             if (UNDEFINED_TIME==alarm->getAbsoluteDate()) {
248                 return JSValueMakeNull(context);
249             } else {
250                 TimeUtilConverter timeConverter(context);
251                 return timeConverter.toJSValueRefTZDate((double)(alarm->getAbsoluteDate()*1000.0), alarm->getTimeZone());
252             }
253         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
254             return JSTimeDuration::createJSObject(context, alarm->getDuration());
255         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
256             return converter.toJSValueRef(alarm->getDescription());
257         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
258             return converter.toJSValueRef(converter.toTizenValue(alarm->getMethods().at(0)));
259         }
260     }
261     Catch(WrtDeviceApis::Commons::Exception)
262     {
263                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
264     }
265     return JSValueMakeUndefined(context);
266 }
267
268 bool JSCalendarAlarm::setProperty(JSContextRef context,
269         JSObjectRef object,
270         JSStringRef propertyName,
271         JSValueRef value,
272         JSValueRef* exception)
273 {
274     LogDebug("entered");
275     CalendarConverter converter(context);
276     Try
277     {
278         EventAlarmPrivateObject* priv =
279             static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
280         if (!priv) {
281             ThrowMsg(NullPointerException, "Private object is NULL.");
282         }
283         EventAlarmPtr alarm = priv->getObject();
284
285         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
286             if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
287                 ThrowMsg(ConversionException, "Wrong absoluteDate type.");
288             }
289             TimeUtilConverter timeConverter(context);
290             long long int absoluteDate = (long long int) (timeConverter.getTimeInMilliseconds(value)/1000);
291             alarm->setAbsoluteDate(absoluteDate);
292
293             if( alarm->getTimeZone().empty() ) {
294                 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
295                 alarm->setTimeZone(timeZone);
296             }
297             return true;
298         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
299             if (!JSValueIsObjectOfClass(context, value, JSTimeDuration::getClassRef())) {
300                 ThrowMsg(ConversionException, "Wrong before type.");
301             }
302             TimeUtilConverter timeConverter(context);
303             alarm->setDuration(timeConverter.getDurationPropertis(value));
304             return true;
305         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
306             alarm->setDescription(converter.toString(value));
307             return true;
308         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
309             std::string method = converter.toString(value);
310             std::vector<EventAlarm::EventAlarmType> convertedMethods;
311             convertedMethods.push_back(converter.toEventAlarmType(method));
312             alarm->setMethods(convertedMethods);
313             return true;
314         }
315     }
316     Catch(WrtDeviceApis::Commons::Exception)
317     {
318                 LogWarning("Exception: "<<_rethrown_exception.GetMessage());
319     }
320     TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
321     return false;
322 }
323
324 }
325 }
326 }