Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Systeminfo / JSDeviceInfo.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 "JSDeviceInfo.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* DEVICE_IMEI_PROPERTY = "imei";
31 const char* DEVICE_MODEL_PROPERTY = "model";
32 const char* DEVICE_VERSION_PROPERTY = "version";
33 const char* DEVICE_VENDOR_PROPERTY = "vendor";
34
35
36 JSClassRef JSDeviceInfo::m_classRef = NULL;
37
38 JSClassDefinition JSDeviceInfo::m_classInfo = {
39     0,
40     kJSClassAttributeNone,
41     "deviceinfo",
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 JSDeviceInfo::m_properties[] = {
59     { DEVICE_IMEI_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
60     { DEVICE_MODEL_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
61     { DEVICE_VERSION_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
62     { DEVICE_VENDOR_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },        
63     { 0, 0, 0, 0 }
64 };
65
66 const JSClassRef JSDeviceInfo::getClassRef()
67 {
68     if (!m_classRef) {
69         m_classRef = JSClassCreate(&m_classInfo);
70     }
71     return m_classRef;
72 }
73
74 const JSClassDefinition* JSDeviceInfo::getClassInfo()
75 {
76     return &m_classInfo;
77 }
78
79 void JSDeviceInfo::Initialize(JSContextRef context, JSObjectRef object)
80 {
81 }
82
83 void JSDeviceInfo::Finalize(JSObjectRef object)
84 {
85     LogDebug("Entered");
86     JSDevicePriv* priv = static_cast<JSDevicePriv*>(JSObjectGetPrivate(object));
87     JSObjectSetPrivate(object, NULL);
88     LogDebug("Deleting DeviceInfo object");
89     delete priv;
90 }
91
92 bool JSDeviceInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
93 {
94     return JSUtils::hasProperty(m_properties, propertyName);
95 }
96
97 JSObjectRef JSDeviceInfo::createJSObject(JSContextRef context, const Api::Systeminfo::DeviceProperties &deviceInfo)
98 {
99     LogDebug("Enter");
100     std::auto_ptr<Api::Systeminfo::DeviceProperties> accProps(new Api::Systeminfo::DeviceProperties(deviceInfo));
101     JSDevicePriv *priv = new JSDevicePriv(context, accProps.get());
102     accProps.release();
103     return JSObjectMake(context, getClassRef(), priv);
104 }
105
106 JSValueRef JSDeviceInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
107 {
108     LogDebug("Enter");
109     JSDevicePriv *priv = static_cast<JSDevicePriv*>(JSObjectGetPrivate(object));
110     if (NULL == priv) {
111         LogError("Private object not set.");
112         return JSValueMakeUndefined(context);
113     }
114
115     Try
116     {
117         Api::Systeminfo::DeviceProperties *deviceInfo = priv->getObject();
118         Converter convert(context);
119
120         if (JSStringIsEqualToUTF8CString(propertyName, DEVICE_IMEI_PROPERTY)) {
121             return convert.toJSValueRef(deviceInfo->imei);
122         } else if (JSStringIsEqualToUTF8CString(propertyName, DEVICE_MODEL_PROPERTY)) {
123             return convert.toJSValueRef(deviceInfo->model);
124         } else if (JSStringIsEqualToUTF8CString(propertyName, DEVICE_VERSION_PROPERTY)) {
125             return convert.toJSValueRef(deviceInfo->version);
126         } else if (JSStringIsEqualToUTF8CString(propertyName, DEVICE_VENDOR_PROPERTY)) {
127             return convert.toJSValueRef(deviceInfo->vendor);
128         }            
129     }
130     Catch(Exception)
131     {
132         LogError("Exception: " << _rethrown_exception.GetMessage());
133     }
134     return JSValueMakeUndefined(context);
135 }
136 }
137 }