2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <CommonsJavaScript/JSUtils.h>
21 #include <CommonsJavaScript/Converter.h>
22 #include <Commons/Exception.h>
23 #include <Tizen/Common/JSTizenExceptionFactory.h>
24 #include <Tizen/Common/JSTizenException.h>
25 #include <API/Application/ApplicationInformation.h>
26 #include "JSApplicationInformation.h"
30 namespace Application {
32 using namespace WrtDeviceApis;
33 using namespace Api::Application;
34 using namespace TizenApis::Commons;
38 const char* APPLICATION_INFORMATION_NAME = "name";
39 const char* APPLICATION_INFORMATION_PACKAGE_NAME = "packageName";
40 const char* APPLICATION_INFORMATION_ICONPATH = "iconPath";
41 const char* APPLICATION_INFORMATION_VERSION = "version";
45 JSClassRef JSApplicationInformation::m_classRef = NULL;
47 JSClassDefinition JSApplicationInformation::m_classInfo = {
49 kJSClassAttributeNone,
50 "ApplicationInformation",
59 NULL, //DeleteProperty,
60 NULL, //GetPropertyNames,
61 NULL, //CallAsFunction,
62 NULL, //CallAsConstructor,
67 JSStaticValue JSApplicationInformation::m_property[] = {
68 { APPLICATION_INFORMATION_NAME, getName, setName, kJSPropertyAttributeReadOnly },
69 { APPLICATION_INFORMATION_PACKAGE_NAME, getPackage, setPackage, kJSPropertyAttributeReadOnly },
70 { APPLICATION_INFORMATION_ICONPATH, getIconPath, setIconPath, kJSPropertyAttributeReadOnly },
71 { APPLICATION_INFORMATION_VERSION, getVersion, setVersion, kJSPropertyAttributeReadOnly },
75 JSClassRef JSApplicationInformation::getClassRef() {
77 m_classRef = JSClassCreate(&m_classInfo);
82 JSValueRef JSApplicationInformation::createJSObject(JSContextRef context,
83 const std::string &name,
84 const std::string &package,
85 const std::string &iconPath,
86 const std::string &version)
88 ApplicationInformationPtr privateData = ApplicationInformationPtr(new ApplicationInformation());
90 privateData->setName(name);
91 privateData->setPackage(package);
92 privateData->setIconPath(iconPath);
93 privateData->setVersion(version);
94 JSApplicationInformationPriv *priv = new JSApplicationInformationPriv(context, privateData);
95 JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
96 if (NULL == jsValueRef) {
97 LogError("object creation error");
98 return JSValueMakeUndefined(context);
103 void JSApplicationInformation::initialize(JSContextRef context, JSObjectRef object)
105 assert(NULL != JSObjectGetPrivate(object));
108 void JSApplicationInformation::finalize(JSObjectRef object)
112 bool JSApplicationInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
114 return JSValueIsObjectOfClass(context, value, getClassRef());
117 ApplicationInformationPtr JSApplicationInformation::getPrivData(JSObjectRef object)
119 JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
121 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
123 ApplicationInformationPtr result = priv->getObject();
125 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
130 ApplicationInformationPtr
131 JSApplicationInformation::getApplicationInformation(JSContextRef context, JSValueRef value)
133 if (!isObjectOfClass(context, value)) {
134 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
136 JSObjectRef object = JSValueToObject(context, value, NULL);
138 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
140 JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
142 Throw(WrtDeviceApis::Commons::NullPointerException);
144 return priv->getObject();
147 JSValueRef JSApplicationInformation::getName(JSContextRef context,
149 JSStringRef propertyName,
150 JSValueRef* exception)
154 CommonsJavaScript::Converter converter(context);
155 ApplicationInformationPtr privateData = getPrivData(object);
156 return converter.toJSValueRef(privateData->getName());
158 Catch(WrtDeviceApis::Commons::Exception)
160 LogWarning("trying to get incorrect value");
162 return JSValueMakeUndefined(context);
165 bool JSApplicationInformation::setName(JSContextRef context,
167 JSStringRef propertyName,
169 JSValueRef* exception)
173 ApplicationInformationPtr privateData = getPrivData(object);
174 CommonsJavaScript::Converter converter(context);
175 privateData->setName(converter.toString(name));
178 Catch(WrtDeviceApis::Commons::Exception)
180 LogWarning("trying to set incorrect value");
182 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
186 JSValueRef JSApplicationInformation::getPackage(JSContextRef context,
188 JSStringRef propertyName,
189 JSValueRef* exception)
193 CommonsJavaScript::Converter converter(context);
194 ApplicationInformationPtr privateData = getPrivData(object);
195 return converter.toJSValueRef(privateData->getPackage());
197 Catch(WrtDeviceApis::Commons::Exception)
199 LogWarning("trying to get incorrect value");
201 return JSValueMakeUndefined(context);
204 bool JSApplicationInformation::setPackage(JSContextRef context,
206 JSStringRef propertyName,
208 JSValueRef* exception)
212 ApplicationInformationPtr privateData = getPrivData(object);
213 CommonsJavaScript::Converter converter(context);
214 privateData->setPackage(converter.toString(package));
217 Catch(WrtDeviceApis::Commons::Exception)
219 LogWarning("trying to set incorrect value");
221 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
225 JSValueRef JSApplicationInformation::getIconPath(JSContextRef context,
227 JSStringRef propertyName,
228 JSValueRef* exception)
232 CommonsJavaScript::Converter converter(context);
233 ApplicationInformationPtr privateData = getPrivData(object);
234 return converter.toJSValueRef(privateData->getIconPath());
236 Catch(WrtDeviceApis::Commons::Exception)
238 LogWarning("trying to get incorrect value");
240 return JSValueMakeUndefined(context);
243 bool JSApplicationInformation::setIconPath(JSContextRef context,
245 JSStringRef propertyName,
247 JSValueRef* exception)
251 ApplicationInformationPtr privateData = getPrivData(object);
252 CommonsJavaScript::Converter converter(context);
253 privateData->setIconPath(converter.toString(iconPath));
256 Catch(WrtDeviceApis::Commons::Exception)
258 LogWarning("trying to set incorrect value");
260 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
264 JSValueRef JSApplicationInformation::getVersion(JSContextRef context,
266 JSStringRef propertyName,
267 JSValueRef* exception)
271 CommonsJavaScript::Converter converter(context);
272 ApplicationInformationPtr privateData = getPrivData(object);
273 return converter.toJSValueRef(privateData->getVersion());
275 Catch(WrtDeviceApis::Commons::Exception)
277 LogWarning("trying to get incorrect value");
279 return JSValueMakeUndefined(context);
282 bool JSApplicationInformation::setVersion(JSContextRef context,
284 JSStringRef propertyName,
286 JSValueRef* exception)
290 ApplicationInformationPtr privateData = getPrivData(object);
291 CommonsJavaScript::Converter converter(context);
292 privateData->setVersion(converter.toString(version));
295 Catch(WrtDeviceApis::Commons::Exception)
297 LogWarning("trying to set incorrect value");
299 JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");