4d880a0f7d68c62240e1d4a00129f5bcbb7e6caf
[framework/osp/social.git] / src / FScl_CategoryImpl.cpp
1 //
2 // Copyright (c) 2012 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_CategoryImpl.cpp
18  * @brief               This is the implementation for _CategoryImpl class.
19  *
20  * This file contains definitions of _CategoryImpl class.
21  */
22
23 #include <new>
24 #include <FBaseString.h>
25 #include <FBaseColHashMap.h>
26 #include <FBaseColArrayListT.h>
27 #include <FBaseUtilStringUtil.h>
28 #include <FBaseUtilStringTokenizer.h>
29 #include <FSclAddressbook.h>
30 #include <FSclCategory.h>
31 #include <FBaseSysLog.h>
32 #include <FIoFile.h>
33 #include <FBase_StringConverter.h>
34 #include <FApp_AppInfo.h>
35 #include "FScl_RecordImpl.h"
36 #include "FScl_AddressbookImpl.h"
37 #include "FScl_AddressbookUtil.h"
38 #include "FScl_CategoryImpl.h"
39 #include "FScl_ContactDbConnector.h"
40
41 using namespace Tizen::App;
42 using namespace Tizen::Base;
43 using namespace Tizen::Base::Utility;
44 using namespace Tizen::Base::Collection;
45 using namespace Tizen::Io;
46
47 static const wchar_t CATEGORY_EXT_DATA_GROUP_NAME_ID_KEY[] = L"group_name_id";
48
49 namespace Tizen { namespace Social
50 {
51 _CategoryImpl::_CategoryImpl(void)
52         : __hasMemberList(false)
53         , __memberCount(0)
54         , __pMembers(null)
55         , __pAddedMembers(null)
56         , __pRemovedMembers(null)
57 {
58         result r = E_SUCCESS;
59         int ret = CONTACTS_ERROR_NONE;
60         contacts_record_h recordHandle = null;
61
62         _AddressbookUtil::InitContactViews();
63
64         std::unique_ptr< ArrayListT<int> > pMembers (new (std::nothrow) ArrayListT<int>());
65         SysTryReturnVoidResult(NID_SCL, pMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
66
67         r = pMembers->Construct();
68         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
69
70         std::unique_ptr< ArrayListT<int> > pAddedMembers (new (std::nothrow) ArrayListT<int>());
71         SysTryReturnVoidResult(NID_SCL, pAddedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
72
73         r = pAddedMembers->Construct();
74         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
75
76         std::unique_ptr< ArrayListT<int> > pRemovedMembers (new (std::nothrow) ArrayListT<int>());
77         SysTryReturnVoidResult(NID_SCL, pRemovedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
78
79         r = pRemovedMembers->Construct();
80         SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
81
82         ret = contacts_record_create(_contacts_group._uri, &recordHandle);
83         SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
84
85         __pMembers = std::move(pMembers);
86         __pAddedMembers = std::move(pAddedMembers);
87         __pRemovedMembers = std::move(pRemovedMembers);
88
89         __recordHandle = recordHandle;
90
91         return;
92 }
93
94 _CategoryImpl::_CategoryImpl(const _CategoryImpl& rhs)
95         : __hasMemberList(false)
96         , __memberCount(0)
97         , __pMembers(null)
98         , __pAddedMembers(null)
99         , __pRemovedMembers(null)
100 {
101         int ret = CONTACTS_ERROR_NONE;
102         contacts_record_h recordHandle = null;
103
104         _AddressbookUtil::InitContactViews();
105
106         std::unique_ptr< IListT<int> > pMembers (rhs.GetMembersN());
107         SysTryReturnVoidResult(NID_SCL, pMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
108
109         std::unique_ptr< IListT<int> > pAddedMembers (rhs.GetAddedMembersN());
110         SysTryReturnVoidResult(NID_SCL, pAddedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
111
112         std::unique_ptr< IListT<int> > pRemovedMembers (rhs.GetRemovedMembersN());
113         SysTryReturnVoidResult(NID_SCL, pRemovedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
114
115         ret = contacts_record_clone(rhs.__recordHandle, &recordHandle);
116         SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
117
118         __hasMemberList = rhs.__hasMemberList;
119         __memberCount = rhs.__memberCount;
120
121         __pMembers = std::move(pMembers);
122         __pAddedMembers = std::move(pAddedMembers);
123         __pRemovedMembers = std::move(pRemovedMembers);
124
125         __recordHandle = recordHandle;
126
127         return;
128 }
129
130 _CategoryImpl::~_CategoryImpl(void)
131 {
132         if (__recordHandle != null)
133         {
134                 contacts_record_destroy(__recordHandle, true);
135         }
136 }
137
138 _CategoryImpl&
139 _CategoryImpl::operator =(const _CategoryImpl& rhs)
140 {
141         if (this == &rhs)
142         {
143                 return *this;
144         }
145
146         int ret = CONTACTS_ERROR_NONE;
147         contacts_record_h recordHandle = null;
148
149         std::unique_ptr< IListT<int> > pMembers (rhs.GetMembersN());
150         SysTryReturn(NID_SCL, pMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
151
152         std::unique_ptr< IListT<int> > pAddedMembers (rhs.GetAddedMembersN());
153         SysTryReturn(NID_SCL, pAddedMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
154
155         std::unique_ptr< IListT<int> > pRemovedMembers (rhs.GetRemovedMembersN());
156         SysTryReturn(NID_SCL, pRemovedMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
157
158         ret = contacts_record_clone(rhs.__recordHandle, &recordHandle);
159         SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
160
161         __hasMemberList = rhs.__hasMemberList;
162         __memberCount = rhs.__memberCount;
163
164         __pMembers = std::move(pMembers);
165         __pAddedMembers = std::move(pAddedMembers);
166         __pRemovedMembers = std::move(pRemovedMembers);
167
168
169         contacts_record_destroy(__recordHandle, true);
170         __recordHandle = recordHandle;
171
172         return *this;
173 }
174
175 bool
176 _CategoryImpl::Equals(const Tizen::Base::Object& rhs) const
177 {
178         const _CategoryImpl* pCategoryImpl = dynamic_cast<const _CategoryImpl*>(&rhs);
179         if (pCategoryImpl == null)
180         {
181                 return false;
182         }
183
184         if (GetName() != pCategoryImpl->GetName())
185         {
186                 return false;
187         }
188
189         if (GetRingtonePath() != pCategoryImpl->GetRingtonePath())
190         {
191                 return false;
192         }
193
194         if (GetThumbnailPath() != pCategoryImpl->GetThumbnailPath())
195         {
196                 return false;
197         }
198
199         return true;
200 }
201
202 bool
203 _CategoryImpl::IsDefault(void) const
204 {
205         bool isReadOnly = false;
206
207         contacts_record_get_bool(__recordHandle, _contacts_group.is_read_only, &isReadOnly);
208         if (isReadOnly)
209         {
210                 return true;
211         }
212
213         return false;
214 }
215
216 bool
217 _CategoryImpl::IsReadOnly(void) const
218 {
219         bool isReadOnly = false;
220
221         contacts_record_get_bool(__recordHandle, _contacts_group.is_read_only, &isReadOnly);
222         if (isReadOnly)
223         {
224                 return true;
225         }
226
227         return false;
228 }
229
230 IMap*
231 _CategoryImpl::GetExtraDataN(void) const
232 {
233         std::unique_ptr<HashMap, AllElementsDeleter> pExtraData(new (std::nothrow) HashMap());
234         SysTryReturn(NID_SCL, pExtraData != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
235
236         result r = pExtraData->Construct();
237         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
238
239         char* pCharValue = null;
240         contacts_record_get_str_p(__recordHandle, _contacts_group.extra_data, &pCharValue);
241
242         if (pCharValue == null)
243         {
244                 return pExtraData.release();
245         }
246
247         String delim(L":,");
248         String extraData(pCharValue);
249         String decodedString(L"");
250         std::unique_ptr<ByteBuffer> pByteBuffer(null);
251
252         StringTokenizer tokerizer(extraData, delim);
253         String token(L"");
254
255         if (tokerizer.GetTokenCount() == 1) // utf8
256         {
257                 if (GetAddressbookId() == DEFAULT_ADDRESSBOOK_ID)
258                 {
259                         if (tokerizer.GetNextToken(token) == E_SUCCESS)
260                         {
261                                 std::unique_ptr<String> pKey(new (std::nothrow) String(CATEGORY_EXT_DATA_GROUP_NAME_ID_KEY));
262                                 SysTryReturn(NID_SCL, pKey != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
263
264                                 std::unique_ptr<String> pValue(new (std::nothrow) String(token));
265                                 SysTryReturn(NID_SCL, pValue != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
266
267                                 r = pExtraData->Add(pKey.get(), pValue.get());
268                                 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
269
270                                 pKey.release();
271                                 pValue.release();
272                         }
273                 }
274         }
275         else // base64
276         {
277                 while (tokerizer.HasMoreTokens())
278                 {
279                         // key
280                         r = tokerizer.GetNextToken(token);
281                         if (r != E_SUCCESS)
282                         {
283                                 break;
284                         }
285
286                         pByteBuffer.reset(StringUtil::DecodeBase64StringN(token));
287                         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
288
289                         std::unique_ptr<String> pKey(new (std::nothrow) String());
290                         SysTryReturn(NID_SCL, pKey != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
291
292                         StringUtil::Utf8ToString((const char*)pByteBuffer->GetPointer(), *pKey);
293
294                         // value
295                         r = tokerizer.GetNextToken(token);
296                         if (r != E_SUCCESS)
297                         {
298                                 break;
299                         }
300
301                         pByteBuffer.reset(StringUtil::DecodeBase64StringN(token));
302                         SysTryReturn(NID_SCL, pByteBuffer != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
303
304                         std::unique_ptr<String> pValue(new (std::nothrow) String());
305                         SysTryReturn(NID_SCL, pValue != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
306                         StringUtil::Utf8ToString((const char*)pByteBuffer->GetPointer(), *pValue);
307
308                         r = pExtraData->Add(pKey.get(), pValue.get());
309                         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
310
311                         pKey.release();
312                         pValue.release();
313                 }
314         }
315
316         return pExtraData.release();
317 }
318
319 void
320 _CategoryImpl::SetRecordHandle(contacts_record_h recordHandle)
321 {
322         contacts_record_destroy(__recordHandle, true);
323
324         __recordHandle = recordHandle;
325 }
326
327 contacts_record_h
328 _CategoryImpl::GetRecordHandle(void) const
329 {
330         return __recordHandle;
331 }
332
333 result
334 _CategoryImpl::AddMember(RecordId contactId)
335 {
336         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
337
338         result r = E_SUCCESS;
339         int tableId = contactId;
340
341         if (!__hasMemberList)
342         {
343                 r = LoadMemberList();
344                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
345         }
346
347         SysTryReturn(NID_SCL, !HasMember(contactId), E_OBJ_ALREADY_EXIST, E_OBJ_ALREADY_EXIST, "[%s] The member already exist in the category.", GetErrorMessage(E_OBJ_ALREADY_EXIST));
348
349         r = __pMembers->Add(tableId);
350         SysTryReturnResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
351
352         if (__pRemovedMembers->Contains(tableId))
353         {
354                 __pRemovedMembers->Remove(tableId);
355         }
356         else
357         {
358                 result r = __pAddedMembers->Add(tableId);
359                 if (IsFailed(r))
360                 {
361                         __pMembers->Remove(tableId);
362
363                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
364
365                         return r;
366                 }
367         }
368
369         __memberCount++;
370
371         return E_SUCCESS;
372 }
373
374 int
375 _CategoryImpl::GetMemberCount(void) const
376 {
377         return __memberCount;
378 }
379
380 void
381 _CategoryImpl::SetMemberCount(int memberCount)
382 {
383         __memberCount = memberCount;
384 }
385
386 String
387 _CategoryImpl::GetName(void) const
388 {
389         char* pCharValue = null;
390
391         contacts_record_get_str_p(__recordHandle, _contacts_group.name, &pCharValue);
392
393         return String(pCharValue);
394 }
395
396 bool
397 _CategoryImpl::HasMember(RecordId contactId) const
398 {
399         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
400
401         result r = E_SUCCESS;
402         int tableId = contactId;
403
404         if (!__hasMemberList)
405         {
406                 _CategoryImpl* pThis = const_cast<_CategoryImpl*>(this);
407                 r = pThis->LoadMemberList();
408                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
409         }
410
411         return __pMembers->Contains(tableId);
412 }
413
414 result
415 _CategoryImpl::RemoveMember(RecordId contactId)
416 {
417         SysTryReturn(NID_SCL, contactId != INVALID_RECORD_ID, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. contactId = %d.", GetErrorMessage(E_INVALID_ARG), contactId);
418
419         result r = E_SUCCESS;
420         int tableId = contactId;
421
422         if (!__hasMemberList)
423         {
424                 r = LoadMemberList();
425                 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
426         }
427
428         if (!_AppInfo::IsOspCompat())
429         {
430                 SysTryReturn(NID_SCL, HasMember(contactId), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid arguemnt is used. The contact does not exist in the category.", GetErrorMessage(E_INVALID_ARG));
431         }
432
433         // check pended delete list
434         if (__pRemovedMembers->Contains(tableId))
435         {
436                 return E_SUCCESS;
437         }
438
439         if (!HasMember(contactId))
440         {
441                 return E_SUCCESS;
442         }
443
444         r = __pMembers->Remove(tableId);
445         SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
446
447         if (__pAddedMembers->Contains(tableId))
448         {
449                 __pAddedMembers->Remove(tableId);
450         }
451         else
452         {
453                 result r = __pRemovedMembers->Add(tableId);
454                 if (IsFailed(r))
455                 {
456                         __pMembers->Add(tableId);
457                         SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
458
459                         return r;
460                 }
461         }
462
463         __memberCount--;
464
465         return E_SUCCESS;
466 }
467
468 result
469 _CategoryImpl::SetName(const String& name)
470 {
471         if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
472         {
473                 SysTryReturn(NID_SCL, !IsReadOnly(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. This category is a default category.", GetErrorMessage(E_INVALID_ARG));
474                 SysTryReturn(NID_SCL, name.GetLength() <= 100, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified name exceeds the max length.", GetErrorMessage(E_INVALID_ARG));
475         }
476         else
477         {
478                 SysTryReturn(NID_SCL, !IsReadOnly(), E_INVALID_OPERATION, E_INVALID_OPERATION, "[%s] This category is a default category.", GetErrorMessage(E_INVALID_OPERATION));
479         }
480
481         SysTryReturn(NID_SCL, !name.IsEmpty(), E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. The specified file path is an empty string", GetErrorMessage(E_INVALID_ARG));
482
483         std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
484         SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
485         contacts_record_set_str(__recordHandle, _contacts_group.name, pCharArray.get());
486
487         return E_SUCCESS;
488 }
489
490 result
491 _CategoryImpl::SetRingtonePath(const String& filePath)
492 {
493         SysTryReturn(NID_SCL, filePath.IsEmpty() || File::IsFileExist(filePath), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[%s] The specified file is not found.", GetErrorMessage(E_FILE_NOT_FOUND));
494
495         if (!filePath.IsEmpty())
496         {
497                 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(filePath));
498                 SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
499
500                 contacts_record_set_str(__recordHandle, _contacts_group.ringtone_path, pCharArray.get());
501         }
502         else
503         {
504                 contacts_record_set_str(__recordHandle, _contacts_group.ringtone_path, null);
505         }
506
507         return E_SUCCESS;
508 }
509
510 String
511 _CategoryImpl::GetRingtonePath(void) const
512 {
513         char* pCharValue = null;
514
515         contacts_record_get_str_p(__recordHandle, _contacts_group.ringtone_path, &pCharValue);
516
517         return String(pCharValue);
518 }
519
520 IListT<int>*
521 _CategoryImpl::GetMembersN(void) const
522 {
523         std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
524         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
525
526         result r = pList->Construct(*__pMembers);
527         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
528
529         return pList.release();
530 }
531
532 IListT<int>*
533 _CategoryImpl::GetAddedMembersN(void) const
534 {
535         std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
536         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
537
538         result r = pList->Construct(*__pAddedMembers);
539         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
540
541         return pList.release();
542 }
543
544 IListT<int>*
545 _CategoryImpl::GetRemovedMembersN(void) const
546 {
547         std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
548         SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
549
550         result r = pList->Construct(*__pRemovedMembers);
551         SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
552
553         return pList.release();
554 }
555
556 result
557 _CategoryImpl::LoadMemberList(void)
558 {
559         _AddressbookUtil::InitContactViews();
560
561         int intValue = 0;
562         result r = E_SUCCESS;
563         std::unique_ptr< ArrayListT<RecordId> > pMembers;
564
565         contacts_record_get_int(__recordHandle, _contacts_group.id, &intValue);
566         if (intValue <= 0)
567         {
568                 __hasMemberList = true;
569
570                 return E_SUCCESS;
571         }
572
573         contacts_record_h currentRecord = null;
574
575         __Filter<__ContactsContactGroupRel> filter;
576         filter.Construct();
577         filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, intValue);
578
579         __Query<__ContactsContactGroupRel> query;
580         query.Construct();
581         query.SetFilter(filter);
582
583         std::unique_ptr<__SearchResult<__ContactsContactGroupRel> > pSearchResult(_AddressbookUtil::ExecuteQuery(query));
584         SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
585
586         while (pSearchResult->MoveNext() == E_SUCCESS)
587         {
588                 currentRecord = pSearchResult->GetCurrentRecord();
589                 SysTryReturn(NID_SCL, currentRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
590
591                 contacts_record_get_int(currentRecord, _contacts_contact_grouprel.contact_id, &intValue);
592
593                 if (intValue > 0)
594                 {
595                         r = __pMembers->Add(intValue);
596                         SysTryReturn(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
597                 }
598         }
599
600         __memberCount = __pMembers->GetCount();
601         __hasMemberList = true;
602
603         return E_SUCCESS;
604 }
605
606 void
607 _CategoryImpl::ClearAddedMemberList(void)
608 {
609         __pAddedMembers->RemoveAll();
610 }
611
612 void
613 _CategoryImpl::ClearRemovedMemberList(void)
614 {
615         __pRemovedMembers->RemoveAll();
616 }
617
618 AddressbookId
619 _CategoryImpl::GetAddressbookId(void) const
620 {
621         int intValue = 0;
622
623         contacts_record_get_int(__recordHandle, _contacts_group.address_book_id, &intValue);
624
625         return intValue;
626 }
627
628 result
629 _CategoryImpl::SetThumbnail(const String& filePath)
630 {
631         SysTryReturn(NID_SCL, filePath.IsEmpty() || File::IsFileExist(filePath), E_FILE_NOT_FOUND, E_FILE_NOT_FOUND, "[%s] The specified file is not found.", GetErrorMessage(E_FILE_NOT_FOUND));
632
633         if (!filePath.IsEmpty())
634         {
635                 std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(filePath));
636                 SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
637
638                 contacts_record_set_str(__recordHandle, _contacts_group.image_path, pCharArray.get());
639         }
640         else
641         {
642                 contacts_record_set_str(__recordHandle, _contacts_group.image_path, null);
643         }
644
645         return E_SUCCESS;
646 }
647
648 String
649 _CategoryImpl::GetThumbnailPath(void) const
650 {
651         char* pCharValue = null;
652
653         contacts_record_get_str_p(__recordHandle, _contacts_group.image_path, &pCharValue);
654
655         return String(pCharValue);
656 }
657
658 result
659 _CategoryImpl::Invalidate(void)
660 {
661         contacts_record_h recordHandle = null;
662         int ret = CONTACTS_ERROR_NONE;
663         char* pCharValue = null;
664
665         ret = contacts_record_create(_contacts_group._uri, &recordHandle);
666         SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
667
668         contacts_record_get_str_p(__recordHandle, _contacts_group.name, &pCharValue);
669         contacts_record_set_str(recordHandle, _contacts_group.name, pCharValue);
670
671         contacts_record_get_str_p(__recordHandle, _contacts_group.ringtone_path, &pCharValue);
672         contacts_record_set_str(recordHandle, _contacts_group.ringtone_path, pCharValue);
673
674         contacts_record_get_str_p(__recordHandle, _contacts_group.image_path, &pCharValue);
675         contacts_record_set_str(recordHandle, _contacts_group.image_path, pCharValue);
676
677         contacts_record_get_str_p(__recordHandle, _contacts_group.vibration, &pCharValue);
678         contacts_record_set_str(recordHandle, _contacts_group.vibration, pCharValue);
679
680         contacts_record_destroy(__recordHandle, true);
681         __recordHandle = recordHandle;
682
683         return E_SUCCESS;
684 }
685
686 const _CategoryImpl*
687 _CategoryImpl::GetInstance(const Category& category)
688 {
689         return category.__pCategoryImpl;
690 }
691
692 _CategoryImpl*
693 _CategoryImpl::GetInstance(Category& category)
694 {
695         return category.__pCategoryImpl;
696 }
697
698 }}  // Tizen::Social