wrt-plugins-tizen_0.4.23
[framework/web/wrt-plugins-tizen.git] / src / DataSync / JSSyncProfileInfo.cpp
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18
19 #include "JSSyncProfileInfo.h"
20 #include "JSSyncInfo.h"
21 #include "JSSyncServiceInfo.h"
22 #include "DataSyncConverter.h"
23
24 #include <JSTizenException.h>
25 #include <JSTizenExceptionFactory.h>
26 #include <CommonsJavaScript/Converter.h>
27 #include <CommonsJavaScript/JSUtils.h>
28 #include <Logger.h>
29
30 using namespace WrtDeviceApis::Commons;
31 using namespace WrtDeviceApis::CommonsJavaScript;
32 using namespace DeviceAPI::Common;
33
34 namespace DeviceAPI {
35 namespace DataSync {
36
37 JSClassDefinition JSSyncProfileInfo::m_classInfo = {
38     0,
39     kJSClassAttributeNone,
40     TIZEN_SYNC_PROFILE_INFO_INTERFACE,
41     NULL, //parentClass
42     m_property,
43     NULL, //staticValues,
44     initialize,
45     finalize,
46     NULL, //hasProperty,
47     NULL, //getProperty,
48     NULL, //setProperty,
49     NULL, //deleteProperty,
50     NULL, //getPropertyNames,
51     NULL, //callAsFunction,
52     constructor,
53     NULL, //hasInstance,
54     NULL, //convertToType,
55 };
56
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 },
62
63     { 0, 0, 0, 0 }
64 };
65
66 JSClassRef JSSyncProfileInfo::m_jsClassRef = JSClassCreate(
67         JSSyncProfileInfo::getClassInfo());
68
69 const JSClassDefinition* JSSyncProfileInfo::getClassInfo()
70 {
71     return &(m_classInfo);
72 }
73
74 JSClassRef JSSyncProfileInfo::getClassRef()
75 {
76     if (!m_jsClassRef) {
77         m_jsClassRef = JSClassCreate(&m_classInfo);
78     }
79     return m_jsClassRef;
80 }
81
82 JSObjectRef JSSyncProfileInfo::createJSSyncProfileInfo(JSContextRef context, SyncProfileInfoPtr syncProfileInfo)
83 {
84     SyncProfileInfoPrivateObject *priv = new SyncProfileInfoPrivateObject(context, syncProfileInfo);
85     return JSObjectMake(context, getClassRef(), priv);
86 }
87
88 void JSSyncProfileInfo::initialize(JSContextRef context,
89         JSObjectRef object)
90 {
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))) {
96             delete priv;
97         }
98     } else {
99         LoggerD("Private object already set.");
100     }
101 }
102
103 void JSSyncProfileInfo::finalize(JSObjectRef object)
104 {
105     SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
106     if (priv) {
107         LoggerD("Deleting JSSyncProfileInfo private object.");
108         delete priv;
109         JSObjectSetPrivate(object, NULL);
110     }
111 }
112
113 SyncProfileInfoPtr JSSyncProfileInfo::getPrivateObject(JSObjectRef object)
114 {
115     SyncProfileInfoPrivateObject *priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
116     if (!priv) {
117         ThrowMsg(NullPointerException, "Private object is null.");
118     }
119
120     SyncProfileInfoPtr syncProfileInfo = priv->getObject();
121     if (!syncProfileInfo) {
122         ThrowMsg(NullPointerException, "JSSyncProfileInfo object is null.");
123     }
124     return syncProfileInfo;
125 }
126
127 void JSSyncProfileInfo::setPrivateObject(const SyncProfileInfoPtr &syncProfileInfo,
128         JSContextRef ctx,
129         const JSObjectRef object)
130 {
131     Try
132     {
133         SyncProfileInfoPrivateObject *priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
134         delete priv;
135         priv = new SyncProfileInfoPrivateObject(ctx, syncProfileInfo);
136         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
137             delete priv;
138         }
139     }
140     Catch(Exception)
141     {
142         LoggerE("Error during setting JSSyncProfileInfo object.");
143     }
144 }
145
146 JSObjectRef JSSyncProfileInfo::constructor(JSContextRef context,
147     JSObjectRef constructor,
148     size_t argumentCount,
149     const JSValueRef arguments[],
150     JSValueRef* exception)
151 {
152     Try
153     {
154         DataSyncConverter converter(context);
155
156         SyncProfileInfoPtr syncProfileInfo( new SyncProfileInfo() );
157
158         if (argumentCount>=1) {
159                         syncProfileInfo->setProfileName(converter.toString(arguments[0]));
160         }
161         if (argumentCount>=2) {
162             if (!JSValueIsObjectOfClass(context, arguments[1], JSSyncInfo::getClassRef())) {
163                 ThrowMsg(ConversionException, "Wrong second parameter type.");
164                 }
165                         syncProfileInfo->setSyncInfo(JSSyncInfo::getPrivateObject(JSValueToObject(context, arguments[1], exception)));
166         }
167         if (argumentCount>=3) {
168                         if(!JSIsArrayValue(context, arguments[2])) {
169                 ThrowMsg(ConversionException, "Wrong third parameter type.");
170                         }
171                         syncProfileInfo->setServiceInfo(converter.toServiceInfoList(arguments[2]));
172         }
173
174         LoggerD("profileName: "<<syncProfileInfo->getProfileName()<<", syncInfo url: "<<syncProfileInfo->getSyncInfo()->getUrl()<<", serviceInfo.length: "<<syncProfileInfo->getServiceInfo()->size());
175
176         return createJSSyncProfileInfo(context, syncProfileInfo);
177     }
178     Catch(UnsupportedException)
179     {
180         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
181         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
182     }
183     Catch(InvalidArgumentException)
184     {
185         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
186         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
187     }
188     Catch(ConversionException)
189     {
190         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
191         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
192     }
193     Catch(Exception)
194     {
195         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
196         *exception = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
197     }
198
199     return NULL;
200 }
201
202 JSValueRef JSSyncProfileInfo::getProperty(JSContextRef context,
203         JSObjectRef object,
204         JSStringRef propertyName,
205         JSValueRef* exception)
206 {
207     Try
208     {
209         SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
210         if (!priv) {
211             ThrowMsg(NullPointerException, "Private object is NULL.");
212         }
213
214         SyncProfileInfoPtr syncProfileInfo = priv->getObject();
215
216         DataSyncConverter converter(context);
217
218         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_ID)) {
219             return converter.toJSValueRef(syncProfileInfo->getProfileId());
220         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME)) {
221             return converter.toJSValueRef(syncProfileInfo->getProfileName());
222         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SYNC_INFO)) {
223                 return JSUtils::makeObject(context, JSSyncInfo::getClassRef(), syncProfileInfo->getSyncInfo());
224         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO)) {
225             return converter.toJSValueRefServiceInfoList(syncProfileInfo->getServiceInfo());
226         } else {
227                 LoggerD("Wrong property name.");
228                 }
229     }
230     Catch(Exception)
231     {
232                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
233     }
234
235     return JSValueMakeUndefined(context);
236 }
237
238 bool JSSyncProfileInfo::setProperty(JSContextRef context,
239         JSObjectRef object,
240         JSStringRef propertyName,
241         JSValueRef value,
242         JSValueRef* exception)
243 {
244     Try
245     {
246         SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
247         if (!priv) {
248             ThrowMsg(NullPointerException, "Private object is NULL.");
249         }
250
251         SyncProfileInfoPtr syncProfileInfo = priv->getObject();
252
253         DataSyncConverter converter(context);
254
255         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME)) {
256             syncProfileInfo->setProfileName(converter.toString(value));
257             return true;
258         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SYNC_INFO)) {
259                         syncProfileInfo->setSyncInfo(JSSyncInfo::getPrivateObject(JSValueToObject(context, value, exception)));
260             return true;
261         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO)) {
262             syncProfileInfo->setServiceInfo(converter.toServiceInfoList(value));
263             return true;
264         } else {
265                 LoggerD("Wrong property name.");
266         }
267     }
268     Catch(Exception)
269     {
270                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
271     }
272
273     return true;
274 }
275
276 }
277 }