Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Bluetooth / JSBluetoothClassDeviceMajor.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 #include <CommonsJavaScript/Converter.h>
18 #include <CommonsJavaScript/Validator.h>
19 #include <CommonsJavaScript/JSUtils.h>
20 #include <CommonsJavaScript/JSCallbackManager.h>
21 #include <CommonsJavaScript/Utils.h>
22 #include <Tizen/Common/JSTizenExceptionFactory.h>
23 #include <Tizen/Common/JSTizenException.h>
24 #include "JSBluetoothClassDeviceMajor.h"
25 #include "plugin_config.h"
26
27
28 using namespace std;
29 using namespace DPL;
30 using namespace WrtDeviceApis;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace TizenApis::Commons;
33
34
35 namespace TizenApis {
36 namespace Tizen1_0 {
37
38 JSClassDefinition JSBluetoothClassDeviceMajor::m_classInfo =
39 {
40         0,
41         kJSClassAttributeNone,
42         "BluetoothClassDeviceMajor",
43         NULL,
44         m_properties,
45         NULL,
46         initialize,
47         finalize,
48         NULL, 
49         NULL, 
50         NULL, 
51         NULL, 
52         NULL, 
53         NULL,
54         NULL,
55         hasInstance,
56         NULL
57 };
58
59         
60
61 JSStaticValue JSBluetoothClassDeviceMajor::m_properties[] = 
62 {
63         {"MISC", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
64         {"COMPUTER", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
65         {"PHONE", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
66         {"NETWORK", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
67         {"AUDIO_VIDEO", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
68         {"PERIPHERAL", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
69         {"IMAGING", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
70         {"WEARABLE", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
71         {"TOY", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
72         {"HEALTH", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
73         {"UNCATEGORIZED", JSBluetoothClassDeviceMajor::getProperty, NULL, kJSPropertyAttributeReadOnly },
74         {0, 0, 0, 0}
75 };
76
77
78 const JSClassRef JSBluetoothClassDeviceMajor::getClassRef() 
79 {
80         if (!m_jsClassRef) {
81                 m_jsClassRef = JSClassCreate(&m_classInfo);
82         }
83         return m_jsClassRef;
84 }
85
86 const JSClassDefinition* JSBluetoothClassDeviceMajor::getClassInfo() 
87 {
88         return &m_classInfo;
89 }
90
91 JSClassRef JSBluetoothClassDeviceMajor::m_jsClassRef = JSClassCreate(JSBluetoothClassDeviceMajor::getClassInfo());
92
93 void JSBluetoothClassDeviceMajor::initialize(JSContextRef context, JSObjectRef object) 
94 {
95         LogDebug("JSBluetoothClassDeviceMajor::initialize ");
96         JSBluetoothClassDeviceMajorPriv* priv = static_cast<JSBluetoothClassDeviceMajorPriv*>(JSObjectGetPrivate(object));
97
98         if (priv == NULL)
99         {
100                 BluetoothClassPtr btClass(new BluetoothClass());
101                 priv = new JSBluetoothClassDeviceMajorPriv( context, btClass);
102                 if(!JSObjectSetPrivate(object, static_cast<void*>(priv))) 
103                 {
104                         LogError("Object can't store private data.");
105                         delete priv;
106                 }
107         }
108         else
109         {
110                 LogDebug("JSBluetoothClassDeviceMajor::already exist ");                
111         }
112 }
113
114
115 JSObjectRef JSBluetoothClassDeviceMajor::createJSObject(JSContextRef context)
116 {
117         BluetoothClassPtr btClass(new BluetoothClass());
118         JSBluetoothClassDeviceMajorPriv* priv = new JSBluetoothClassDeviceMajorPriv( context, btClass);
119         btClass->setReadOnly();
120
121         return JSObjectMake(context, getClassRef(), priv);
122 }
123
124
125 void JSBluetoothClassDeviceMajor::finalize(JSObjectRef object) 
126 {
127         JSBluetoothClassDeviceMajorPriv* priv = static_cast<JSBluetoothClassDeviceMajorPriv*>(JSObjectGetPrivate(object));
128
129         LogDebug("JSBluetoothClassDeviceMajor::Finalrize");
130
131         if (priv != NULL) 
132         {
133                 JSObjectSetPrivate(object, NULL);
134                 delete priv;
135         }
136 }
137
138 bool JSBluetoothClassDeviceMajor::hasInstance(JSContextRef context, JSObjectRef constructor,
139                 JSValueRef possibleInstance, JSValueRef* exception) {
140         return JSValueIsObjectOfClass(context, possibleInstance, getClassRef());
141 }
142
143 JSValueRef JSBluetoothClassDeviceMajor::getProperty(JSContextRef context,
144                                                                                 JSObjectRef object,
145                                                                                 JSStringRef propertyName,
146                                                                                 JSValueRef* exception)
147 {
148         LogDebug("OK"); 
149
150         JSBluetoothClassDeviceMajorPriv* priv = static_cast<JSBluetoothClassDeviceMajorPriv*>(JSObjectGetPrivate(object));      
151         
152         try 
153         {
154                 
155                 if (priv == NULL)
156                 {
157                         Throw(WrtDeviceApis::Commons::InvalidArgumentException);
158                 }
159                 
160                 Converter converter(context);
161                 BluetoothClassPtr btClass(priv->getObject());
162                 std::string key = converter.toString(propertyName);
163                 int value = 0;
164                 
165                 if (btClass->find(key, value) == false)
166                 {
167                         ThrowMsg(WrtDeviceApis::Commons::UnknownException, "can not find property" << key);
168                 }
169                 return converter.toJSValueRef(value);
170         }
171         Catch (WrtDeviceApis::Commons::InvalidArgumentException) 
172         {
173                 LogError("InvalidArgumentException");
174                 return JSTizenExceptionFactory::postException(context, exception, 
175                         JSTizenException::INVALID_VALUES_ERROR, "invalid parameter error");     
176         }       
177         Catch (WrtDeviceApis::Commons::SecurityException) 
178         {
179                 LogError("permission denied error");
180                 return JSTizenExceptionFactory::postException(context, exception, 
181                         JSTizenException::PERMISSION_DENIED_ERROR, "permission denied error");  
182
183         }
184         Catch (WrtDeviceApis::Commons::Exception) 
185         {
186                 LogError("UnkownException");
187                 return JSTizenExceptionFactory::postException(context, exception, 
188                         JSTizenException::UNKNOWN_ERROR, "Unkown error");
189         }
190         return JSValueMakeNull(context);
191 }
192 }
193 }
194