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_AccountManagerImpl.cpp
18 * @brief This is the implementation for _AccountManagerImpl class.
20 * This file contains definitions of @e _AccountManagerImpl class.
24 #include <unique_ptr.h>
26 #include <account-types.h>
27 #include <FBaseColArrayList.h>
28 #include <FBaseResult.h>
29 #include <FBaseString.h>
30 #include <FBaseSysLog.h>
31 #include <FSclAccount.h>
32 #include <FSclAccountManager.h>
33 #include "FScl_AccountDbConnector.h"
34 #include "FScl_AccountImpl.h"
35 #include "FScl_AccountManagerImpl.h"
36 #include "FScl_AccountManagerUtil.h"
38 using namespace Tizen::Base::Collection;
40 namespace Tizen { namespace Social
43 _AccountManagerImpl::_AccountManagerImpl(void)
48 _AccountManagerImpl::~_AccountManagerImpl(void)
54 _AccountManagerImpl::Construct(void)
56 result r = _AccountDbConnector::EnsureDbConnection();
57 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
63 _AccountManagerImpl::AddAccount(Account& account)
65 SysTryReturnResult(NID_SCL, account.GetId() == INVALID_ACCOUNT_ID, E_INVALID_ARG, "Invalid argument is used. The account ID is not INVALID_ACCOUNT_ID.");
66 SysTryReturnResult(NID_SCL, !((account.GetUserName()).IsEmpty()), E_INVALID_ARG, "Invalid argument is used. The user name is not set in the account.");
67 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
69 account_h accountHandle = _AccountManagerUtil::CreateAccountHandleN(account);
70 result r = GetLastResult();
71 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
74 int ret = account_insert_to_db(accountHandle, &accountDbId);
75 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, r = E_INVALID_OPERATION, E_INVALID_OPERATION, "[%s] The application does not register the account provider.", GetErrorMessage(E_INVALID_OPERATION));
76 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_NOT_ALLOW_MULTIPLE, r = E_INVALID_OPERATION, E_INVALID_OPERATION, "[%s] The multiple accounts are not supported.", GetErrorMessage(E_INVALID_OPERATION));
77 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
80 _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(account);
83 pAccountImpl->SetId(accountDbId);
85 // Set the account provider
86 char* pTempAppId = null;
88 ret = account_get_package_name(accountHandle, &pTempAppId);
89 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE && pTempAppId != null, r = E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
91 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
93 account_type_h accountTypeHandle = null;
94 ret = account_type_create(&accountTypeHandle);
95 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountTypeHandle != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
97 ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
98 if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
100 SysLogException(NID_SCL, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
102 account_type_destroy(accountTypeHandle);
103 account_destroy(accountHandle);
107 std::unique_ptr<AccountProvider> pAccountProvider(_AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
108 SysTryCatch(NID_SCL, pAccountProvider != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
110 (_AccountImpl::GetInstance(account))->SetAccountProvider(*pAccountProvider);
112 account_type_destroy(accountTypeHandle);
115 account_destroy(accountHandle);
120 account_destroy(accountHandle);
126 _AccountManagerImpl::RemoveAccount(AccountId accountId)
128 SysTryReturnResult(NID_SCL, accountId > 0, E_INVALID_ARG, "Invalid argument is used. The specified accountId is invalid.");
129 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
131 int ret = account_delete_from_db_by_id(accountId);
132 SysTryReturnResult(NID_SCL, ret != ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, E_INVALID_OPERATION, "The application does not register the account provider.");
133 SysTryReturnResult(NID_SCL, ret != ACCOUNT_ERROR_PERMISSION_DENIED, E_INVALID_OPERATION, "The application has no permission to remove this account.");
134 SysTryReturnResult(NID_SCL, ret != ACCOUNT_ERROR_RECORD_NOT_FOUND, E_OBJ_NOT_FOUND, "The specified account is not found.");
135 SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
141 _AccountManagerImpl::UpdateAccount(const Account& account)
143 SysTryReturnResult(NID_SCL, account.GetId() > 0, E_INVALID_ARG, "Invalid argument is used. The specified accountId is invalid.");
144 SysTryReturnResult(NID_SCL, !((account.GetUserName()).IsEmpty()), E_INVALID_ARG, "Invalid argument is used. The user name is not set in the account.");
145 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
147 account_h accountHandle = _AccountManagerUtil::CreateAccountHandleN(account);
148 result r = GetLastResult();
149 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
151 int ret = account_update_to_db_by_id(accountHandle, account.GetId());
152 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, r = E_INVALID_OPERATION, E_INVALID_OPERATION, "[%s] The application does not register the account provider.", GetErrorMessage(E_INVALID_OPERATION));
153 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_PERMISSION_DENIED, r = E_INVALID_OPERATION, E_INVALID_OPERATION, "[%s] The application has no permission to update this account.", GetErrorMessage(E_INVALID_OPERATION));
154 SysTryCatch(NID_SCL, ret != ACCOUNT_ERROR_RECORD_NOT_FOUND, r = E_OBJ_NOT_FOUND, E_OBJ_NOT_FOUND, "[%s] The specified @c account does not exist.", GetErrorMessage(E_OBJ_NOT_FOUND));
155 SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, r = E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
157 account_destroy(accountHandle);
162 account_destroy(accountHandle);
168 _AccountManagerImpl::GetInstance(AccountManager& accountManager)
170 return accountManager.__pAccountManagerImpl;
173 const _AccountManagerImpl*
174 _AccountManagerImpl::GetInstance(const AccountManager& accountManager)
176 return accountManager.__pAccountManagerImpl;