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