Fixed update instance
[framework/osp/social.git] / src / FScl_AccountManagerImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17 /**
18  * @file                FScl_AccountManagerImpl.cpp
19  * @brief               This is the implementation for _AccountManagerImpl class.
20  *
21  * This file contains definitions of @e _AccountManagerImpl class.
22  */
23
24 #include <new>
25 #include <unique_ptr.h>
26 #include <account.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"
38
39 using namespace Tizen::Base::Collection;
40
41 namespace Tizen { namespace Social
42 {
43
44 _AccountManagerImpl::_AccountManagerImpl(void)
45 {
46         // empty body
47 }
48
49 _AccountManagerImpl::~_AccountManagerImpl(void)
50 {
51         // empty body
52 }
53
54 result
55 _AccountManagerImpl::Construct(void)
56 {
57         result r = _AccountDbConnector::EnsureDbConnection();
58         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
59
60         return E_SUCCESS;
61 }
62
63 result
64 _AccountManagerImpl::AddAccount(Account& account)
65 {
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()));
69
70         account_h accountHandle = _AccountManagerUtil::CreateAccountHandleN(account);
71         result r = GetLastResult();
72         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
73
74         int accountDbId = 0;
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));
79
80         {
81                 _AccountImpl* pAccountImpl = _AccountImpl::GetInstance(account);
82
83                 // Set the account ID
84                 pAccountImpl->SetId(accountDbId);
85
86                 // Set the account provider
87                 char* pTempAppId = null;
88
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));
91
92                 std::unique_ptr<char[], _CharDeleter> pCharArrayAppId(pTempAppId);
93
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));
97
98                 ret = account_type_query_by_app_id(pTempAppId, &accountTypeHandle);
99                 if (ret != ACCOUNT_ERROR_NONE || accountTypeHandle == null)
100                 {
101                         SysLogException(NID_SCL, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
102
103                         account_type_destroy(accountTypeHandle);
104                         account_destroy(accountHandle);
105                         return E_SYSTEM;
106                 }
107
108                 std::unique_ptr<AccountProvider> pAccountProvider(_AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
109                 SysTryCatch(NID_SCL, pAccountProvider != null, r = GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
110
111                 (_AccountImpl::GetInstance(account))->SetAccountProvider(*pAccountProvider);
112
113                 account_type_destroy(accountTypeHandle);
114         }
115
116         account_destroy(accountHandle);
117
118         return E_SUCCESS;
119
120 CATCH:
121         account_destroy(accountHandle);
122
123         return r;
124 }
125
126 result
127 _AccountManagerImpl::RemoveAccount(AccountId accountId)
128 {
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()));
131
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.");
137
138         return E_SUCCESS;
139 }
140
141 result
142 _AccountManagerImpl::UpdateAccount(const Account& account)
143 {
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()));
147
148         account_h accountHandle = _AccountManagerUtil::CreateAccountHandleN(account);
149         result r = GetLastResult();
150         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
151
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));
157
158         account_destroy(accountHandle);
159
160         return E_SUCCESS;
161
162 CATCH:
163         account_destroy(accountHandle);
164
165         return r;
166 }
167
168 _AccountManagerImpl*
169 _AccountManagerImpl::GetInstance(AccountManager& accountManager)
170 {
171         return accountManager.__pAccountManagerImpl;
172 }
173
174 const _AccountManagerImpl*
175 _AccountManagerImpl::GetInstance(const AccountManager& accountManager)
176 {
177         return accountManager.__pAccountManagerImpl;
178 }
179
180 }}  // Tizen::Social