wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Package / JSPackageInformation.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 #include <JSWebAPIException.h>
19 #include <CommonsJavaScript/Converter.h>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/Utils.h>
22 #include <JSUtil.h>
23 #include "JSPackageInformation.h"
24 #include "PackageInformation.h"
25 #include <Logger.h>
26
27 namespace DeviceAPI {
28 namespace Package {     
29
30 using namespace std;
31 using namespace WrtDeviceApis::Commons;
32 using namespace WrtDeviceApis::CommonsJavaScript;
33 using namespace DeviceAPI::Common;
34
35 JSClassRef JSPackageInformation::m_classRef = NULL;
36
37 JSClassDefinition JSPackageInformation::m_classInfo = {
38                 0,                                                                              // current (and only) version is 0
39                 kJSClassAttributeNone,                                  // attributes
40                 TIZEN_INTERFACE_PACKAGE_INFORMATION,    // class name
41                 NULL,                                                                   // parent class
42                 m_property,                                                             // static values
43                 NULL,                                                                   // static functions
44                 initialize,                                                             // initialize
45                 finalize,                                                               // finalize
46                 NULL,                                                                   // hasProperty
47                 NULL,                                                                   // getProperty
48                 NULL,                                                                   // setProperty
49                 NULL,                                                                   // deleteProperty
50                 NULL,                                                                   // getPropertyNames
51                 NULL,                                                                   // callAsConstructor
52                 NULL,                                                                   // callAsConstructor
53                 NULL,                                                                   // hasInstance
54                 NULL                                                                    // convertToType
55 };
56
57 JSStaticValue JSPackageInformation::m_property[] = {
58     { TIZEN_PACKAGE_INFORMATION_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
59     { TIZEN_PACKAGE_INFORMATION_NAME, getProperty, NULL, kJSPropertyAttributeReadOnly },
60     { TIZEN_PACKAGE_INFORMATION_ICONPATH, getProperty, NULL, kJSPropertyAttributeReadOnly },
61     { TIZEN_PACKAGE_INFORMATION_VERSION, getProperty, NULL, kJSPropertyAttributeReadOnly },
62         { TIZEN_PACKAGE_INFORMATION_TOTAL_SIZE, getProperty, NULL, kJSPropertyAttributeReadOnly },
63         { TIZEN_PACKAGE_INFORMATION_DATA_SIZE, getProperty, NULL, kJSPropertyAttributeReadOnly },       
64         { TIZEN_PACKAGE_INFORMATION_LAST_MODIFIED, getProperty, NULL, kJSPropertyAttributeReadOnly },
65         { TIZEN_PACKAGE_INFORMATION_AUTHOR, getProperty, NULL, kJSPropertyAttributeReadOnly },
66         { TIZEN_PACKAGE_INFORMATION_DESCRIPTION, getProperty, NULL, kJSPropertyAttributeReadOnly },
67         { TIZEN_PACKAGE_INFORMATION_APP_IDS, getProperty, NULL, kJSPropertyAttributeReadOnly },
68     { 0, 0, 0, 0 }
69 };
70
71 // type is defined to partner feature on native
72 //{ TIZEN_PACKAGE_INFORMATION_TYPE, getProperty, setProperty, kJSPropertyAttributeReadOnly },
73
74 JSClassRef JSPackageInformation::getClassRef() {
75         if (!m_classRef) {
76                 m_classRef = JSClassCreate(&m_classInfo);
77         }
78         return m_classRef;
79 }
80
81
82 JSValueRef JSPackageInformation::createJSObject(JSContextRef context, PackageInformation *pkgInfo)
83 {
84         //LoggerE("createJSObject enter");
85         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(pkgInfo));
86         if (NULL == jsValueRef) {
87                 LoggerE("object creation error");
88                 return JSValueMakeUndefined(context);
89         }
90         
91         return jsValueRef;
92 }
93
94
95 void JSPackageInformation::initialize(JSContextRef context, JSObjectRef object)
96 {
97         LoggerD(">> initialize");
98 }
99
100 void JSPackageInformation::finalize(JSObjectRef object)
101 {
102     LoggerD(">> finalize");
103 }
104
105 bool JSPackageInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
106 {
107         return JSValueIsObjectOfClass(context, value, getClassRef());
108 }
109
110 PackageInformation* JSPackageInformation::getPrivData(JSObjectRef object)
111 {
112         PackageInformation *priv = static_cast<PackageInformation*>(JSObjectGetPrivate(object));
113         if (!priv) {
114                 throw TypeMismatchException("Private object is null");
115         }
116
117         return priv;
118 }
119
120 JSValueRef JSPackageInformation::getProperty(JSContextRef context,
121                 JSObjectRef object,
122                 JSStringRef propertyName,
123                 JSValueRef* exception)
124 {
125         try {
126                 //WrtDeviceApis::CommonsJavaScript::Converter converter(context);
127                 PackageInformation* privateData = getPrivData(object);
128         if (!privateData) {
129             throw TypeMismatchException("Private object is NULL");
130         }               
131         
132                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_ID)) {
133                         return JSUtil::toJSValueRef(context, privateData->m_id);
134                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_NAME)) {
135                         return JSUtil::toJSValueRef(context, privateData->m_name);
136                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_ICONPATH)) {
137                         return JSUtil::toJSValueRef(context, privateData->m_iconPath);
138                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_VERSION)) {
139                         return JSUtil::toJSValueRef(context, privateData->m_version);
140                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_TOTAL_SIZE)) {
141                         return JSUtil::toJSValueRef(context, privateData->m_totalSize);
142                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_DATA_SIZE)) {
143                         return JSUtil::toJSValueRef(context, privateData->m_dataSize);
144                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_LAST_MODIFIED)) {
145                         return JSUtil::makeDateObject(context, privateData->m_lastModified);
146                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_AUTHOR)) {
147                         return JSUtil::toJSValueRef(context, privateData->m_author);
148                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_DESCRIPTION)) {
149                         return JSUtil::toJSValueRef(context, privateData->m_description);
150                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_PACKAGE_INFORMATION_APP_IDS)) {
151                         return JSUtil::toJSValueRef(context, privateData->m_appIds);
152                 }
153         } catch (const BasePlatformException &err) {
154         return JSWebAPIException::throwException(context, exception, err);
155     } catch (...) {
156         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
157         return JSWebAPIException::throwException(context, exception, err);
158     }
159         
160         return JSValueMakeUndefined(context);
161 }
162
163 }
164 }