Modify the spec file for secure log
[framework/osp/social.git] / src / FScl_AccountAccessorImpl.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_AccountAccessorImpl.cpp
18  * @brief               This is the implementation for _AccountAccessorImpl class.
19  *
20  * This file contains definitions of @e _AccountAccessorImpl class.
21  */
22
23 #include <new>
24 #include <unique_ptr.h>
25 #include <account.h>
26 #include <FBaseColArrayList.h>
27 #include <FBaseResult.h>
28 #include <FBaseString.h>
29 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31 #include <FSclAccount.h>
32 #include <FSclAccountAccessor.h>
33 #include <FSclAccountProvider.h>
34 #include <FSclIAccountEventListener.h>
35 #include "FScl_AccountImpl.h"
36 #include "FScl_AccountAccessorImpl.h"
37 #include "FScl_AccountDbConnector.h"
38 #include "FScl_AccountDbMonitor.h"
39 #include "FScl_AccountManagerUtil.h"
40
41 using namespace Tizen::App;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Collection;
44
45 namespace Tizen { namespace Social
46 {
47
48 bool
49 OnAccountReceived(account_h accountHandle, void* pUserData)
50 {
51         SysTryReturn(NID_SCL, accountHandle != null && pUserData != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
52
53         ArrayList* pAccountList = static_cast<ArrayList*> (pUserData);
54
55         std::unique_ptr<Account> pAccount(_AccountManagerUtil::ConvertAccountHandleToAccountN(accountHandle));
56         SysTryReturn(NID_SCL, pAccount != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
57
58         result r = pAccountList->Add(pAccount.get());
59         SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
60
61         pAccount.release();
62
63         return true;
64 }
65
66 bool
67 OnAccountTypeReceived(account_type_h accountTypeHandle, void* pUserData)
68 {
69         SysTryReturn(NID_SCL, accountTypeHandle != null && pUserData != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
70
71         ArrayList* pAccountProviderList = static_cast<ArrayList*> (pUserData);
72
73         std::unique_ptr<AccountProvider> pAccountProvider(_AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle));
74         SysTryReturn(NID_SCL, pAccountProvider != null, false, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
75
76         result r = pAccountProviderList->Add(pAccountProvider.get());
77         SysTryReturn(NID_SCL, !IsFailed(r), false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
78
79         pAccountProvider.release();
80
81         return true;
82 }
83
84 _AccountAccessorImpl::_AccountAccessorImpl(void)
85         : __pIAccountEventListener(null)
86 {
87         // empty body
88 }
89
90 _AccountAccessorImpl::~_AccountAccessorImpl(void)
91 {
92         // empty body
93 }
94
95 result
96 _AccountAccessorImpl::Construct(void)
97 {
98         result r = _AccountDbConnector::EnsureDbConnection();
99         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
100
101         return E_SUCCESS;
102 }
103
104 result
105 _AccountAccessorImpl::SetEventListener(IAccountEventListener* pListener)
106 {
107         SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
108
109         result r = E_SUCCESS;
110
111         if (pListener != null)
112         {
113                 if (__pIAccountEventListener == null)
114                 {
115                         _AccountDbMonitor* pAccountDbMonitor = _AccountDbMonitor::GetInstance();
116                         SysTryReturn(NID_SCL, pAccountDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
117
118                         r = pAccountDbMonitor->AddListener(*this);
119                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
120                 }
121
122                 __pIAccountEventListener = pListener;
123         }
124         else
125         {
126                 if (__pIAccountEventListener != null)
127                 {
128                         _AccountDbMonitor* pAccountDbMonitor = _AccountDbMonitor::GetInstance();
129                         SysTryReturn(NID_SCL, pAccountDbMonitor != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
130
131                         r = pAccountDbMonitor->RemoveListener(*this);
132                         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
133                 }
134
135                 __pIAccountEventListener = null;
136         }
137
138         return E_SUCCESS;
139 }
140
141 Account
142 _AccountAccessorImpl::GetAccount(AccountId accountId) const
143 {
144         ClearLastResult();
145         Account account(L"");
146
147         SysTryReturn(NID_SCL, accountId > 0, account, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountId is invalid.", GetErrorMessage(E_INVALID_ARG));
148         SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, account, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
149
150         account_h accountHandle = null;
151         Account* pAccount = null;
152
153         int ret = account_create(&accountHandle);
154         SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountHandle != null, account, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
155
156         ret = account_query_account_by_account_id(accountId, &accountHandle);
157         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));
158         SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
159
160         pAccount = _AccountManagerUtil::ConvertAccountHandleToAccountN(accountHandle);
161         SysTryCatch(NID_SCL, pAccount != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
162
163         account_destroy(accountHandle);
164
165         account = *pAccount;
166         delete pAccount;
167
168         return account;
169
170 CATCH:
171         account_destroy(accountHandle);
172
173         return account;
174 }
175
176 IList*
177 _AccountAccessorImpl::GetAccountsByAccountProviderN(const AppId& accountProviderAppId) const
178 {
179         ClearLastResult();
180
181         SysTryReturn(NID_SCL, !accountProviderAppId.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountProviderAppId is empty.", GetErrorMessage(E_INVALID_ARG));
182         SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
183
184         std::unique_ptr<ArrayList, AllElementsDeleter> pAccountList(new (std::nothrow) ArrayList());
185         SysTryReturn(NID_SCL, pAccountList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
186
187         result r = pAccountList->Construct();
188         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
189
190         std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(accountProviderAppId));
191         SysTryReturn(NID_SCL, pCharArrayAppId != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
192
193         ArrayList* pTempAccountList = pAccountList.get();
194         int ret = account_query_account_by_package_name(OnAccountReceived, pCharArrayAppId.get(), (void*) pTempAccountList);
195         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));
196
197         r = GetLastResult();
198         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
199
200         return pAccountList.release();
201 }
202
203 IList*
204 _AccountAccessorImpl::GetAllAccountsN(void) const
205 {
206         ClearLastResult();
207
208         SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
209
210         std::unique_ptr<ArrayList, AllElementsDeleter> pAccountList(new (std::nothrow) ArrayList());
211         SysTryReturn(NID_SCL, pAccountList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
212
213         result r = pAccountList->Construct();
214         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
215
216         ArrayList* pTempAccountList = pAccountList.get();
217         int ret = account_foreach_account_from_db(OnAccountReceived, (void*) pTempAccountList);
218         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));
219
220         r = GetLastResult();
221         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
222
223         return pAccountList.release();
224 }
225
226 AccountProvider
227 _AccountAccessorImpl::GetAccountProvider(const AppId& accountProviderAppId) const
228 {
229         ClearLastResult();
230         AccountProvider accountProvider;
231
232         SysTryReturn(NID_SCL, !accountProviderAppId.IsEmpty(), accountProvider, E_INVALID_ARG, "[%s] Invalid argument is used. The specified accountProviderAppId is empty.", GetErrorMessage(E_INVALID_ARG));
233         SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, accountProvider, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
234
235         account_type_h accountTypeHandle = null;
236         AccountProvider* pAccountProvider = null;
237
238         int ret = account_type_create(&accountTypeHandle);
239         SysTryReturn(NID_SCL, ret == ACCOUNT_ERROR_NONE && accountTypeHandle != null, accountProvider, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
240
241         std::unique_ptr<char[]> pCharArrayAppId(_StringConverter::CopyToCharArrayN(accountProviderAppId));
242         SysTryCatch(NID_SCL, pCharArrayAppId != null, , E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
243
244         ret = account_type_query_by_app_id(pCharArrayAppId.get(), &accountTypeHandle);
245         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));
246         SysTryCatch(NID_SCL, ret == ACCOUNT_ERROR_NONE, , E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
247
248         pAccountProvider = _AccountManagerUtil::ConvertAccountTypeHandleToAccountProviderN(accountTypeHandle);
249         SysTryCatch(NID_SCL, pAccountProvider != null, , GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
250
251         account_type_destroy(accountTypeHandle);
252
253         accountProvider = *pAccountProvider;
254         delete pAccountProvider;
255
256         return accountProvider;
257
258 CATCH:
259         account_type_destroy(accountTypeHandle);
260
261         return accountProvider;
262 }
263
264 IList*
265 _AccountAccessorImpl::GetAccountProvidersByCapabilityN(const String& capability) const
266 {
267         ClearLastResult();
268
269         SysTryReturn(NID_SCL, !capability.IsEmpty(), null, E_INVALID_ARG, "[%s] Invalid argument is used. The specified capability is empty.", GetErrorMessage(E_INVALID_ARG));
270         SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
271
272         std::unique_ptr<ArrayList, AllElementsDeleter> pAccountProviderList(new (std::nothrow) ArrayList());
273         SysTryReturn(NID_SCL, pAccountProviderList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
274
275         result r = pAccountProviderList->Construct();
276         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
277
278         std::unique_ptr<char[]> pCharArrayCapability(_StringConverter::CopyToCharArrayN(capability));
279         SysTryReturn(NID_SCL, pCharArrayCapability != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
280
281         ArrayList* pTempAccountProviderList = pAccountProviderList.get();
282         int ret = account_type_query_by_provider_feature(OnAccountTypeReceived, pCharArrayCapability.get(), (void*) pTempAccountProviderList);
283         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));
284
285         r = GetLastResult();
286         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
287
288         return pAccountProviderList.release();
289 }
290
291 IList*
292 _AccountAccessorImpl::GetAllAccountProvidersN(void) const
293 {
294         ClearLastResult();
295
296         SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
297
298         std::unique_ptr<ArrayList, AllElementsDeleter> pAccountProviderList(new (std::nothrow) ArrayList());
299         SysTryReturn(NID_SCL, pAccountProviderList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
300
301         result r = pAccountProviderList->Construct();
302         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
303
304         ArrayList* pTempAccountProviderList = pAccountProviderList.get();
305         int ret = account_type_foreach_account_type_from_db(OnAccountTypeReceived, (void*) pTempAccountProviderList);
306         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));
307
308         r = GetLastResult();
309         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
310
311         return pAccountProviderList.release();
312 }
313
314 _AccountAccessorImpl*
315 _AccountAccessorImpl::GetInstance(AccountAccessor& accountAccessor)
316 {
317         return accountAccessor.__pAccountAccessorImpl;
318 }
319
320 const _AccountAccessorImpl*
321 _AccountAccessorImpl::GetInstance(const AccountAccessor& accountAccessor)
322 {
323         return accountAccessor.__pAccountAccessorImpl;
324 }
325
326 void
327 _AccountAccessorImpl::OnAccountAdded(AccountId accountId)
328 {
329         if (__pIAccountEventListener != null)
330         {
331                 __pIAccountEventListener->OnAccountAdded(accountId);
332         }
333 }
334
335 void
336 _AccountAccessorImpl::OnAccountUpdated(AccountId accountId)
337 {
338         if (__pIAccountEventListener != null)
339         {
340                 __pIAccountEventListener->OnAccountUpdated(accountId);
341         }
342 }
343
344 void
345 _AccountAccessorImpl::OnAccountRemoved(AccountId accountId)
346 {
347         if (__pIAccountEventListener != null)
348         {
349                 __pIAccountEventListener->OnAccountRemoved(accountId);
350         }
351 }
352
353 }}  // Tizen::Social