Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Calendar / JSCalendarItemGeo.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 "JSCalendarItemGeo.h"
19 #include <dpl/log/log.h>
20 #include <Tizen/Common/JSTizenExceptionFactory.h>
21 #include <Tizen/Common/JSTizenException.h>
22 #include <CommonsJavaScript/Converter.h>
23
24 using namespace TizenApis::Api::Calendar;
25 using namespace WrtDeviceApis::Commons;
26 using namespace WrtDeviceApis::CommonsJavaScript;
27
28 namespace TizenApis {
29 namespace Tizen1_0 {
30 namespace Calendar {
31
32 #define TIZEN_CALENDAR_ITEM_GEO_ATTRIBUTENAME "CalendarItemGeo"
33
34 JSClassDefinition JSCalendarItemGeo::m_classInfo = {
35     0,
36     kJSClassAttributeNone,
37     TIZEN_CALENDAR_ITEM_GEO_ATTRIBUTENAME,
38     0,
39     m_property,
40     NULL, //m_function,
41     initialize,
42     finalize,
43     NULL, //hasProperty,
44     NULL, //getProperty,
45     NULL, //setProperty,
46     NULL, //deleteProperty,
47     NULL, //getPropertyNames,
48     NULL, //callAsFunction,
49     NULL, //callAsConstructor,
50     NULL, //hasInstance,
51     NULL, //convertToType,
52 };
53
54 JSStaticValue JSCalendarItemGeo::m_property[] = {
55     { TIZEN_CALENDAR_ITEM_GEO_LATITUDE, getProperty, setProperty, kJSPropertyAttributeNone },
56     { TIZEN_CALENDAR_ITEM_GEO_LONGITUDE, getProperty, setProperty, kJSPropertyAttributeNone },
57     { 0, 0, 0, 0 }
58 };
59
60 JSClassRef JSCalendarItemGeo::m_jsClassRef = JSClassCreate(
61         JSCalendarItemGeo::getClassInfo());
62
63 const JSClassDefinition* JSCalendarItemGeo::getClassInfo()
64 {
65     return &(m_classInfo);
66 }
67
68 JSClassRef JSCalendarItemGeo::getClassRef()
69 {
70     if (!m_jsClassRef) {
71         m_jsClassRef = JSClassCreate(&m_classInfo);
72     }
73     return m_jsClassRef;
74 }
75
76 CalendarItemGeoPtr JSCalendarItemGeo::getPrivateObject(JSObjectRef object)
77 {
78     LogDebug("entered");
79     CalendarItemGeoPrivateObject *priv =
80         static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
81     if (!priv) {
82         ThrowMsg(NullPointerException, "Private object is null");
83     }
84     CalendarItemGeoPtr result = priv->getObject();
85     if (!result) {
86         ThrowMsg(NullPointerException, "Private object is null");
87     }
88     return result;
89 }
90
91 void JSCalendarItemGeo::initialize(JSContextRef context,
92         JSObjectRef object)
93 {
94     LogDebug("enter");
95 }
96
97 void JSCalendarItemGeo::finalize(JSObjectRef object)
98 {
99     LogDebug("enter");
100     CalendarItemGeoPrivateObject* priv =
101         static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
102     delete priv;
103     JSObjectSetPrivate(object, NULL);
104 }
105
106 JSObjectRef JSCalendarItemGeo::createJSCalendarItemGeo(JSContextRef context, CalendarItemGeoPtr geo)
107 {
108     CalendarItemGeoPrivateObject *priv = new CalendarItemGeoPrivateObject(context, geo);
109     return JSObjectMake(context, getClassRef(), priv);
110 }
111
112 JSValueRef JSCalendarItemGeo::getProperty(JSContextRef context,
113         JSObjectRef object,
114         JSStringRef propertyName,
115         JSValueRef* exception)
116 {
117     LogDebug("enter");
118     Converter converter(context);
119     Try
120     {
121         CalendarItemGeoPrivateObject* priv =
122             static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
123         if (!priv) {
124             Throw(NullPointerException);
125         }
126         CalendarItemGeoPtr geoInfo = priv->getObject();
127
128         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LATITUDE)) {
129             double latitude = geoInfo->getLatitude();
130             if( latitude==CalendarItemGeo::GEO_UNDEFINED ) {
131                 return JSValueMakeUndefined(context);
132             } else {
133                 return converter.toJSValueRef(geoInfo->getLatitude());
134             }
135         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LONGITUDE)) {
136             double longitude = geoInfo->getLongitude();
137             if( longitude==CalendarItemGeo::GEO_UNDEFINED ) {
138                 return JSValueMakeUndefined(context);
139             } else {
140                 return converter.toJSValueRef(geoInfo->getLongitude());
141             }
142         }
143     }
144     Catch(Exception)
145     {
146         LogError("invalid property");
147     }
148
149     return JSValueMakeUndefined(context);
150 }
151
152 bool JSCalendarItemGeo::setProperty(JSContextRef context,
153         JSObjectRef object,
154         JSStringRef propertyName,
155         JSValueRef value,
156         JSValueRef* exception)
157 {
158     LogDebug("entered");
159     Converter converter(context);
160     Try
161     {
162         CalendarItemGeoPrivateObject* priv =
163             static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
164         if (!priv) {
165             Throw(NullPointerException);
166         }
167         CalendarItemGeoPtr geoInfo = priv->getObject();
168
169         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LATITUDE)) {
170             if (!JSValueIsNumber(context, value)) {
171                 Throw(InvalidArgumentException);
172             }
173             double latitude = converter.toDouble(value);
174             if( latitude>CalendarItemGeo::GEO_LATITUDE_MAX ) {
175                 geoInfo->setLatitude(CalendarItemGeo::GEO_LATITUDE_MAX);
176             } else if( latitude<CalendarItemGeo::GEO_LATITUDE_MIN ) {
177                 geoInfo->setLatitude(CalendarItemGeo::GEO_LATITUDE_MIN);
178             } else {
179                 geoInfo->setLatitude(latitude);
180             }
181             return true;
182         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LONGITUDE)) {
183             if (!JSValueIsNumber(context, value)) {
184                 Throw(InvalidArgumentException);
185             }
186             double longitude = converter.toDouble(value);
187             if( longitude>CalendarItemGeo::GEO_LONGITUDE_MAX ) {
188                 geoInfo->setLongitude(CalendarItemGeo::GEO_LONGITUDE_MAX);
189             } else if( longitude<CalendarItemGeo::GEO_LONGITUDE_MIN ) {
190                 geoInfo->setLongitude(CalendarItemGeo::GEO_LONGITUDE_MIN);
191             } else {
192                 geoInfo->setLongitude(longitude);
193             }
194             return true;
195         }
196     }
197     Catch(Exception)
198     {
199         LogWarning("trying to set incorrect value");
200     }
201
202     TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
203     return false;
204 }
205
206 }
207 }
208 }