Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Geocoder / JSAddress.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
19
20
21 /*
22  * @file          JSAcceleration.cpp
23  * @author
24  * @version     0.1
25  * @brief
26  */
27
28 #include <cassert>
29 #include <memory>
30 #include <dpl/log.h>
31 #include <CommonsJavaScript/JSUtils.h>
32 #include <CommonsJavaScript/Converter.h>
33 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
34 #include "JSAddress.h"
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38
39 using namespace WrtDeviceApis;
40 using namespace Api::Geocoder;
41
42 //namespace {
43 #define GEOCODER_COUNTRY                                                 "country"
44 #define GEOCODER_REGION                                                  "region"
45 #define GEOCODER_COUNTY                                                  "county"
46 #define GEOCODER_CITY                                                   "city"
47 #define GEOCODER_STREET                                                  "street"
48 #define GEOCODER_STREET_NUMBER                           "streetNumber"
49 #define GEOCODER_PREMISES                                        "premises"
50 #define GEOCODER_ADDITIONAL_INFORMATION          "additionalInformation"
51 #define GEOCODER_POSTAL_CODE                             "postalCode"
52 //} //private namespace
53
54 JSClassDefinition JSAddress::m_classInfo = {
55                 0,
56                 kJSClassAttributeNone,
57                 "address",
58                 0,
59                 m_property,
60                 0,
61                 initialize,
62                 finalize,
63                 NULL, //HasProperty,
64                 getProperty,
65                 NULL, //SetProperty,
66                 NULL, //DeleteProperty,
67                 NULL, //GetPropertyNames,
68                 NULL, //CallAsFunction,
69                 NULL, //CallAsConstructor,
70                 hasInstance, NULL, //ConvertToType
71                 };
72
73 JSStaticValue JSAddress::m_property[] = {
74                 { GEOCODER_COUNTRY,                                             getProperty, NULL, kJSPropertyAttributeReadOnly },
75                 { GEOCODER_REGION,                                              getProperty, NULL, kJSPropertyAttributeReadOnly },
76                 { GEOCODER_COUNTY,                                              getProperty, NULL, kJSPropertyAttributeReadOnly },
77                 { GEOCODER_CITY,                                                        getProperty, NULL, kJSPropertyAttributeReadOnly },
78                 { GEOCODER_STREET,                                              getProperty, NULL, kJSPropertyAttributeReadOnly },
79                 { GEOCODER_STREET_NUMBER,                               getProperty, NULL, kJSPropertyAttributeReadOnly },
80                 { GEOCODER_PREMISES,                                    getProperty, NULL, kJSPropertyAttributeReadOnly },
81                 { GEOCODER_ADDITIONAL_INFORMATION,      getProperty, NULL, kJSPropertyAttributeReadOnly },
82                 { GEOCODER_POSTAL_CODE,                                 getProperty, NULL, kJSPropertyAttributeReadOnly },
83                 { 0, 0, 0, 0 }
84 };
85
86 const JSClassRef JSAddress::getClassRef() {
87         if (!m_jsClassRef) {
88                 m_jsClassRef = JSClassCreate(&m_classInfo);
89         }
90         return m_jsClassRef;
91 }
92
93 const JSClassDefinition* JSAddress::getClassInfo() {
94         return &m_classInfo;
95 }
96
97 JSClassRef JSAddress::m_jsClassRef = JSClassCreate(JSAddress::getClassInfo());
98
99 JSObjectRef JSAddress::createJSObject(JSContextRef context, const GeocoderProperties &geocoderProperties) {
100         LogDebug("<<< context:" << context);
101
102         std::auto_ptr<GeocoderProperties> accProps(new GeocoderProperties(geocoderProperties));
103         JSAddressPriv *priv = new JSAddressPriv(context, accProps.get());
104         accProps.release();
105         if (!priv) {
106                 ThrowMsg(Commons::NullPointerException, "Can not new an object");
107         }
108
109         LogDebug(">>>");
110         return JSObjectMake(context, getClassRef(), priv);
111 }
112
113 void JSAddress::initialize(JSContextRef context, JSObjectRef object) {
114         LogDebug("Entered. Nothing to do.");
115 }
116
117 void JSAddress::finalize(JSObjectRef object) {
118         LogDebug("Entered");
119         JSAddressPriv* priv = static_cast<JSAddressPriv*> (JSObjectGetPrivate(object));
120         JSObjectSetPrivate(object, NULL);
121         LogDebug("Deleting acceleration object");
122         delete priv;
123 }
124
125 JSValueRef JSAddress::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) {
126         LogDebug("<<< object" << object);
127
128         JSAddressPriv *priv = static_cast<JSAddressPriv*> (JSObjectGetPrivate(object));
129         assert(priv && "Private object not set.");
130
131         Try {
132                 GeocoderProperties *geocoderProperties = priv->getObject();
133                 WrtDeviceApis::CommonsJavaScript::Converter convert(context);
134
135
136                 //TODO Add checking NULL String     2011.09.15
137                 JSValueRef retValue;
138                 if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_ADDITIONAL_INFORMATION)) {
139                         LogDebug("propertyName: " << GEOCODER_ADDITIONAL_INFORMATION);
140                         LogDebug("geocoderProperties->strAdditionalInformation:[" << geocoderProperties->strAdditionalInformation << "]");
141                         retValue = convert.toJSValueRef(geocoderProperties->strAdditionalInformation);
142                 } else if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_CITY)) {
143                         LogDebug("propertyName: " << GEOCODER_CITY);
144                         LogDebug("geocoderProperties->strCity:[" << geocoderProperties->strCity << "]");
145                         retValue = convert.toJSValueRef(geocoderProperties->strCity);
146                 } else if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_COUNTRY)) {
147                         LogDebug("propertyName: " << GEOCODER_COUNTRY);
148                         LogDebug("geocoderProperties->strCountryCode:[" << geocoderProperties->strCountry <<"]");
149                         retValue = convert.toJSValueRef(geocoderProperties->strCountry);
150                 } else if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_COUNTY)) {
151                         LogDebug("propertyName: " << GEOCODER_COUNTY);
152                         LogDebug("geocoderProperties->strCounty:[" << geocoderProperties->strCounty << "]");
153                         retValue = convert.toJSValueRef(geocoderProperties->strCounty);
154                 } else if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_POSTAL_CODE)) {
155                         LogDebug("propertyName: " << GEOCODER_POSTAL_CODE);
156                         LogDebug("geocoderProperties->strPostalCode:[" << geocoderProperties->strPostalCode << "]");
157                         retValue = convert.toJSValueRef(geocoderProperties->strPostalCode);
158                 } else if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_PREMISES)) {
159                         LogDebug("propertyName: " << GEOCODER_PREMISES);
160                         LogDebug("geocoderProperties->strBuildingNumber:[" << geocoderProperties->strPremises << "]");
161                         retValue = convert.toJSValueRef(geocoderProperties->strPremises);
162                 } else if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_REGION)) {
163                         LogDebug("propertyName: " << GEOCODER_REGION);
164                         LogDebug("geocoderProperties->strState:[" << geocoderProperties->strRegion << "]");
165                         retValue = convert.toJSValueRef(geocoderProperties->strRegion);
166                 } else if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_STREET)) {
167                         LogDebug("propertyName: " << GEOCODER_STREET);
168                         LogDebug("geocoderProperties->strStreet:[" << geocoderProperties->strStreet << "]");
169                         retValue = convert.toJSValueRef(geocoderProperties->strStreet);
170                 } else if (JSStringIsEqualToUTF8CString(propertyName, GEOCODER_STREET_NUMBER)) {
171                         LogDebug("propertyName: " << GEOCODER_STREET_NUMBER);
172                         LogDebug("geocoderProperties->strStreetNumber:[" << geocoderProperties->strStreetNumber <<"]");
173                         retValue = convert.toJSValueRef(geocoderProperties->strStreetNumber);
174                 } else {
175                         LogDebug(">>> make undefined");
176                         return JSValueMakeUndefined(context);
177                 }
178
179                 LogDebug(">>>");
180                 return retValue;
181         }Catch (Commons::Exception){
182                 LogError("Exception: " << _rethrown_exception.GetMessage());
183         }
184
185         return NULL;
186 }
187
188 bool JSAddress::hasInstance(JSContextRef context, JSObjectRef constructor,
189         JSValueRef possibleInstance, JSValueRef* exception) {
190 return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
191 }
192
193
194 } //WrtPlugins
195