Fixed update instance
[framework/osp/social.git] / src / FSclAccountAccessor.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                FSclAccountAccessor.cpp
19  * @brief               This is the implementation for AccountAccessor class.
20  *
21  * This file contains definitions of @e AccountAccessor class.
22  */
23
24 #include <new>
25 #include <pthread.h>
26 #include <unique_ptr.h>
27 #include <FBaseResult.h>
28 #include <FBaseSysLog.h>
29 #include <FBaseColIList.h>
30 #include <FSec_AccessController.h>
31 #include <FSclAccount.h>
32 #include <FSclAccountAccessor.h>
33 #include <FSclAccountProvider.h>
34 #include <FSclIAccountEventListener.h>
35 #include "FScl_AccountAccessorImpl.h"
36
37 using namespace Tizen::App;
38 using namespace Tizen::Base;
39 using namespace Tizen::Base::Collection;
40 using namespace Tizen::Security;
41
42 namespace Tizen { namespace Social
43 {
44
45 AccountAccessor* AccountAccessor::__pInstance = null;
46
47 AccountAccessor::AccountAccessor(void)
48         : __pAccountAccessorImpl(null)
49 {
50         // empty body
51 }
52
53 AccountAccessor::~AccountAccessor(void)
54 {
55         delete __pAccountAccessorImpl;
56 }
57
58 result
59 AccountAccessor::SetEventListener(IAccountEventListener* pListener)
60 {
61         SysTryReturnResult(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ) == E_SUCCESS, E_PRIVILEGE_DENIED
62                 , "The application does not have the privilege to call this method.");
63         SysAssertf(__pAccountAccessorImpl != null,
64                         "Not yet constructed. Construct() should be called before use.");
65
66         result r = __pAccountAccessorImpl->SetEventListener(pListener);
67         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
68
69         return E_SUCCESS;
70 }
71
72 Account
73 AccountAccessor::GetAccount(AccountId accountId) const
74 {
75         Account account(L"");
76
77         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ) == E_SUCCESS, account, E_PRIVILEGE_DENIED
78                 , "The application does not have the privilege to call this method.");
79         SysAssertf(__pAccountAccessorImpl != null,
80                         "Not yet constructed. Construct() should be called before use.");
81
82         account = __pAccountAccessorImpl->GetAccount(accountId);
83         SysTryReturn(NID_SCL, !IsFailed(GetLastResult()), account, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
84
85         return account;
86 }
87
88 IList*
89 AccountAccessor::GetAccountsByAccountProviderN(const AppId& accountProviderAppId) const
90 {
91         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED
92                 , "The application does not have the privilege to call this method.");
93         SysAssertf(__pAccountAccessorImpl != null,
94                         "Not yet constructed. Construct() should be called before use.");
95
96         IList* pList = __pAccountAccessorImpl->GetAccountsByAccountProviderN(accountProviderAppId);
97         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
98
99         return pList;
100 }
101
102 IList*
103 AccountAccessor::GetAllAccountsN(void) const
104 {
105         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED
106                 , "The application does not have the privilege to call this method.");
107         SysAssertf(__pAccountAccessorImpl != null,
108                         "Not yet constructed. Construct() should be called before use.");
109
110         IList* pList = __pAccountAccessorImpl->GetAllAccountsN();
111         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
112
113         return pList;
114 }
115
116 AccountProvider
117 AccountAccessor::GetAccountProvider(const AppId& accountProviderAppId) const
118 {
119         AccountProvider accountProvider;
120
121         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ) == E_SUCCESS, accountProvider, E_PRIVILEGE_DENIED
122                 , "The application does not have the privilege to call this method.");
123         SysAssertf(__pAccountAccessorImpl != null,
124                         "Not yet constructed. Construct() should be called before use.");
125
126         accountProvider = __pAccountAccessorImpl->GetAccountProvider(accountProviderAppId);
127         SysTryReturn(NID_SCL, !IsFailed(GetLastResult()), accountProvider, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
128
129         return accountProvider;
130 }
131
132 IList*
133 AccountAccessor::GetAccountProvidersByCapabilityN(const String& capability) const
134 {
135         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED
136                 , "The application does not have the privilege to call this method.");
137         SysAssertf(__pAccountAccessorImpl != null,
138                         "Not yet constructed. Construct() should be called before use.");
139
140         IList* pList = __pAccountAccessorImpl->GetAccountProvidersByCapabilityN(capability);
141         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
142
143         return pList;
144 }
145
146 IList*
147 AccountAccessor::GetAllAccountProvidersN(void) const
148 {
149         SysTryReturn(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_READ) == E_SUCCESS, null, E_PRIVILEGE_DENIED
150                 , "The application does not have the privilege to call this method.");
151         SysAssertf(__pAccountAccessorImpl != null,
152                         "Not yet constructed. Construct() should be called before use.");
153
154         IList* pList = __pAccountAccessorImpl->GetAllAccountProvidersN();
155         SysTryReturn(NID_SCL, pList != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
156
157         return pList;
158 }
159
160 AccountAccessor*
161 AccountAccessor::GetInstance(void)
162 {
163         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
164
165         if (__pInstance == null)
166         {
167                 ClearLastResult();
168
169                 pthread_once(&onceBlock, InitAccountAccessor);
170
171                 result r = GetLastResult();
172
173                 if (IsFailed(r))
174                 {
175                         onceBlock = PTHREAD_ONCE_INIT;
176                 }
177         }
178
179         return __pInstance;
180 }
181
182 result
183 AccountAccessor::Construct(void)
184 {
185         SysAssertf(__pAccountAccessorImpl == null,
186                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
187
188         _AccountAccessorImpl* pAccountAccessorImpl = new (std::nothrow) _AccountAccessorImpl();
189         SysTryReturnResult(NID_SCL, pAccountAccessorImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
190
191         result r = pAccountAccessorImpl->Construct();
192         SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Failed to construct pAccountAccessorImpl.", GetErrorMessage(r));
193
194         __pAccountAccessorImpl = pAccountAccessorImpl;
195
196         return E_SUCCESS;
197
198 CATCH:
199         delete pAccountAccessorImpl;
200
201         return r;
202 }
203
204 void
205 AccountAccessor::InitAccountAccessor(void)
206 {
207         std::unique_ptr<AccountAccessor> pInstance(new (std::nothrow) AccountAccessor());
208         SysTryReturnVoidResult(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
209
210         result r = pInstance->Construct();
211         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Failed to construct pInstance.", GetErrorMessage(r));
212
213         __pInstance = pInstance.release();
214
215         std::atexit(DestroyAccountAccessor);
216 }
217
218 void
219 AccountAccessor::DestroyAccountAccessor(void)
220 {
221         delete __pInstance;
222         __pInstance = null;
223 }
224
225 }}  // Tizen::Social