facaba62afad789a035d16d0f724459c5823aaf9
[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 JSValueRef JSCalendarItemGeo::getProperty(JSContextRef context,
107         JSObjectRef object,
108         JSStringRef propertyName,
109         JSValueRef* exception)
110 {
111     LogDebug("enter");
112     Converter converter(context);
113     Try
114     {
115         CalendarItemGeoPrivateObject* priv =
116             static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
117         if (!priv) {
118             Throw(NullPointerException);
119         }
120         CalendarItemGeoPtr geoInfo = priv->getObject();
121
122         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LATITUDE)) {
123             double latitude = geoInfo->getLatitude();
124             if( latitude==CalendarItemGeo::GEO_UNDEFINED ) {
125                 return JSValueMakeUndefined(context);
126             } else {
127                 return converter.toJSValueRef(geoInfo->getLatitude());
128             }
129         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LONGITUDE)) {
130             double longitude = geoInfo->getLongitude();
131             if( longitude==CalendarItemGeo::GEO_UNDEFINED ) {
132                 return JSValueMakeUndefined(context);
133             } else {
134                 return converter.toJSValueRef(geoInfo->getLongitude());
135             }
136         }
137     }
138     Catch(Exception)
139     {
140         LogError("invalid property");
141     }
142
143     return JSValueMakeUndefined(context);
144 }
145
146 bool JSCalendarItemGeo::setProperty(JSContextRef context,
147         JSObjectRef object,
148         JSStringRef propertyName,
149         JSValueRef value,
150         JSValueRef* exception)
151 {
152     LogDebug("entered");
153     Converter converter(context);
154     Try
155     {
156         CalendarItemGeoPrivateObject* priv =
157             static_cast<CalendarItemGeoPrivateObject*>(JSObjectGetPrivate(object));
158         if (!priv) {
159             Throw(NullPointerException);
160         }
161         CalendarItemGeoPtr geoInfo = priv->getObject();
162
163         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LATITUDE)) {
164             if (!JSValueIsNumber(context, value)) {
165                 Throw(InvalidArgumentException);
166             }
167             double latitude = converter.toDouble(value);
168             if( latitude>CalendarItemGeo::GEO_LATITUDE_MAX ) {
169                 geoInfo->setLatitude(CalendarItemGeo::GEO_LATITUDE_MAX);
170             } else if( latitude<CalendarItemGeo::GEO_LATITUDE_MIN ) {
171                 geoInfo->setLatitude(CalendarItemGeo::GEO_LATITUDE_MIN);
172             } else {
173                 geoInfo->setLatitude(latitude);
174             }
175             return true;
176         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_CALENDAR_ITEM_GEO_LONGITUDE)) {
177             if (!JSValueIsNumber(context, value)) {
178                 Throw(InvalidArgumentException);
179             }
180             double longitude = converter.toDouble(value);
181             if( longitude>CalendarItemGeo::GEO_LONGITUDE_MAX ) {
182                 geoInfo->setLongitude(CalendarItemGeo::GEO_LONGITUDE_MAX);
183             } else if( longitude<CalendarItemGeo::GEO_LONGITUDE_MIN ) {
184                 geoInfo->setLongitude(CalendarItemGeo::GEO_LONGITUDE_MIN);
185             } else {
186                 geoInfo->setLongitude(longitude);
187             }
188             return true;
189         }
190     }
191     Catch(Exception)
192     {
193         LogWarning("trying to set incorrect value");
194     }
195
196     TizenApis::Commons::JSTizenExceptionFactory::postException(context, exception, TizenApis::Commons::JSTizenException::TYPE_MISMATCH_ERROR);
197     return false;
198 }
199
200 }
201 }
202 }