f0920a5b1960e027dece7389d32b802460751e1f
[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     NULL, //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()<<", serviceInfo.length: "<<syncProfileInfo->getServiceInfo()->size());
175
176         return createJSSyncProfileInfo(context, syncProfileInfo);
177     }
178     Catch(UnsupportedException)
179     {
180         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
181                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::NOT_SUPPORTED_ERROR, _rethrown_exception.GetMessage());
182         *exception = error;
183                 return error;
184     }
185     Catch(InvalidArgumentException)
186     {
187         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
188                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::INVALID_VALUES_ERROR, _rethrown_exception.GetMessage());
189         *exception = error;
190                 return error;
191     }
192     Catch(ConversionException)
193     {
194         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
195                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::TYPE_MISMATCH_ERROR, _rethrown_exception.GetMessage());
196         *exception = error;
197                 return error;
198     }
199     Catch(Exception)
200     {
201         LoggerW("Exception: "<<_rethrown_exception.GetMessage());
202                 JSObjectRef error = JSTizenExceptionFactory::makeErrorObject(context, JSTizenException::UNKNOWN_ERROR, _rethrown_exception.GetMessage());
203         *exception = error;
204                 return error;
205     }
206 }
207
208 JSValueRef JSSyncProfileInfo::getProperty(JSContextRef context,
209         JSObjectRef object,
210         JSStringRef propertyName,
211         JSValueRef* exception)
212 {
213     Try
214     {
215         SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
216         if (!priv) {
217             ThrowMsg(NullPointerException, "Private object is NULL.");
218         }
219
220         SyncProfileInfoPtr syncProfileInfo = priv->getObject();
221
222         DataSyncConverter converter(context);
223
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());
232         } else {
233                 LoggerD("Wrong property name.");
234                 }
235     }
236     Catch(Exception)
237     {
238                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
239     }
240
241     return JSValueMakeUndefined(context);
242 }
243
244 bool JSSyncProfileInfo::setProperty(JSContextRef context,
245         JSObjectRef object,
246         JSStringRef propertyName,
247         JSValueRef value,
248         JSValueRef* exception)
249 {
250     Try
251     {
252         SyncProfileInfoPrivateObject* priv = static_cast<SyncProfileInfoPrivateObject*>(JSObjectGetPrivate(object));
253         if (!priv) {
254             ThrowMsg(NullPointerException, "Private object is NULL.");
255         }
256
257         SyncProfileInfoPtr syncProfileInfo = priv->getObject();
258
259         DataSyncConverter converter(context);
260
261         if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_PROFILE_NAME)) {
262             syncProfileInfo->setProfileName(converter.toString(value));
263             return true;
264         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SYNC_INFO)) {
265                         syncProfileInfo->setSyncInfo(JSSyncInfo::getPrivateObject(JSValueToObject(context, value, exception)));
266             return true;
267         } else if (JSStringIsEqualToUTF8CString(propertyName, TIZEN_SYNC_PROFILE_INFO_SERVICE_INFO)) {
268             syncProfileInfo->setServiceInfo(converter.toServiceInfoList(value));
269             return true;
270         } else {
271                 LoggerD("Wrong property name.");
272         }
273     }
274     Catch(Exception)
275     {
276                 LoggerW("Exception: "<<_rethrown_exception.GetMessage());
277     }
278
279     return true;
280 }
281
282 }
283 }