2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file AccountManager.cpp
20 * @author Jihwa Park (jh7979.park@samsung.com)
27 #include <dpl/log/log.h>
28 #include <dpl/scoped_ptr.h>
30 #include "AccountManager.h"
32 #include "API/Account/OnAddEventsChanged.h"
33 #include "API/Account/OnUpdateEventsChanged.h"
34 #include "API/Account/OnDeleteEventsChanged.h"
37 #include <CommonsJavaScript/Converter.h>
39 using namespace TizenApis::Api::Account;
40 using namespace WrtDeviceApis::Commons;
41 using namespace WrtDeviceApis::CommonsJavaScript;
43 int id_list[1024] = {0, };
46 bool GetAccountList(account_h handle, void* user_data)
49 account_get_account_id(handle, &id);
58 int AccountManager::m_instanceCount = 0;
60 AccountManager::AccountManager()
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");
74 AccountManager::~AccountManager()
77 DPL::Mutex::ScopedLock mx(&m_constructorMutex);
79 if (m_instanceCount == 0) {
80 LogDebug("closing account DB");
81 if (ACCOUNT_ERROR_NONE != account_disconnect()) {
82 LogError("Account database not clearly closed.");
87 void AccountManager::OnRequestReceived(const IEventGetAccountServicesPtr &event)
90 static std::vector<IAccountServicePtr> AccountServices;
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);
101 if (!event->checkCancelled()) {
102 std::vector<IAccountServicePtr>::const_iterator it = AccountServices.begin();
103 for (; it != AccountServices.end(); ++it) {
104 event->addAccountService(*it);
106 event->setResult(true);
111 LogError("error occuered during obtaining data");
112 event->setResult(false);
114 event->setCancelAllowed(true);
119 void AccountManager::OnRequestReceived(const IEventDeleteAccountPtr &account)
125 if (!account->getEvent()) {
126 ThrowMsg(NullPointerException, "event parameter is NULL");
129 //TODO: check if ID is valid
131 if (!account->getEvent()->getIdIsSet()) {
132 ThrowMsg(Commons::InvalidArgumentException,
133 "Cannot delete non-existing event.");
136 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
137 accountWrapper->convertAbstractAccountToPlatformAccount();
138 if (account->checkCancelled()) {
139 account->setCancelAllowed(true);
140 account->setResult(true);
143 accountWrapper->deleteAccount();
144 account->setResult(true);
146 catch (const NotFoundException &ex)
148 LogError("event doesn't exist");
149 account->setResult(false);
150 account->setExceptionCode(ExceptionCodes::NotFoundException);
152 catch (const Exception &ex)
154 LogError("Error during deleting event " << ex.DumpToString());
155 account->setResult(false);
157 account->setCancelAllowed(false);
161 void AccountManager::OnRequestReceived(const IEventAddAccountPtr &account)
166 if (!account->getEvent()) {
167 ThrowMsg(NullPointerException, "event parameter is NULL");
169 /* if (account->getEvent()->getIdIsSet()) {
170 LogWarning("adding event that is already added");
171 account->getEvent()->resetId();
173 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
174 accountWrapper->convertAbstractAccountToPlatformAccount();
175 if (account->checkCancelled()) {
176 account->setCancelAllowed(true);
177 account->setResult(true);
181 accountWrapper->setDummyServices();
183 accountWrapper->saveAccount();
184 account->setResult(true);
186 catch (const Exception &ex)
188 LogError("Error during adding event" << ex.DumpToString());
189 account->setResult(false);
191 account->setCancelAllowed(false);
194 void AccountManager::OnRequestReceived(const IEventFindAccountsPtr &event)
197 const AccountFilterPtr &filter = event->getFilter();
200 memset(id_list, 0x00, 1024);
203 if(filter->getIdIsSet()) {
204 std::istringstream istream(filter->getIdFilter());
207 LogDebug("id : " << id);
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++)
217 event->tryCancelled();
218 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper());
219 accountWrapper->loadAccount(id_list[i]);
220 event->addEvent(accountWrapper->getAbstractAccount());
223 int error = account_foreach_account_from_db(GetAccountList, NULL);
224 if ((ACCOUNT_ERROR_NONE == error) || (ACCOUNT_ERROR_RECORD_NOT_FOUND == error))
228 ThrowMsg(PlatformException, "Can't get all records");
232 for(i = 0; i < count; i++)
234 event->tryCancelled();
235 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper());
236 accountWrapper->loadAccount(id_list[i]);
237 event->addEvent(accountWrapper->getAbstractAccount());
239 event->setResult(true);
241 catch (const Exception &ex)
243 LogError("Exception: " << ex.DumpToString());
244 event->setResult(false);
246 event->setCancelAllowed(true);
250 void AccountManager::OnRequestReceived(const IEventUpdateAccountPtr &account)
256 if (!account->getEvent()) {
257 ThrowMsg(NullPointerException, "event parameter is NULL");
259 /* if (!account->getEvent()->getIdIsSet()) {
261 Commons::InvalidArgumentException,
262 "Cannot update non-existing event. Event needs adding or ID is wrong");
264 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
265 accountWrapper->convertAbstractAccountToPlatformAccount();
266 if (account->checkCancelled()) {
267 account->setCancelAllowed(true);
268 account->setResult(true);
271 accountWrapper->saveAccount();
272 account->setResult(true);
274 catch (const Exception &ex)
276 LogError("Error during updating event " << ex.DumpToString());
277 account->setResult(false);
279 account->setCancelAllowed(false);
282 void AccountManager:: OnRequestReceived(const IEventGetAccountByIdPtr &account)
287 if (!account->getEvent()) {
288 ThrowMsg(NullPointerException, "event parameter is NULL");
290 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
291 accountWrapper->getAccountbyId();
292 if (account->checkCancelled()) {
293 account->setCancelAllowed(true);
294 account->setResult(true);
297 account->setEvent(accountWrapper->getAbstractAccount());
298 account->setResult(true);
300 catch (const Exception &ex)
302 LogError("Error during updating event " << ex.DumpToString());
303 account->setResult(false);
305 account->setCancelAllowed(false);
308 void AccountManager:: OnRequestReceived(const IEventGetServiceTypeByIdPtr &eventGetServicetypeById)
313 if (!eventGetServicetypeById->getEvent()) {
314 ThrowMsg(NullPointerException, "event parameter is NULL");
317 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(eventGetServicetypeById->getEvent()));
318 accountWrapper->getServiceTypebyId();
319 if (eventGetServicetypeById->checkCancelled()) {
320 eventGetServicetypeById->setCancelAllowed(true);
321 eventGetServicetypeById->setResult(true);
324 eventGetServicetypeById->setEvent(accountWrapper->getAbstractAccount());
326 //TODO set real value sangtai.kim
327 eventGetServicetypeById->setAccountServiceTypeProperty(accountWrapper->getDummyAccountServiceType());
329 eventGetServicetypeById->setResult(true);
330 } catch (const Exception &ex) {
331 LogError("Error during updating event " << ex.DumpToString());
332 eventGetServicetypeById->setResult(false);
334 eventGetServicetypeById->setCancelAllowed(false);
337 void AccountManager:: OnRequestReceived(const IEventGetProviderByIdPtr &event)
341 if (!event->getEvent()) {
342 ThrowMsg(NullPointerException, "event parameter is NULL");
344 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(event->getEvent()));
345 if (event->checkCancelled()) {
346 event->setCancelAllowed(true);
347 event->setResult(true);
351 //TODO set real value sangtai.kim
352 event->setAccountServiceProviderProperty(accountWrapper->getDummyAccountServiceProviderProperty());
354 event->setResult(true);
355 }catch (const Exception &ex) {
356 LogError("Error during updating event " << ex.DumpToString());
357 event->setResult(false);
359 event->setCancelAllowed(false);
363 void AccountManager:: OnRequestReceived(const IEventFindProvidersPtr &account)
368 if (!account->getEvent()) {
369 ThrowMsg(NullPointerException, "event parameter is NULL");
371 DPL::ScopedPtr<AccountWrapper> accountWrapper(new AccountWrapper(account->getEvent()));
372 accountWrapper->findProviders();
373 if (account->checkCancelled()) {
374 account->setCancelAllowed(true);
375 account->setResult(true);
378 account->setEvent(accountWrapper->getAbstractAccount());
379 account->setResult(true);
381 catch (const Exception &ex)
383 LogError("Error during updating event " << ex.DumpToString());
384 account->setResult(false);
386 account->setCancelAllowed(false);
389 void AccountManager::OnRequestReceived(const IEventFindServiceTypesPtr &event) {
390 LogDebug("<<< DUMMY IMPLEMENTATION");
392 std::string prefix = event->getPrefix();
393 LogDebug("prefix:[" << prefix << "]");
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);
407 event->setServiceTypeProperties(accountServiceTypeProperties);
409 event->setResult(true);