update for beta universally
[profile/ivi/wrt-plugins-tizen.git] / src / standards / Tizen / Account / AccountConverter.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 /*
19  * @file        EventFilterConverter.cpp
20  * @author      Jihwa Park (jh7979.park@samsung.com)
21  * @author      Sangtai Kim
22  * @version     0.1
23  */
24
25 #include <dpl/log/log.h>
26 #include <CommonsJavaScript/ScopedJSStringRef.h>
27 #include <CommonsJavaScript/Validator.h>
28 #include <CommonsJavaScript/JSUtils.h>
29 #include <Commons/RegexUtils.h>
30 #include "JSAccount.h"
31 #include "AccountConverter.h"
32 //#include "JSAccountService.h"
33 #include "JSAccountServiceType.h"
34 #include "JSAccountManager.h"
35 #include "JSFeatureArray.h"
36 #include "JSAccountServices.h"
37 #include "JSAccountServiceProvider.h"
38
39 #define ACCOUNT_SERVICES_NAME "serviceName"
40 #define ACCOUNT_SERVICES_TYPE "serviceType"
41
42 using namespace TizenApis::Api::Account;
43 using namespace WrtDeviceApis::Commons;
44 using namespace WrtDeviceApis::CommonsJavaScript;
45
46 namespace {
47 const int DEFAULT_EVENT_INTERVAL = 1;
48 }
49
50 namespace TizenApis {
51 namespace Tizen1_0 {
52 namespace Account{
53
54 std::vector<std::string> AccountConverter::m_allowedAttributes;
55 std::vector<std::string> AccountConverter::m_allowedCreateProperties;
56
57 AccountConverter::AccountConverter(JSContextRef context) : Converter(context)
58 {
59     LogDebug("entered");
60     if (m_allowedAttributes.size() == 0) {
61         m_allowedAttributes.push_back("id");
62         m_allowedAttributes.push_back("iconPath");
63     }
64     if (m_allowedCreateProperties.size() == 0) {
65                 m_allowedCreateProperties.push_back("id");
66                 m_allowedCreateProperties.push_back("iconPath");
67     }
68 }
69
70 AccountConverter::~AccountConverter()
71 {
72
73 }
74
75 JSValueRef AccountConverter::toJSValueRefAccount(const EventAccountPtr& arg)
76 {
77     LogDebug("<<<");
78     return JSAccount::createJSAccount(m_context, arg);
79 }
80
81 JSValueRef AccountConverter::toJSValueRefAccountServiceType(const AccountServiceTypePropertyPtr& arg)
82 {
83     LogDebug("<<<");
84     return JSAccountServiceType::createJSObject(m_context, arg);
85 }
86
87 JSValueRef AccountConverter::toJSValueRefAccountServiceProvider(const AccountServiceProviderPropertyPtr& arg)
88 {
89         LogDebug("<<<");
90         return JSAccountServiceProvider::createJSObject(m_context, arg);
91 }
92
93
94
95
96 JSValueRef AccountConverter::toJSValueRef(
97         const std::vector<EventAccountPtr> &arg)
98 {
99     LogDebug("entered");
100     return toJSValueRef_(arg, &AccountConverter::toJSValueRefAccount, this);
101 }
102
103 JSValueRef AccountConverter::toJSValueRef(const EventAccountListPtr &arg){
104         JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
105         if(arg){
106                 if (NULL == jsResult) {
107                         ThrowMsg(NullPointerException, "Could not create js array object");
108                 }
109
110 //              for(unsigned int i = 0; i < arg->size(); i++) {
111 //                      if (!JSSetArrayElement(m_context, jsResult, i, JSAccountServiceType::createJSObject(m_context, arg->at(i)))) {
112 //                              ThrowMsg(UnknownException, "Could not insert value into js array");
113 //                      }
114 //              }// for
115
116         }
117                 LogDebug(">>>");
118                 return jsResult;
119 }
120
121 JSValueRef AccountConverter::toFunctionOrNull(const JSValueRef& arg)
122 {
123     LogDebug("entered");
124     if (Validator(m_context).isCallback(arg)) {
125         return arg;
126     } else if (!JSValueIsNull(m_context,
127                               arg) && !JSValueIsUndefined(m_context, arg)) {
128         ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
129     }
130     return NULL;
131 }
132
133 JSValueRef AccountConverter::toFunction(const JSValueRef& arg)
134 {
135     LogDebug("entered");
136     if (Validator(m_context).isCallback(arg)) {
137         return arg;
138     } else if (JSValueIsNull(m_context,
139                              arg) || JSValueIsUndefined(m_context, arg)) {
140         ThrowMsg(ConversionException, "JS null passed as function.");
141     }
142     ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
143 }
144
145 AccountServicesPtr AccountConverter::toService(const JSValueRef jsValue)
146 {
147         const ScopedJSStringRef serviceNameStr(JSStringCreateWithUTF8CString(ACCOUNT_SERVICES_NAME));
148         const ScopedJSStringRef serviceTypeStr(JSStringCreateWithUTF8CString(ACCOUNT_SERVICES_TYPE));
149
150         JSObjectRef jsObject = toJSObjectRef(jsValue);
151
152         JSValueRef serviceNameData = JSObjectGetProperty(m_context, jsObject, serviceNameStr.get(), NULL);
153         JSValueRef serviceTypeData = JSObjectGetProperty(m_context, jsObject, serviceTypeStr.get(), NULL);
154
155         std::string serviceName;
156         std::string serviceTypeId;
157
158         AccountServicesPtr result = AccountServicesPtr(new AccountServices());
159         if (!result) {
160                 Throw(ConversionException);
161         }
162
163         if (!JSValueIsUndefined(m_context, serviceNameData)) {
164                 serviceName = toString(serviceNameData);
165                 result->setName(serviceName);
166         }
167
168         if (!JSValueIsUndefined(m_context, serviceTypeData)) {
169                 serviceTypeId = toString(serviceTypeData);
170                 result->setServiceTypeId(serviceTypeId);
171         }
172
173         return result;
174 }
175
176 AccountServicesArrayPtr AccountConverter::toAccountServiceArray(const JSValueRef jsValue)
177 {
178         AccountServicesArrayPtr result(new AccountServicesArray());
179
180         std::vector<AccountServicesPtr> resultVector;
181         JSObjectRef objArg = toJSObjectRef(jsValue);
182         LogDebug("array length "<<JSGetArrayLength(m_context, objArg));
183         for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
184                 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
185                 resultVector.push_back(toService(element));
186         }
187
188         *result = resultVector;
189         return result;
190 }
191
192 std::vector<std::string> AccountConverter::toTags(const JSValueRef jsValue){
193         std::vector<std::string> tags;
194
195         JSObjectRef objArg = toJSObjectRef(jsValue);
196         LogDebug("array length:" << JSGetArrayLength(m_context, objArg));
197
198         for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
199                 JSValueRef element = JSGetArrayElement(m_context, objArg, i);
200                 tags.push_back(toString(element));
201         }
202
203         return tags;
204 }
205
206
207 AccountServiceFilterPropertyPtr AccountConverter::toAccountServiceFilterProperty(const JSValueRef jsValue){
208         AccountServiceFilterPropertyPtr filterPropertyPtr(new AccountServiceFilterProperty());
209
210         JSValueRef serviceTypeIdRef = JSUtils::getJSProperty(m_context, jsValue, "serviceTypeId");
211         if(serviceTypeIdRef!=NULL){
212                 std::string serviceType = toString(serviceTypeIdRef);
213                 LogDebug("serviceType:[" << serviceType << "]");
214                 filterPropertyPtr->setServiceTypeId(serviceType);
215         }else{
216                 AccountLogWarning("serviceTypeIdRef is NULL");
217         }
218
219         JSValueRef tagsRef = JSUtils::getJSProperty(m_context, jsValue, "tags");
220         if(tagsRef != NULL){
221                 std::vector<std::string> tags = toTags(tagsRef);
222                 LogDebug("tags length:" << tags.size());
223                 filterPropertyPtr->setTags(tags);
224         }else{
225                 AccountLogWarning("tagsRef is NULL");
226         }
227
228         JSValueRef providerRef = JSUtils::getJSProperty(m_context, jsValue, "provider");
229         if(providerRef != NULL){
230                 std::string provider = toString(providerRef);
231                 LogDebug("provider:[" << provider << "]");
232                 filterPropertyPtr->setProvider(provider);
233         }else{
234                 AccountLogWarning("providerRef is NULL");
235         }
236
237         return filterPropertyPtr;
238 }
239
240 void AccountConverter::setProvider(const JSValueRef jsValue)
241 {
242     LogDebug("entered");
243 /*
244     const ScopedJSStringRef providerStr(JSStringCreateWithUTF8CString("providerId"));
245     JSObjectRef arg = toJSObjectRef(jsValue);
246     JSValueRef providerData = JSObjectGetProperty(m_context, arg, providerStr.get(), NULL);
247
248     if (!JSValueIsUndefined(m_context, providerData)) {
249         m_provider = toString(providerData);
250             LogDebug("m_provider : " << m_provider);
251     }
252 */
253     m_provider = toString(jsValue);
254 }
255
256 std::string AccountConverter::convertTostring(const JSValueRef jsValue)
257 {
258         std::string string;
259     LogDebug("entered");
260     string = toString(jsValue);
261         return string;
262 }
263
264 EventAccountPtr AccountConverter::toAccount(const std::string& accountServiceProviderId){
265         LogDebug("<<<");
266         EventAccountPtr result(new EventAccount());
267
268         const ScopedJSStringRef displayNameStr(JSStringCreateWithUTF8CString("displayName"));
269         const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString("iconPath"));
270
271         //TODO set displayName
272         //TODO set iconPath
273 //      JSValueRef displayNameData = JSObjectGetProperty(m_context, arg, displayNameStr.get(), NULL);
274 //      JSValueRef iconPathData = JSObjectGetProperty(m_context, arg, iconPathStr.get(), NULL);
275
276 //      if (!JSValueIsUndefined(m_context, displayNameData)) {
277 //              result->setDisplayName(toString(displayNameData));
278 //      }
279
280 //      if (!JSValueIsUndefined(m_context, iconPathData)) {
281 //              result->setIconPath(toString(iconPathData));
282 //      }
283
284         result->setProviderName(accountServiceProviderId);
285
286         return result;
287 }
288
289
290 EventAccountPtr AccountConverter::toAccount(const JSValueRef account)
291 {
292         LogDebug("<<<");
293         EventAccountPtr result(new EventAccount());
294
295         const ScopedJSStringRef displayNameStr(JSStringCreateWithUTF8CString("displayName"));
296         const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString("iconPath"));
297         const ScopedJSStringRef providerIdStr(JSStringCreateWithUTF8CString("providerId"));
298
299         JSObjectRef arg = toJSObjectRef(account);
300
301         JSValueRef displayNameData = JSObjectGetProperty(m_context, arg, displayNameStr.get(), NULL);
302         JSValueRef iconPathData    = JSObjectGetProperty(m_context, arg, iconPathStr.get(),    NULL);
303         JSValueRef providerIdData  = JSObjectGetProperty(m_context, arg, providerIdStr.get(),  NULL);
304
305         if (!JSValueIsUndefined(m_context, displayNameData)) {
306                 result->setDisplayName(toString(displayNameData));
307         }
308
309         if (!JSValueIsUndefined(m_context, iconPathData)) {
310                 result->setIconPath(toString(iconPathData));
311         }
312
313         if (!JSValueIsUndefined(m_context, providerIdData)) {
314                 result->setProviderId(toString(providerIdData));
315         }
316
317         return result;
318 }
319
320 void AccountConverter::toEventFilterConvertId(
321         const AccountFilterPtr &result,
322         const JSValueRef &filters)
323 {
324     const ScopedJSStringRef idStr(JSStringCreateWithUTF8CString("id"));
325     JSObjectRef filterObj = toJSObjectRef(filters);
326     if (JSObjectHasProperty(m_context, filterObj, idStr.get())) {
327         LogDebug("converting id");
328         JSValueRef value = JSObjectGetProperty(m_context, filterObj, idStr.get(), NULL);
329         result->setIdFilter(toString(value));
330     }
331 }
332
333 AccountFilterPtr AccountConverter::toAccountFilter(const JSValueRef &filters)
334 {
335     LogDebug("entered");
336     AccountFilterPtr result(new AccountFilter());
337     Validator validator(m_context);
338     if (!JSValueIsObject(m_context, filters)) {
339         LogError("Wrong filter parameter");
340         Throw(InvalidArgumentException);
341     }
342     if (!validator.checkArrayKeys(m_allowedAttributes, filters)) {
343         LogError("Wrong filter's attribue");
344         Throw(ConversionException);
345     }
346
347     toEventFilterConvertId(result, filters);
348     return result;
349 }
350
351 EventAccountListPtr AccountConverter::toVectorOfAccounts(JSValueRef events)
352 {
353     LogDebug("entered");
354     EventAccountListPtr result(new EventAccountList());
355
356     std::vector<EventAccountPtr> resultVector;
357     JSObjectRef objArg = toJSObjectRef(events);
358     LogDebug("array length " + JSGetArrayLength(m_context, objArg));
359     for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
360         JSValueRef element = JSGetArrayElement(m_context, objArg, i);
361         resultVector.push_back(toAccount(element));
362     }
363
364     *result = resultVector;
365     return result;
366 }
367
368 JSValueRef AccountConverter::toJSONString(const std::string& arg){
369         return JSValueMakeFromJSONString(m_context, toJSStringRef(arg));
370 }
371
372 }
373 }
374 }