// // Copyright (c) 2013 Samsung Electronics Co., Ltd. // // Licensed under the Apache License, Version 2.0 (the License); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // /** * @file FScl_AccountAccessorImpl.cpp * @brief This is the implementation for _AccountAccessorImpl class. * * This file contains definitions of @e _AccountAccessorImpl class. */ #include #include #include #include #include #include #include #include #include #include #include #include #include "FScl_AccountImpl.h" #include "FScl_AccountAccessorImpl.h" #include "FScl_AccountDbConnector.h" #include "FScl_AccountDbMonitor.h" #include "FScl_AccountManagerUtil.h" using namespace Tizen::App; using namespace Tizen::Base; using namespace Tizen::Base::Collection; namespace Tizen { namespace Social { bool OnAccountReceived(account_h accountHandle, void* pUserData) { SysTryReturn(NID_SCL, accountHandle != null && pUserData != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM)); ArrayList* pAccountList = static_cast (pUserData); std::unique_ptr pAccount(_AccountManagerUtil::ConvertAccountHandleToAccountN(accountHandle)); SysTryReturn(NID_SCL, pAccount != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); result r = pAccountList->Add(pAccount.get()); SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); pAccount.release(); return true; } bool OnAccountTypeReceived(account_type_h accountTypeHandle, void* pUserData) { SysTryReturn(NID_SCL, accountTypeHandle != null && pUserData != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM)); ArrayList* pAccountProviderList = static_cast (pUserData); std::unique_ptr pAccountProvider(_AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle)); SysTryReturn(NID_SCL, pAccountProvider != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); result r = pAccountProviderList->Add(pAccountProvider.get()); SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); pAccountProvider.release(); return true; } _AccountAccessorImpl::_AccountAccessorImpl(void) : __pIAccountEventListener(null) { // empty body } _AccountAccessorImpl::~_AccountAccessorImpl(void) { // empty body } result _AccountAccessorImpl::Construct(void) { result r = _AccountDbConnector::EnsureDbConnection(); SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r)); return E_SUCCESS; } result _AccountAccessorImpl::SetEventListener(IAccountEventListener* pListener) { SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); result r = E_SUCCESS; if (pListener != null) { if (__pIAccountEventListener == null) { _AccountDbMonitor* pAccountDbMonitor = _AccountDbMonitor::GetInstance(); SysTryReturn(NID_SCL, pAccountDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); r = pAccountDbMonitor->AddListener(*this); SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r)); } __pIAccountEventListener = pListener; } else { if (__pIAccountEventListener != null) { _AccountDbMonitor* pAccountDbMonitor = _AccountDbMonitor::GetInstance(); SysTryReturn(NID_SCL, pAccountDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); r = pAccountDbMonitor->RemoveListener(*this); SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r)); } __pIAccountEventListener = null; } return E_SUCCESS; } Account _AccountAccessorImpl::GetAccount(AccountId accountId) const { ClearLastResult(); Account account(L""); SysTryReturn(NID_SCL, accountId > 0, account, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountId is invalid.", GetErrorMessage(E_INVALID_ARG)); SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, account, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); account_h accountHandle = null; Account* pAccount = null; int ret = account_create(&accountHandle); SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountHandle != null, account, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); ret = account_query_account_by_account_id(accountId, &accountHandle); 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)); SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM)); pAccount = _AccountManagerUtil::ConvertAccountHandleToAccountN(accountHandle); SysTryCatch(NID_SCL, pAccount != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); account_destroy(accountHandle); account = *pAccount; delete pAccount; return account; CATCH: account_destroy(accountHandle); return account; } IList* _AccountAccessorImpl::GetAccountsByAccountProviderN(const AppId& accountProviderAppId) const { ClearLastResult(); SysTryReturn(NID_SCL, !accountProviderAppId.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountProviderAppId is empty.", GetErrorMessage(E_INVALID_ARG)); SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); std::unique_ptr pAccountList(new (std::nothrow) ArrayList()); SysTryReturn(NID_SCL, pAccountList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); result r = pAccountList->Construct(); SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); std::unique_ptr pCharArrayAppId(_StringConverter::CopyToCharArrayN(accountProviderAppId)); SysTryReturn(NID_SCL, pCharArrayAppId != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); ArrayList* pTempAccountList = pAccountList.get(); int ret = account_query_account_by_package_name(OnAccountReceived, pCharArrayAppId.get(), (void*) pTempAccountList); 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)); r = GetLastResult(); SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r)); return pAccountList.release(); } IList* _AccountAccessorImpl::GetAllAccountsN(void) const { ClearLastResult(); SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); std::unique_ptr pAccountList(new (std::nothrow) ArrayList()); SysTryReturn(NID_SCL, pAccountList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); result r = pAccountList->Construct(); SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); ArrayList* pTempAccountList = pAccountList.get(); int ret = account_foreach_account_from_db(OnAccountReceived, (void*) pTempAccountList); 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)); r = GetLastResult(); SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r)); return pAccountList.release(); } AccountProvider _AccountAccessorImpl::GetAccountProvider(const AppId& accountProviderAppId) const { ClearLastResult(); AccountProvider accountProvider; SysTryReturn(NID_SCL, !accountProviderAppId.IsEmpty(), accountProvider, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountProviderAppId is empty.", GetErrorMessage(E_INVALID_ARG)); SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, accountProvider, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); account_type_h accountTypeHandle = null; AccountProvider* pAccountProvider = null; int ret = account_type_create(&accountTypeHandle); SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountTypeHandle != null, accountProvider, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); std::unique_ptr pCharArrayAppId(_StringConverter::CopyToCharArrayN(accountProviderAppId)); SysTryCatch(NID_SCL, pCharArrayAppId != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); ret = account_type_query_by_app_id(pCharArrayAppId.get(), &accountTypeHandle); 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)); SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM)); pAccountProvider = _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle); SysTryCatch(NID_SCL, pAccountProvider != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); account_type_destroy(accountTypeHandle); accountProvider = *pAccountProvider; delete pAccountProvider; return accountProvider; CATCH: account_type_destroy(accountTypeHandle); return accountProvider; } IList* _AccountAccessorImpl::GetAccountProvidersByCapabilityN(const String& capability) const { ClearLastResult(); SysTryReturn(NID_SCL, !capability.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified capability is empty.", GetErrorMessage(E_INVALID_ARG)); SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); std::unique_ptr pAccountProviderList(new (std::nothrow) ArrayList()); SysTryReturn(NID_SCL, pAccountProviderList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); result r = pAccountProviderList->Construct(); SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); std::unique_ptr pCharArrayCapability(_StringConverter::CopyToCharArrayN(capability)); SysTryReturn(NID_SCL, pCharArrayCapability != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); ArrayList* pTempAccountProviderList = pAccountProviderList.get(); int ret = account_type_query_by_provider_feature(OnAccountTypeReceived, pCharArrayCapability.get(), (void*) pTempAccountProviderList); 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)); r = GetLastResult(); SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r)); return pAccountProviderList.release(); } IList* _AccountAccessorImpl::GetAllAccountProvidersN(void) const { ClearLastResult(); SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult())); std::unique_ptr pAccountProviderList(new (std::nothrow) ArrayList()); SysTryReturn(NID_SCL, pAccountProviderList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); result r = pAccountProviderList->Construct(); SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY)); ArrayList* pTempAccountProviderList = pAccountProviderList.get(); int ret = account_type_foreach_account_type_from_db(OnAccountTypeReceived, (void*) pTempAccountProviderList); 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)); r = GetLastResult(); SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r)); return pAccountProviderList.release(); } _AccountAccessorImpl* _AccountAccessorImpl::GetInstance(AccountAccessor& accountAccessor) { return accountAccessor.__pAccountAccessorImpl; } const _AccountAccessorImpl* _AccountAccessorImpl::GetInstance(const AccountAccessor& accountAccessor) { return accountAccessor.__pAccountAccessorImpl; } void _AccountAccessorImpl::OnAccountAdded(AccountId accountId) { if (__pIAccountEventListener != null) { __pIAccountEventListener->OnAccountAdded(accountId); } } void _AccountAccessorImpl::OnAccountUpdated(AccountId accountId) { if (__pIAccountEventListener != null) { __pIAccountEventListener->OnAccountUpdated(accountId); } } void _AccountAccessorImpl::OnAccountRemoved(AccountId accountId) { if (__pIAccountEventListener != null) { __pIAccountEventListener->OnAccountRemoved(accountId); } } }} // Tizen::Social