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_CategoryImpl.cpp
18 * @brief This is the implementation for _CategoryImpl class.
20 * This file contains definitions of _CategoryImpl class.
24 #include <FBaseString.h>
25 #include <FBaseColArrayListT.h>
26 #include <FSclAddressbook.h>
27 #include <FSclCategory.h>
28 #include <FBaseSysLog.h>
30 #include <FBase_StringConverter.h>
31 #include <FApp_AppInfo.h>
32 #include "FScl_RecordImpl.h"
33 #include "FScl_AddressbookImpl.h"
34 #include "FScl_AddressbookUtil.h"
35 #include "FScl_CategoryImpl.h"
36 #include "FScl_ContactDbConnector.h"
38 using namespace Tizen::App;
39 using namespace Tizen::Base;
40 using namespace Tizen::Base::Collection;
41 using namespace Tizen::Io;
43 namespace Tizen { namespace Social
45 _CategoryImpl::_CategoryImpl(void)
46 : __hasMemberList(false)
49 , __pAddedMembers(null)
50 , __pRemovedMembers(null)
53 int ret = CONTACTS_ERROR_NONE;
54 contacts_record_h recordHandle = null;
56 SysTryReturnVoidResult(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
58 std::unique_ptr< ArrayListT<int> > pMembers (new (std::nothrow) ArrayListT<int>());
59 SysTryReturnVoidResult(NID_SCL, pMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
61 r = pMembers->Construct();
62 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
64 std::unique_ptr< ArrayListT<int> > pAddedMembers (new (std::nothrow) ArrayListT<int>());
65 SysTryReturnVoidResult(NID_SCL, pAddedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
67 r = pAddedMembers->Construct();
68 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
70 std::unique_ptr< ArrayListT<int> > pRemovedMembers (new (std::nothrow) ArrayListT<int>());
71 SysTryReturnVoidResult(NID_SCL, pRemovedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
73 r = pRemovedMembers->Construct();
74 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
76 ret = contacts_record_create(_contacts_group._uri, &recordHandle);
77 SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
79 __pMembers = std::move(pMembers);
80 __pAddedMembers = std::move(pAddedMembers);
81 __pRemovedMembers = std::move(pRemovedMembers);
83 __recordHandle = recordHandle;
88 _CategoryImpl::_CategoryImpl(const _CategoryImpl& rhs)
89 : __hasMemberList(false)
92 , __pAddedMembers(null)
93 , __pRemovedMembers(null)
95 int ret = CONTACTS_ERROR_NONE;
96 contacts_record_h recordHandle = null;
98 SysTryReturnVoidResult(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
100 std::unique_ptr< IListT<int> > pMembers (rhs.GetMembersN());
101 SysTryReturnVoidResult(NID_SCL, pMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
103 std::unique_ptr< IListT<int> > pAddedMembers (rhs.GetAddedMembersN());
104 SysTryReturnVoidResult(NID_SCL, pAddedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
106 std::unique_ptr< IListT<int> > pRemovedMembers (rhs.GetRemovedMembersN());
107 SysTryReturnVoidResult(NID_SCL, pRemovedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
109 ret = contacts_record_clone(rhs.__recordHandle, &recordHandle);
110 SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
112 __hasMemberList = rhs.__hasMemberList;
113 __memberCount = rhs.__memberCount;
115 __pMembers = std::move(pMembers);
116 __pAddedMembers = std::move(pAddedMembers);
117 __pRemovedMembers = std::move(pRemovedMembers);
119 __recordHandle = recordHandle;
124 _CategoryImpl::~_CategoryImpl(void)
126 if (__recordHandle != null)
128 contacts_record_destroy(__recordHandle, true);
133 _CategoryImpl::operator =(const _CategoryImpl& rhs)
140 int ret = CONTACTS_ERROR_NONE;
141 contacts_record_h recordHandle = null;
143 std::unique_ptr< IListT<int> > pMembers (rhs.GetMembersN());
144 SysTryReturn(NID_SCL, pMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
146 std::unique_ptr< IListT<int> > pAddedMembers (rhs.GetAddedMembersN());
147 SysTryReturn(NID_SCL, pAddedMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
149 std::unique_ptr< IListT<int> > pRemovedMembers (rhs.GetRemovedMembersN());
150 SysTryReturn(NID_SCL, pRemovedMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
152 ret = contacts_record_clone(rhs.__recordHandle, &recordHandle);
153 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
155 __hasMemberList = rhs.__hasMemberList;
156 __memberCount = rhs.__memberCount;
158 __pMembers = std::move(pMembers);
159 __pAddedMembers = std::move(pAddedMembers);
160 __pRemovedMembers = std::move(pRemovedMembers);
163 contacts_record_destroy(__recordHandle, true);
164 __recordHandle = recordHandle;
170 _CategoryImpl::IsDefault(void) const
172 bool isDefault = false;
174 contacts_record_get_bool(__recordHandle, _contacts_group.is_read_only, &isDefault);
180 _CategoryImpl::SetRecordHandle(contacts_record_h recordHandle)
182 contacts_record_destroy(__recordHandle, true);
184 __recordHandle = recordHandle;
188 _CategoryImpl::GetRecordHandle(void) const
190 return __recordHandle;
194 _CategoryImpl::AddMember(RecordId contactId)
196 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);
198 result r = E_SUCCESS;
199 int tableId = contactId;
201 if (!__hasMemberList)
203 r = LoadMemberList();
204 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
207 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));
209 r = __pMembers->Add(tableId);
210 SysTryReturnResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
212 if (__pRemovedMembers->Contains(tableId))
214 __pRemovedMembers->Remove(tableId);
218 result r = __pAddedMembers->Add(tableId);
221 __pMembers->Remove(tableId);
223 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
235 _CategoryImpl::GetMemberCount(void) const
237 return __memberCount;
241 _CategoryImpl::SetMemberCount(int memberCount)
243 __memberCount = memberCount;
247 _CategoryImpl::GetName(void) const
249 char* pCharValue = null;
251 contacts_record_get_str_p(__recordHandle, _contacts_group.name, &pCharValue);
253 return String(pCharValue);
257 _CategoryImpl::HasMember(RecordId contactId) const
259 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);
261 result r = E_SUCCESS;
262 int tableId = contactId;
264 if (!__hasMemberList)
266 _CategoryImpl* pThis = const_cast<_CategoryImpl*>(this);
267 r = pThis->LoadMemberList();
268 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
271 return __pMembers->Contains(tableId);
275 _CategoryImpl::RemoveMember(RecordId contactId)
277 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);
279 result r = E_SUCCESS;
280 int tableId = contactId;
282 if (!__hasMemberList)
284 r = LoadMemberList();
285 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
288 if (!_AppInfo::IsOspCompat())
290 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));
293 // check pended delete list
294 if (__pRemovedMembers->Contains(tableId))
299 if (!HasMember(contactId))
304 r = __pMembers->Remove(tableId);
305 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
307 if (__pAddedMembers->Contains(tableId))
309 __pAddedMembers->Remove(tableId);
313 result r = __pRemovedMembers->Add(tableId);
316 __pMembers->Add(tableId);
317 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
329 _CategoryImpl::SetName(const String& name)
331 bool isDefault = false;
332 contacts_record_get_bool(__recordHandle, _contacts_group.is_read_only, &isDefault);
334 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
336 SysTryReturn(NID_SCL, !isDefault, E_INVALID_ARG, E_INVALID_ARG, "[%s] Invalid argument is used. This category is a default category.", GetErrorMessage(E_INVALID_ARG));
337 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));
341 SysTryReturn(NID_SCL, !isDefault, E_INVALID_OPERATION, E_INVALID_OPERATION, "[%s] This category is a default category.", GetErrorMessage(E_INVALID_OPERATION));
344 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));
346 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
347 SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
348 contacts_record_set_str(__recordHandle, _contacts_group.name, pCharArray.get());
354 _CategoryImpl::SetRingtonePath(const String& filePath)
356 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));
358 if (!filePath.IsEmpty())
360 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(filePath));
361 SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
363 contacts_record_set_str(__recordHandle, _contacts_group.ringtone_path, pCharArray.get());
367 contacts_record_set_str(__recordHandle, _contacts_group.ringtone_path, null);
374 _CategoryImpl::GetRingtonePath(void) const
376 char* pCharValue = null;
378 contacts_record_get_str_p(__recordHandle, _contacts_group.ringtone_path, &pCharValue);
380 return String(pCharValue);
384 _CategoryImpl::GetMembersN(void) const
386 std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
387 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
389 result r = pList->Construct(*__pMembers);
390 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
392 return pList.release();
396 _CategoryImpl::GetAddedMembersN(void) const
398 std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
399 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
401 result r = pList->Construct(*__pAddedMembers);
402 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
404 return pList.release();
408 _CategoryImpl::GetRemovedMembersN(void) const
410 std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
411 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
413 result r = pList->Construct(*__pRemovedMembers);
414 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
416 return pList.release();
420 _CategoryImpl::LoadMemberList(void)
422 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
425 result r = E_SUCCESS;
426 std::unique_ptr< ArrayListT<RecordId> > pMembers;
428 contacts_record_get_int(__recordHandle, _contacts_group.id, &intValue);
431 __hasMemberList = true;
436 contacts_record_h currentRecord = null;
438 __Filter<__ContactsContactGroupRel> filter;
440 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, intValue);
442 __Query<__ContactsContactGroupRel> query;
444 query.SetFilter(filter);
446 std::unique_ptr<__SearchResult<__ContactsContactGroupRel> > pSearchResult(_AddressbookUtil::ExecuteQuery(query));
447 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
449 while (pSearchResult->MoveNext() == E_SUCCESS)
451 currentRecord = pSearchResult->GetCurrentRecord();
452 SysTryReturn(NID_SCL, currentRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
454 contacts_record_get_int(currentRecord, _contacts_contact_grouprel.contact_id, &intValue);
458 r = __pMembers->Add(intValue);
459 SysTryReturn(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
463 __memberCount = __pMembers->GetCount();
464 __hasMemberList = true;
470 _CategoryImpl::ClearAddedMemberList(void)
472 __pAddedMembers->RemoveAll();
476 _CategoryImpl::ClearRemovedMemberList(void)
478 __pRemovedMembers->RemoveAll();
482 _CategoryImpl::GetAddressbookId(void) const
486 contacts_record_get_int(__recordHandle, _contacts_group.address_book_id, &intValue);
492 _CategoryImpl::SetThumbnail(const String& filePath)
494 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));
496 if (!filePath.IsEmpty())
498 std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(filePath));
499 SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
501 contacts_record_set_str(__recordHandle, _contacts_group.image_path, pCharArray.get());
505 contacts_record_set_str(__recordHandle, _contacts_group.image_path, null);
512 _CategoryImpl::GetThumbnailPath(void) const
514 char* pCharValue = null;
516 contacts_record_get_str_p(__recordHandle, _contacts_group.image_path, &pCharValue);
518 return String(pCharValue);
522 _CategoryImpl::Invalidate(void)
524 contacts_record_h recordHandle = null;
525 int ret = CONTACTS_ERROR_NONE;
526 char* pCharValue = null;
528 ret = contacts_record_create(_contacts_group._uri, &recordHandle);
529 SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
531 contacts_record_get_str_p(__recordHandle, _contacts_group.name, &pCharValue);
532 contacts_record_set_str(recordHandle, _contacts_group.name, pCharValue);
534 contacts_record_get_str_p(__recordHandle, _contacts_group.ringtone_path, &pCharValue);
535 contacts_record_set_str(recordHandle, _contacts_group.ringtone_path, pCharValue);
537 contacts_record_get_str_p(__recordHandle, _contacts_group.image_path, &pCharValue);
538 contacts_record_set_str(recordHandle, _contacts_group.image_path, pCharValue);
540 contacts_record_get_str_p(__recordHandle, _contacts_group.vibration, &pCharValue);
541 contacts_record_set_str(recordHandle, _contacts_group.vibration, pCharValue);
543 contacts_record_destroy(__recordHandle, true);
544 __recordHandle = recordHandle;
550 _CategoryImpl::GetInstance(const Category& category)
552 return category.__pCategoryImpl;
556 _CategoryImpl::GetInstance(Category& category)
558 return category.__pCategoryImpl;