Beta merge 2
[profile/ivi/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<2 || argumentCount>3) {
135             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
136         }
137
138         EventAlarmPrivateObject* privateObject = static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(constructor));
139         JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
140
141         CalendarConverter converter(globalContext);
142         EventAlarmPtr alarm( new EventAlarm());
143
144         TimeUtilConverter timeConverter(context);
145         if (JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())){ // absoluteDate case
146             std::time_t absoluteDate = timeConverter.toTZDateTimeT(arguments[0]);
147             alarm->setAbsoluteDate(absoluteDate);
148             alarm->setTimeZone(timeConverter.getPropertiesInTZDate(arguments[0]).timezone);
149             if (JSValueIsString(context, arguments[1])) {
150                 std::string method = converter.toString(arguments[1]);
151                 std::vector<CalendarEvent::EventAlarmType> convertedMethods;
152                 convertedMethods.push_back(converter.toEventAlarmType(method));
153                 alarm->setMethods(convertedMethods);
154             } else {
155                 ThrowMsg(ConversionException, "Second parameter conversion failed.");
156             }
157             if (argumentCount==3) {
158                 if (JSValueIsString(context, arguments[2])) {
159                     alarm->setDescription(converter.toString(arguments[2]));
160                 } else {
161                     ThrowMsg(ConversionException, "Third parameter conversion failed.");
162                 }
163             }
164         } else { // if (JSValueIsObjectOfClass(context, arguments[0], JSTimeDuration::getClassRef())){ // before case
165             alarm->setDuration(timeConverter.getDurationPropertis(arguments[0]));
166             if (JSValueIsString(context, arguments[1])) {
167                 std::string method = converter.toString(arguments[1]);
168                 std::vector<CalendarEvent::EventAlarmType> convertedMethods;
169                 convertedMethods.push_back(converter.toEventAlarmType(method));
170                 alarm->setMethods(convertedMethods);
171             } else {
172                 ThrowMsg(ConversionException, "Second parameter conversion failed.");
173             }
174             if (argumentCount==3) {
175                 if (JSValueIsString(context, arguments[2])) {
176                     alarm->setDescription(converter.toString(arguments[2]));
177                 } else {
178                     ThrowMsg(ConversionException, "Thrid parameter conversion failed.");
179                 }
180             }
181         }/* else { // Workaround until plugin link issue is resolved.
182             ThrowMsg(ConversionException, "First parameter conversion failed.");
183         }*/
184
185         return createJSCalendarAlarm(globalContext, alarm);
186     }
187     Catch(UnsupportedException)
188     {
189         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
190         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
191     }
192     Catch(InvalidArgumentException)
193     {
194         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
195         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
196     }
197     Catch(ConversionException)
198     {
199         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
200         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
201     }
202     Catch(Exception)
203     {
204         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
205         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
206     }
207
208     return NULL;
209 }
210
211 void JSCalendarAlarm::initialize(JSContextRef context,
212         JSObjectRef object)
213 {
214     LogDebug("enter");
215 }
216
217 void JSCalendarAlarm::finalize(JSObjectRef object)
218 {
219     LogDebug("enter");
220     EventAlarmPrivateObject* priv =
221         static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
222     delete priv;
223     JSObjectSetPrivate(object, NULL);
224 }
225
226 JSValueRef JSCalendarAlarm::getProperty(JSContextRef context,
227         JSObjectRef object,
228         JSStringRef propertyName,
229         JSValueRef* exception)
230 {
231     LogDebug("enter");
232     WrtDeviceApis::CommonsJavaScript::Converter converter(context);
233     Try
234     {
235         EventAlarmPrivateObject* priv =
236             static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
237         if (!priv) {
238             Throw(WrtDeviceApis::Commons::NullPointerException);
239         }
240         EventAlarmPtr alarm = priv->getObject();
241
242         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
243             if (!alarm) {
244                 Throw(WrtDeviceApis::Commons::NullPointerException);
245             }
246             if (alarm->getAbsoluteDate() != 0) {
247                 // Use the global context saved in the event struct.
248                 return JSTZDate::createJSObject(priv->getContext(), alarm->getAbsoluteDate(), alarm->getTimeZone());
249             } else {
250                 return JSValueMakeUndefined(context);
251             }
252         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
253             return JSTimeDuration::createJSObject(priv->getContext(), alarm->getDuration());
254         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
255             return converter.toJSValueRef(alarm->getDescription());
256         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
257             std::vector<JSValueRef> result;
258             std::vector<CalendarEvent::EventAlarmType> methods = alarm->getMethods();
259             for(std::vector<CalendarEvent::EventAlarmType>::iterator i = methods.begin(); i != methods.end(); i++)
260                 result.push_back(converter.toJSValueRef(*i));
261             return converter.toJSValueRef(result);
262         }
263     }
264     Catch(WrtDeviceApis::Commons::Exception)
265     {
266         LogError("invalid property");
267     }
268     return JSValueMakeUndefined(context);
269 }
270
271 bool JSCalendarAlarm::setProperty(JSContextRef context,
272         JSObjectRef object,
273         JSStringRef propertyName,
274         JSValueRef value,
275         JSValueRef* exception)
276 {
277     LogDebug("entered");
278     CalendarConverter converter(context);
279     Try
280     {
281         EventAlarmPrivateObject* priv =
282             static_cast<EventAlarmPrivateObject*>(JSObjectGetPrivate(object));
283         if (!priv) {
284             Throw(WrtDeviceApis::Commons::NullPointerException);
285         }
286         EventAlarmPtr alarm = priv->getObject();
287
288         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_ABSOLUTE_DATE)) {
289             if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
290                 Throw(InvalidArgumentException);
291             }
292             TimeUtilConverter timeConverter(context);
293             std::time_t absoluteDate = timeConverter.toTZDateTimeT(value);
294             alarm->setAbsoluteDate(absoluteDate);
295
296             if( alarm->getTimeZone().empty() ) {
297                 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
298                 alarm->setTimeZone(timeZone);
299             }
300             return true;
301         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_BEFORE)) {
302             if (!JSValueIsObjectOfClass(context, value, JSTimeDuration::getClassRef())) {
303                 Throw(InvalidArgumentException);
304             }
305             TimeUtilConverter timeConverter(context);
306             alarm->setDuration(timeConverter.getDurationPropertis(value));
307             return true;
308         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_DESCRIPTION)) {
309             if (!JSValueIsString(context, value)) {
310                 Throw(InvalidArgumentException);
311             }
312             alarm->setDescription(converter.toString(value));
313             return true;
314         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ALARM_METHOD)) {
315             std::string method = converter.toString(value);
316             std::vector<CalendarEvent::EventAlarmType> convertedMethods;
317             convertedMethods.push_back(converter.toEventAlarmType(method));
318             alarm->setMethods(convertedMethods);
319             return true;
320         }
321     }
322     Catch(WrtDeviceApis::Commons::Exception)
323     {
324         LogWarning("trying to set incorrect value");
325     }
326     TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
327     return false;
328 }
329
330 }
331 }
332 }