Update change log and spec for wrt-plugins-tizen_0.4.25-1
[platform/framework/web/wrt-plugins-tizen.git] / src / DataSync / JSSyncServiceInfo.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 "JSSyncServiceInfo.h"
20 #include <JSTizenException.h>
21 #include <JSTizenExceptionFactory.h>
22 #include <CommonsJavaScript/Converter.h>
23 #include "DataSyncConverter.h"
24 #include <Logger.h>
25
26 using namespace WrtDeviceApis::Commons;
27 using namespace WrtDeviceApis::CommonsJavaScript;
28 using namespace DeviceAPI::Common;
29
30 namespace DeviceAPI {
31 namespace DataSync {
32
33 JSClassDefinition JSSyncServiceInfo::m_classInfo = {
34     0,
35     kJSClassAttributeNone,
36     TIZEN_SYNC_SERVICE_INFO_INTERFACE,
37     NULL, //parentClass
38     m_property,
39     NULL, //staticValues,
40     initialize,
41     finalize,
42     NULL, //hasProperty,
43     NULL, //getProperty,
44     NULL, //setProperty,
45     NULL, //deleteProperty,
46     NULL, //getPropertyNames,
47     NULL, //callAsFunction,
48     NULL, //constructor,
49     NULL, //hasInstance,
50     NULL, //convertToType,
51 };
52
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 },
59
60     { 0, 0, 0, 0 }
61 };
62
63 JSClassRef JSSyncServiceInfo::m_jsClassRef = JSClassCreate(
64         JSSyncServiceInfo::getClassInfo());
65
66 const JSClassDefinition* JSSyncServiceInfo::getClassInfo()
67 {
68     return &(m_classInfo);
69 }
70
71 JSClassRef JSSyncServiceInfo::getClassRef()
72 {
73     if (!m_jsClassRef) {
74         m_jsClassRef = JSClassCreate(&m_classInfo);
75     }
76     return m_jsClassRef;
77 }
78
79 JSObjectRef JSSyncServiceInfo::createJSSyncServiceInfo(JSContextRef context, SyncServiceInfoPtr syncServiceInfo)
80 {
81     SyncServiceInfoPrivateObject *priv = new SyncServiceInfoPrivateObject(context, syncServiceInfo);
82     return JSObjectMake(context, getClassRef(), priv);
83 }
84
85 void JSSyncServiceInfo::initialize(JSContextRef context,
86         JSObjectRef object)
87 {
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))) {
93             delete priv;
94         }
95     } else {
96         LoggerD("Private object already set.");
97     }
98 }
99
100 void JSSyncServiceInfo::finalize(JSObjectRef object)
101 {
102     SyncServiceInfoPrivateObject* priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
103     if (priv) {
104         LoggerD("Deleting JSSyncServiceInfo private object.");
105         delete priv;
106         JSObjectSetPrivate(object, NULL);
107     }
108 }
109
110 SyncServiceInfoPtr JSSyncServiceInfo::getPrivateObject(JSObjectRef object)
111 {
112     SyncServiceInfoPrivateObject *priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
113     if (!priv) {
114         ThrowMsg(NullPointerException, "Private object is null.");
115     }
116
117     SyncServiceInfoPtr syncServiceInfo = priv->getObject();
118     if (!syncServiceInfo) {
119         ThrowMsg(NullPointerException, "JSSyncServiceInfo object is null.");
120     }
121     return syncServiceInfo;
122 }
123
124 void JSSyncServiceInfo::setPrivateObject(const SyncServiceInfoPtr &syncServiceInfo,
125         JSContextRef ctx,
126         const JSObjectRef object)
127 {
128     Try
129     {
130         SyncServiceInfoPrivateObject *priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
131         delete priv;
132         priv = new SyncServiceInfoPrivateObject(ctx, syncServiceInfo);
133         if (!JSObjectSetPrivate(object, static_cast<void*>(priv))) {
134             delete priv;
135         }
136     }
137     Catch(Exception)
138     {
139         LoggerE("Error during setting JSSyncServiceInfo object.");
140     }
141 }
142
143 JSObjectRef JSSyncServiceInfo::constructor(JSContextRef context,
144     JSObjectRef constructor,
145     size_t argumentCount,
146     const JSValueRef arguments[],
147     JSValueRef* exception)
148 {
149     Try
150     {
151         DataSyncConverter converter(context);
152
153         SyncServiceInfoPtr syncServiceInfo( new SyncServiceInfo() );
154
155         if (argumentCount>=1) {
156                         syncServiceInfo->setEnable(converter.toBool(arguments[0]));
157         }
158         if (argumentCount>=2) {
159                         syncServiceInfo->setSyncServiceType(converter.toSyncServiceType(converter.toString(arguments[1])));
160         }
161         if (argumentCount>=3) {
162                         syncServiceInfo->setServerDatabaseUri(converter.toString(arguments[2]));
163         }
164         if (argumentCount>=4) {
165                         syncServiceInfo->setId(converter.toString(arguments[3]));
166         }
167         if (argumentCount>=5) {
168                         syncServiceInfo->setPassword(converter.toString(arguments[4]));
169         }
170
171         LoggerD("enable: "<<syncServiceInfo->getEnable()<<", serviceType: "<<syncServiceInfo->getSyncServiceType());
172
173         return createJSSyncServiceInfo(context, syncServiceInfo);
174     }
175     Catch(UnsupportedException)
176     {
177         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
178                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
179         *exception = error;
180                 return error;
181     }
182     Catch(InvalidArgumentException)
183     {
184         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
185                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
186         *exception = error;
187                 return error;
188     }
189     Catch(ConversionException)
190     {
191         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
192                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
193         *exception = error;
194                 return error;
195     }
196     Catch(Exception)
197     {
198         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
199                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
200         *exception = error;
201                 return error;
202     }
203 }
204
205 JSValueRef JSSyncServiceInfo::getProperty(JSContextRef context,
206         JSObjectRef object,
207         JSStringRef propertyName,
208         JSValueRef* exception)
209 {
210     Try
211     {
212         SyncServiceInfoPrivateObject* priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
213         if (!priv) {
214             ThrowMsg(NullPointerException, "Private object is NULL.");
215         }
216
217         SyncServiceInfoPtr syncServiceInfo = priv->getObject();
218
219         DataSyncConverter converter(context);
220
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);
233         } else {
234                 LoggerD("Wrong property name.");
235                 }
236     }
237     Catch(Exception)
238     {
239                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
240     }
241
242     return JSValueMakeUndefined(context);
243 }
244
245 bool JSSyncServiceInfo::setProperty(JSContextRef context,
246         JSObjectRef object,
247         JSStringRef propertyName,
248         JSValueRef value,
249         JSValueRef* exception)
250 {
251     Try
252     {
253         SyncServiceInfoPrivateObject* priv = static_cast<SyncServiceInfoPrivateObject*>(JSObjectGetPrivate(object));
254         if (!priv) {
255             ThrowMsg(NullPointerException, "Private object is NULL.");
256         }
257
258         SyncServiceInfoPtr syncServiceInfo = priv->getObject();
259
260         DataSyncConverter converter(context);
261
262         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_ENABLE)) {
263             syncServiceInfo->setEnable(converter.toBool(value));
264             return true;
265         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_SERVICE_TYPE)) {
266             syncServiceInfo->setSyncServiceType(converter.toSyncServiceType(converter.toString(value)));
267             return true;
268         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_SERVER_DATABSE_URI)) {
269             syncServiceInfo->setServerDatabaseUri(converter.toString(value));
270             return true;
271                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_ID)) {
272                         syncServiceInfo->setId(converter.toString(value));
273                         return true;
274                 } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_SERVICE_INFO_PASSWORD)) {
275                         syncServiceInfo->setPassword(converter.toString(value));
276                         return true;
277         } else {
278                 LoggerD("Wrong property name.");
279         }
280     }
281     Catch(Exception)
282     {
283                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
284     }
285
286     return true;
287 }
288
289 }
290 }