2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
24 using namespace TizenApis::Api::Calendar;
25 using namespace WrtDeviceApis::Commons;
26 using namespace WrtDeviceApis::CommonsJavaScript;
32 #define TIZEN_CALENDAR_ITEM_GEO_ATTRIBUTENAME "CalendarItemGeo"
34 JSClassDefinition JSCalendarItemGeo::m_classInfo = {
36 kJSClassAttributeNone,
37 TIZEN_CALENDAR_ITEM_GEO_ATTRIBUTENAME,
46 NULL, //deleteProperty,
47 NULL, //getPropertyNames,
48 NULL, //callAsFunction,
49 NULL, //callAsConstructor,
51 NULL, //convertToType,
54 JSStaticValue JSCalendarItemGeo::m_property[] = {
55 { TIZEN_CALENDAR_ITEM_GEO_LATITUDE, getProperty, setProperty, kJSPropertyAttributeNone },
56 { TIZEN_CALENDAR_ITEM_GEO_LONGITUDE, getProperty, setProperty, kJSPropertyAttributeNone },
60 JSClassRef JSCalendarItemGeo::m_jsClassRef = JSClassCreate(
61 JSCalendarItemGeo::getClassInfo());
63 const JSClassDefinition* JSCalendarItemGeo::getClassInfo()
65 return &(m_classInfo);
68 JSClassRef JSCalendarItemGeo::getClassRef()
71 m_jsClassRef = JSClassCreate(&m_classInfo);
76 CalendarItemGeoPtr JSCalendarItemGeo::getPrivateObject(JSObjectRef object)
79 CalendarItemGeoPrivateObject *priv =
80 static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
82 ThrowMsg(NullPointerException, "Private object is null");
84 CalendarItemGeoPtr result = priv->getObject();
86 ThrowMsg(NullPointerException, "Private object is null");
91 void JSCalendarItemGeo::initialize(JSContextRef context,
97 void JSCalendarItemGeo::finalize(JSObjectRef object)
100 CalendarItemGeoPrivateObject* priv =
101 static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
103 JSObjectSetPrivate(object, NULL);
106 JSObjectRef JSCalendarItemGeo::createJSCalendarItemGeo(JSContextRef context, CalendarItemGeoPtr geo)
108 CalendarItemGeoPrivateObject *priv = new CalendarItemGeoPrivateObject(context, geo);
109 return JSObjectMake(context, getClassRef(), priv);
112 JSValueRef JSCalendarItemGeo::getProperty(JSContextRef context,
114 JSStringRef propertyName,
115 JSValueRef* exception)
118 Converter converter(context);
121 CalendarItemGeoPrivateObject* priv =
122 static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
124 Throw(NullPointerException);
126 CalendarItemGeoPtr geoInfo = priv->getObject();
128 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LATITUDE)) {
129 double latitude = geoInfo->getLatitude();
130 if( latitude==CalendarItemGeo::GEO_UNDEFINED ) {
131 return JSValueMakeUndefined(context);
133 return converter.toJSValueRef(geoInfo->getLatitude());
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);
140 return converter.toJSValueRef(geoInfo->getLongitude());
146 LogError("invalid property");
149 return JSValueMakeUndefined(context);
152 bool JSCalendarItemGeo::setProperty(JSContextRef context,
154 JSStringRef propertyName,
156 JSValueRef* exception)
159 Converter converter(context);
162 CalendarItemGeoPrivateObject* priv =
163 static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
165 Throw(NullPointerException);
167 CalendarItemGeoPtr geoInfo = priv->getObject();
169 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LATITUDE)) {
170 if (!JSValueIsNumber(context, value)) {
171 Throw(InvalidArgumentException);
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);
179 geoInfo->setLatitude(latitude);
182 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LONGITUDE)) {
183 if (!JSValueIsNumber(context, value)) {
184 Throw(InvalidArgumentException);
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);
192 geoInfo->setLongitude(longitude);
199 LogWarning("trying to set incorrect value");
202 TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);