29f5974bd5493b7ea15e5379dfdd2022b964e1b2
[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/JSTizenExceptionFactory.h>
24 #include <API/Calendar/EventId.h>
25 #include "JSCalendarItem.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_ITEM_ATTRIBUTENAME "CalendarItem"
41
42 JSClassDefinition JSCalendarItem::m_classInfo = {
43     0,
44     kJSClassAttributeNone,
45     TIZEN_CALENDAR_ITEM_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 JSCalendarItem::m_property[] = {
63     //EventProperties
64     { TIZEN_CALENDAR_ITEM_ID, getPropertyId,
65       NULL, kJSPropertyAttributeReadOnly },
66     { TIZEN_CALENDAR_ITEM_LAST_MODIFICATION_DATE, getPropertyLastModificationDate,
67       NULL, kJSPropertyAttributeReadOnly },
68
69     { 0, 0, 0, 0 }
70 };
71
72 JSClassRef JSCalendarItem::m_jsClassRef = JSClassCreate(JSCalendarItem::getClassInfo());
73
74 void JSCalendarItem::initialize(JSContextRef context,
75         JSObjectRef object)
76 {
77     LogDebug("entered");
78     CalendarItemPrivObject *priv =
79         static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
80     if (!priv) {
81         CalendarEventPtr privateData(new CalendarEvent());
82         priv = new CalendarItemPrivObject(context, privateData);
83         JSObjectSetPrivate(object, static_cast<void*>(priv));
84         LogDebug("new event is created");
85     } else {
86         LogDebug("private object already exists");
87     }
88 }
89
90 void JSCalendarItem::finalize(JSObjectRef object)
91 {
92     LogDebug("entered");
93 }
94
95 const JSClassRef JSCalendarItem::getClassRef()
96 {
97     if (!m_jsClassRef) {
98         m_jsClassRef = JSClassCreate(&m_classInfo);
99     }
100     return m_jsClassRef;
101 }
102
103 const JSClassDefinition* JSCalendarItem::getClassInfo()
104 {
105     return &m_classInfo;
106 }
107
108 CalendarEventPtr JSCalendarItem::getPrivateObject(JSObjectRef object)
109 {
110     LogDebug("entered");
111     CalendarItemPrivObject *priv =
112         static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
113     if (!priv) {
114         ThrowMsg(NullPointerException, "Private object is null");
115     }
116     CalendarEventPtr result = priv->getObject();
117     if (!result) {
118         ThrowMsg(NullPointerException, "Private object is null");
119     }
120     return result;
121 }
122
123 void JSCalendarItem::setPrivateObject(const CalendarEventPtr &event,
124         JSContextRef ctx,
125         const JSObjectRef object)
126 {
127     LogDebug("entered");
128     Try
129     {
130         CalendarItemPrivObject *priv =
131             static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
132         delete priv;
133         priv = new CalendarItemPrivObject(ctx, event);
134         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
135             delete priv;
136         }
137     }
138     Catch(Exception)
139     {
140         LogError("Error during replacing event object");
141     }
142 }
143
144 JSValueRef JSCalendarItem::getPropertyId(JSContextRef context,
145         JSObjectRef object,
146         JSStringRef propertyName,
147         JSValueRef* exception)
148 {
149     LogDebug("entered");
150     Try
151     {
152         CalendarConverterFactory::ConverterType converter =
153             CalendarConverterFactory::getConverter(context);
154         CalendarEventPtr event = getPrivateObject(object);
155
156         EventIdPtr eventId( new EventId() );
157         eventId->setUId(event->getUId());
158         LogDebug("event->getRecurrenceId() : " << event->getRecurrenceId());
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 JSCalendarItem::getPropertyLastModificationDate(JSContextRef context,
171         JSObjectRef object,
172         JSStringRef propertyName,
173         JSValueRef* exception)
174 {
175     LogDebug("entered");
176     Try
177     {
178         CalendarItemPrivObject *privateObject =
179             static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
180         CalendarEventPtr item = privateObject->getObject();
181
182         if (!item) {
183             Throw(NullPointerException);
184         }
185         if (item->getLastModifiedDate() != 0) {
186             // Use the global context saved in the event struct.
187             return JSTZDate::createJSObject(privateObject->getContext(), item->getLastModifiedDate(), item->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 bool JSCalendarItem::validate(JSContextRef ctx,
200         const JSObjectRef object,
201         JSValueRef* exception)
202 {
203     LogDebug("entered");
204     CalendarItemPrivObject *priv =
205         static_cast<CalendarItemPrivObject*>(JSObjectGetPrivate(object));
206     if (priv == NULL) {
207         return false;
208     }
209     CalendarEventPtr event = priv->getObject();
210     if (!event) {
211         return false;
212     }
213     return event->validate();
214 }
215
216 }
217 }
218 }