Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Systeminfo / JSWifiNetworkInfo.cpp
1 /*
2  *
3  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved 
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 #include <memory>
20 #include <dpl/log.h>
21 #include "JSWifiNetworkInfo.h"
22
23 namespace TizenApis {
24 namespace Tizen1_0 {
25 using namespace WrtDeviceApis::CommonsJavaScript;
26 using namespace WrtDeviceApis::Commons;
27 using namespace Api::Systeminfo;
28
29 namespace {
30 const char* WIFINETWORK_STATUS_PROPERTY = "status";
31 const char* WIFINETWORK_SSID_PROPERTY = "ssid";
32 const char* WIFINETWORK_IPADDRESS_PROPERTY = "ipAddress";
33 const char* WIFINETWORK_SIGNALSTRENGTH_PROPERTY = "signalStrength";
34
35
36 JSClassRef JSWifiNetworkInfo::m_classRef = NULL;
37
38 JSClassDefinition JSWifiNetworkInfo::m_classInfo = {
39     0,
40     kJSClassAttributeNone,
41     "wifinetworkinfo",
42     0,
43     m_properties,
44     NULL,
45     Initialize,
46     Finalize,
47     hasProperty,
48     getProperty,
49     NULL,
50     NULL,
51     NULL,
52     NULL,
53     NULL,
54     NULL,
55     NULL
56 };
57
58 JSStaticValue JSWifiNetworkInfo::m_properties[] = {
59     { WIFINETWORK_STATUS_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
60     { WIFINETWORK_SSID_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
61     { WIFINETWORK_IPADDRESS_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },        
62     { WIFINETWORK_SIGNALSTRENGTH_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },        
63     { 0, 0, 0, 0 }
64 };
65
66 const JSClassRef JSWifiNetworkInfo::getClassRef()
67 {
68     if (!m_classRef) {
69         m_classRef = JSClassCreate(&m_classInfo);
70     }
71     return m_classRef;
72 }
73
74 const JSClassDefinition* JSWifiNetworkInfo::getClassInfo()
75 {
76     return &m_classInfo;
77 }
78
79 void JSWifiNetworkInfo::Initialize(JSContextRef context, JSObjectRef object)
80 {
81 }
82
83 void JSWifiNetworkInfo::Finalize(JSObjectRef object)
84 {
85     LogDebug("Entered");
86     JSWifiNetworkPriv* priv = static_cast<JSWifiNetworkPriv*>(JSObjectGetPrivate(object));
87     JSObjectSetPrivate(object, NULL);
88     LogDebug("Deleting WifiNetworkInfo object");
89     delete priv;
90 }
91
92 bool JSWifiNetworkInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
93 {
94     return JSUtils::hasProperty(m_properties, propertyName);
95 }
96
97 JSObjectRef JSWifiNetworkInfo::createJSObject(JSContextRef context, const Api::Systeminfo::WifiNetworkProperties &wifiNetworkInfo)
98 {
99     LogDebug("Enter");
100     std::auto_ptr<Api::Systeminfo::WifiNetworkProperties> accProps(new Api::Systeminfo::WifiNetworkProperties(wifiNetworkInfo));
101     JSWifiNetworkPriv *priv = new JSWifiNetworkPriv(context, accProps.get());
102     accProps.release();
103     return JSObjectMake(context, getClassRef(), priv);
104 }
105
106 JSValueRef JSWifiNetworkInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
107 {
108     LogDebug("Enter");
109     JSWifiNetworkPriv *priv = static_cast<JSWifiNetworkPriv*>(JSObjectGetPrivate(object));
110     if (NULL == priv) {
111         LogError("Private object not set.");
112         return JSValueMakeUndefined(context);
113     }
114
115     Try
116     {
117         Api::Systeminfo::WifiNetworkProperties *wifiNetworkInfo = priv->getObject();
118         Converter convert(context);
119
120         if (JSStringIsEqualToUTF8CString(propertyName, WIFINETWORK_STATUS_PROPERTY)) {
121             return convert.toJSValueRef(wifiNetworkInfo->status);
122         } else if (JSStringIsEqualToUTF8CString(propertyName, WIFINETWORK_SSID_PROPERTY)) {
123             return convert.toJSValueRef(wifiNetworkInfo->ssid);
124         } else if (JSStringIsEqualToUTF8CString(propertyName, WIFINETWORK_IPADDRESS_PROPERTY)) {
125             return convert.toJSValueRef(wifiNetworkInfo->ipAddress);
126         } else if (JSStringIsEqualToUTF8CString(propertyName, WIFINETWORK_SIGNALSTRENGTH_PROPERTY)) {
127             return convert.toJSValueRef(wifiNetworkInfo->signalStrength);
128         }            
129     }
130     Catch(Exception)
131     {
132         LogError("Exception: " << _rethrown_exception.GetMessage());
133     }
134     return JSValueMakeUndefined(context);
135 }
136 }
137 }