Git Init
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Application / JSApplicationInformation.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <cassert>
18 #include <memory>
19 #include <dpl/log.h>
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"
27
28 namespace TizenApis {
29 namespace Tizen1_0 {
30 namespace Application { 
31         
32 using namespace WrtDeviceApis;
33 using namespace Api::Application;
34 using namespace TizenApis::Commons;
35
36
37 namespace {
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";
42
43 } //private namespace
44
45 JSClassRef JSApplicationInformation::m_classRef = NULL;
46
47 JSClassDefinition JSApplicationInformation::m_classInfo = {
48     0,
49     kJSClassAttributeNone,
50     "ApplicationInformation",
51     0,
52     m_property,
53     0,
54     initialize,
55     finalize,
56     NULL,     //HasProperty,
57     NULL,
58     NULL,     //SetProperty,
59     NULL,     //DeleteProperty,
60     NULL,     //GetPropertyNames,
61     NULL,     //CallAsFunction,
62     NULL,     //CallAsConstructor,
63     NULL,
64     NULL,     //ConvertToType
65 };
66
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 },    
72     { 0, 0, 0, 0 }
73 };
74
75 JSClassRef JSApplicationInformation::getClassRef() {
76         if (!m_classRef) {
77                 m_classRef = JSClassCreate(&m_classInfo);
78         }
79         return m_classRef;
80 }
81
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)
87 {
88         ApplicationInformationPtr privateData = ApplicationInformationPtr(new ApplicationInformation());
89
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);
99         }
100         return jsValueRef;
101 }
102
103 void JSApplicationInformation::initialize(JSContextRef context, JSObjectRef object)
104 {
105         assert(NULL != JSObjectGetPrivate(object));
106 }
107
108 void JSApplicationInformation::finalize(JSObjectRef object)
109 {
110 }
111
112 bool JSApplicationInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
113 {
114         return JSValueIsObjectOfClass(context, value, getClassRef());
115 }
116
117 ApplicationInformationPtr JSApplicationInformation::getPrivData(JSObjectRef object)
118 {
119         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
120         if (!priv) {
121                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
122         }
123         ApplicationInformationPtr result = priv->getObject();
124         if (!result) {
125                 ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
126         }
127         return result;
128 }
129
130 ApplicationInformationPtr 
131         JSApplicationInformation::getApplicationInformation(JSContextRef context, JSValueRef value)
132 {
133         if (!isObjectOfClass(context, value)) {
134                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
135         }
136         JSObjectRef object = JSValueToObject(context, value, NULL);
137         if (!object) {
138                 Throw(WrtDeviceApis::Commons::InvalidArgumentException);
139         }
140         JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
141         if (!priv) {
142                 Throw(WrtDeviceApis::Commons::NullPointerException);
143         }
144         return priv->getObject();
145 }
146
147 JSValueRef JSApplicationInformation::getName(JSContextRef context,
148                 JSObjectRef object,
149                 JSStringRef propertyName,
150                 JSValueRef* exception)
151 {
152         Try
153         {
154                 CommonsJavaScript::Converter converter(context);
155                 ApplicationInformationPtr privateData = getPrivData(object);
156                 return converter.toJSValueRef(privateData->getName());
157         }
158         Catch(WrtDeviceApis::Commons::Exception)
159         {
160                 LogWarning("trying to get incorrect value");
161         }
162         return JSValueMakeUndefined(context);
163 }
164
165 bool JSApplicationInformation::setName(JSContextRef context,
166                 JSObjectRef object,
167                 JSStringRef propertyName,
168                 JSValueRef name,
169                 JSValueRef* exception)
170 {
171         Try
172         {
173                 ApplicationInformationPtr privateData = getPrivData(object);
174                 CommonsJavaScript::Converter converter(context);
175                 privateData->setName(converter.toString(name));
176                 return true;
177         }
178         Catch(WrtDeviceApis::Commons::Exception)
179         {
180                 LogWarning("trying to set incorrect value");
181         }
182         JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
183         return false;
184 }
185
186 JSValueRef JSApplicationInformation::getPackage(JSContextRef context,
187                 JSObjectRef object,
188                 JSStringRef propertyName,
189                 JSValueRef* exception)
190 {
191         Try
192         {
193                 CommonsJavaScript::Converter converter(context);
194                 ApplicationInformationPtr privateData = getPrivData(object);
195                 return converter.toJSValueRef(privateData->getPackage());
196         }
197         Catch(WrtDeviceApis::Commons::Exception)
198         {
199                 LogWarning("trying to get incorrect value");
200         }
201         return JSValueMakeUndefined(context);
202 }
203
204 bool JSApplicationInformation::setPackage(JSContextRef context,
205                 JSObjectRef object,
206                 JSStringRef propertyName,
207                 JSValueRef package,
208                 JSValueRef* exception)
209 {
210         Try
211         {
212                 ApplicationInformationPtr privateData = getPrivData(object);
213                 CommonsJavaScript::Converter converter(context);
214                 privateData->setPackage(converter.toString(package));
215                 return true;
216         }
217         Catch(WrtDeviceApis::Commons::Exception)
218         {
219                 LogWarning("trying to set incorrect value");
220         }
221         JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
222         return false;
223 }
224
225 JSValueRef JSApplicationInformation::getIconPath(JSContextRef context,
226                 JSObjectRef object,
227                 JSStringRef propertyName,
228                 JSValueRef* exception)
229 {
230         Try
231         {
232                 CommonsJavaScript::Converter converter(context);
233                 ApplicationInformationPtr privateData = getPrivData(object);
234                 return converter.toJSValueRef(privateData->getIconPath());
235         }
236         Catch(WrtDeviceApis::Commons::Exception)
237         {
238                 LogWarning("trying to get incorrect value");
239         }
240         return JSValueMakeUndefined(context);
241 }
242
243 bool JSApplicationInformation::setIconPath(JSContextRef context,
244                 JSObjectRef object,
245                 JSStringRef propertyName,
246                 JSValueRef iconPath,
247                 JSValueRef* exception)
248 {
249         Try
250         {
251                 ApplicationInformationPtr privateData = getPrivData(object);
252                 CommonsJavaScript::Converter converter(context);
253                 privateData->setIconPath(converter.toString(iconPath));
254                 return true;
255         }
256         Catch(WrtDeviceApis::Commons::Exception)
257         {
258                 LogWarning("trying to set incorrect value");
259         }
260         JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
261         return false;
262 }
263
264 JSValueRef JSApplicationInformation::getVersion(JSContextRef context,
265                 JSObjectRef object,
266                 JSStringRef propertyName,
267                 JSValueRef* exception)
268 {
269         Try
270         {
271                 CommonsJavaScript::Converter converter(context);
272                 ApplicationInformationPtr privateData = getPrivData(object);
273                 return converter.toJSValueRef(privateData->getVersion());
274         }
275         Catch(WrtDeviceApis::Commons::Exception)
276         {
277                 LogWarning("trying to get incorrect value");
278         }
279         return JSValueMakeUndefined(context);
280 }
281
282 bool JSApplicationInformation::setVersion(JSContextRef context,
283                 JSObjectRef object,
284                 JSStringRef propertyName,
285                 JSValueRef version,
286                 JSValueRef* exception)
287 {
288         Try
289         {
290                 ApplicationInformationPtr privateData = getPrivData(object);
291                 CommonsJavaScript::Converter converter(context);
292                 privateData->setVersion(converter.toString(version));
293                 return true;
294         }
295         Catch(WrtDeviceApis::Commons::Exception)
296         {
297                 LogWarning("trying to set incorrect value");
298         }
299         JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
300         return false;
301 }
302
303
304 }
305 }
306 }