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