summaryrefslogtreecommitdiff
path: root/src/standards/Tizen/Application/JSApplicationInformation.cpp
blob: 36cea3a3b4e63f898d0ff9b1bced4fc25aca5074 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <cassert>
#include <memory>
#include <dpl/log.h>
#include <CommonsJavaScript/JSUtils.h>
#include <CommonsJavaScript/Converter.h>
#include <Commons/Exception.h>
#include <Tizen/Common/JSTizenExceptionFactory.h>
#include <Tizen/Common/JSTizenException.h>
#include <API/Application/ApplicationInformation.h>
#include "JSApplicationInformation.h"

namespace TizenApis {
namespace Tizen1_0 {
namespace Application {	
	
using namespace WrtDeviceApis;
using namespace Api::Application;
using namespace TizenApis::Commons;


namespace {
const char* APPLICATION_INFORMATION_NAME = "name";
const char* APPLICATION_INFORMATION_PACKAGE_NAME = "packageName";
const char* APPLICATION_INFORMATION_ICONPATH = "iconPath";
const char* APPLICATION_INFORMATION_VERSION = "version";

} //private namespace

JSClassRef JSApplicationInformation::m_classRef = NULL;

JSClassDefinition JSApplicationInformation::m_classInfo = {
    0,
    kJSClassAttributeNone,
    "ApplicationInformation",
    0,
    m_property,
    0,
    initialize,
    finalize,
    NULL,     //HasProperty,
    NULL,
    NULL,     //SetProperty,
    NULL,     //DeleteProperty,
    NULL,     //GetPropertyNames,
    NULL,     //CallAsFunction,
    NULL,     //CallAsConstructor,
    NULL,
    NULL,     //ConvertToType
};

JSStaticValue JSApplicationInformation::m_property[] = {
    { APPLICATION_INFORMATION_NAME, getName, setName, kJSPropertyAttributeReadOnly },
    { APPLICATION_INFORMATION_PACKAGE_NAME, getPackage, setPackage, kJSPropertyAttributeReadOnly },
    { APPLICATION_INFORMATION_ICONPATH, getIconPath, setIconPath, kJSPropertyAttributeReadOnly },
    { APPLICATION_INFORMATION_VERSION, getVersion, setVersion, kJSPropertyAttributeReadOnly },    
    { 0, 0, 0, 0 }
};

JSClassRef JSApplicationInformation::getClassRef() {
	if (!m_classRef) {
		m_classRef = JSClassCreate(&m_classInfo);
	}
	return m_classRef;
}

JSValueRef JSApplicationInformation::createJSObject(JSContextRef context,
		const std::string &name,
		const std::string &package,
		const std::string &iconPath,
		const std::string &version)
{
	ApplicationInformationPtr privateData = ApplicationInformationPtr(new ApplicationInformation());

	privateData->setName(name);
	privateData->setPackage(package);
	privateData->setIconPath(iconPath);
	privateData->setVersion(version);	
	JSApplicationInformationPriv *priv = new JSApplicationInformationPriv(context, privateData);
	JSObjectRef jsValueRef = JSObjectMake(context, getClassRef(), static_cast<void*>(priv));
	if (NULL == jsValueRef) {
		LogError("object creation error");
		return JSValueMakeUndefined(context);
	}
	return jsValueRef;
}

void JSApplicationInformation::initialize(JSContextRef context, JSObjectRef object)
{
	assert(NULL != JSObjectGetPrivate(object));
}

void JSApplicationInformation::finalize(JSObjectRef object)
{
}

bool JSApplicationInformation::isObjectOfClass(JSContextRef context, JSValueRef value)
{
	return JSValueIsObjectOfClass(context, value, getClassRef());
}

ApplicationInformationPtr JSApplicationInformation::getPrivData(JSObjectRef object)
{
	JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
	if (!priv) {
		ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
	}
	ApplicationInformationPtr result = priv->getObject();
	if (!result) {
		ThrowMsg(WrtDeviceApis::Commons::NullPointerException, "Private object is null");
	}
	return result;
}

ApplicationInformationPtr 
	JSApplicationInformation::getApplicationInformation(JSContextRef context, JSValueRef value)
{
	if (!isObjectOfClass(context, value)) {
		Throw(WrtDeviceApis::Commons::InvalidArgumentException);
	}
	JSObjectRef object = JSValueToObject(context, value, NULL);
	if (!object) {
		Throw(WrtDeviceApis::Commons::InvalidArgumentException);
	}
	JSApplicationInformationPriv *priv = static_cast<JSApplicationInformationPriv*>(JSObjectGetPrivate(object));
	if (!priv) {
		Throw(WrtDeviceApis::Commons::NullPointerException);
	}
	return priv->getObject();
}

JSValueRef JSApplicationInformation::getName(JSContextRef context,
		JSObjectRef object,
		JSStringRef propertyName,
		JSValueRef* exception)
{
	Try
	{
		CommonsJavaScript::Converter converter(context);
		ApplicationInformationPtr privateData = getPrivData(object);
		return converter.toJSValueRef(privateData->getName());
	}
	Catch(WrtDeviceApis::Commons::Exception)
	{
		LogWarning("trying to get incorrect value");
	}
	return JSValueMakeUndefined(context);
}

bool JSApplicationInformation::setName(JSContextRef context,
		JSObjectRef object,
		JSStringRef propertyName,
		JSValueRef name,
		JSValueRef* exception)
{
	Try
	{
		ApplicationInformationPtr privateData = getPrivData(object);
		CommonsJavaScript::Converter converter(context);
		privateData->setName(converter.toString(name));
		return true;
	}
	Catch(WrtDeviceApis::Commons::Exception)
	{
		LogWarning("trying to set incorrect value");
	}
	JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
	return false;
}

JSValueRef JSApplicationInformation::getPackage(JSContextRef context,
		JSObjectRef object,
		JSStringRef propertyName,
		JSValueRef* exception)
{
	Try
	{
		CommonsJavaScript::Converter converter(context);
		ApplicationInformationPtr privateData = getPrivData(object);
		return converter.toJSValueRef(privateData->getPackage());
	}
	Catch(WrtDeviceApis::Commons::Exception)
	{
		LogWarning("trying to get incorrect value");
	}
	return JSValueMakeUndefined(context);
}

bool JSApplicationInformation::setPackage(JSContextRef context,
		JSObjectRef object,
		JSStringRef propertyName,
		JSValueRef package,
		JSValueRef* exception)
{
	Try
	{
		ApplicationInformationPtr privateData = getPrivData(object);
		CommonsJavaScript::Converter converter(context);
		privateData->setPackage(converter.toString(package));
		return true;
	}
	Catch(WrtDeviceApis::Commons::Exception)
	{
		LogWarning("trying to set incorrect value");
	}
	JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
	return false;
}

JSValueRef JSApplicationInformation::getIconPath(JSContextRef context,
		JSObjectRef object,
		JSStringRef propertyName,
		JSValueRef* exception)
{
	Try
	{
		CommonsJavaScript::Converter converter(context);
		ApplicationInformationPtr privateData = getPrivData(object);
		return converter.toJSValueRef(privateData->getIconPath());
	}
	Catch(WrtDeviceApis::Commons::Exception)
	{
		LogWarning("trying to get incorrect value");
	}
	return JSValueMakeUndefined(context);
}

bool JSApplicationInformation::setIconPath(JSContextRef context,
		JSObjectRef object,
		JSStringRef propertyName,
		JSValueRef iconPath,
		JSValueRef* exception)
{
	Try
	{
		ApplicationInformationPtr privateData = getPrivData(object);
		CommonsJavaScript::Converter converter(context);
		privateData->setIconPath(converter.toString(iconPath));
		return true;
	}
	Catch(WrtDeviceApis::Commons::Exception)
	{
		LogWarning("trying to set incorrect value");
	}
	JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
	return false;
}

JSValueRef JSApplicationInformation::getVersion(JSContextRef context,
		JSObjectRef object,
		JSStringRef propertyName,
		JSValueRef* exception)
{
	Try
	{
		CommonsJavaScript::Converter converter(context);
		ApplicationInformationPtr privateData = getPrivData(object);
		return converter.toJSValueRef(privateData->getVersion());
	}
	Catch(WrtDeviceApis::Commons::Exception)
	{
		LogWarning("trying to get incorrect value");
	}
	return JSValueMakeUndefined(context);
}

bool JSApplicationInformation::setVersion(JSContextRef context,
		JSObjectRef object,
		JSStringRef propertyName,
		JSValueRef version,
		JSValueRef* exception)
{
	Try
	{
		ApplicationInformationPtr privateData = getPrivData(object);
		CommonsJavaScript::Converter converter(context);
		privateData->setVersion(converter.toString(version));
		return true;
	}
	Catch(WrtDeviceApis::Commons::Exception)
	{
		LogWarning("trying to set incorrect value");
	}
	JSTizenExceptionFactory::postException(context, exception,JSTizenException::INVALID_VALUES_ERROR, "Invalid value error");
	return false;
}


}
}
}