c92242635d75196014d8d6bbdec466a61398f399
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarEvent.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 <ctime>
19 #include <dpl/log/log.h>
20 #include <CommonsJavaScript/PrivateObject.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <API/Calendar/EventId.h>
25 #include "JSCalendarEvent.h"
26 #include "JSCalendarItemProperties.h"
27 #include "CalendarConverter.h"
28 #include "JSEventId.h"
29 #include <Tizen/TimeUtil/TimeUtilConverter.h>
30 #include <Tizen/TimeUtil/JSTZDate.h>
31
32 using namespace TizenApis::Api::Calendar;
33 using namespace WrtDeviceApis::Commons;
34 using namespace WrtDeviceApis::CommonsJavaScript;
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38 namespace Calendar {
39
40 #define TIZEN_CALENDAR_EVENT_ATTRIBUTENAME "CalendarEvent"
41
42 JSClassDefinition JSCalendarEvent::m_classInfo = {
43     0,
44     kJSClassAttributeNone,
45     TIZEN_CALENDAR_EVENT_ATTRIBUTENAME,
46     JSCalendarItemProperties::getClassRef(),
47     m_property,
48     NULL, //    m_function,
49     initialize,
50     finalize,
51     NULL, //hasProperty,
52     NULL, //getProperty,
53     NULL, //setProperty,
54     NULL, //DeleteProperty,
55     NULL, //GetPropertyNames,
56     NULL, //CallAsFunction,
57     NULL, //CallAsConstructor,
58     NULL, //HasInstance,
59     NULL  //ConvertToType
60 };
61
62 JSStaticValue JSCalendarEvent::m_property[] = {
63     { TIZEN_CALENDAR_EVENT_ID, getPropertyId,
64       NULL, kJSPropertyAttributeReadOnly },
65     { TIZEN_CALENDAR_EVENT_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
66       NULL, kJSPropertyAttributeReadOnly },
67     { TIZEN_CALENDAR_EVENT_IS_DETACHED, getPropertyIsDetached,
68       NULL, kJSPropertyAttributeReadOnly },
69
70     { 0, 0, 0, 0 }
71 };
72
73 JSClassRef JSCalendarEvent::m_jsClassRef = JSClassCreate(JSCalendarEvent::getClassInfo());
74
75 void JSCalendarEvent::initialize(JSContextRef context,
76         JSObjectRef object)
77 {
78     LogDebug("entered");
79     CalendarEventPrivObject *priv =
80         static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
81     if (!priv) {
82         CalendarEventPtr privateData(new CalendarEvent());
83         priv = new CalendarEventPrivObject(context, privateData);
84         JSObjectSetPrivate(object, static_cast<void*>(priv));
85         LogDebug("new event is created");
86     } else {
87         LogDebug("private object already exists");
88     }
89 }
90
91 void JSCalendarEvent::finalize(JSObjectRef object)
92 {
93     LogDebug("entered");
94 }
95
96 const JSClassRef JSCalendarEvent::getClassRef()
97 {
98     if (!m_jsClassRef) {
99         m_jsClassRef = JSClassCreate(&m_classInfo);
100     }
101     return m_jsClassRef;
102 }
103
104 const JSClassDefinition* JSCalendarEvent::getClassInfo()
105 {
106     return &m_classInfo;
107 }
108
109 CalendarEventPtr JSCalendarEvent::getPrivateObject(JSObjectRef object)
110 {
111     LogDebug("entered");
112     CalendarEventPrivObject *priv =
113         static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
114     if (!priv) {
115         ThrowMsg(NullPointerException, "Private object is null");
116     }
117     CalendarEventPtr result = priv->getObject();
118     if (!result) {
119         ThrowMsg(NullPointerException, "Private object is null");
120     }
121     return result;
122 }
123
124 void JSCalendarEvent::setPrivateObject(const CalendarEventPtr &event,
125         JSContextRef ctx,
126         const JSObjectRef object)
127 {
128     LogDebug("entered");
129     Try
130     {
131         CalendarEventPrivObject *priv =
132             static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
133         delete priv;
134         priv = new CalendarEventPrivObject(ctx, event);
135         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
136             delete priv;
137         }
138     }
139     Catch(Exception)
140     {
141         LogError("Error during replacing event object");
142     }
143 }
144
145 JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
146         JSObjectRef object,
147         JSStringRef propertyName,
148         JSValueRef* exception)
149 {
150     LogDebug("entered");
151     Try
152     {
153         CalendarConverterFactory::ConverterType converter =
154             CalendarConverterFactory::getConverter(context);
155         CalendarEventPtr event = getPrivateObject(object);
156
157         EventIdPtr eventId( new EventId() );
158         eventId->setUId(event->getUId());
159         eventId->setRecurrenceId(event->getRecurrenceId());
160         eventId->setTimeZone(event->getTimeZone());
161         return converter->toJSValueRef(eventId);
162     }
163     Catch(Exception)
164     {
165         LogWarning("trying to get incorrect value");
166     }
167     return JSValueMakeUndefined(context);
168 }
169
170 JSValueRef JSCalendarEvent::getPropertyLastModificationDate(JSContextRef context,
171         JSObjectRef object,
172         JSStringRef propertyName,
173         JSValueRef* exception)
174 {
175     LogDebug("entered");
176     Try
177     {
178         CalendarEventPrivObject *privateObject =
179             static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
180         CalendarEventPtr event = privateObject->getObject();
181
182         if (!event) {
183             Throw(NullPointerException);
184         }
185         if (event->getLastModifiedDate() != 0) {
186             // Use the global context saved in the event struct.
187             return JSTZDate::createJSObject(privateObject->getContext(), event->getLastModifiedDate(), event->getTimeZone());
188         } else {
189             return JSValueMakeUndefined(context);
190         }
191     }
192     Catch(Exception)
193     {
194         LogWarning("trying to get incorrect value");
195     }
196     return JSValueMakeUndefined(context);
197 }
198
199 JSValueRef JSCalendarEvent::getPropertyIsDetached(JSContextRef context,
200         JSObjectRef object,
201         JSStringRef propertyName,
202         JSValueRef* exception)
203 {
204     LogDebug("entered");
205     Try
206     {
207         CalendarEventPtr event = getPrivateObject(object);
208         if(CalendarEvent::EVENT_TYPE != event->getCalendarType()) {
209             return JSValueMakeUndefined(context);
210         }
211
212         Converter converter(context);
213         return converter.toJSValueRef(event->getIsDetached());
214     }
215     Catch(Exception)
216     {
217         LogWarning("trying to get incorrect value");
218     }
219     return JSValueMakeUndefined(context);
220 }
221
222 bool JSCalendarEvent::validate(JSContextRef ctx,
223         const JSObjectRef object,
224         JSValueRef* exception)
225 {
226     LogDebug("entered");
227     CalendarEventPrivObject *priv =
228         static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
229     if (priv == NULL) {
230         return false;
231     }
232     CalendarEventPtr event = priv->getObject();
233     if (!event) {
234         return false;
235     }
236     return event->validate();
237 }
238
239 }
240 }
241 }