4f6b8fc43c7678e2e6c3e380f8111d816dabf7af
[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::toJSValueRefAccountService(const IAccountServicePtr& arg)
104 {
105     LogDebug("entered");
106     AccountServicePrivObject *priv = new AccountServicePrivObject(m_context, arg);
107     return JSObjectMake(m_context, JSAccountService::getClassRef(), priv);
108 }
109
110 JSValueRef AccountConverter::toJSValueRef(const std::vector<IAccountServicePtr> &arg)
111 {
112     LogDebug("entered");
113     return toJSValueRef_(arg, &AccountConverter::toJSValueRefAccountService, this);
114 }
115
116
117 JSValueRef AccountConverter::toFunctionOrNull(const JSValueRef& arg)
118 {
119     LogDebug("entered");
120     if (Validator(m_context).isCallback(arg)) {
121         return arg;
122     } else if (!JSValueIsNull(m_context,
123                               arg) && !JSValueIsUndefined(m_context, arg)) {
124         ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
125     }
126     return NULL;
127 }
128
129 JSValueRef AccountConverter::toFunction(const JSValueRef& arg)
130 {
131     LogDebug("entered");
132     if (Validator(m_context).isCallback(arg)) {
133         return arg;
134     } else if (JSValueIsNull(m_context,
135                              arg) || JSValueIsUndefined(m_context, arg)) {
136         ThrowMsg(ConversionException, "JS null passed as function.");
137     }
138     ThrowMsg(InvalidArgumentException, "Not a function nor JS null.");
139 }
140
141 AccountServicesPtr AccountConverter::toService(const JSValueRef jsValue)
142 {
143         const ScopedJSStringRef serviceNameStr(JSStringCreateWithUTF8CString(ACCOUNT_SERVICES_NAME));
144         const ScopedJSStringRef serviceTypeStr(JSStringCreateWithUTF8CString(ACCOUNT_SERVICES_TYPE));
145
146         JSObjectRef jsObject = toJSObjectRef(jsValue);
147
148         JSValueRef serviceNameData = JSObjectGetProperty(m_context, jsObject, serviceNameStr.get(), NULL);
149         JSValueRef serviceTypeData = JSObjectGetProperty(m_context, jsObject, serviceTypeStr.get(), NULL);
150
151         std::string serviceName;
152         std::string serviceTypeId;
153
154         AccountServicesPtr result = AccountServicesPtr(new AccountServices());
155         if (!result) {
156                 Throw(ConversionException);
157         }
158
159         if (!JSValueIsUndefined(m_context, serviceNameData)) {
160                 serviceName = toString(serviceNameData);
161                 result->setName(serviceName);
162         }
163
164         if (!JSValueIsUndefined(m_context, serviceTypeData)) {
165                 serviceTypeId = toString(serviceTypeData);
166                 result->setServiceTypeId(serviceTypeId);
167         }
168
169         return result;
170 }
171
172 AccountServicesArrayPtr AccountConverter::toAccountService(const JSValueRef jsValue)
173 {
174     AccountServicesArrayPtr result(new AccountServicesArray());
175
176     std::vector<AccountServicesPtr> resultVector;
177     JSObjectRef objArg = toJSObjectRef(jsValue);
178     LogDebug("array length "<<JSGetArrayLength(m_context, objArg));
179     for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
180         JSValueRef element = JSGetArrayElement(m_context, objArg, i);
181         resultVector.push_back(toService(element));
182     }
183
184     *result = resultVector;
185     return result;
186 }
187
188 void AccountConverter::setProvider(const JSValueRef jsValue)
189 {
190     LogDebug("entered");
191 /*
192     const ScopedJSStringRef providerStr(JSStringCreateWithUTF8CString("providerId"));
193     JSObjectRef arg = toJSObjectRef(jsValue);
194     JSValueRef providerData = JSObjectGetProperty(m_context, arg, providerStr.get(), NULL);
195
196     if (!JSValueIsUndefined(m_context, providerData)) {
197         m_provider = toString(providerData);
198             LogDebug("m_provider : " << m_provider);
199     }
200 */
201     m_provider = toString(jsValue);
202 }
203
204 std::string AccountConverter::convertTostring(const JSValueRef jsValue)
205 {
206         std::string string;
207     LogDebug("entered");
208     string = toString(jsValue);
209         return string;
210 }
211
212 EventAccountPtr AccountConverter::toAccount(const std::string& accountServiceProviderId){
213         LogDebug("<<<");
214         EventAccountPtr result(new EventAccount());
215
216         const ScopedJSStringRef displayNameStr(JSStringCreateWithUTF8CString("displayName"));
217         const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString("iconPath"));
218
219         //TODO set displayName
220         //TODO set iconPath
221 //      JSValueRef displayNameData = JSObjectGetProperty(m_context, arg, displayNameStr.get(), NULL);
222 //      JSValueRef iconPathData = JSObjectGetProperty(m_context, arg, iconPathStr.get(), NULL);
223
224 //      if (!JSValueIsUndefined(m_context, displayNameData)) {
225 //              result->setDisplayName(toString(displayNameData));
226 //      }
227
228 //      if (!JSValueIsUndefined(m_context, iconPathData)) {
229 //              result->setIconPath(toString(iconPathData));
230 //      }
231
232         result->setProviderName(accountServiceProviderId);
233
234         return result;
235 }
236
237
238 EventAccountPtr AccountConverter::toAccount(const JSValueRef account)
239 {
240     LogDebug("<<<");
241     EventAccountPtr result(new EventAccount());
242
243     const ScopedJSStringRef displayNameStr(JSStringCreateWithUTF8CString("displayName"));
244     const ScopedJSStringRef iconPathStr(JSStringCreateWithUTF8CString("iconPath"));
245
246     JSObjectRef arg = toJSObjectRef(account);
247
248     JSValueRef displayNameData = JSObjectGetProperty(m_context, arg, displayNameStr.get(), NULL);
249     JSValueRef iconPathData = JSObjectGetProperty(m_context, arg, iconPathStr.get(), NULL);
250
251     if (!JSValueIsUndefined(m_context, displayNameData)) {
252         result->setDisplayName(toString(displayNameData));
253     }
254
255     if (!JSValueIsUndefined(m_context, iconPathData)) {
256         result->setIconPath(toString(iconPathData));
257     }
258
259     result->setProviderName(m_provider);
260 /*
261     if (!JSValueIsUndefined(m_context, providerNameData)) {
262         result->setProviderName(toString(providerNameData));
263     }
264
265     if (!JSValueIsUndefined(m_context, emailAddressData)) {
266         result->setEmailAddress(toString(emailAddressData));
267     }
268
269     if (!JSValueIsUndefined(m_context, domainNameData)) {
270         result->setDomainName(toString(domainNameData));
271     }
272
273         if (!JSValueIsUndefined(m_context, serviceData)) {
274                 AccountServicesArrayPtr m_service = toAccountService(serviceData);
275                 result->setService(m_service);
276         }
277 */
278     return result;
279 }
280
281 void AccountConverter::toEventFilterConvertId(
282         const AccountFilterPtr &result,
283         const JSValueRef &filters)
284 {
285     const ScopedJSStringRef idStr(JSStringCreateWithUTF8CString("id"));
286     JSObjectRef filterObj = toJSObjectRef(filters);
287     if (JSObjectHasProperty(m_context, filterObj, idStr.get())) {
288         LogDebug("converting id");
289         JSValueRef value = JSObjectGetProperty(m_context, filterObj, idStr.get(), NULL);
290         result->setIdFilter(toString(value));
291     }
292 }
293
294 AccountFilterPtr AccountConverter::toAccountFilter(const JSValueRef &filters)
295 {
296     LogDebug("entered");
297     AccountFilterPtr result(new AccountFilter());
298     Validator validator(m_context);
299     if (!JSValueIsObject(m_context, filters)) {
300         LogError("Wrong filter parameter");
301         Throw(InvalidArgumentException);
302     }
303     if (!validator.checkArrayKeys(m_allowedAttributes, filters)) {
304         LogError("Wrong filter's attribue");
305         Throw(ConversionException);
306     }
307
308     toEventFilterConvertId(result, filters);
309     return result;
310 }
311
312 EventAccountListPtr AccountConverter::toVectorOfAccounts(JSValueRef events)
313 {
314     LogDebug("entered");
315     EventAccountListPtr result(new EventAccountList());
316
317     std::vector<EventAccountPtr> resultVector;
318     JSObjectRef objArg = toJSObjectRef(events);
319     LogDebug("array length " + JSGetArrayLength(m_context, objArg));
320     for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); i++) {
321         JSValueRef element = JSGetArrayElement(m_context, objArg, i);
322         resultVector.push_back(toAccount(element));
323     }
324
325     *result = resultVector;
326     return result;
327 }
328
329 }
330 }
331 }