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