a87ea6d05bb3e822ec4165c608a0f5ebf692bc00
[framework/web/wrt-plugins-tizen.git] / src / Contact / JSContactWebSite.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        JSContactWebSite.cpp
20  * @author      Kisub Song (kisubs.song@samsung.com)
21  * @version     0.1
22  * @brief       Implementation of the JSContactWebSite class
23  */
24
25 #include <dpl/shared_ptr.h>
26 #include <CommonsJavaScript/Validator.h>
27 #include <JSWebAPIErrorFactory.h>
28 #include "ContactConverter.h"
29 #include "JSContactWebSite.h"
30 #include <Logger.h>
31 #include <Export.h>
32
33 #define CONTACT_CLASS_NAME "ContactWebSite"
34
35 #define CONTACT_ATTR_URL  "url"
36 #define CONTACT_ATTR_TYPE "type"
37
38 namespace DeviceAPI {
39 namespace Contact {
40
41 using namespace DeviceAPI::Common;
42 using namespace WrtDeviceApis::Commons;
43 using namespace WrtDeviceApis::CommonsJavaScript;
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, //CallAsFunction,
61         constructor, //CallAsConstructor,
62         hasInstance, //HasInstance,
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::m_classRef = JSClassCreate(&m_classInfo);
78
79 JSClassRef DLL_EXPORT JSContactWebSite::getClassRef() {
80         if (!m_classRef) {
81                 m_classRef = JSClassCreate(&m_classInfo);
82         }
83         return m_classRef;
84 }
85
86 bool JSContactWebSite::isObjectOfClass(JSContextRef context, JSValueRef value)
87 {
88         return JSValueIsObjectOfClass(context, value, getClassRef());
89 }
90
91 ContactWebSitePtr JSContactWebSite::getContactWebSite(JSContextRef context, JSValueRef value)
92 {
93         if (!isObjectOfClass(context, value)) {
94                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
95         }
96         JSObjectRef object = JSValueToObject(context, value, NULL);
97         if (!object) {
98                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
99         }
100         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
101         if (!priv) {
102                 Throw(WrtDeviceApis::Commons::NullPointerException);
103         }
104         return priv->getObject();
105 }
106
107 void JSContactWebSite::Initialize(JSContextRef context, JSObjectRef object)
108 {
109         if (!JSObjectGetPrivate(object))
110         {
111                 ContactWebSitePtr webSite(new ContactWebSite());
112                 JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, ContactWebSitePtr(webSite));
113                 if (!JSObjectSetPrivate(object, priv)) {
114                         delete priv;
115                 }
116         }
117 }
118
119 void JSContactWebSite::Finalize(JSObjectRef object)
120 {
121         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
122
123         if (priv != NULL)
124                 delete (priv);
125 }
126
127 JSObjectRef JSContactWebSite::createJSObject(JSContextRef context, ContactWebSitePtr contactWebSite)
128 {
129         JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, contactWebSite);
130         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
131         if (NULL == jsObjectRef) {
132                 LoggerE("object creation error");
133                 return NULL;
134         }
135         return jsObjectRef;
136 }
137
138 ContactWebSitePtr JSContactWebSite::getPrivData(JSObjectRef object)
139 {
140         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
141         if (!priv) {
142                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
143         }
144         ContactWebSitePtr result = priv->getObject();
145         if (!result) {
146                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
147         }
148         return result;
149 }
150
151 JSObjectRef JSContactWebSite::constructor(JSContextRef context,
152                 JSObjectRef constructor,
153                 size_t argumentCount,
154                 const JSValueRef arguments[],
155                 JSValueRef* exception)
156 {
157         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(constructor));
158         if (!priv) {
159                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
160         }
161         JSContextRef gContext = priv->getContext();
162
163         std::string url;
164         std::string stringType;
165         ContactWebSiteType type = WEBSITE_TYPE_HOMEPAGE;
166         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
167
168         try {
169                 ArgumentValidator Argvalidator(context, argumentCount, arguments);
170                 url = Argvalidator.toString(0, false);
171
172                 if (argumentCount >= 2){
173                         stringType = Argvalidator.toString(1, true);
174                         type = converter->toContactWebSiteType(stringType);
175                 }
176
177         } catch (const TypeMismatchException& err ) {
178                 JSWebAPIErrorFactory::postException(context, exception, err);
179                 return NULL;
180         } catch(const BasePlatformException& err) {
181                 JSWebAPIErrorFactory::postException(context, exception, err);
182                 return NULL;
183         } catch(const ConversionException& err) {
184                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
185                 return NULL;
186         } catch(const NullPointerException& err) {
187                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
188                 return NULL;
189         }
190
191         ContactWebSitePtr contactWebSite(new ContactWebSite());
192         contactWebSite->setUrl(url);
193         contactWebSite->setType(type);
194
195         JSObjectRef jsobject;
196
197         Try {
198                 jsobject = createJSObject(gContext, contactWebSite);
199         } Catch(Exception) {
200                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
201                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
202                 return NULL;
203         }
204
205         return jsobject;
206 }
207
208 bool JSContactWebSite::hasInstance(JSContextRef context,
209                 JSObjectRef constructor,
210                 JSValueRef possibleInstance,
211                 JSValueRef* exception)
212 {
213         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
214 }
215
216 JSValueRef JSContactWebSite::getUrl(JSContextRef context,
217                 JSObjectRef object,
218                 JSStringRef propertyName,
219                 JSValueRef* exception)
220 {
221         Try
222         {
223                 ContactConverterFactory::ConverterType converter =
224                                 ContactConverterFactory::getConverter(context);
225                 ContactWebSitePtr webSite = getPrivData(object);
226                 return converter->toJSValueRef(webSite->getUrl());
227         }
228         Catch(WrtDeviceApis::Commons::Exception)
229         {
230                 LoggerW("trying to get incorrect value");
231         }
232         return JSValueMakeUndefined(context);
233 }
234
235 bool JSContactWebSite::setUrl(JSContextRef context,
236                 JSObjectRef object,
237                 JSStringRef propertyName,
238                 JSValueRef value,
239                 JSValueRef* exception)
240 {
241         Try
242         {
243                 ContactWebSitePtr webSite = getPrivData(object);
244                 ContactConverterFactory::ConverterType converter =
245                                 ContactConverterFactory::getConverter(context);
246                 webSite->setUrl(converter->toString(value));
247         }
248         Catch(WrtDeviceApis::Commons::Exception)
249         {
250                 LoggerW("trying to set incorrect value");
251                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
252         }
253         return true;
254 }
255
256
257 JSValueRef JSContactWebSite::getType(JSContextRef context,
258                 JSObjectRef object,
259                 JSStringRef propertyName,
260                 JSValueRef* exception)
261 {
262         Try
263         {
264                 ContactConverterFactory::ConverterType converter =
265                                 ContactConverterFactory::getConverter(context);
266                 ContactWebSitePtr webSite = getPrivData(object);
267                 std::string ret = converter->toContactWebSiteTypeStr(webSite->getType());
268                 if(!webSite->getTypeIsSet())
269                         return JSValueMakeNull(context);
270                 else
271                         return converter->toJSValueRef(ret);
272         }
273         Catch(WrtDeviceApis::Commons::Exception)
274         {
275                 LoggerW("trying to get incorrect value");
276         }
277         return JSValueMakeUndefined(context);
278 }
279
280 bool JSContactWebSite::setType(JSContextRef context,
281                 JSObjectRef object,
282                 JSStringRef propertyName,
283                 JSValueRef value,
284                 JSValueRef* exception)
285 {
286         Try
287         {
288                 ContactWebSitePtr webSite = getPrivData(object);
289                 ContactConverterFactory::ConverterType converter =
290                                 ContactConverterFactory::getConverter(context);
291                 BasicValidator validator =
292                                 BasicValidatorFactory::getValidator(context, exception);
293                 if(validator->isNullOrUndefined(value))
294                         webSite->unsetType();
295                 else
296                 {
297                         ContactWebSiteType type = converter->toContactWebSiteType(converter->toString(value));
298                         webSite->setType(type);
299                 }
300         }
301         Catch(WrtDeviceApis::Commons::Exception)
302         {
303                 LoggerW("trying to set incorrect value");
304                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
305         }
306         return true;
307 }
308
309 } // Contact
310 } // DeviceAPI