Update change log and spec for wrt-plugins-tizen_0.4.49
[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 #define CONTACT_ATTR_LABEL "label"
38
39 namespace DeviceAPI {
40 namespace Contact {
41
42 using namespace DeviceAPI::Common;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45
46 JSClassDefinition JSContactWebSite::m_classInfo =
47 {
48         0,
49         kJSClassAttributeNone,
50         CONTACT_CLASS_NAME,
51         NULL,
52         m_property,
53         m_functions,
54         Initialize,
55         Finalize,
56         NULL, //hasProperty,
57         NULL, //GetProperty,
58         NULL, //SetProperty,
59         NULL, //DeleteProperty,
60         NULL, //getPropertyNames,
61         NULL, //CallAsFunction,
62         constructor, //CallAsConstructor,
63         hasInstance, //HasInstance,
64         NULL, //ConvertToType,
65 };
66
67 JSStaticValue JSContactWebSite::m_property[] = {
68         { CONTACT_ATTR_URL, getUrl, setUrl, kJSPropertyAttributeNone },
69         { CONTACT_ATTR_TYPE, getType, setType, kJSPropertyAttributeNone },
70         { CONTACT_ATTR_LABEL, getLabel, setLabel, kJSPropertyAttributeNone },
71         { 0, 0, 0, 0 }
72 };
73
74 JSStaticFunction JSContactWebSite::m_functions[] =
75 {
76         { 0, 0, 0 }
77 };
78
79 JSClassRef JSContactWebSite::m_classRef = JSClassCreate(&m_classInfo);
80
81 JSClassRef DLL_EXPORT JSContactWebSite::getClassRef() {
82         if (!m_classRef) {
83                 m_classRef = JSClassCreate(&m_classInfo);
84         }
85         return m_classRef;
86 }
87
88 bool JSContactWebSite::isObjectOfClass(JSContextRef context, JSValueRef value)
89 {
90         return JSValueIsObjectOfClass(context, value, getClassRef());
91 }
92
93 ContactWebSitePtr JSContactWebSite::getContactWebSite(JSContextRef context, JSValueRef value)
94 {
95         if (!isObjectOfClass(context, value)) {
96                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
97         }
98         JSObjectRef object = JSValueToObject(context, value, NULL);
99         if (!object) {
100                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
101         }
102         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
103         if (!priv) {
104                 Throw(WrtDeviceApis::Commons::NullPointerException);
105         }
106         return priv->getObject();
107 }
108
109 void JSContactWebSite::Initialize(JSContextRef context, JSObjectRef object)
110 {
111         if (!JSObjectGetPrivate(object))
112         {
113                 ContactWebSitePtr webSite(new ContactWebSite());
114                 JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, ContactWebSitePtr(webSite));
115                 if (!JSObjectSetPrivate(object, priv)) {
116                         delete priv;
117                 }
118         }
119 }
120
121 void JSContactWebSite::Finalize(JSObjectRef object)
122 {
123         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
124
125         if (priv != NULL)
126                 delete (priv);
127 }
128
129 JSObjectRef JSContactWebSite::createJSObject(JSContextRef context, ContactWebSitePtr contactWebSite)
130 {
131         JSContactWebSitePriv *priv = new JSContactWebSitePriv(context, contactWebSite);
132         JSObjectRef jsObjectRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
133         if (NULL == jsObjectRef) {
134                 LoggerE("object creation error");
135                 return NULL;
136         }
137         return jsObjectRef;
138 }
139
140 ContactWebSitePtr JSContactWebSite::getPrivData(JSObjectRef object)
141 {
142         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(object));
143         if (!priv) {
144                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
145         }
146         ContactWebSitePtr result = priv->getObject();
147         if (!result) {
148                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
149         }
150         return result;
151 }
152
153 JSObjectRef JSContactWebSite::constructor(JSContextRef context,
154                 JSObjectRef constructor,
155                 size_t argumentCount,
156                 const JSValueRef arguments[],
157                 JSValueRef* exception)
158 {
159         JSContactWebSitePriv *priv = static_cast<JSContactWebSitePriv*>(JSObjectGetPrivate(constructor));
160         if (!priv) {
161                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
162         }
163         JSContextRef gContext = priv->getContext();
164
165         std::string url;
166         std::string stringType;
167         ContactWebSiteType type = WEBSITE_TYPE_HOMEPAGE;
168         ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(gContext);
169         std::string label;
170
171         try {
172                 ArgumentValidator Argvalidator(context, argumentCount, arguments);
173                 url = Argvalidator.toString(0, false);
174
175                 if (argumentCount >= 2){
176                         stringType = Argvalidator.toString(1, false);
177                         type = converter->toContactWebSiteType(stringType);
178                 }
179
180                 if (argumentCount >= 3){
181                         label = Argvalidator.toString(2, false);
182                 }
183
184         } catch (const TypeMismatchException& err ) {
185                 JSWebAPIErrorFactory::postException(context, exception, err);
186                 return NULL;
187         } catch(const BasePlatformException& err) {
188                 JSWebAPIErrorFactory::postException(context, exception, err);
189                 return NULL;
190         } catch(const ConversionException& err) {
191                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
192                 return NULL;
193         } catch(const NullPointerException& err) {
194                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "");
195                 return NULL;
196         }
197
198         ContactWebSitePtr contactWebSite(new ContactWebSite());
199         contactWebSite->setUrl(url);
200         contactWebSite->setType(type);
201         contactWebSite->setLabel(label);
202
203         JSObjectRef jsobject;
204
205         Try {
206                 jsobject = createJSObject(gContext, contactWebSite);
207         } Catch(Exception) {
208                 LoggerE("Argument type mismatch : " << _rethrown_exception.GetMessage());
209                 *exception = JSWebAPIErrorFactory::makeErrorObject(context, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Wrong arguments");
210                 return NULL;
211         }
212
213         return jsobject;
214 }
215
216 bool JSContactWebSite::hasInstance(JSContextRef context,
217                 JSObjectRef constructor,
218                 JSValueRef possibleInstance,
219                 JSValueRef* exception)
220 {
221         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
222 }
223
224 JSValueRef JSContactWebSite::getUrl(JSContextRef context,
225                 JSObjectRef object,
226                 JSStringRef propertyName,
227                 JSValueRef* exception)
228 {
229         Try
230         {
231                 ContactConverterFactory::ConverterType converter =
232                                 ContactConverterFactory::getConverter(context);
233                 ContactWebSitePtr webSite = getPrivData(object);
234                 return converter->toJSValueRef(webSite->getUrl());
235         }
236         Catch(WrtDeviceApis::Commons::Exception)
237         {
238                 LoggerW("trying to get incorrect value");
239         }
240         return JSValueMakeUndefined(context);
241 }
242
243 bool JSContactWebSite::setUrl(JSContextRef context,
244                 JSObjectRef object,
245                 JSStringRef propertyName,
246                 JSValueRef value,
247                 JSValueRef* exception)
248 {
249         Try
250         {
251                 ContactWebSitePtr webSite = getPrivData(object);
252                 ContactConverterFactory::ConverterType converter =
253                                 ContactConverterFactory::getConverter(context);
254                 webSite->setUrl(converter->toString(value));
255         }
256         Catch(WrtDeviceApis::Commons::Exception)
257         {
258                 LoggerW("trying to set incorrect value");
259                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
260         }
261         return true;
262 }
263
264
265 JSValueRef JSContactWebSite::getType(JSContextRef context,
266                 JSObjectRef object,
267                 JSStringRef propertyName,
268                 JSValueRef* exception)
269 {
270         Try
271         {
272                 ContactConverterFactory::ConverterType converter =
273                                 ContactConverterFactory::getConverter(context);
274                 ContactWebSitePtr webSite = getPrivData(object);
275                 std::string ret = converter->toContactWebSiteTypeStr(webSite->getType());
276                 if(!webSite->getTypeIsSet())
277                         return JSValueMakeNull(context);
278                 else
279                         return converter->toJSValueRef(ret);
280         }
281         Catch(WrtDeviceApis::Commons::Exception)
282         {
283                 LoggerW("trying to get incorrect value");
284         }
285         return JSValueMakeUndefined(context);
286 }
287
288 bool JSContactWebSite::setType(JSContextRef context,
289                 JSObjectRef object,
290                 JSStringRef propertyName,
291                 JSValueRef value,
292                 JSValueRef* exception)
293 {
294         Try
295         {
296                 ContactWebSitePtr webSite = getPrivData(object);
297                 ContactConverterFactory::ConverterType converter =
298                                 ContactConverterFactory::getConverter(context);
299                 BasicValidator validator =
300                                 BasicValidatorFactory::getValidator(context, exception);
301                 if(validator->isNullOrUndefined(value))
302                         webSite->unsetType();
303                 else
304                 {
305                         ContactWebSiteType type = converter->toContactWebSiteType(converter->toString(value));
306                         webSite->setType(type);
307                 }
308         }
309         Catch(WrtDeviceApis::Commons::Exception)
310         {
311                 LoggerW("trying to set incorrect value");
312                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
313         }
314         return true;
315 }
316
317 JSValueRef JSContactWebSite::getLabel(JSContextRef context,
318                 JSObjectRef object,
319                 JSStringRef propertyName,
320                 JSValueRef* exception)
321 {
322         Try
323         {
324                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
325                 ContactWebSitePtr webSite = getPrivData(object);
326                 return converter->toJSValueRef(webSite->getLabel());
327         }
328         Catch(WrtDeviceApis::Commons::Exception)
329         {
330                 LoggerW("trying to get incorrect value");
331         }
332         return JSValueMakeUndefined(context);
333 }
334
335 bool JSContactWebSite::setLabel(JSContextRef context,
336                 JSObjectRef object,
337                 JSStringRef propertyName,
338                 JSValueRef value,
339                 JSValueRef* exception)
340 {
341         Try
342         {
343                 ContactWebSitePtr webSite = getPrivData(object);
344                 ContactConverterFactory::ConverterType converter = ContactConverterFactory::getConverter(context);
345                 webSite->setLabel(converter->toString(value));
346         }
347         Catch(WrtDeviceApis::Commons::Exception)
348         {
349                 LoggerW("trying to set incorrect value");
350                 JSWebAPIErrorFactory::postException(context, exception, JSWebAPIErrorFactory::TYPE_MISMATCH_ERROR, "Type mismatch");
351         }
352         return true;
353 }
354
355 } // Contact
356 } // DeviceAPI