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