Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Systeminfo / JSDisplayInfo.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 "JSDisplayInfo.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* DISPLAY_RESOLUTIONW_PROPERTY = "resolutionWidth";
31 const char* DISPLAY_RESOLUTIONH_PROPERTY = "resolutionHeight";
32 const char* DISPLAY_INCHW_PROPERTY = "dotsPerInchWidth";
33 const char* DISPLAY_INCHH_PROPERTY = "dotsPerInchHeight";
34 const char* DISPLAY_PHYSICALW_PROPERTY = "physicalWidth";
35 const char* DISPLAY_PHYSICALH_PROPERTY = "physicalHeight";
36 const char* DISPLAY_BRIGHTNESS_PROPERTY = "brightness";
37
38
39 JSClassRef JSDisplayInfo::m_classRef = NULL;
40
41 JSClassDefinition JSDisplayInfo::m_classInfo = {
42     0,
43     kJSClassAttributeNone,
44     "displayinfo",
45     0,
46     m_properties,
47     NULL,
48     Initialize,
49     Finalize,
50     hasProperty,
51     getProperty,
52     NULL,
53     NULL,
54     NULL,
55     NULL,
56     NULL,
57     NULL,
58     NULL
59 };
60
61 JSStaticValue JSDisplayInfo::m_properties[] = {
62     { DISPLAY_RESOLUTIONW_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
63     { DISPLAY_RESOLUTIONH_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
64     { DISPLAY_INCHW_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
65     { DISPLAY_INCHH_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
66     { DISPLAY_PHYSICALW_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
67     { DISPLAY_PHYSICALH_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },        
68     { DISPLAY_BRIGHTNESS_PROPERTY, getProperty, NULL, kJSPropertyAttributeReadOnly },
69     { 0, 0, 0, 0 }
70 };
71
72 const JSClassRef JSDisplayInfo::getClassRef()
73 {
74     if (!m_classRef) {
75         m_classRef = JSClassCreate(&m_classInfo);
76     }
77     return m_classRef;
78 }
79
80 const JSClassDefinition* JSDisplayInfo::getClassInfo()
81 {
82     return &m_classInfo;
83 }
84
85 void JSDisplayInfo::Initialize(JSContextRef context, JSObjectRef object)
86 {
87 }
88
89 void JSDisplayInfo::Finalize(JSObjectRef object)
90 {
91     LogDebug("Entered");
92     JSDisplayPriv* priv = static_cast<JSDisplayPriv*>(JSObjectGetPrivate(object));
93     JSObjectSetPrivate(object, NULL);
94     LogDebug("Deleting DisplayInfo object");
95     delete priv;
96 }
97
98 bool JSDisplayInfo::hasProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName)
99 {
100     return JSUtils::hasProperty(m_properties, propertyName);
101 }
102
103 JSObjectRef JSDisplayInfo::createJSObject(JSContextRef context, const Api::Systeminfo::DisplayProperties &displayInfo)
104 {
105     std::auto_ptr<Api::Systeminfo::DisplayProperties> accProps(new Api::Systeminfo::DisplayProperties(displayInfo));
106     JSDisplayPriv *priv = new JSDisplayPriv(context, accProps.get());
107     accProps.release();
108     return JSObjectMake(context, getClassRef(), priv);
109 }
110
111 JSValueRef JSDisplayInfo::getProperty(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
112 {
113     LogDebug("Enter");
114     JSDisplayPriv *priv = static_cast<JSDisplayPriv*>(JSObjectGetPrivate(object));
115     if (NULL == priv) {
116         LogError("Private object not set.");
117         return JSValueMakeUndefined(context);
118     }
119
120     Try
121     {
122         Api::Systeminfo::DisplayProperties *displayInfo = priv->getObject();
123         Converter convert(context);
124         if (JSStringIsEqualToUTF8CString(propertyName, DISPLAY_RESOLUTIONW_PROPERTY)) {
125             return convert.toJSValueRef(displayInfo->resolutionWidth);
126         } else if (JSStringIsEqualToUTF8CString(propertyName, DISPLAY_RESOLUTIONH_PROPERTY)) {
127             return convert.toJSValueRef(displayInfo->resolutionHeight);
128         } else if (JSStringIsEqualToUTF8CString(propertyName, DISPLAY_INCHW_PROPERTY)) {
129             return convert.toJSValueRef(displayInfo->dotsPerInchWidth);
130         } else if (JSStringIsEqualToUTF8CString(propertyName, DISPLAY_INCHH_PROPERTY)) {
131             return convert.toJSValueRef(displayInfo->dotsPerInchHeight);
132         } else if (JSStringIsEqualToUTF8CString(propertyName, DISPLAY_PHYSICALW_PROPERTY)) {
133             return convert.toJSValueRef(displayInfo->physicalWidth);
134         } else if (JSStringIsEqualToUTF8CString(propertyName, DISPLAY_PHYSICALH_PROPERTY)) {
135             return convert.toJSValueRef(displayInfo->physicalHeight);
136         } else if (JSStringIsEqualToUTF8CString(propertyName, DISPLAY_BRIGHTNESS_PROPERTY)) {
137             return convert.toJSValueRef(displayInfo->brightness);
138         }
139     }
140     Catch(Exception)
141     {
142         LogError("Exception: " << _rethrown_exception.GetMessage());
143     }
144     return JSValueMakeUndefined(context);
145 }
146 }
147 }