2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 * @file FScl_AccountDbMonitor.cpp
18 * @brief This is the implementation for _AccountDbMonitor class.
20 * This file contains definitions of @e _AccountDbMonitor class.
24 #include <FBaseRtMutex.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseColArrayListT.h>
27 #include "FScl_IAccountDbChangeEventListener.h"
28 #include "FScl_AccountDbChangeEvent.h"
29 #include "FScl_AccountDbChangeEventArg.h"
30 #include "FScl_AccountDbConnector.h"
31 #include "FScl_AccountDbMonitor.h"
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Runtime;
36 namespace Tizen { namespace Social
39 _AccountDbMonitor* _AccountDbMonitor::__pTheInstance = null;
41 _AccountDbMonitor::__Callback::__Callback(account_subscribe_h accountSubscribeHandle, account_event_cb callback, void* pUserData)
42 : __accountSubscribeHandle(accountSubscribeHandle)
43 , __callback(callback)
44 , __pUserData(pUserData)
50 _AccountDbMonitor::__Callback::Register(void)
52 int ret = account_subscribe_notification(__accountSubscribeHandle, __callback, __pUserData);
53 SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_SYSTEM, "The method cannot proceed due to a severe system error.");
58 _AccountDbMonitor::__Callback::~__Callback(void)
60 if (__accountSubscribeHandle)
62 account_unsubscribe_notification(__accountSubscribeHandle);
66 _AccountDbMonitor::_AccountDbMonitor(void)
69 , __pAccountChangeCallback(null)
74 _AccountDbMonitor::~_AccountDbMonitor(void)
80 _AccountDbMonitor::Construct(void)
82 SysTryReturn(NID_SCL, _AccountDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
84 std::unique_ptr<Mutex> pMutex(new (std::nothrow) Mutex());
85 SysTryReturn(NID_SCL, pMutex != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
87 result r = pMutex->Create();
88 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
90 std::unique_ptr<_AccountDbChangeEvent> pEvent(new (std::nothrow) _AccountDbChangeEvent());
91 SysTryReturnResult(NID_SCL, pEvent != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
93 r = pEvent->Construct();
94 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
96 account_subscribe_h accountSubscribeHandle = null;
97 int ret = account_subscribe_create(&accountSubscribeHandle);
98 SysTryReturnResult(NID_SCL, ret == ACCOUNT_ERROR_NONE, E_OUT_OF_MEMORY, "Memory allocation failed.");
100 std::unique_ptr<__Callback> pAccountChangeCallback(new (std::nothrow) __Callback(accountSubscribeHandle, OnAccountChangeEventReceived, this));
101 SysTryCatch(NID_SCL, pAccountChangeCallback != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
103 r = pAccountChangeCallback->Register();
104 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
106 __pMutex = move(pMutex);
107 __pEvent = move(pEvent);
108 __pAccountChangeCallback = move(pAccountChangeCallback);
113 account_unsubscribe_notification(accountSubscribeHandle);
119 _AccountDbMonitor::AddListener(const _IAccountDbChangeEventListener& listener)
121 result r = __pMutex->Acquire();
122 SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
124 r = __pEvent->AddListener(listener);
125 SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
127 r = __pMutex->Release();
128 SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
133 r = __pMutex->Release();
134 SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
140 _AccountDbMonitor::RemoveListener(const _IAccountDbChangeEventListener& listener)
142 result r = __pMutex->Acquire();
143 SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
145 r = __pEvent->RemoveListener(listener);
146 SysTryCatch(NID_SCL, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
148 r = __pMutex->Release();
149 SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
154 r = __pMutex->Release();
155 SysTryReturn(NID_SCL, !IsFailed(r), E_SYSTEM, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
161 _AccountDbMonitor::GetInstance(void)
163 static pthread_once_t onceBlock = PTHREAD_ONCE_INIT;
164 if (__pTheInstance == null)
168 pthread_once(&onceBlock, InitSingleton);
170 result r = GetLastResult();
173 onceBlock = PTHREAD_ONCE_INIT;
177 return __pTheInstance;
181 _AccountDbMonitor::OnAccountChangeEventReceived(const char* pEventType, int accountId, void* pUserData)
183 SysTryReturn(NID_SCL, pEventType != null, false, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
185 std::unique_ptr<String> pAccountChangeEventType(new (std::nothrow) String(pEventType));
186 SysTryReturn(NID_SCL, pAccountChangeEventType != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
188 _AccountDbMonitor* pAccountDbMonitor = static_cast<_AccountDbMonitor*>(pUserData);
189 _AccountDbChangeEvent* pEvent = pAccountDbMonitor->__pEvent.get();
191 std::unique_ptr<_AccountDbChangeEventArg> pArg(null);
193 if (pAccountChangeEventType->Equals(String(ACCOUNT_NOTI_NAME_INSERT)))
195 pArg.reset(new (std::nothrow) _AccountDbChangeEventArg(accountId, _ACCOUNT_DB_CHANGE_TYPE_ADD));
196 SysTryReturn(NID_SCL, pArg != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
198 else if (pAccountChangeEventType->Equals(String(ACCOUNT_NOTI_NAME_UPDATE)))
200 pArg.reset(new (std::nothrow) _AccountDbChangeEventArg(accountId, _ACCOUNT_DB_CHANGE_TYPE_UPDATE));
201 SysTryReturn(NID_SCL, pArg != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
203 else if (pAccountChangeEventType->Equals(String(ACCOUNT_NOTI_NAME_DELETE)))
205 pArg.reset(new (std::nothrow) _AccountDbChangeEventArg(accountId, _ACCOUNT_DB_CHANGE_TYPE_REMOVE));
206 SysTryReturn(NID_SCL, pArg != null, false, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
210 SysLogException(NID_SCL, E_SYSTEM, "[%s] The method cannot proceed due to a severe system error.", GetErrorMessage(E_SYSTEM));
214 result r = pEvent->Fire(*pArg);
215 SysTryReturn(NID_SCL, !IsFailed(r), false, r, "[%s] Propagating.", GetErrorMessage(r));
223 _AccountDbMonitor::InitSingleton(void)
225 std::unique_ptr<_AccountDbMonitor> pInst(new (std::nothrow) _AccountDbMonitor());
226 SysTryReturnVoidResult(NID_SCL, pInst, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
228 result r = pInst->Construct();
229 SysTryReturnVoidResult(NID_SCL, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
231 __pTheInstance = pInst.release();
233 std::atexit(DestroySingleton);
237 _AccountDbMonitor::DestroySingleton(void)
239 delete __pTheInstance;