2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 * @file FScl_AccountAccessorImpl.cpp
19 * @brief This is the implementation for _AccountAccessorImpl class.
21 * This file contains definitions of @e _AccountAccessorImpl class.
25 #include <unique_ptr.h>
27 #include <FBaseColArrayList.h>
28 #include <FBaseResult.h>
29 #include <FBaseString.h>
30 #include <FBaseSysLog.h>
31 #include <FBase_StringConverter.h>
32 #include <FSclAccount.h>
33 #include <FSclAccountAccessor.h>
34 #include <FSclAccountProvider.h>
35 #include <FSclIAccountEventListener.h>
36 #include "FScl_AccountImpl.h"
37 #include "FScl_AccountAccessorImpl.h"
38 #include "FScl_AccountDbConnector.h"
39 #include "FScl_AccountDbMonitor.h"
40 #include "FScl_AccountManagerUtil.h"
42 using namespace Tizen::App;
43 using namespace Tizen::Base;
44 using namespace Tizen::Base::Collection;
46 namespace Tizen { namespace Social
50 OnAccountReceived(account_h accountHandle, void* pUserData)
52 SysTryReturn(NID_SCL, accountHandle != null && pUserData != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
54 ArrayList* pAccountList = static_cast<ArrayList*> (pUserData);
56 std::unique_ptr<Account> pAccount(_AccountManagerUtil::ConvertAccountHandleToAccountN(accountHandle));
57 SysTryReturn(NID_SCL, pAccount != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
59 result r = pAccountList->Add(pAccount.get());
60 SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
68 OnAccountTypeReceived(account_type_h accountTypeHandle, void* pUserData)
70 SysTryReturn(NID_SCL, accountTypeHandle != null && pUserData != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
72 ArrayList* pAccountProviderList = static_cast<ArrayList*> (pUserData);
74 std::unique_ptr<AccountProvider> pAccountProvider(_AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
75 SysTryReturn(NID_SCL, pAccountProvider != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
77 result r = pAccountProviderList->Add(pAccountProvider.get());
78 SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
80 pAccountProvider.release();
85 _AccountAccessorImpl::_AccountAccessorImpl(void)
86 : __pIAccountEventListener(null)
91 _AccountAccessorImpl::~_AccountAccessorImpl(void)
97 _AccountAccessorImpl::Construct(void)
99 result r = _AccountDbConnector::EnsureDbConnection();
100 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
106 _AccountAccessorImpl::SetEventListener(IAccountEventListener* pListener)
108 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
110 result r = E_SUCCESS;
112 if (pListener != null)
114 if (__pIAccountEventListener == null)
116 _AccountDbMonitor* pAccountDbMonitor = _AccountDbMonitor::GetInstance();
117 SysTryReturn(NID_SCL, pAccountDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
119 r = pAccountDbMonitor->AddListener(*this);
120 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
123 __pIAccountEventListener = pListener;
127 if (__pIAccountEventListener != null)
129 _AccountDbMonitor* pAccountDbMonitor = _AccountDbMonitor::GetInstance();
130 SysTryReturn(NID_SCL, pAccountDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
132 r = pAccountDbMonitor->RemoveListener(*this);
133 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
136 __pIAccountEventListener = null;
143 _AccountAccessorImpl::GetAccount(AccountId accountId) const
146 Account account(L"");
148 SysTryReturn(NID_SCL, accountId > 0, account, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountId is invalid.", GetErrorMessage(E_INVALID_ARG));
149 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, account, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
151 account_h accountHandle = null;
152 Account* pAccount = null;
154 int ret = account_create(&accountHandle);
155 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountHandle != null, account, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
157 ret = account_query_account_by_account_id(accountId, &accountHandle);
158 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_RECORD_NOT_FOUND, , E_OBJ_NOT_FOUND, "[%s] The specified account is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
159 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
161 pAccount = _AccountManagerUtil::ConvertAccountHandleToAccountN(accountHandle);
162 SysTryCatch(NID_SCL, pAccount != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
164 account_destroy(accountHandle);
172 account_destroy(accountHandle);
178 _AccountAccessorImpl::GetAccountsByAccountProviderN(const AppId& accountProviderAppId) const
182 SysTryReturn(NID_SCL, !accountProviderAppId.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountProviderAppId is empty.", GetErrorMessage(E_INVALID_ARG));
183 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
185 std::unique_ptr<ArrayList, AllElementsDeleter> pAccountList(new (std::nothrow) ArrayList());
186 SysTryReturn(NID_SCL, pAccountList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
188 result r = pAccountList->Construct();
189 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
191 std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(accountProviderAppId));
192 SysTryReturn(NID_SCL, pCharArrayAppId != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
194 ArrayList* pTempAccountList = pAccountList.get();
195 int ret = account_query_account_by_package_name(OnAccountReceived, pCharArrayAppId.get(), (void*) pTempAccountList);
196 SysTryReturn(NID_SCL, (ret == ACCOUNT_ERROR_NONE) || (ret == ACCOUNT_ERROR_RECORD_NOT_FOUND), null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
199 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
201 return pAccountList.release();
205 _AccountAccessorImpl::GetAllAccountsN(void) const
209 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
211 std::unique_ptr<ArrayList, AllElementsDeleter> pAccountList(new (std::nothrow) ArrayList());
212 SysTryReturn(NID_SCL, pAccountList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
214 result r = pAccountList->Construct();
215 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
217 ArrayList* pTempAccountList = pAccountList.get();
218 int ret = account_foreach_account_from_db(OnAccountReceived, (void*) pTempAccountList);
219 SysTryReturn(NID_SCL, (ret == ACCOUNT_ERROR_NONE) || (ret == ACCOUNT_ERROR_RECORD_NOT_FOUND), null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
222 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
224 return pAccountList.release();
228 _AccountAccessorImpl::GetAccountProvider(const AppId& accountProviderAppId) const
231 AccountProvider accountProvider;
233 SysTryReturn(NID_SCL, !accountProviderAppId.IsEmpty(), accountProvider, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountProviderAppId is empty.", GetErrorMessage(E_INVALID_ARG));
234 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, accountProvider, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
236 account_type_h accountTypeHandle = null;
237 AccountProvider* pAccountProvider = null;
239 int ret = account_type_create(&accountTypeHandle);
240 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountTypeHandle != null, accountProvider, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
242 std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(accountProviderAppId));
243 SysTryCatch(NID_SCL, pCharArrayAppId != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
245 ret = account_type_query_by_app_id(pCharArrayAppId.get(), &accountTypeHandle);
246 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_RECORD_NOT_FOUND, , E_OBJ_NOT_FOUND, "[%s] The specified account is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
247 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
249 pAccountProvider = _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle);
250 SysTryCatch(NID_SCL, pAccountProvider != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
252 account_type_destroy(accountTypeHandle);
254 accountProvider = *pAccountProvider;
255 delete pAccountProvider;
257 return accountProvider;
260 account_type_destroy(accountTypeHandle);
262 return accountProvider;
266 _AccountAccessorImpl::GetAccountProvidersByCapabilityN(const String& capability) const
270 SysTryReturn(NID_SCL, !capability.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified capability is empty.", GetErrorMessage(E_INVALID_ARG));
271 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
273 std::unique_ptr<ArrayList, AllElementsDeleter> pAccountProviderList(new (std::nothrow) ArrayList());
274 SysTryReturn(NID_SCL, pAccountProviderList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
276 result r = pAccountProviderList->Construct();
277 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
279 std::unique_ptr<char[]> pCharArrayCapability(_StringConverter::CopyToCharArrayN(capability));
280 SysTryReturn(NID_SCL, pCharArrayCapability != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
282 ArrayList* pTempAccountProviderList = pAccountProviderList.get();
283 int ret = account_type_query_by_provider_feature(OnAccountTypeReceived, pCharArrayCapability.get(), (void*) pTempAccountProviderList);
284 SysTryReturn(NID_SCL, (ret == ACCOUNT_ERROR_NONE) || (ret == ACCOUNT_ERROR_RECORD_NOT_FOUND), null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
287 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
289 return pAccountProviderList.release();
293 _AccountAccessorImpl::GetAllAccountProvidersN(void) const
297 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
299 std::unique_ptr<ArrayList, AllElementsDeleter> pAccountProviderList(new (std::nothrow) ArrayList());
300 SysTryReturn(NID_SCL, pAccountProviderList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
302 result r = pAccountProviderList->Construct();
303 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
305 ArrayList* pTempAccountProviderList = pAccountProviderList.get();
306 int ret = account_type_foreach_account_type_from_db(OnAccountTypeReceived, (void*) pTempAccountProviderList);
307 SysTryReturn(NID_SCL, (ret == ACCOUNT_ERROR_NONE) || (ret == ACCOUNT_ERROR_RECORD_NOT_FOUND), null, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
310 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
312 return pAccountProviderList.release();
315 _AccountAccessorImpl*
316 _AccountAccessorImpl::GetInstance(AccountAccessor& accountAccessor)
318 return accountAccessor.__pAccountAccessorImpl;
321 const _AccountAccessorImpl*
322 _AccountAccessorImpl::GetInstance(const AccountAccessor& accountAccessor)
324 return accountAccessor.__pAccountAccessorImpl;
328 _AccountAccessorImpl::OnAccountAdded(AccountId accountId)
330 if (__pIAccountEventListener != null)
332 __pIAccountEventListener->OnAccountAdded(accountId);
337 _AccountAccessorImpl::OnAccountUpdated(AccountId accountId)
339 if (__pIAccountEventListener != null)
341 __pIAccountEventListener->OnAccountUpdated(accountId);
346 _AccountAccessorImpl::OnAccountRemoved(AccountId accountId)
348 if (__pIAccountEventListener != null)
350 __pIAccountEventListener->OnAccountRemoved(accountId);