2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "JSSyncServiceInfo.h"
20 #include <JSTizenException.h>
21 #include <JSTizenExceptionFactory.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include "DataSyncConverter.h"
26 using namespace WrtDeviceApis::Commons;
27 using namespace WrtDeviceApis::CommonsJavaScript;
28 using namespace DeviceAPI::Common;
33 JSClassDefinition JSSyncServiceInfo::m_classInfo = {
35 kJSClassAttributeNone,
36 TIZEN_SYNC_SERVICE_INFO_INTERFACE,
45 NULL, //deleteProperty,
46 NULL, //getPropertyNames,
47 NULL, //callAsFunction,
50 NULL, //convertToType,
53 JSStaticValue JSSyncServiceInfo::m_property[] = {
54 { TIZEN_SYNC_SERVICE_INFO_ENABLE, getProperty, setProperty, kJSPropertyAttributeNone },
55 { TIZEN_SYNC_SERVICE_INFO_SERVICE_TYPE, getProperty, setProperty, kJSPropertyAttributeNone },
56 { TIZEN_SYNC_SERVICE_INFO_SERVER_DATABSE_URI, getProperty, setProperty, kJSPropertyAttributeNone },
57 { TIZEN_SYNC_SERVICE_INFO_ID, getProperty, setProperty, kJSPropertyAttributeNone },
58 { TIZEN_SYNC_SERVICE_INFO_PASSWORD, getProperty, setProperty, kJSPropertyAttributeNone },
63 JSClassRef JSSyncServiceInfo::m_jsClassRef = JSClassCreate(
64 JSSyncServiceInfo::getClassInfo());
66 const JSClassDefinition* JSSyncServiceInfo::getClassInfo()
68 return &(m_classInfo);
71 JSClassRef JSSyncServiceInfo::getClassRef()
74 m_jsClassRef = JSClassCreate(&m_classInfo);
79 JSObjectRef JSSyncServiceInfo::createJSSyncServiceInfo(JSContextRef context, SyncServiceInfoPtr syncServiceInfo)
81 SyncServiceInfoPrivateObject *priv = new SyncServiceInfoPrivateObject(context, syncServiceInfo);
82 return JSObjectMake(context, getClassRef(), priv);
85 void JSSyncServiceInfo::initialize(JSContextRef context,
88 if (!JSObjectGetPrivate(object)) {
89 LoggerD("Create JSSyncServiceInfo private object.");
90 SyncServiceInfoPtr syncServiceInfo( new SyncServiceInfo() );
91 SyncServiceInfoPrivateObject *priv = new SyncServiceInfoPrivateObject(context, syncServiceInfo);
92 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
96 LoggerD("Private object already set.");
100 void JSSyncServiceInfo::finalize(JSObjectRef object)
102 SyncServiceInfoPrivateObject* priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
104 LoggerD("Deleting JSSyncServiceInfo private object.");
106 JSObjectSetPrivate(object, NULL);
110 SyncServiceInfoPtr JSSyncServiceInfo::getPrivateObject(JSObjectRef object)
112 SyncServiceInfoPrivateObject *priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
114 ThrowMsg(NullPointerException, "Private object is null.");
117 SyncServiceInfoPtr syncServiceInfo = priv->getObject();
118 if (!syncServiceInfo) {
119 ThrowMsg(NullPointerException, "JSSyncServiceInfo object is null.");
121 return syncServiceInfo;
124 void JSSyncServiceInfo::setPrivateObject(const SyncServiceInfoPtr &syncServiceInfo,
126 const JSObjectRef object)
130 SyncServiceInfoPrivateObject *priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
132 priv = new SyncServiceInfoPrivateObject(ctx, syncServiceInfo);
133 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
139 LoggerE("Error during setting JSSyncServiceInfo object.");
143 JSObjectRef JSSyncServiceInfo::constructor(JSContextRef context,
144 JSObjectRef constructor,
145 size_t argumentCount,
146 const JSValueRef arguments[],
147 JSValueRef* exception)
151 DataSyncConverter converter(context);
153 SyncServiceInfoPtr syncServiceInfo( new SyncServiceInfo() );
155 if (argumentCount>=1) {
156 syncServiceInfo->setEnable(converter.toBool(arguments[0]));
158 if (argumentCount>=2) {
159 syncServiceInfo->setSyncServiceType(converter.toSyncServiceType(converter.toString(arguments[1])));
161 if (argumentCount>=3) {
162 syncServiceInfo->setServerDatabaseUri(converter.toString(arguments[2]));
164 if (argumentCount>=4) {
165 syncServiceInfo->setId(converter.toString(arguments[3]));
167 if (argumentCount>=5) {
168 syncServiceInfo->setPassword(converter.toString(arguments[4]));
171 LoggerD("enable: "<<syncServiceInfo->getEnable()<<", serviceType: "<<syncServiceInfo->getSyncServiceType());
173 return createJSSyncServiceInfo(context, syncServiceInfo);
175 Catch(UnsupportedException)
177 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
178 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
182 Catch(InvalidArgumentException)
184 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
185 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
189 Catch(ConversionException)
191 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
192 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
198 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
199 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
205 JSValueRef JSSyncServiceInfo::getProperty(JSContextRef context,
207 JSStringRef propertyName,
208 JSValueRef* exception)
212 SyncServiceInfoPrivateObject* priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
214 ThrowMsg(NullPointerException, "Private object is NULL.");
217 SyncServiceInfoPtr syncServiceInfo = priv->getObject();
219 DataSyncConverter converter(context);
221 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_ENABLE)) {
222 return converter.toJSValueRef(syncServiceInfo->getEnable());
223 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_SERVICE_TYPE)) {
224 return converter.toJSValueRef(converter.toTizenValue(syncServiceInfo->getSyncServiceType()));
225 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_SERVER_DATABSE_URI)) {
226 return converter.toJSValueRef(syncServiceInfo->getServerDatabaseUri());
227 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_ID)) {
228 // For a privacy reason.
229 return JSValueMakeNull(context);
230 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_PASSWORD)) {
231 // For a privacy reason.
232 return JSValueMakeNull(context);
234 LoggerD("Wrong property name.");
239 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
242 return JSValueMakeUndefined(context);
245 bool JSSyncServiceInfo::setProperty(JSContextRef context,
247 JSStringRef propertyName,
249 JSValueRef* exception)
253 SyncServiceInfoPrivateObject* priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
255 ThrowMsg(NullPointerException, "Private object is NULL.");
258 SyncServiceInfoPtr syncServiceInfo = priv->getObject();
260 DataSyncConverter converter(context);
262 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_ENABLE)) {
263 syncServiceInfo->setEnable(converter.toBool(value));
265 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_SERVICE_TYPE)) {
266 syncServiceInfo->setSyncServiceType(converter.toSyncServiceType(converter.toString(value)));
268 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_SERVER_DATABSE_URI)) {
269 syncServiceInfo->setServerDatabaseUri(converter.toString(value));
271 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_ID)) {
272 syncServiceInfo->setId(converter.toString(value));
274 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_PASSWORD)) {
275 syncServiceInfo->setPassword(converter.toString(value));
278 LoggerD("Wrong property name.");
283 LoggerW("Exception: "<<_rethrown_exception.GetMessage());