Git Init
[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.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 #define TIZEN_CALENDAR_EVENT_ID "id"
43 #define TIZEN_CALENDAR_EVENT_LASTMODIFIEDDATE "lastModificationDate"
44 #define TIZEN_CALENDAR_EVENT_IS_DETACHED "isDetached"
45
46 JSClassDefinition JSCalendarEvent::m_classInfo = {
47     0,
48     kJSClassAttributeNone,
49     TIZEN_CALENDAR_EVENT_ATTRIBUTENAME,
50     JSCalendarItemProperties::getClassRef(),
51     m_property,
52     NULL, //    m_function,
53     initialize,
54     finalize,
55     NULL, //hasProperty,
56     NULL, //getProperty,
57     NULL, //setProperty,
58     NULL, //DeleteProperty,
59     NULL, //GetPropertyNames,
60     NULL, //CallAsFunction,
61     NULL, //CallAsConstructor,
62     NULL, //HasInstance,
63     NULL  //ConvertToType
64 };
65
66 JSStaticValue JSCalendarEvent::m_property[] = {
67     { TIZEN_CALENDAR_EVENT_ID, getPropertyId,
68       NULL, kJSPropertyAttributeReadOnly },
69     { TIZEN_CALENDAR_EVENT_LASTMODIFIEDDATE, getPropertyLastModifiedDate,
70       NULL, kJSPropertyAttributeReadOnly },
71     { TIZEN_CALENDAR_EVENT_IS_DETACHED, getPropertyIsDetached,
72       NULL, kJSPropertyAttributeReadOnly },
73
74     { 0, 0, 0, 0 }
75 };
76
77 JSClassRef JSCalendarEvent::m_jsClassRef = JSClassCreate(JSCalendarEvent::getClassInfo());
78
79 void JSCalendarEvent::initialize(JSContextRef context,
80         JSObjectRef object)
81 {
82     LogDebug("entered");
83     CalendarEventPrivObject *priv =
84         static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
85     if (!priv) {
86         CalendarEventPtr privateData(new CalendarEvent());
87         priv = new CalendarEventPrivObject(context, privateData);
88         JSObjectSetPrivate(object, static_cast<void*>(priv));
89         LogDebug("new event is created");
90     } else {
91         LogDebug("private object already exists");
92     }
93 }
94
95 void JSCalendarEvent::finalize(JSObjectRef object)
96 {
97     LogDebug("entered");
98 }
99
100 const JSClassRef JSCalendarEvent::getClassRef()
101 {
102     if (!m_jsClassRef) {
103         m_jsClassRef = JSClassCreate(&m_classInfo);
104     }
105     return m_jsClassRef;
106 }
107
108 const JSClassDefinition* JSCalendarEvent::getClassInfo()
109 {
110     return &m_classInfo;
111 }
112
113 CalendarEventPtr JSCalendarEvent::getPrivateObject(JSObjectRef object)
114 {
115     LogDebug("entered");
116     CalendarEventPrivObject *priv =
117         static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
118     if (!priv) {
119         ThrowMsg(NullPointerException, "Private object is null");
120     }
121     CalendarEventPtr result = priv->getObject();
122     if (!result) {
123         ThrowMsg(NullPointerException, "Private object is null");
124     }
125     return result;
126 }
127
128 void JSCalendarEvent::setPrivateObject(const CalendarEventPtr &event,
129         JSContextRef ctx,
130         const JSObjectRef object)
131 {
132     LogDebug("entered");
133     Try
134     {
135         CalendarEventPrivObject *priv =
136             static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
137         delete priv;
138         priv = new CalendarEventPrivObject(ctx, event);
139         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
140             delete priv;
141         }
142     }
143     Catch(Exception)
144     {
145         LogError("Error during replacing event object");
146     }
147 }
148
149 JSValueRef JSCalendarEvent::getPropertyId(JSContextRef context,
150         JSObjectRef object,
151         JSStringRef propertyName,
152         JSValueRef* exception)
153 {
154     LogDebug("entered");
155     Try
156     {
157         CalendarConverterFactory::ConverterType converter =
158             CalendarConverterFactory::getConverter(context);
159         CalendarEventPtr event = getPrivateObject(object);
160
161         EventIdPtr eventId( new EventId() );
162         eventId->setUId(event->getUId());
163         eventId->setRecurrenceId(event->getRecurrenceId());
164         eventId->setTimeZone(event->getTimeZone());
165         return converter->toJSValueRef(eventId);
166     }
167     Catch(Exception)
168     {
169         LogWarning("trying to get incorrect value");
170     }
171     return JSValueMakeUndefined(context);
172 }
173
174 JSValueRef JSCalendarEvent::getPropertyLastModifiedDate(JSContextRef context,
175         JSObjectRef object,
176         JSStringRef propertyName,
177         JSValueRef* exception)
178 {
179     LogDebug("entered");
180     Try
181     {
182         CalendarEventPrivObject *privateObject =
183             static_cast<CalendarEventPrivObject*>(JSObjectGetPrivate(object));
184         CalendarEventPtr event = privateObject->getObject();
185
186         if (!event) {
187             Throw(NullPointerException);
188         }
189         if (event->getLastModifiedDate() != 0) {
190             // Use the global context saved in the event struct.
191             return JSTZDate::createJSObject(privateObject->getContext(), event->getLastModifiedDate(), event->getTimeZone());
192         } else {
193             return JSValueMakeUndefined(context);
194         }
195     }
196     Catch(Exception)
197     {
198         LogWarning("trying to get incorrect value");
199     }
200     return JSValueMakeUndefined(context);
201 }
202
203 JSValueRef JSCalendarEvent::getPropertyIsDetached(JSContextRef context,
204         JSObjectRef object,
205         JSStringRef propertyName,
206         JSValueRef* exception)
207 {
208     LogDebug("entered");
209     Try
210     {
211         Converter converter(context);
212         CalendarEventPtr event = getPrivateObject(object);
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 }