Add DRAFT stubs for Vehicle plugin
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Call / JSRemoteParty.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 <cassert>
19 #include <memory>
20 #include <dpl/log/log.h>
21 #include <dpl/shared_ptr.h>
22 #include <CommonsJavaScript/JSUtils.h>
23 #include <CommonsJavaScript/JSDOMExceptionFactory.h>
24 #include <Tizen/Common/JSTizenExceptionFactory.h>
25 #include <Tizen/Common/JSTizenException.h> 
26 #include <API/Call/CallDefine.h>
27 #include <API/Call/RemoteParty.h>
28 #include "JSRemoteParty.h"
29 #include "Converter.h"
30
31 namespace TizenApis {
32 namespace Tizen1_0 {
33
34 using namespace WrtDeviceApis;
35 using namespace WrtDeviceApis::Commons;
36 using namespace WrtDeviceApis::CommonsJavaScript;
37 using namespace Api::Call;
38 using namespace TizenApis::Commons;
39
40 JSClassDefinition JSRemoteParty::m_classInfo = {
41         0,
42         kJSClassAttributeNone,
43         "remoteparty",
44         0,
45         m_property,
46         0,
47         initialize,
48         finalize,
49         NULL,     //HasProperty,
50         getProperty,
51         NULL,     //SetProperty,
52         NULL,     //DeleteProperty,
53         NULL,     //GetPropertyNames,
54         NULL,     //CallAsFunction,
55         NULL,     //CallAsConstructor,
56         hasInstance,
57         NULL,     //ConvertToType
58 };
59
60 JSStaticValue JSRemoteParty::m_property[] = {
61         { STR_REMOTE_PARTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
62         { STR_DISPLAY_NAME, getProperty, NULL, kJSPropertyAttributeReadOnly },
63         { STR_CONTACT_REF, getProperty, NULL, kJSPropertyAttributeReadOnly },
64         { 0, 0, 0, 0 }
65 };
66
67 const JSClassRef JSRemoteParty::getClassRef()
68 {
69         if (!m_jsClassRef) {
70                 m_jsClassRef = JSClassCreate(&m_classInfo);
71         }
72         return m_jsClassRef;
73 }
74
75 const JSClassDefinition* JSRemoteParty::getClassInfo()
76 {
77         return &m_classInfo;
78 }
79
80 JSClassRef JSRemoteParty::m_jsClassRef = JSClassCreate(JSRemoteParty::getClassInfo());
81
82 JSObjectRef JSRemoteParty::createJSObject(JSContextRef context,
83         const RemoteParty &remoteParty)
84 {
85         RemotePartyPtr RemotePartyProps(new RemoteParty(remoteParty));
86         JSRemotePartyPriv *priv = new JSRemotePartyPriv(context, RemotePartyPtr(RemotePartyProps));
87         if (!priv) {
88                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Can not new an object");
89         }
90         return JSObjectMake(context, getClassRef(), priv);
91 }
92
93 RemotePartyPtr JSRemoteParty::getRemoteParty(JSContextRef context, JSValueRef value)
94 {
95         if (!JSValueIsObjectOfClass(context, value, getClassRef())) {
96                 Throw(WrtDeviceApis::Commons::ConversionException);
97         }
98         JSObjectRef object = JSValueToObject(context, value, NULL);
99         if (!object) {
100                 Throw(WrtDeviceApis::Commons::ConversionException);
101         }
102         JSRemotePartyPriv *priv = static_cast<JSRemotePartyPriv*>(JSObjectGetPrivate(object));
103         if (!priv) {
104                 Throw(WrtDeviceApis::Commons::ConversionException);
105         }
106         return priv->getObject();
107 }
108
109 void JSRemoteParty::initialize(JSContextRef context, JSObjectRef object)
110 {
111 }
112
113 void JSRemoteParty::finalize(JSObjectRef object)
114 {
115         JSRemotePartyPriv* priv = static_cast<JSRemotePartyPriv*>(JSObjectGetPrivate(object));
116         if (priv != NULL) {
117                 JSObjectSetPrivate(object, NULL);
118                 delete priv;
119         }
120 }
121
122 JSValueRef JSRemoteParty::getProperty(JSContextRef context,
123         JSObjectRef object,
124         JSStringRef propertyName,
125         JSValueRef* exception)
126 {
127         JSRemotePartyPriv *priv = static_cast<JSRemotePartyPriv*>(JSObjectGetPrivate(object));
128         if (!priv) {
129                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, "Type missmatch error : Invalid private pointer");
130         }
131
132         try {
133                 RemotePartyPtr remoteParty = priv->getObject();
134                 Converter convert(context);
135                 Tizen1_0::Contact::ContactConverter contactConverter(context);
136
137                 if (JSStringIsEqualToUTF8CString(propertyName, STR_REMOTE_PARTY)) {
138                         return convert.toJSValueRef(remoteParty->getRemoteParty());
139                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_DISPLAY_NAME)) {
140                         return convert.toJSValueRef(remoteParty->getDisplayName());
141                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_CONTACT_REF)) {
142                         return contactConverter.toJSValueRef(remoteParty->getContactRef());
143                 }
144         } catch(const WrtDeviceApis::Commons::ConversionException& ex) {
145                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::TYPE_MISMATCH_ERROR, ex.GetMessage());
146         } catch(const WrtDeviceApis::Commons::Exception& ex) {
147                 return JSTizenExceptionFactory::postException(context, exception, JSTizenException::UNKNOWN_ERROR, ex.GetMessage());
148         }
149         return JSValueMakeUndefined(context);
150 }
151
152 bool JSRemoteParty::hasInstance(JSContextRef context,
153         JSObjectRef constructor,
154         JSValueRef possibleInstance,
155         JSValueRef* exception)
156 {
157         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
158 }
159 }
160 }
161