Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Contact / JSContactWebSite.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  * @file        JSContactWebSite.cpp
19  * @author      Kisub Song (kisubs.song@samsung.com)
20  * @version     0.1
21  * @brief       Implementation of the JSContactWebSite class
22  */
23
24 #include <dpl/shared_ptr.h>
25 #include <CommonsJavaScript/Converter.h>
26 #include <Tizen/Common/JSTizenExceptionFactory.h>
27 #include <Tizen/Common/JSTizenException.h>
28 #include "ContactConverter.h"
29 #include "JSContactWebSite.h"
30
31 #define CONTACT_CLASS_NAME "ContactWebSite"
32
33 #define CONTACT_ATTR_URL  "url"
34 #define CONTACT_ATTR_TYPE "type"
35
36 namespace TizenApis {
37 namespace Tizen1_0 {
38 namespace Contact {
39
40 using namespace TizenApis::Commons;
41 using namespace TizenApis::Api::Contact;
42
43 JSClassRef JSContactWebSite::m_classRef = NULL;
44
45 JSClassDefinition JSContactWebSite::m_classInfo =
46 {
47         0,
48         kJSClassAttributeNone,
49         CONTACT_CLASS_NAME,
50         NULL,
51         m_property,
52         m_functions,
53         Initialize,
54         Finalize,
55         NULL, //hasProperty,
56         NULL, //GetProperty,
57         NULL, //SetProperty,
58         NULL, //DeleteProperty,
59         NULL, //getPropertyNames,
60         NULL,
61         NULL,
62         NULL,
63         NULL, //ConvertToType,
64 };
65
66 JSStaticValue JSContactWebSite::m_property[] = {
67         { CONTACT_ATTR_URL, getUrl, setUrl, kJSPropertyAttributeNone },
68         { CONTACT_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
69         { 0, 0, 0, 0 }
70 };
71
72 JSStaticFunction JSContactWebSite::m_functions[] =
73 {
74         { 0, 0, 0 }
75 };
76
77 JSClassRef JSContactWebSite::getClassRef() {
78         if (!m_classRef) {
79                 m_classRef = JSClassCreate(&m_classInfo);
80         }
81         return m_classRef;
82 }
83
84 JSValueRef JSContactWebSite::createJSObject(JSContextRef context,
85             const std::string url,
86             const ContactWebSiteType type)
87 {
88         ContactWebSitePtr privateData = ContactWebSitePtr(new ContactWebSite());
89         if(url != "")
90         {
91                 privateData->setUrl(url);
92                 privateData->setType(type);
93         }
94         JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, privateData);
95         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
96         if (NULL == jsValueRef) {
97                 LogError("object creation error");
98                 return JSValueMakeUndefined(context);
99         }
100         return jsValueRef;
101 }
102
103 bool JSContactWebSite::isObjectOfClass(JSContextRef context, JSValueRef value)
104 {
105         return JSValueIsObjectOfClass(context, value, getClassRef());
106 }
107
108 ContactWebSitePtr JSContactWebSite::getContactWebSite(JSContextRef context, JSValueRef value)
109 {
110         if (!isObjectOfClass(context, value)) {
111                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
112         }
113         JSObjectRef object = JSValueToObject(context, value, NULL);
114         if (!object) {
115                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
116         }
117         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
118         if (!priv) {
119                 Throw(WrtDeviceApis::Commons::NullPointerException);
120         }
121         return priv->getObject();
122 }
123
124 void JSContactWebSite::Initialize(JSContextRef context, JSObjectRef object)
125 {
126         assert(NULL != JSObjectGetPrivate(object));
127 }
128
129 void JSContactWebSite::Finalize(JSObjectRef object)
130 {
131         //delete (JSObjectGetPrivate(object));
132 }
133
134 ContactWebSitePtr JSContactWebSite::getPrivData(JSObjectRef object)
135 {
136         //LogDebug("entered");
137         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
138         if (!priv) {
139                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
140         }
141         ContactWebSitePtr result = priv->getObject();
142         if (!result) {
143                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
144         }
145         return result;
146 }
147
148 JSValueRef JSContactWebSite::getUrl(JSContextRef context,
149                 JSObjectRef object,
150                 JSStringRef propertyName,
151                 JSValueRef* exception)
152 {
153         //LogDebug("entered");
154         Try
155         {
156                 ContactConverterFactory::ConverterType converter =
157                                 ContactConverterFactory::getConverter(context);
158                 ContactWebSitePtr webSite = getPrivData(object);
159                 return converter->toJSValueRef(webSite->getUrl());
160         }
161         Catch(WrtDeviceApis::Commons::Exception)
162         {
163                 LogWarning("trying to get incorrect value");
164         }
165         return JSValueMakeUndefined(context);
166 }
167
168 bool JSContactWebSite::setUrl(JSContextRef context,
169                 JSObjectRef object,
170                 JSStringRef propertyName,
171                 JSValueRef value,
172                 JSValueRef* exception)
173 {
174         Try
175         {
176                 ContactWebSitePtr webSite = getPrivData(object);
177                 ContactConverterFactory::ConverterType converter =
178                                 ContactConverterFactory::getConverter(context);
179                 webSite->setUrl(converter->toString(value));
180                 return true;
181         }
182         Catch(WrtDeviceApis::Commons::Exception)
183         {
184                 LogWarning("trying to set incorrect value");
185         }
186         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
187         return false;
188 }
189
190
191 JSValueRef JSContactWebSite::getType(JSContextRef context,
192                 JSObjectRef object,
193                 JSStringRef propertyName,
194                 JSValueRef* exception)
195 {
196         //LogDebug("entered");
197         Try
198         {
199                 ContactConverterFactory::ConverterType converter =
200                                 ContactConverterFactory::getConverter(context);
201                 ContactWebSitePtr webSite = getPrivData(object);
202                 std::string ret = converter->toContactWebSiteTypeStr(webSite->getType());
203                 return converter->toJSValueRef(ret);
204         }
205         Catch(WrtDeviceApis::Commons::Exception)
206         {
207                 LogWarning("trying to get incorrect value");
208         }
209         return JSValueMakeUndefined(context);
210 }
211
212 bool JSContactWebSite::setType(JSContextRef context,
213                 JSObjectRef object,
214                 JSStringRef propertyName,
215                 JSValueRef value,
216                 JSValueRef* exception)
217 {
218         Try
219         {
220                 ContactWebSitePtr webSite = getPrivData(object);
221                 ContactConverterFactory::ConverterType converter =
222                                 ContactConverterFactory::getConverter(context);
223                 ContactWebSiteType type = converter->toContactWebSiteType(converter->toString(value));
224                 webSite->setType(type);
225                 return true;
226         }
227         Catch(WrtDeviceApis::Commons::Exception)
228         {
229                 LogWarning("trying to set incorrect value");
230         }
231         JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type mismatch");
232         return false;
233 }
234
235 } // Contact
236 } // Tizen1_0
237 } // TizenApis