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