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