Modify the spec file for secure log
[framework/osp/social.git] / src / FScl_AccountImpl.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_AccountImpl.cpp
18  * @brief               This is the implementation for _AccountImpl class.
19  *
20  * This file contains definitions of @e _AccountImpl class.
21  */
22
23 #include <new>
24 #include <FBaseResult.h>
25 #include <FBaseString.h>
26 #include <FBaseSysLog.h>
27 #include <FSclAccount.h>
28 #include "FScl_AccountImpl.h"
29 #include "FScl_AccountProviderImpl.h"
30
31 #include <FBaseLog.h>
32
33 using namespace Tizen::Base;
34 using namespace Tizen::Base::Collection;
35
36 namespace Tizen { namespace Social
37 {
38
39 _AccountImpl::_AccountImpl(const Tizen::Base::String& userName)
40         : __id(INVALID_RECORD_ID)
41 {
42         std::unique_ptr<HashMap, AllElementsDeleter> pExtendedData(new (std::nothrow) HashMap());
43         SysTryReturnVoidResult(NID_SCL, pExtendedData != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
44
45         result r = pExtendedData->Construct();
46         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Failed to construct pExtendedData.", GetErrorMessage(r));
47
48         __pExtendedData = std::move(pExtendedData);
49         __userName = userName;
50 }
51
52 _AccountImpl::_AccountImpl(const _AccountImpl& rhs)
53 {
54         std::unique_ptr<HashMap, AllElementsDeleter> pExtendedData(new (std::nothrow) HashMap());
55         SysTryReturnVoidResult(NID_SCL, pExtendedData != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
56
57         result r = pExtendedData->Construct();
58         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Failed to construct pExtendedData.", GetErrorMessage(r));
59
60         std::unique_ptr<IMapEnumerator> pMapEnum((rhs.__pExtendedData)->GetMapEnumeratorN());
61         SysTryReturnVoidResult(NID_SCL, pMapEnum != null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
62
63         std::unique_ptr<String> pKey(null);
64         std::unique_ptr<String> pVal(null);
65         String* pStr = null;
66
67         while (pMapEnum->MoveNext() == E_SUCCESS)
68         {
69                 pStr = static_cast<String*> (pMapEnum->GetKey());
70                 pKey.reset(new (std::nothrow) String(*pStr));
71                 SysTryReturnVoidResult(NID_SCL, pKey != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
72
73                 pStr = static_cast<String*> (pMapEnum->GetValue());
74                 pVal.reset(new (std::nothrow) String(*pStr));
75                 SysTryReturnVoidResult(NID_SCL, pVal != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
76
77                 r = pExtendedData->Add(pKey.get(), pVal.get());
78                 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), r, "[%s] Propagating.", GetErrorMessage(r));
79
80                 pKey.release();
81                 pVal.release();
82         }
83
84         __pExtendedData = std::move(pExtendedData);
85
86         __id = rhs.__id;
87         __userName = rhs.__userName;
88         __accountProvider = rhs.__accountProvider;
89 }
90
91 _AccountImpl::~_AccountImpl(void)
92 {
93         // empty body.
94 }
95
96 _AccountImpl&
97 _AccountImpl::operator =(const _AccountImpl& rhs)
98 {
99         if (this == &rhs)
100         {
101                 return *this;
102         }
103
104         std::unique_ptr<HashMap, AllElementsDeleter> pExtendedData(new (std::nothrow) HashMap());
105         SysTryReturn(NID_SCL, pExtendedData != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
106
107         result r = pExtendedData->Construct();
108         SysTryReturn(NID_SCL, !IsFailed(r), *this, r, "[%s] Failed to construct pExtendedData.", GetErrorMessage(r));
109
110         result prevResult = GetLastResult();
111
112         std::unique_ptr<IMapEnumerator> pMapEnum((rhs.__pExtendedData)->GetMapEnumeratorN());
113         SysTryReturn(NID_SCL, pMapEnum != null, *this, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
114
115         SetLastResult(prevResult);
116
117         std::unique_ptr<String> pKey(null);
118         std::unique_ptr<String> pVal(null);
119         String* pStr = null;
120
121         while (pMapEnum->MoveNext() == E_SUCCESS)
122         {
123                 pStr = static_cast<String*> (pMapEnum->GetKey());
124                 pKey.reset(new (std::nothrow) String(*pStr));
125                 SysTryReturn(NID_SCL, pKey != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
126
127                 pStr = static_cast<String*> (pMapEnum->GetValue());
128                 pVal.reset(new (std::nothrow) String(*pStr));
129                 SysTryReturn(NID_SCL, pVal != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
130
131                 r = pExtendedData->Add(pKey.get(), pVal.get());
132                 SysTryReturn(NID_SCL, !IsFailed(r), *this, r, "[%s] Propagating.", GetErrorMessage(r));
133
134                 pKey.release();
135                 pVal.release();
136         }
137
138         __pExtendedData = std::move(pExtendedData);
139
140         __id = rhs.__id;
141         __userName = rhs.__userName;
142         __accountProvider = rhs.__accountProvider;
143
144         return *this;
145 }
146
147 bool
148 _AccountImpl::Equals(const Object& rhs) const
149 {
150         const _AccountImpl* pAccountImpl = dynamic_cast<const _AccountImpl*> (&rhs);
151         if (pAccountImpl == null)
152         {
153                 return false;
154         }
155
156         if (__id != pAccountImpl->__id
157                 || __userName != pAccountImpl->__userName)
158         {
159                 return false;
160         }
161
162         if (*(_AccountProviderImpl::GetInstance(__accountProvider)) != *(_AccountProviderImpl::GetInstance(pAccountImpl->__accountProvider)))
163         {
164                 return false;
165         }
166
167         if (!(__pExtendedData->Equals(*(pAccountImpl->__pExtendedData))))
168         {
169                 return false;
170         }
171
172         return true;
173 }
174
175 int
176 _AccountImpl::GetHashCode(void) const
177 {
178         int hashCode = 0xffffffff & __id;
179         hashCode += hashCode;
180
181         return hashCode;
182 }
183
184 AccountId
185 _AccountImpl::GetId(void) const
186 {
187         return __id;
188 }
189
190 AccountProvider
191 _AccountImpl::GetAccountProvider(void) const
192 {
193         return __accountProvider;
194 }
195
196 String
197 _AccountImpl::GetUserName(void) const
198 {
199         return __userName;
200 }
201
202 IMap*
203 _AccountImpl::GetExtendedDataN(void) const
204 {
205         ClearLastResult();
206
207         std::unique_ptr<HashMap, AllElementsDeleter> pExtendedData(new (std::nothrow) HashMap());
208         SysTryReturn(NID_SCL, pExtendedData != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
209
210         result r = pExtendedData->Construct();
211         SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Failed to construct pExtendedData.", GetErrorMessage(r));
212
213         std::unique_ptr<IMapEnumerator> pMapEnum(__pExtendedData->GetMapEnumeratorN());
214         SysTryReturn(NID_SCL, pMapEnum != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
215
216         std::unique_ptr<String> pKey(null);
217         std::unique_ptr<String> pVal(null);
218         String* pStr = null;
219
220         while (pMapEnum->MoveNext() == E_SUCCESS)
221         {
222                 pStr = static_cast<String*> (pMapEnum->GetKey());
223                 pKey.reset(new (std::nothrow) String(*pStr));
224                 SysTryReturn(NID_SCL, pKey != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
225
226                 pStr = static_cast<String*> (pMapEnum->GetValue());
227                 pVal.reset(new (std::nothrow) String(*pStr));
228                 SysTryReturn(NID_SCL, pVal != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
229
230                 r = pExtendedData->Add(pKey.get(), pVal.get());
231                 SysTryReturn(NID_SCL, !IsFailed(r), null, r, "[%s] Propagating.", GetErrorMessage(r));
232
233                 pKey.release();
234                 pVal.release();
235         }
236
237         return pExtendedData.release();
238 }
239
240 void
241 _AccountImpl::SetId(AccountId accountId)
242 {
243         __id = accountId;
244 }
245
246 void
247 _AccountImpl::SetAccountProvider(const AccountProvider& accountProvider)
248 {
249         __accountProvider = accountProvider;
250 }
251
252 result
253 _AccountImpl::SetUserName(const String& userName)
254 {
255         SysTryReturnResult(NID_SCL, !(userName.IsEmpty()), E_INVALID_ARG, "Invalid argument is used. The userName is an empty string.");
256
257         __userName = userName;
258
259         return E_SUCCESS;
260 }
261
262 result
263 _AccountImpl::SetExtendedData(const String& key, const Tizen::Base::String& value)
264 {
265         SysTryReturnResult(NID_SCL, !(key.IsEmpty()), E_INVALID_ARG, "Invalid argument is used. The key is an empty string.");
266
267         std::unique_ptr<String> pValue(new (std::nothrow) String(value));
268         SysTryReturn(NID_SCL, pValue != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
269
270         if (__pExtendedData->ContainsKey(key))
271         {
272                 result r = __pExtendedData->SetValue(key, pValue.get());
273                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
274
275                 pValue.release();
276         }
277         else
278         {
279                 std::unique_ptr<String> pKey(new (std::nothrow) String(key));
280                 SysTryReturn(NID_SCL, pKey != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
281
282                 result r = __pExtendedData->Add(pKey.get(), pValue.get());
283                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
284
285                 pKey.release();
286                 pValue.release();
287         }
288
289         return E_SUCCESS;
290 }
291
292 const _AccountImpl*
293 _AccountImpl::GetInstance(const Account& account)
294 {
295         return account.__pAccountImpl;
296 }
297
298 _AccountImpl*
299 _AccountImpl::GetInstance(Account& account)
300 {
301         return account.__pAccountImpl;
302 }
303
304 }} //Tizen::Social