2 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
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.
17 * @file FScl_AccountAccessorImpl.cpp
18 * @brief This is the implementation for _AccountAccessorImpl class.
20 * This file contains definitions of @e _AccountAccessorImpl class.
24 #include <unique_ptr.h>
26 #include <FBaseColArrayList.h>
27 #include <FBaseResult.h>
28 #include <FBaseString.h>
29 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31 #include <FSclAccount.h>
32 #include <FSclAccountAccessor.h>
33 #include <FSclAccountProvider.h>
34 #include <FSclIAccountEventListener.h>
35 #include "FScl_AccountImpl.h"
36 #include "FScl_AccountAccessorImpl.h"
37 #include "FScl_AccountDbConnector.h"
38 #include "FScl_AccountDbMonitor.h"
39 #include "FScl_AccountManagerUtil.h"
41 using namespace Tizen::App;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
45 namespace Tizen { namespace Social
49 OnAccountReceived(account_h accountHandle, void* pUserData)
51 SysTryReturn(NID_SCL, accountHandle != null && pUserData != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
53 ArrayList* pAccountList = static_cast<ArrayList*> (pUserData);
55 std::unique_ptr<Account> pAccount(_AccountManagerUtil::ConvertAccountHandleToAccountN(accountHandle));
56 SysTryReturn(NID_SCL, pAccount != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
58 result r = pAccountList->Add(pAccount.get());
59 SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
67 OnAccountTypeReceived(account_type_h accountTypeHandle, void* pUserData)
69 SysTryReturn(NID_SCL, accountTypeHandle != null && pUserData != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
71 ArrayList* pAccountProviderList = static_cast<ArrayList*> (pUserData);
73 std::unique_ptr<AccountProvider> pAccountProvider(_AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
74 SysTryReturn(NID_SCL, pAccountProvider != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
76 result r = pAccountProviderList->Add(pAccountProvider.get());
77 SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
79 pAccountProvider.release();
84 _AccountAccessorImpl::_AccountAccessorImpl(void)
85 : __pIAccountEventListener(null)
90 _AccountAccessorImpl::~_AccountAccessorImpl(void)
96 _AccountAccessorImpl::Construct(void)
98 result r = _AccountDbConnector::EnsureDbConnection();
99 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
105 _AccountAccessorImpl::SetEventListener(IAccountEventListener* pListener)
107 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
109 result r = E_SUCCESS;
111 if (pListener != null)
113 if (__pIAccountEventListener == null)
115 _AccountDbMonitor* pAccountDbMonitor = _AccountDbMonitor::GetInstance();
116 SysTryReturn(NID_SCL, pAccountDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
118 r = pAccountDbMonitor->AddListener(*this);
119 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
122 __pIAccountEventListener = pListener;
126 if (__pIAccountEventListener != null)
128 _AccountDbMonitor* pAccountDbMonitor = _AccountDbMonitor::GetInstance();
129 SysTryReturn(NID_SCL, pAccountDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
131 r = pAccountDbMonitor->RemoveListener(*this);
132 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
135 __pIAccountEventListener = null;
142 _AccountAccessorImpl::GetAccount(AccountId accountId) const
145 Account account(L"");
147 SysTryReturn(NID_SCL, accountId > 0, account, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountId is invalid.", GetErrorMessage(E_INVALID_ARG));
148 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, account, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
150 account_h accountHandle = null;
151 Account* pAccount = null;
153 int ret = account_create(&accountHandle);
154 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountHandle != null, account, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
156 ret = account_query_account_by_account_id(accountId, &accountHandle);
157 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));
158 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
160 pAccount = _AccountManagerUtil::ConvertAccountHandleToAccountN(accountHandle);
161 SysTryCatch(NID_SCL, pAccount != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
163 account_destroy(accountHandle);
171 account_destroy(accountHandle);
177 _AccountAccessorImpl::GetAccountsByAccountProviderN(const AppId& accountProviderAppId) const
181 SysTryReturn(NID_SCL, !accountProviderAppId.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountProviderAppId is empty.", GetErrorMessage(E_INVALID_ARG));
182 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
184 std::unique_ptr<ArrayList, AllElementsDeleter> pAccountList(new (std::nothrow) ArrayList());
185 SysTryReturn(NID_SCL, pAccountList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
187 result r = pAccountList->Construct();
188 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
190 std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(accountProviderAppId));
191 SysTryReturn(NID_SCL, pCharArrayAppId != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
193 ArrayList* pTempAccountList = pAccountList.get();
194 int ret = account_query_account_by_package_name(OnAccountReceived, pCharArrayAppId.get(), (void*) pTempAccountList);
195 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));
198 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
200 return pAccountList.release();
204 _AccountAccessorImpl::GetAllAccountsN(void) const
208 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
210 std::unique_ptr<ArrayList, AllElementsDeleter> pAccountList(new (std::nothrow) ArrayList());
211 SysTryReturn(NID_SCL, pAccountList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
213 result r = pAccountList->Construct();
214 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
216 ArrayList* pTempAccountList = pAccountList.get();
217 int ret = account_foreach_account_from_db(OnAccountReceived, (void*) pTempAccountList);
218 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));
221 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
223 return pAccountList.release();
227 _AccountAccessorImpl::GetAccountProvider(const AppId& accountProviderAppId) const
230 AccountProvider accountProvider;
232 SysTryReturn(NID_SCL, !accountProviderAppId.IsEmpty(), accountProvider, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountProviderAppId is empty.", GetErrorMessage(E_INVALID_ARG));
233 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, accountProvider, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
235 account_type_h accountTypeHandle = null;
236 AccountProvider* pAccountProvider = null;
238 int ret = account_type_create(&accountTypeHandle);
239 SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountTypeHandle != null, accountProvider, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
241 std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(accountProviderAppId));
242 SysTryCatch(NID_SCL, pCharArrayAppId != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
244 ret = account_type_query_by_app_id(pCharArrayAppId.get(), &accountTypeHandle);
245 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));
246 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
248 pAccountProvider = _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle);
249 SysTryCatch(NID_SCL, pAccountProvider != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
251 account_type_destroy(accountTypeHandle);
253 accountProvider = *pAccountProvider;
254 delete pAccountProvider;
256 return accountProvider;
259 account_type_destroy(accountTypeHandle);
261 return accountProvider;
265 _AccountAccessorImpl::GetAccountProvidersByCapabilityN(const String& capability) const
269 SysTryReturn(NID_SCL, !capability.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified capability is empty.", GetErrorMessage(E_INVALID_ARG));
270 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
272 std::unique_ptr<ArrayList, AllElementsDeleter> pAccountProviderList(new (std::nothrow) ArrayList());
273 SysTryReturn(NID_SCL, pAccountProviderList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
275 result r = pAccountProviderList->Construct();
276 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
278 std::unique_ptr<char[]> pCharArrayCapability(_StringConverter::CopyToCharArrayN(capability));
279 SysTryReturn(NID_SCL, pCharArrayCapability != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
281 ArrayList* pTempAccountProviderList = pAccountProviderList.get();
282 int ret = account_type_query_by_provider_feature(OnAccountTypeReceived, pCharArrayCapability.get(), (void*) pTempAccountProviderList);
283 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));
286 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
288 return pAccountProviderList.release();
292 _AccountAccessorImpl::GetAllAccountProvidersN(void) const
296 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
298 std::unique_ptr<ArrayList, AllElementsDeleter> pAccountProviderList(new (std::nothrow) ArrayList());
299 SysTryReturn(NID_SCL, pAccountProviderList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
301 result r = pAccountProviderList->Construct();
302 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
304 ArrayList* pTempAccountProviderList = pAccountProviderList.get();
305 int ret = account_type_foreach_account_type_from_db(OnAccountTypeReceived, (void*) pTempAccountProviderList);
306 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));
309 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
311 return pAccountProviderList.release();
314 _AccountAccessorImpl*
315 _AccountAccessorImpl::GetInstance(AccountAccessor& accountAccessor)
317 return accountAccessor.__pAccountAccessorImpl;
320 const _AccountAccessorImpl*
321 _AccountAccessorImpl::GetInstance(const AccountAccessor& accountAccessor)
323 return accountAccessor.__pAccountAccessorImpl;
327 _AccountAccessorImpl::OnAccountAdded(AccountId accountId)
329 if (__pIAccountEventListener != null)
331 __pIAccountEventListener->OnAccountAdded(accountId);
336 _AccountAccessorImpl::OnAccountUpdated(AccountId accountId)
338 if (__pIAccountEventListener != null)
340 __pIAccountEventListener->OnAccountUpdated(accountId);
345 _AccountAccessorImpl::OnAccountRemoved(AccountId accountId)
347 if (__pIAccountEventListener != null)
349 __pIAccountEventListener->OnAccountRemoved(accountId);