Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarEventId.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 "JSCalendarEventId.h"
19 #include "CalendarConverter.h"
20 #include <dpl/log/log.h>
21 #include <Tizen/Common/JSTizenExceptionFactory.h>
22 #include <Tizen/Common/JSTizenException.h>
23 #include <CommonsJavaScript/Converter.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 using namespace TizenApis::Commons;
31
32 namespace TizenApis {
33 namespace Tizen1_0 {
34 namespace Calendar {
35
36 JSClassDefinition JSCalendarEventId::m_classInfo = {
37     0,
38     kJSClassAttributeNone,
39     TIZEN_INTERFACE_CALENDAR_EVENT_ID,
40     0,
41     m_property,
42     NULL, //m_function,
43     initialize,
44     finalize,
45     NULL, //hasProperty,
46     NULL, //getProperty,
47     NULL, //setProperty,
48     NULL, //deleteProperty,
49     NULL, //getPropertyNames,
50     NULL, //callAsFunction,
51     constructor,
52     NULL, //hasInstance,
53     NULL, //convertToType,
54 };
55
56 JSStaticValue JSCalendarEventId::m_property[] = {
57     { TIZEN_CALENDAR_EVENT_ID_UID, getProperty, setProperty, kJSPropertyAttributeNone },
58     { TIZEN_CALENDAR_EVENT_ID_RECURRENCEID, getProperty, setProperty, kJSPropertyAttributeNone },
59     { 0, 0, 0, 0 }
60 };
61
62 JSClassRef JSCalendarEventId::m_jsClassRef = JSClassCreate(
63         JSCalendarEventId::getClassInfo());
64
65 const JSClassDefinition* JSCalendarEventId::getClassInfo()
66 {
67     return &(m_classInfo);
68 }
69
70 JSClassRef JSCalendarEventId::getClassRef()
71 {
72     if (!m_jsClassRef) {
73         m_jsClassRef = JSClassCreate(&m_classInfo);
74     }
75     return m_jsClassRef;
76 }
77
78 EventIdPtr JSCalendarEventId::getPrivateObject(JSObjectRef object)
79 {
80     LogDebug("entered");
81     EventIdPrivateObject *priv =
82         static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
83     if (!priv) {
84         ThrowMsg(NullPointerException, "Private object is null");
85     }
86     EventIdPtr result = priv->getObject();
87     if (!result) {
88         ThrowMsg(NullPointerException, "Private object is null");
89     }
90     return result;
91 }
92
93 void JSCalendarEventId::setPrivateObject(const EventIdPtr &eventId,
94         JSContextRef ctx,
95         const JSObjectRef object)
96 {
97     LogDebug("entered");
98     Try
99     {
100         EventIdPrivateObject *priv =
101             static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
102         delete priv;
103         priv = new EventIdPrivateObject(ctx, eventId);
104         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
105             delete priv;
106         }
107     }
108     Catch(Exception)
109     {
110         LogError("Error during replacing event id object.");
111     }
112 }
113
114 JSObjectRef JSCalendarEventId::createJSCalendarEventId(JSContextRef context, EventIdPtr eventId)
115 {
116     EventIdPrivateObject *priv = new EventIdPrivateObject(context, eventId);
117     return JSObjectMake(context, getClassRef(), priv);
118 }
119
120 void JSCalendarEventId::initialize(JSContextRef context,
121         JSObjectRef object)
122 {
123     LogDebug("enter");
124 }
125
126 void JSCalendarEventId::finalize(JSObjectRef object)
127 {
128     LogDebug("enter");
129     EventIdPrivateObject* priv =
130         static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
131     delete priv;
132     JSObjectSetPrivate(object, NULL);
133 }
134
135 JSObjectRef JSCalendarEventId::constructor(JSContextRef context,
136     JSObjectRef constructor,
137     size_t argumentCount,
138     const JSValueRef arguments[],
139     JSValueRef* exception)
140 {
141     LogDebug("entered");
142
143     Try
144     {
145         EventIdPrivateObject* privateObject = static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(constructor));
146         JSContextRef globalContext = privateObject ? privateObject->getContext() : context;
147
148         CalendarConverter converter(globalContext);
149         EventIdPtr eventId;
150
151         if (argumentCount==1) {
152             if (!JSValueIsString(context, arguments[0])) {
153                 ThrowMsg(ConversionException, "Wrong parameter type.");
154             }
155
156             EventIdPtr result( new EventId());
157             eventId = result;
158             eventId->setUId(converter.toString(arguments[0]));
159         } else if (argumentCount==2) {
160             if (!JSValueIsString(context, arguments[0])) {
161                 ThrowMsg(ConversionException, "Wrong parameter type.");
162             }
163             if (!JSValueIsObjectOfClass(context, arguments[0], JSTZDate::getClassRef())) {
164                 ThrowMsg(ConversionException, "Wrong parameter type.");
165             }
166
167             EventIdPtr result( new EventId());
168             eventId = result;
169             eventId->setUId(converter.toString(arguments[0]));
170
171             TimeUtilConverter timeUtilConverter(globalContext);
172             eventId->setRecurrenceId(timeUtilConverter.toTZDateTimeT(arguments[1]));
173         } else {
174             ThrowMsg(InvalidArgumentException, "Wrong number of parameters.");
175         }
176
177         return createJSCalendarEventId(globalContext, eventId);
178     }
179     Catch(UnsupportedException)
180     {
181         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
182         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
183     }
184     Catch(InvalidArgumentException)
185     {
186         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
187         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
188     }
189     Catch(ConversionException)
190     {
191         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
192         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
193     }
194     Catch(Exception)
195     {
196         LogWarning("Exception: "<<_rethrown_exception.GetMessage());
197         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
198     }
199
200     return NULL;
201 }
202
203 JSValueRef JSCalendarEventId::getProperty(JSContextRef context,
204         JSObjectRef object,
205         JSStringRef propertyName,
206         JSValueRef* exception)
207 {
208     LogDebug("enter");
209     Converter converter(context);
210     Try
211     {
212         EventIdPrivateObject* priv =
213             static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
214         if (!priv) {
215             Throw(NullPointerException);
216         }
217         EventIdPtr eventId = priv->getObject();
218
219         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_EVENT_ID_UID)) {
220             return converter.toJSValueRef(eventId->getUId());
221         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_EVENT_ID_RECURRENCEID)) {
222             if (!eventId) {
223                 Throw(NullPointerException);
224             }
225             if (eventId->getRecurrenceId() != 0) {
226                 // Use the global context saved in the event struct.
227                 return JSTZDate::createJSObject(priv->getContext(), eventId->getRecurrenceId(), eventId->getTimeZone());
228             } else {
229                 return JSValueMakeUndefined(context);
230             }
231         }
232     }
233     Catch(Exception)
234     {
235         LogError("invalid property");
236     }
237     return JSValueMakeUndefined(context);
238 }
239
240 bool JSCalendarEventId::setProperty(JSContextRef context,
241         JSObjectRef object,
242         JSStringRef propertyName,
243         JSValueRef value,
244         JSValueRef* exception)
245 {
246     LogDebug("entered");
247     Converter converter(context);
248     Try
249     {
250         EventIdPrivateObject* priv =
251             static_cast<EventIdPrivateObject*>(JSObjectGetPrivate(object));
252         if (!priv) {
253             Throw(NullPointerException);
254         }
255         EventIdPtr eventId = priv->getObject();
256
257         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_EVENT_ID_UID)) {
258             if (!JSValueIsString(context, value)) {
259                 Throw(InvalidArgumentException);
260             }
261             std::string uid = converter.toString(value);
262             eventId->setUId(uid);
263             return true;
264         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_EVENT_ID_RECURRENCEID)) {
265             if (!JSValueIsObjectOfClass(context, value, JSTZDate::getClassRef())) {
266                 Throw(InvalidArgumentException);
267             }
268             TimeUtilConverter timeConverter(context);
269             std::time_t recurrenceId = timeConverter.toTZDateTimeT(value);
270             eventId->setRecurrenceId(recurrenceId);
271
272             if( eventId->getTimeZone().empty() ) {
273                 std::string timeZone = timeConverter.getPropertiesInTZDate(value).timezone;
274                 eventId->setTimeZone(timeZone);
275             }
276             return true;
277         }
278     }
279     Catch(Exception)
280     {
281         LogWarning("trying to set incorrect value");
282     }
283     JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR);
284     return false;
285 }
286
287 }
288 }
289 }