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 "JSSyncProfileInfo.h"
20 #include "JSSyncInfo.h"
21 #include "JSSyncServiceInfo.h"
22 #include "DataSyncConverter.h"
24 #include <JSTizenException.h>
25 #include <JSTizenExceptionFactory.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <CommonsJavaScript/JSUtils.h>
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
37 JSClassDefinition JSSyncProfileInfo::m_classInfo = {
39 kJSClassAttributeNone,
40 TIZEN_SYNC_PROFILE_INFO_INTERFACE,
49 NULL, //deleteProperty,
50 NULL, //getPropertyNames,
51 NULL, //callAsFunction,
54 NULL, //convertToType,
57 JSStaticValue JSSyncProfileInfo::m_property[] = {
58 { TIZEN_SYNC_PROFILE_INFO_PROFILE_ID, getProperty, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete },
59 { TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME, getProperty, setProperty, kJSPropertyAttributeNone },
60 { TIZEN_SYNC_PROFILE_INFO_SYNC_INFO, getProperty, setProperty, kJSPropertyAttributeNone },
61 { TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO, getProperty, setProperty, kJSPropertyAttributeNone },
66 JSClassRef JSSyncProfileInfo::m_jsClassRef = JSClassCreate(
67 JSSyncProfileInfo::getClassInfo());
69 const JSClassDefinition* JSSyncProfileInfo::getClassInfo()
71 return &(m_classInfo);
74 JSClassRef JSSyncProfileInfo::getClassRef()
77 m_jsClassRef = JSClassCreate(&m_classInfo);
82 JSObjectRef JSSyncProfileInfo::createJSSyncProfileInfo(JSContextRef context, SyncProfileInfoPtr syncProfileInfo)
84 SyncProfileInfoPrivateObject *priv = new SyncProfileInfoPrivateObject(context, syncProfileInfo);
85 return JSObjectMake(context, getClassRef(), priv);
88 void JSSyncProfileInfo::initialize(JSContextRef context,
91 if (!JSObjectGetPrivate(object)) {
92 LoggerD("Create JSSyncProfileInfo private object.");
93 SyncProfileInfoPtr syncProfileInfo( new SyncProfileInfo() );
94 SyncProfileInfoPrivateObject *priv = new SyncProfileInfoPrivateObject(context, syncProfileInfo);
95 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
99 LoggerD("Private object already set.");
103 void JSSyncProfileInfo::finalize(JSObjectRef object)
105 SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
107 LoggerD("Deleting JSSyncProfileInfo private object.");
109 JSObjectSetPrivate(object, NULL);
113 SyncProfileInfoPtr JSSyncProfileInfo::getPrivateObject(JSObjectRef object)
115 SyncProfileInfoPrivateObject *priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
117 ThrowMsg(NullPointerException, "Private object is null.");
120 SyncProfileInfoPtr syncProfileInfo = priv->getObject();
121 if (!syncProfileInfo) {
122 ThrowMsg(NullPointerException, "JSSyncProfileInfo object is null.");
124 return syncProfileInfo;
127 void JSSyncProfileInfo::setPrivateObject(const SyncProfileInfoPtr &syncProfileInfo,
129 const JSObjectRef object)
133 SyncProfileInfoPrivateObject *priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
135 priv = new SyncProfileInfoPrivateObject(ctx, syncProfileInfo);
136 if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
142 LoggerE("Error during setting JSSyncProfileInfo object.");
146 JSObjectRef JSSyncProfileInfo::constructor(JSContextRef context,
147 JSObjectRef constructor,
148 size_t argumentCount,
149 const JSValueRef arguments[],
150 JSValueRef* exception)
154 DataSyncConverter converter(context);
156 SyncProfileInfoPtr syncProfileInfo( new SyncProfileInfo() );
158 if (argumentCount>=1) {
159 syncProfileInfo->setProfileName(converter.toString(arguments[0]));
161 if (argumentCount>=2) {
162 if (!JSValueIsObjectOfClass(context, arguments[1], JSSyncInfo::getClassRef())) {
163 ThrowMsg(ConversionException, "Wrong second parameter type.");
165 syncProfileInfo->setSyncInfo(JSSyncInfo::getPrivateObject(JSValueToObject(context, arguments[1], exception)));
167 if (argumentCount>=3) {
168 if(!JSIsArrayValue(context, arguments[2])) {
169 ThrowMsg(ConversionException, "Wrong third parameter type.");
171 syncProfileInfo->setServiceInfo(converter.toServiceInfoList(arguments[2]));
174 LoggerD("profileName: "<<syncProfileInfo->getProfileName()<<", serviceInfo.length: "<<syncProfileInfo->getServiceInfo()->size());
176 return createJSSyncProfileInfo(context, syncProfileInfo);
178 Catch(UnsupportedException)
180 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
181 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
185 Catch(InvalidArgumentException)
187 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
188 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
192 Catch(ConversionException)
194 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
195 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
201 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
202 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
208 JSValueRef JSSyncProfileInfo::getProperty(JSContextRef context,
210 JSStringRef propertyName,
211 JSValueRef* exception)
215 SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
217 ThrowMsg(NullPointerException, "Private object is NULL.");
220 SyncProfileInfoPtr syncProfileInfo = priv->getObject();
222 DataSyncConverter converter(context);
224 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_ID)) {
225 return converter.toJSValueRef(syncProfileInfo->getProfileId());
226 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME)) {
227 return converter.toJSValueRef(syncProfileInfo->getProfileName());
228 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SYNC_INFO)) {
229 return JSUtils::makeObject(context, JSSyncInfo::getClassRef(), syncProfileInfo->getSyncInfo());
230 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO)) {
231 return converter.toJSValueRefServiceInfoList(syncProfileInfo->getServiceInfo());
233 LoggerD("Wrong property name.");
238 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
241 return JSValueMakeUndefined(context);
244 bool JSSyncProfileInfo::setProperty(JSContextRef context,
246 JSStringRef propertyName,
248 JSValueRef* exception)
252 SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
254 ThrowMsg(NullPointerException, "Private object is NULL.");
257 SyncProfileInfoPtr syncProfileInfo = priv->getObject();
259 DataSyncConverter converter(context);
261 if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME)) {
262 syncProfileInfo->setProfileName(converter.toString(value));
264 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SYNC_INFO)) {
265 syncProfileInfo->setSyncInfo(JSSyncInfo::getPrivateObject(JSValueToObject(context, value, exception)));
267 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO)) {
268 syncProfileInfo->setServiceInfo(converter.toServiceInfoList(value));
271 LoggerD("Wrong property name.");
276 LoggerW("Exception: "<<_rethrown_exception.GetMessage());