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_AccountManagerImpl.cpp
19 * @brief This is the implementation for _AccountManagerImpl class.
21 * This file contains definitions of @e _AccountManagerImpl class.
25 #include <unique_ptr.h>
27 #include <account-types.h>
28 #include <FBaseColArrayList.h>
29 #include <FBaseResult.h>
30 #include <FBaseString.h>
31 #include <FBaseSysLog.h>
32 #include <FSclAccount.h>
33 #include <FSclAccountManager.h>
34 #include "FScl_AccountDbConnector.h"
35 #include "FScl_AccountImpl.h"
36 #include "FScl_AccountManagerImpl.h"
37 #include "FScl_AccountManagerUtil.h"
39 using namespace Tizen::Base::Collection;
41 namespace Tizen { namespace Social
44 _AccountManagerImpl::_AccountManagerImpl(void)
49 _AccountManagerImpl::~_AccountManagerImpl(void)
55 _AccountManagerImpl::Construct(void)
57 result r = _AccountDbConnector::EnsureDbConnection();
58 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
64 _AccountManagerImpl::AddAccount(Account& account)
66 SysTryReturnResult(NID_SCL, account.GetId() == INVALID_ACCOUNT_ID, E_INVALID_ARG, "Invalid argument is used. The account ID is not INVALID_ACCOUNT_ID.");
67 SysTryReturnResult(NID_SCL, !((account.GetUserName()).IsEmpty()), E_INVALID_ARG, "Invalid argument is used. The user name is not set in the account.");
68 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
70 account_h accountHandle = _AccountManagerUtil::CreateAccountHandleN(account);
71 result r = GetLastResult();
72 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
75 int ret = account_insert_to_db(accountHandle, &accountDbId);
76 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));
77 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));
78 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));
81 _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(account);
84 pAccountImpl->SetId(accountDbId);
86 // Set the account provider
87 char* pTempAppId = null;
89 ret = account_get_package_name(accountHandle, &pTempAppId);
90 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));
92 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
94 account_type_h accountTypeHandle = null;
95 ret = account_type_create(&accountTypeHandle);
96 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));
98 ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
99 if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
101 SysLogException(NID_SCL, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
103 account_type_destroy(accountTypeHandle);
104 account_destroy(accountHandle);
108 std::unique_ptr<AccountProvider> pAccountProvider(_AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
109 SysTryCatch(NID_SCL, pAccountProvider != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
111 (_AccountImpl::GetInstance(account))->SetAccountProvider(*pAccountProvider);
113 account_type_destroy(accountTypeHandle);
116 account_destroy(accountHandle);
121 account_destroy(accountHandle);
127 _AccountManagerImpl::RemoveAccount(AccountId accountId)
129 SysTryReturnResult(NID_SCL, accountId > 0, E_INVALID_ARG, "Invalid argument is used. The specified accountId is invalid.");
130 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
132 int ret = account_delete_from_db_by_id(accountId);
133 SysTryReturnResult(NID_SCL, ret != ACCOUNT_ERROR_NOT_REGISTERED_PROVIDER, E_INVALID_OPERATION, "The application does not register the account provider.");
134 SysTryReturnResult(NID_SCL, ret != ACCOUNT_ERROR_PERMISSION_DENIED, E_INVALID_OPERATION, "The application has no permission to remove this account.");
135 SysTryReturnResult(NID_SCL, ret != ACCOUNT_ERROR_RECORD_NOT_FOUND, E_OBJ_NOT_FOUND, "The specified account is not found.");
136 SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
142 _AccountManagerImpl::UpdateAccount(const Account& account)
144 SysTryReturnResult(NID_SCL, account.GetId() > 0, E_INVALID_ARG, "Invalid argument is used. The specified accountId is invalid.");
145 SysTryReturnResult(NID_SCL, !((account.GetUserName()).IsEmpty()), E_INVALID_ARG, "Invalid argument is used. The user name is not set in the account.");
146 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
148 account_h accountHandle = _AccountManagerUtil::CreateAccountHandleN(account);
149 result r = GetLastResult();
150 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
152 int ret = account_update_to_db_by_id(accountHandle, account.GetId());
153 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));
154 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));
155 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));
156 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));
158 account_destroy(accountHandle);
163 account_destroy(accountHandle);
169 _AccountManagerImpl::GetInstance(AccountManager& accountManager)
171 return accountManager.__pAccountManagerImpl;
174 const _AccountManagerImpl*
175 _AccountManagerImpl::GetInstance(const AccountManager& accountManager)
177 return accountManager.__pAccountManagerImpl;