Fixed update instance
[framework/osp/social.git] / src / FSclAccountManager.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                FSclAccountManager.cpp
19  * @brief               This is the implementation for AccountManager class.
20  *
21  * This file contains definitions of @e AccountManager 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 <FSclAccountManager.h>
33 #include "FScl_AccountManagerImpl.h"
34
35 using namespace Tizen::Base;
36 using namespace Tizen::Base::Collection;
37 using namespace Tizen::Security;
38
39 namespace Tizen { namespace Social
40 {
41
42 AccountManager* AccountManager::__pInstance = null;
43
44 AccountManager::AccountManager(void)
45         : __pAccountManagerImpl(null)
46 {
47         // empty body
48 }
49
50 AccountManager::~AccountManager(void)
51 {
52         delete __pAccountManagerImpl;
53 }
54
55 result
56 AccountManager::AddAccount(Account& account)
57 {
58         SysTryReturnResult(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED
59                 , "The application does not have the privilege to call this method.");
60         SysAssertf(__pAccountManagerImpl != null,
61                         "Not yet constructed. Construct() should be called before use.");
62
63         result r = __pAccountManagerImpl->AddAccount(account);
64         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
65
66         return E_SUCCESS;
67 }
68
69 result
70 AccountManager::RemoveAccount(AccountId accountId)
71 {
72         SysTryReturnResult(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED
73                 , "The application does not have the privilege to call this method.");
74         SysAssertf(__pAccountManagerImpl != null,
75                         "Not yet constructed. Construct() should be called before use.");
76
77         result r = __pAccountManagerImpl->RemoveAccount(accountId);
78         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
79
80         return E_SUCCESS;
81 }
82
83 result
84 AccountManager::UpdateAccount(const Account& account)
85 {
86         SysTryReturnResult(NID_SCL, _AccessController::CheckUserPrivilege(_PRV_ACCOUNT_WRITE) == E_SUCCESS, E_PRIVILEGE_DENIED
87                 , "The application does not have the privilege to call this method.");
88         SysAssertf(__pAccountManagerImpl != null,
89                         "Not yet constructed. Construct() should be called before use.");
90
91         result r = __pAccountManagerImpl->UpdateAccount(account);
92         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
93
94         return E_SUCCESS;
95 }
96
97 AccountManager*
98 AccountManager::GetInstance(void)
99 {
100         static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
101
102         if (__pInstance == null)
103         {
104                 ClearLastResult();
105
106                 pthread_once(&onceBlock, InitAccountManager);
107
108                 result r = GetLastResult();
109
110                 if (IsFailed(r))
111                 {
112                         onceBlock = PTHREAD_ONCE_INIT;
113                 }
114         }
115
116         return __pInstance;
117 }
118
119 result
120 AccountManager::Construct(void)
121 {
122         SysAssertf(__pAccountManagerImpl == null,
123                         "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
124
125         _AccountManagerImpl* pAccountManagerImpl = new (std::nothrow) _AccountManagerImpl();
126         SysTryReturnResult(NID_SCL, pAccountManagerImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
127
128         result r = pAccountManagerImpl->Construct();
129         SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Failed to construct pAccountManagerImpl.", GetErrorMessage(r));
130
131         __pAccountManagerImpl = pAccountManagerImpl;
132
133         return E_SUCCESS;
134
135 CATCH:
136         delete pAccountManagerImpl;
137
138         return r;
139 }
140
141 void
142 AccountManager::InitAccountManager(void)
143 {
144         std::unique_ptr<AccountManager> pInstance(new (std::nothrow) AccountManager());
145         SysTryReturnVoidResult(NID_SCL, pInstance != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
146
147         result r = pInstance->Construct();
148         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Failed to construct pInstance.", GetErrorMessage(r));
149
150         __pInstance = pInstance.release();
151
152         std::atexit(DestroyAccountManager);
153 }
154
155 void
156 AccountManager::DestroyAccountManager(void)
157 {
158         delete __pInstance;
159         __pInstance = null;
160 }
161
162 }}  // Tizen::Social