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