merge wrt-plugins-tizen_0.2.0-3
[platform/framework/web/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 <API/Call/CallDefine.h>
25 #include <API/Call/RemoteParty.h>
26 #include "JSRemoteParty.h"
27 #include "Converter.h"
28
29 namespace TizenApis {
30 namespace Tizen1_0 {
31
32 using namespace WrtDeviceApis;
33 using namespace WrtDeviceApis::Commons;
34 using namespace WrtDeviceApis::CommonsJavaScript;
35 using namespace Api::Call;
36
37 JSClassDefinition JSRemoteParty::m_classInfo = {
38         0,
39         kJSClassAttributeNone,
40         "remoteparty",
41         0,
42         m_property,
43         0,
44         initialize,
45         finalize,
46         NULL,     //HasProperty,
47         getProperty,
48         NULL,     //SetProperty,
49         NULL,     //DeleteProperty,
50         NULL,     //GetPropertyNames,
51         NULL,     //CallAsFunction,
52         NULL,     //CallAsConstructor,
53         hasInstance,
54         NULL,     //ConvertToType
55 };
56
57 JSStaticValue JSRemoteParty::m_property[] = {
58         { STR_REMOTE_PARTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
59         { STR_DISPLAY_NAME, getProperty, NULL, kJSPropertyAttributeReadOnly },
60         { STR_CONTACT_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
61         { 0, 0, 0, 0 }
62 };
63
64 const JSClassRef JSRemoteParty::getClassRef()
65 {
66         if (!m_jsClassRef) {
67                 m_jsClassRef = JSClassCreate(&m_classInfo);
68         }
69         return m_jsClassRef;
70 }
71
72 const JSClassDefinition* JSRemoteParty::getClassInfo()
73 {
74         return &m_classInfo;
75 }
76
77 JSClassRef JSRemoteParty::m_jsClassRef = JSClassCreate(JSRemoteParty::getClassInfo());
78
79 JSObjectRef JSRemoteParty::createJSObject(JSContextRef context,
80         const RemoteParty &remoteParty)
81 {
82         std::auto_ptr<RemoteParty> RemotePartyProps(new RemoteParty(remoteParty));
83         JSRemotePartyPriv *priv = new JSRemotePartyPriv(context, RemotePartyProps.get());
84         RemotePartyProps.release();
85         if (!priv) {
86                 ThrowMsg(Commons::NullPointerException, "Can not new an object");
87         }
88         return JSObjectMake(context, getClassRef(), priv);
89 }
90
91 void JSRemoteParty::initialize(JSContextRef context, JSObjectRef object)
92 {
93 }
94
95 void JSRemoteParty::finalize(JSObjectRef object)
96 {
97         JSRemotePartyPriv* priv = static_cast<JSRemotePartyPriv*>(JSObjectGetPrivate(object));
98         JSObjectSetPrivate(object, NULL);
99         delete priv;
100 }
101
102 JSValueRef JSRemoteParty::getProperty(JSContextRef context,
103         JSObjectRef object,
104         JSStringRef propertyName,
105         JSValueRef* exception)
106 {
107         JSRemotePartyPriv *priv = static_cast<JSRemotePartyPriv*>(JSObjectGetPrivate(object));
108         assert(priv && "Private object not set.");
109
110         try {
111                 RemoteParty* remoteParty = priv->getObject();
112                 Converter convert(context);
113
114                 if (JSStringIsEqualToUTF8CString(propertyName, STR_REMOTE_PARTY)) {
115                         return convert.toJSValueRef(remoteParty->getRemoteParty());
116                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_DISPLAY_NAME)) {
117                         return convert.toJSValueRef(remoteParty->getDisplayName());
118                 } else if (JSStringIsEqualToUTF8CString(propertyName, STR_CONTACT_ID)) {
119                         return convert.toJSValueRef(remoteParty->getContactId());
120                 }
121         } catch(Commons::Exception) {
122                 LogWarning("trying to get incorrect value");
123         }
124         return JSValueMakeUndefined(context);
125 }
126
127 bool JSRemoteParty::hasInstance(JSContextRef context,
128         JSObjectRef constructor,
129         JSValueRef possibleInstance,
130         JSValueRef* exception)
131 {
132         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
133 }
134 }
135 }
136