wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / Application / JSApplicationInformation.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 <cassert>
19 #include <memory>
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <Commons/Exception.h>
23 #include <JSWebAPIException.h>
24 //#include <JSTizenExceptionFactory.h>
25 //#include <JSTizenException.h>
26 #include "ApplicationInformation.h"
27 #include "JSApplicationInformation.h"
28 #include "AppManagerWrapper.h"
29 #include <Logger.h>
30
31 namespace DeviceAPI {
32 namespace Application { 
33         
34 using namespace WrtDeviceApis;
35 using namespace DeviceAPI::Common;
36
37
38 JSClassRef JSApplicationInformation::m_classRef = NULL;
39
40 JSClassDefinition JSApplicationInformation::m_classInfo = {
41     0,
42     kJSClassAttributeNone,
43     TIZEN_INTERFACE_APPLICATION_INFORMATION,
44     0,
45     m_property,
46     0,
47     initialize,
48     finalize,
49     NULL,     //HasProperty,
50     NULL,       //GetProperty,
51     NULL,     //SetProperty,
52     NULL,     //DeleteProperty,
53     NULL,     //GetPropertyNames,
54     NULL,     //CallAsFunction,
55     NULL,     //CallAsConstructor,
56     NULL,
57     NULL,     //ConvertToType
58 };
59
60 JSStaticValue JSApplicationInformation::m_property[] = {
61     { TIZEN_APPLICATION_INFORMATION_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
62     { TIZEN_APPLICATION_INFORMATION_NAME, getProperty, NULL, kJSPropertyAttributeReadOnly },
63     { TIZEN_APPLICATION_INFORMATION_ICONPATH, getProperty, NULL, kJSPropertyAttributeReadOnly },
64     { TIZEN_APPLICATION_INFORMATION_VERSION, getProperty, NULL, kJSPropertyAttributeReadOnly },
65     { TIZEN_APPLICATION_INFORMATION_SHOW, getProperty, NULL, kJSPropertyAttributeReadOnly },
66     { TIZEN_APPLICATION_INFORMATION_CATEGORIES, getProperty, NULL, kJSPropertyAttributeReadOnly },
67         { TIZEN_APPLICATION_INFORMATION_INSTALL_DATE, getProperty, NULL, kJSPropertyAttributeReadOnly },
68         { TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE, getProperty, NULL, kJSPropertyAttributeReadOnly },
69         { TIZEN_APPLICATION_INFORMATION_PACKAGE_ID, getProperty, NULL, kJSPropertyAttributeReadOnly },
70     { 0, 0, 0, 0 }
71 };
72
73 JSClassRef JSApplicationInformation::getClassRef() {
74         if (!m_classRef) {
75                 m_classRef = JSClassCreate(&m_classInfo);
76         }
77         return m_classRef;
78 }
79
80 JSValueRef JSApplicationInformation::createJSObject(JSContextRef context,
81                 const std::string &name,
82                 const std::string &appId,
83                 const std::string &iconPath,
84                 const std::string &version,
85                 const bool &show,
86                 const std::vector<std::string> &categories,
87                 const time_t &installDate,
88                 const long &installSize,
89                 const std::string &pkgId)
90 {
91         ApplicationInformationPtr privateData = ApplicationInformationPtr(new ApplicationInformation());
92         privateData->setName(name);
93         privateData->setAppId(appId);
94         privateData->setIconPath(iconPath);
95         privateData->setVersion(version);
96         privateData->setShow(show);
97         privateData->setCategories(categories);
98         privateData->setInstallDate(installDate);
99         privateData->setInstallSize(installSize);
100         privateData->setPackageId(pkgId);
101         
102         JSApplicationInformationPriv *priv = new JSApplicationInformationPriv(context, privateData);
103         
104         JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
105         if (NULL == jsValueRef) {
106                 LoggerE("object creation error");
107                 return JSValueMakeUndefined(context);
108         }
109         
110         return jsValueRef;
111 }
112
113 void JSApplicationInformation::initialize(JSContextRef context, JSObjectRef object)
114 {
115         //LoggerI(">> initialize");
116 }
117
118 void JSApplicationInformation::finalize(JSObjectRef object)
119 {
120     LoggerI(">> finalize");
121     JSApplicationInformationPriv* priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
122     JSObjectSetPrivate(object, NULL);
123     LoggerD("Deleting JSApplicationInformation object");
124     delete priv;                
125 }
126
127 bool JSApplicationInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
128 {
129         return JSValueIsObjectOfClass(context, value, getClassRef());
130 }
131
132 ApplicationInformationPtr JSApplicationInformation::getPrivData(JSContextRef context, JSObjectRef object)
133 {
134         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
135         if (!priv) {
136                 throw TypeMismatchException("Private object is null");
137         }
138         ApplicationInformationPtr result = priv->getObject();
139         if (!result) {
140                 throw TypeMismatchException("Private object is null");
141         }
142         return result;
143 }
144
145 ApplicationInformationPtr JSApplicationInformation::getApplicationInformation(JSContextRef context, JSValueRef value)
146 {
147         if (!isObjectOfClass(context, value)) {
148                 throw TypeMismatchException("is not a object class");
149         }
150         
151         JSObjectRef object = JSValueToObject(context, value, NULL);
152         if (!object) {
153                 throw TypeMismatchException("Fail to get object");
154         }
155         
156         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
157         if (!priv) {
158                 throw TypeMismatchException("Private object is null");
159         }
160         
161         return priv->getObject();
162 }
163
164 JSValueRef JSApplicationInformation::getProperty(JSContextRef context,
165         JSObjectRef object,
166         JSStringRef propertyName,
167         JSValueRef* exception)
168 {
169         try {
170                 CommonsJavaScript::Converter converter(context);
171                 ApplicationInformationPtr privateData = getPrivData(context, object);
172
173                 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_ID)) {
174                         return converter.toJSValueRef(privateData->getAppId());
175                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_NAME)) {
176                         return converter.toJSValueRef(privateData->getName());
177                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_ICONPATH)) {
178                         return converter.toJSValueRef(privateData->getIconPath());
179                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_SHOW)) {
180                         return converter.toJSValueRef(privateData->getShow());
181                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_CATEGORIES)) {
182                         return converter.toJSValueRef(privateData->getCategories());
183                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_PACKAGE_ID)) {
184                         return converter.toJSValueRef(privateData->getPackageId());
185                 } else {
186 // below code is temporal. if package manager issue is solved, rollback this code.
187 #if 1           // below code is used to fill attribute lazy
188                         if (!privateData->isInitialized()) {
189                                 //LoggerI(">> is Not Initialized");
190                                 AppManagerWrapperSingleton::Instance().initializeAppInfo(privateData);
191                         }
192 #endif
193                         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_VERSION)) {
194                                 return converter.toJSValueRef(privateData->getVersion());
195                         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_INSTALL_DATE)) {
196                                 return converter.toJSValueRef(privateData->getInstallDate());
197                         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_APPLICATION_INFORMATION_INSTALL_SIZE)) {
198                                 return converter.toJSValueRefLong(privateData->getInstallSize());
199                         } 
200                 }
201         } catch (const BasePlatformException &err) {
202         return JSWebAPIException::throwException(context, exception, err);
203     } catch (...) {
204         DeviceAPI::Common::TypeMismatchException err("TypeMismatchException occured");
205         return JSWebAPIException::throwException(context, exception, err);
206     }
207         
208         return JSValueMakeUndefined(context);
209 }
210
211 }
212 }