Update change log and spec for wrt-plugins-tizen_0.4.29
[framework/web/wrt-plugins-tizen.git] / src / Calendar / JSCalendarAlarm.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
19 #include "JSCalendarAlarm.h"
20 #include "CalendarConverter.h"
21 #include <JSWebAPIErrorFactory.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include <TimeUtilConverter.h>
24 #include <JSTZDate.h>
25 #include <JSTimeDuration.h>
26 #include <Logger.h>
27 #include <Export.h>
28
29
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
33 using namespace DeviceAPI::Time;
34
35 namespace DeviceAPI {
36 namespace Calendar {
37
38 JSClassDefinition JSCalendarAlarm::m_classInfo = {
39     0,
40     kJSClassAttributeNone,
41     TIZEN_INTERFACE_CALENDAR_ALARM,
42     0,
43     m_property,
44     NULL, //m_function,
45     initialize,
46     finalize,
47     NULL, //hasProperty,
48     NULL, //getProperty,
49     NULL, //setProperty,
50     NULL, //deleteProperty,
51     NULL, //getPropertyNames,
52     NULL, //callAsFunction,
53     NULL, //constructor,
54     NULL, //hasInstance,
55     NULL, //convertToType,
56 };
57
58 JSStaticValue JSCalendarAlarm::m_property[] = {
59     { TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE, getProperty, setProperty, kJSPropertyAttributeNone },
60     { TIZEN_CALENDAR_ALARM_BEFORE, getProperty, setProperty, kJSPropertyAttributeNone },
61     { TIZEN_CALENDAR_ALARM_METHOD, getProperty, setProperty, kJSPropertyAttributeNone },
62     { TIZEN_CALENDAR_ALARM_DESCRIPTION, getProperty, setProperty, kJSPropertyAttributeNone },
63
64     { 0, 0, 0, 0 }
65 };
66
67 JSClassRef JSCalendarAlarm::m_jsClassRef = JSClassCreate(
68         JSCalendarAlarm::getClassInfo());
69
70 const JSClassDefinition* JSCalendarAlarm::getClassInfo()
71 {
72     return &(m_classInfo);
73 }
74
75 JSClassRef DLL_EXPORT JSCalendarAlarm::getClassRef()
76 {
77     if (!m_jsClassRef) {
78         m_jsClassRef = JSClassCreate(&m_classInfo);
79     }
80     return m_jsClassRef;
81 }
82
83 JSObjectRef JSCalendarAlarm::createJSCalendarAlarm(JSContextRef context, EventAlarmPtr alarm)
84 {
85     EventAlarmPrivateObject *priv = new EventAlarmPrivateObject(context, alarm);
86     return JSObjectMake(context, getClassRef(), priv);
87 }
88
89 EventAlarmPtr JSCalendarAlarm::getPrivateObject(JSObjectRef object)
90 {
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     Try
108     {
109         EventAlarmPrivateObject *priv =
110             static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
111         delete priv;
112         priv = new EventAlarmPrivateObject(ctx, alarm);
113         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
114             delete priv;
115         }
116     }
117     Catch(Exception)
118     {
119         LoggerE("Error during replacing alarm object.");
120     }
121 }
122
123 JSObjectRef DLL_EXPORT JSCalendarAlarm::constructor(JSContextRef context,
124     JSObjectRef constructor,
125     size_t argumentCount,
126     const JSValueRef arguments[],
127     JSValueRef* exception)
128 {
129     Try
130     {
131         if (argumentCount<1) {
132             ThrowMsg(ConversionException, "Wrong parameter type.");
133         }
134
135         CalendarConverter converter(context);
136         TimeUtilConverter timeConverter(context);
137
138         EventAlarmPtr alarm( new EventAlarm() );
139
140         if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())){
141             LoggerD("absoluteDate case");
142             long long int absoluteDate = (long long int) (timeConverter.getTimeInMilliseconds(arguments[0])/1000);
143             alarm->setAbsoluteDate(absoluteDate);
144             alarm->setTimeZone(timeConverter.getPropertiesInTZDate(arguments[0]).timezone);
145
146             if (argumentCount>=2) {
147                 alarm->setMethod(converter.toEventAlarmType(converter.toString(arguments[1])));
148             }
149             if (argumentCount>=3) {
150                 alarm->setDescription(converter.toString(arguments[2]));
151             }
152         } else if (JSValueIsObjectOfClass(context, arguments[0], JSTimeDuration::getClassRef())){
153             LoggerD("before case");
154             alarm->setDuration(timeConverter.getDurationPropertis(arguments[0]));
155
156             if (argumentCount>=2) {
157                 alarm->setMethod(converter.toEventAlarmType(converter.toString(arguments[1])));
158             }
159             if (argumentCount>=3) {
160                 alarm->setDescription(converter.toString(arguments[2]));
161             }
162         } else {
163             ThrowMsg(ConversionException, "Wrong first parameter type.");
164         }
165
166         return createJSCalendarAlarm(context, alarm);
167     }
168     Catch(UnsupportedException)
169     {
170         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
171         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
172     }
173     Catch(InvalidArgumentException)
174     {
175         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
176         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
177     }
178     Catch(ConversionException)
179     {
180         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
181         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
182     }
183     Catch(Exception)
184     {
185         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
186         return JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
187     }
188 }
189
190 void JSCalendarAlarm::initialize(JSContextRef context,
191         JSObjectRef object)
192 {
193     if (!JSObjectGetPrivate(object)) {
194         LoggerD("Create calendar alarm private object.");
195         EventAlarmPtr alarm( new EventAlarm() );
196         EventAlarmPrivateObject *priv = new EventAlarmPrivateObject(context, alarm);
197         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
198             delete priv;
199         }
200     } else {
201         LoggerD("Private object already set.");
202     }
203 }
204
205 void JSCalendarAlarm::finalize(JSObjectRef object)
206 {
207     EventAlarmPrivateObject* priv = static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
208     if (priv) {
209         delete priv;
210         JSObjectSetPrivate(object, NULL);
211     }
212 }
213
214 JSValueRef JSCalendarAlarm::getProperty(JSContextRef context,
215         JSObjectRef object,
216         JSStringRef propertyName,
217         JSValueRef* exception)
218 {
219     CalendarConverter converter(context);
220     Try
221     {
222         EventAlarmPrivateObject* priv =
223             static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
224         if (!priv) {
225             ThrowMsg(NullPointerException, "Private object is NULL.");
226         }
227         EventAlarmPtr alarm = priv->getObject();
228
229         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
230             if (!alarm) {
231                 ThrowMsg(NullPointerException, "Alarm object is NULL.");
232             }
233
234             if (UNDEFINED_TIME==alarm->getAbsoluteDate()) {
235                 return JSValueMakeNull(context);
236             } else {
237                 TimeUtilConverter timeConverter(context);
238                 return timeConverter.toJSValueRefTZDate((double)(alarm->getAbsoluteDate()*1000.0), alarm->getTimeZone());
239             }
240         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
241             return JSTimeDuration::createJSObject(context, alarm->getDuration());
242         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
243             return converter.toJSValueRef(alarm->getDescription());
244         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
245                 LoggerD("Alarm method: "<<converter.toTizenValue(alarm->getMethod()));
246             return converter.toJSValueRef(converter.toTizenValue(alarm->getMethod()));
247         }
248     }
249     Catch(WrtDeviceApis::Commons::Exception)
250     {
251                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
252     }
253     return JSValueMakeUndefined(context);
254 }
255
256 bool JSCalendarAlarm::setProperty(JSContextRef context,
257         JSObjectRef object,
258         JSStringRef propertyName,
259         JSValueRef value,
260         JSValueRef* exception)
261 {
262     CalendarConverter converter(context);
263     Try
264     {
265         EventAlarmPrivateObject* priv =
266             static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
267         if (!priv) {
268             ThrowMsg(NullPointerException, "Private object is NULL.");
269         }
270         EventAlarmPtr alarm = priv->getObject();
271
272         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
273             if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
274                 ThrowMsg(ConversionException, "Wrong absoluteDate type.");
275             }
276             TimeUtilConverter timeConverter(context);
277             long long int absoluteDate = (long long int) (timeConverter.getTimeInMilliseconds(value)/1000);
278             alarm->setAbsoluteDate(absoluteDate);
279
280             if( alarm->getTimeZone().empty() ) {
281                 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
282                 alarm->setTimeZone(timeZone);
283             }
284             return true;
285         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
286             if (!JSValueIsObjectOfClass(context, value, JSTimeDuration::getClassRef())) {
287                 ThrowMsg(ConversionException, "Wrong before type.");
288             }
289             TimeUtilConverter timeConverter(context);
290             alarm->setDuration(timeConverter.getDurationPropertis(value));
291             return true;
292         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
293             alarm->setDescription(converter.toString(value));
294             return true;
295         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
296             alarm->setMethod(converter.toEventAlarmType(converter.toString(value)));
297             return true;
298         }
299     }
300     Catch(WrtDeviceApis::Commons::Exception)
301     {
302                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
303     }
304
305     return true;
306 }
307
308 }
309 }