Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / Account / AccountManager.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        AccountManager.cpp
20  * @author      Jihwa Park (jh7979.park@samsung.com)
21  * @author      Sangtai Kim
22  * @version     0.1
23  */
24 #include <algorithm>
25 #include <vector>
26
27 #include <dpl/log/log.h>
28 #include <dpl/scoped_ptr.h>
29
30 #include "AccountManager.h"
31 #include "AccountWrapper.h"
32
33 #include "API/Account/OnAddEventsChanged.h"
34 #include "API/Account/OnUpdateEventsChanged.h"
35 #include "API/Account/OnDeleteEventsChanged.h"
36 #include "account.h"
37
38 #include <CommonsJavaScript/Converter.h>
39
40 using namespace TizenApis::Api::Account;
41 using namespace WrtDeviceApis::Commons;
42 using namespace WrtDeviceApis::CommonsJavaScript;
43
44 int id_list[1024] = {0, };
45 int count = 0;
46
47 bool GetAccountList(account_h handle, void* user_data)
48 {
49         int id = 0;
50         account_get_account_id(handle, &id);
51         id_list[count] = id;
52         count++;
53         return true;
54 }
55
56 namespace TizenApis {
57 namespace Platform {
58 namespace Account{
59 int AccountManager::m_instanceCount = 0;
60
61 AccountManager::AccountManager()
62 {
63     LogDebug("entered");
64
65     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
66     if (m_instanceCount == 0) {
67         LogDebug("opening account DB");
68         if (ACCOUNT_ERROR_NONE != account_connect()) {
69             ThrowMsg(PlatformException, "Account DB initialization failed");
70         }
71     }
72     m_instanceCount++;
73 }
74
75 AccountManager::~AccountManager()
76 {
77     LogDebug("entered");
78     DPL::Mutex::ScopedLock mx(&m_constructorMutex);
79     m_instanceCount--;
80     if (m_instanceCount == 0) {
81         LogDebug("closing account DB");
82         if (ACCOUNT_ERROR_NONE != account_disconnect()) {
83             LogError("Account database not clearly closed.");
84         }
85     }
86 }
87
88 //void AccountManager::OnRequestReceived(const IEventGetAccountServicesPtr &event)
89 //{
90 //    LogDebug("entered");
91 //    static std::vector<IAccountServicePtr> AccountServices;
92 //
93 //    Try
94 //    {
95 //              if (AccountServices.empty()) {
96 //                      IAccountServicePtr newAccountService(new AccountService());
97 //                      newAccountService->setName("default internet account");
98 //                      newAccountService->setId(0);
99 //                      newAccountService->setType(AccountService::TYPE_INTERNET);
100 //                      AccountServices.push_back(newAccountService);
101 //              }
102 //              if (!event->checkCancelled()) {
103 //                      std::vector<IAccountServicePtr>::const_iterator it = AccountServices.begin();
104 //                      for (; it != AccountServices.end(); ++it) {
105 //                              event->addAccountService(*it);
106 //                      }
107 //                      event->setResult(true);
108 //              }
109 //      }
110 //    Catch(Exception)
111 //    {
112 //        LogError("error occuered during obtaining data");
113 //        event->setResult(false);
114 //    }
115 //    event->setCancelAllowed(true);
116 //
117 //}
118
119
120 void AccountManager::OnRequestReceived(const IEventDeleteAccountPtr &account)
121 {
122     LogDebug("entered");
123
124     Try
125     {
126         if (!account->getEvent()) {
127             ThrowMsg(NullPointerException, "event parameter is NULL");
128         }
129
130         //TODO: check if ID is valid
131          /*
132         if (!account->getEvent()->getIdIsSet()) {
133             ThrowMsg(Commons::InvalidArgumentException,
134                      "Cannot delete non-existing event.");
135         }
136         */
137         DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
138         accountWrapper->convertAbstractAccountToPlatformAccount();
139         if (account->checkCancelled()) {
140             account->setCancelAllowed(true);
141             account->setResult(true);
142             return;
143         }
144         accountWrapper->deleteAccount();
145         account->setResult(true);
146     }
147     catch (const NotFoundException &ex)
148     {
149         LogError("event doesn't exist");
150         account->setResult(false);
151         account->setExceptionCode(ExceptionCodes::NotFoundException);
152     }
153     catch (const Exception &ex)
154     {
155         LogError("Error during deleting event " << ex.DumpToString());
156         account->setResult(false);
157     }
158     account->setCancelAllowed(false);
159 }
160
161
162 void AccountManager::OnRequestReceived(const IEventAddAccountPtr &account)
163 {
164     LogDebug("entered");
165     Try
166     {
167         if (!account->getEvent()) {
168             ThrowMsg(NullPointerException, "event parameter is NULL");
169         }
170        /* if (account->getEvent()->getIdIsSet()) {
171             LogWarning("adding event that is already added");
172             account->getEvent()->resetId();
173         }*/
174         DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
175         accountWrapper->convertAbstractAccountToPlatformAccount();
176         if (account->checkCancelled()) {
177             account->setCancelAllowed(true);
178             account->setResult(true);
179             return;
180         }
181
182         accountWrapper->setDummyServices();
183
184         accountWrapper->saveAccount();
185         account->setResult(true);
186     }
187     catch (const Exception &ex)
188     {
189         LogError("Error during adding event" << ex.DumpToString());
190         account->setResult(false);
191     }
192     account->setCancelAllowed(false);
193 }
194
195 void AccountManager::OnRequestReceived(const IEventFindAccountsPtr &event)
196 {
197         LogDebug("<<<");
198         Try{
199                 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper());
200                 if (event->checkCancelled()) {
201                         event->setCancelAllowed(true);
202                         event->setResult(true);
203                         return;
204                 }
205
206                 EventAccountListPtr accountListPtr = accountWrapper->findAccountsByFilter(event->getFilterProperty());
207                 event->setAccountLists(accountListPtr);
208                 event->setResult(true);
209         } catch (const Exception &ex) {
210                 LogError("Error during adding event" << ex.DumpToString());
211         event->setResult(false);
212         }
213         event->setCancelAllowed(false);
214 }
215
216 void AccountManager::OnRequestReceived(const IEventFindServicesPtr &event){
217         LogDebug("<<<");
218         Try{
219                 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper());
220                 if (event->checkCancelled()) {
221                         event->setCancelAllowed(true);
222                         event->setResult(true);
223                         return;
224         }
225
226                 AccountServicesArrayPtr serviceListPtr = accountWrapper->findServiceByFilter(event->getFilterProperty());
227                 event->setAccountServiceList(serviceListPtr);
228                 event->setResult(true);
229         } catch (const Exception &ex) {
230                 LogError("Error during adding event" << ex.DumpToString());
231         event->setResult(false);
232     }
233         event->setCancelAllowed(false);
234         LogDebug(">>>");
235 }
236
237
238 void AccountManager::OnRequestReceived(const IEventUpdateAccountPtr &account)
239 {
240     LogDebug("entered");
241
242     Try
243     {
244         if (!account->getEvent()) {
245             ThrowMsg(NullPointerException, "event parameter is NULL");
246         }
247   /*      if (!account->getEvent()->getIdIsSet()) {
248             ThrowMsg(
249                 Commons::InvalidArgumentException,
250                 "Cannot update non-existing event. Event needs adding or ID is wrong");
251         }*/
252         DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
253         accountWrapper->convertAbstractAccountToPlatformAccount();
254         if (account->checkCancelled()) {
255             account->setCancelAllowed(true);
256             account->setResult(true);
257             return;
258         }
259         accountWrapper->saveAccount();
260         account->setResult(true);
261     }
262     catch (const Exception &ex)
263     {
264         LogError("Error during updating event " << ex.DumpToString());
265         account->setResult(false);
266     }
267     account->setCancelAllowed(false);
268 }
269
270 void AccountManager:: OnRequestReceived(const IEventGetAccountByIdPtr &account)
271 {
272     LogDebug("entered");
273     Try
274     {
275         if (!account->getEvent()) {
276             ThrowMsg(NullPointerException, "event parameter is NULL");
277         }
278         DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
279         accountWrapper->getAccountbyId();
280         if (account->checkCancelled()) {
281             account->setCancelAllowed(true);
282             account->setResult(true);
283             return;
284         }
285                 account->setEvent(accountWrapper->getAbstractAccount());
286         account->setResult(true);
287     }
288     catch (const Exception &ex)
289     {
290         LogError("Error during updating event " << ex.DumpToString());
291         account->setResult(false);
292     }
293     account->setCancelAllowed(false);
294 }
295
296 void AccountManager:: OnRequestReceived(const IEventGetServiceTypeByIdPtr &eventGetServicetypeById)
297 {
298         LogDebug("<<<");
299
300         Try{
301                 if (!eventGetServicetypeById->getEvent()) {
302             ThrowMsg(NullPointerException, "event parameter is NULL");
303         }
304
305                 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(eventGetServicetypeById->getEvent()));
306                 if (eventGetServicetypeById->checkCancelled()) {
307                         eventGetServicetypeById->setCancelAllowed(true);
308                         eventGetServicetypeById->setResult(true);
309             return;
310         }
311                 eventGetServicetypeById->setEvent(accountWrapper->getAbstractAccount());
312                 eventGetServicetypeById->setAccountServiceTypeProperty(accountWrapper->getAccountServiceTypebyId());
313
314                 eventGetServicetypeById->setResult(true);
315         } catch (const Exception &ex) {
316         LogError("Error during updating event " << ex.DumpToString());
317                 eventGetServicetypeById->setResult(false);
318     }
319         eventGetServicetypeById->setCancelAllowed(false);
320 }
321
322 void AccountManager:: OnRequestReceived(const IEventGetProviderByIdPtr &event)
323 {
324         LogDebug("<<<");
325         Try{
326                 if (!event->getEvent()) {
327             ThrowMsg(NullPointerException, "event parameter is NULL");
328         }
329                 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(event->getEvent()));
330                 if (event->checkCancelled()) {
331                         event->setCancelAllowed(true);
332                         event->setResult(true);
333             return;
334         }
335
336                 event->setAccountServiceProviderProperty(accountWrapper->getAccountServiceProviderProperty());
337                 event->setResult(true);
338         }catch (const Exception &ex)    {
339         LogError("Error during updating event " << ex.DumpToString());
340                 event->setResult(false);
341     }
342         event->setCancelAllowed(false);
343         LogDebug(">>>");
344 }
345
346 void AccountManager:: OnRequestReceived(const IEventFindProvidersPtr &eventFindProvidersPtr)
347 {
348         LogDebug("<<<");
349         Try{
350                 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper());
351                 AccountServiceProviderPropertyArrayPtr serviceProviderPropertiesPtr = accountWrapper->findProviders(eventFindProvidersPtr->getServiceTypeId());
352
353                 if (eventFindProvidersPtr->checkCancelled()) {
354                         eventFindProvidersPtr->setCancelAllowed(true);
355                         LogDebug(">>>");
356                         return;
357                 }
358
359                 eventFindProvidersPtr->setServiceProviderProperties(serviceProviderPropertiesPtr);
360         }catch (const Exception &ex){
361                 LogError("Error during updating event " << ex.DumpToString());
362                 eventFindProvidersPtr->setResult(false);
363         }
364         eventFindProvidersPtr->setResult(true);
365         eventFindProvidersPtr->setCancelAllowed(false);
366         LogDebug(">>>");
367 }
368
369 void AccountManager::OnRequestReceived(const IEventFindServiceTypesPtr &event)
370 {
371         LogDebug("<<<");
372     EventAccountPtr m_event;
373
374         AccountWrapperPtr accountWrapper(new AccountWrapper());
375
376         event->setServiceTypeProperties(accountWrapper->findServiceTypes());
377         event->setResult(true);
378         LogDebug(">>>");
379 }
380
381 void AccountManager::OnRequestReceived(const IEventGetServiceByNamePtr &event){
382         LogDebug("<<< DUMMY IMPLEMENTATION");
383
384         std::string serviceName = event->getServiceName();
385         LogDebug("serviceName:[" << serviceName << "]");
386
387         //dummy implementation
388         AccountServicesPtr dummyServices1(new AccountServices());
389         dummyServices1->setProviderId("dummyProviderId");
390         dummyServices1->setServiceTypeId("dummyServiceTypeId");
391         dummyServices1->setDisplayName("dummyDisplayName");
392         dummyServices1->setName("dummyName");
393         dummyServices1->setId("dummyServiceId");
394
395         event->setAccountService(dummyServices1);
396
397         event->setResult(true);
398
399         LogDebug(">>>");
400 }
401
402 void AccountManager::OnRequestReceived(const IEventGetServiceByIdPtr &event){
403         LogDebug("<<< DUMMY IMPLEMENTATION");
404
405         std::string serviceId = event->getServiceId();
406         LogDebug("serviceId:[" << serviceId << "]");
407
408         //dummy implementation
409         AccountServicesPtr dummyServices1(new AccountServices());
410         dummyServices1->setProviderId("dummyProviderId");
411         dummyServices1->setServiceTypeId("dummyServiceTypeId");
412         dummyServices1->setDisplayName("dummyDisplayName");
413         dummyServices1->setName("dummyName");
414         dummyServices1->setId("dummyServiceId");
415
416         event->setAccountService(dummyServices1);
417
418         event->setResult(true);
419
420         LogDebug(">>>");
421 }
422
423 }
424 }
425 }