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 char* pCharValue = null;
174 contacts_record_get_str_p(__recordHandle, _contacts_group.extra_data, &pCharValue);
175 if (pCharValue != null)
184 _CategoryImpl::SetRecordHandle(contacts_record_h recordHandle)
186 contacts_record_destroy(__recordHandle, true);
188 __recordHandle = recordHandle;
192 _CategoryImpl::GetRecordHandle(void) const
194 return __recordHandle;
198 _CategoryImpl::AddMember(RecordId contactId)
200 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);
202 result r = E_SUCCESS;
203 int tableId = contactId;
205 if (!__hasMemberList)
207 r = LoadMemberList();
208 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
211 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));
213 r = __pMembers->Add(tableId);
214 SysTryReturnResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
216 if (__pRemovedMembers->Contains(tableId))
218 __pRemovedMembers->Remove(tableId);
222 result r = __pAddedMembers->Add(tableId);
225 __pMembers->Remove(tableId);
227 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
239 _CategoryImpl::GetMemberCount(void) const
241 return __memberCount;
245 _CategoryImpl::SetMemberCount(int memberCount)
247 __memberCount = memberCount;
251 _CategoryImpl::GetName(void) const
253 char* pCharValue = null;
255 contacts_record_get_str_p(__recordHandle, _contacts_group.name, &pCharValue);
257 return String(pCharValue);
261 _CategoryImpl::HasMember(RecordId contactId) const
263 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);
265 result r = E_SUCCESS;
266 int tableId = contactId;
268 if (!__hasMemberList)
270 _CategoryImpl* pThis = const_cast<_CategoryImpl*>(this);
271 r = pThis->LoadMemberList();
272 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
275 return __pMembers->Contains(tableId);
279 _CategoryImpl::RemoveMember(RecordId contactId)
281 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);
283 result r = E_SUCCESS;
284 int tableId = contactId;
286 if (!__hasMemberList)
288 r = LoadMemberList();
289 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
292 if (!_AppInfo::IsOspCompat())
294 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));
297 // check pended delete list
298 if (__pRemovedMembers->Contains(tableId))
303 if (!HasMember(contactId))
308 r = __pMembers->Remove(tableId);
309 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
311 if (__pAddedMembers->Contains(tableId))
313 __pAddedMembers->Remove(tableId);
317 result r = __pRemovedMembers->Add(tableId);
320 __pMembers->Add(tableId);
321 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
333 _CategoryImpl::SetName(const String& name)
335 if (_AppInfo::GetApiVersion() == _API_VERSION_2_0 && _AppInfo::IsOspCompat())
337 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));
338 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));
342 SysTryReturn(NID_SCL, !IsDefault(), E_INVALID_OPERATION, E_INVALID_OPERATION, "[%s] This category is a default category.", GetErrorMessage(E_INVALID_OPERATION));
345 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));
347 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(name));
348 SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
349 contacts_record_set_str(__recordHandle, _contacts_group.name, pCharArray.get());
355 _CategoryImpl::SetRingtonePath(const String& filePath)
357 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));
359 if (!filePath.IsEmpty())
361 std::unique_ptr<char[]> pCharArray(_StringConverter::CopyToCharArrayN(filePath));
362 SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
364 contacts_record_set_str(__recordHandle, _contacts_group.ringtone_path, pCharArray.get());
368 contacts_record_set_str(__recordHandle, _contacts_group.ringtone_path, null);
375 _CategoryImpl::GetRingtonePath(void) const
377 char* pCharValue = null;
379 contacts_record_get_str_p(__recordHandle, _contacts_group.ringtone_path, &pCharValue);
381 return String(pCharValue);
385 _CategoryImpl::GetMembersN(void) const
387 std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
388 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
390 result r = pList->Construct(*__pMembers);
391 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
393 return pList.release();
397 _CategoryImpl::GetAddedMembersN(void) const
399 std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
400 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
402 result r = pList->Construct(*__pAddedMembers);
403 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
405 return pList.release();
409 _CategoryImpl::GetRemovedMembersN(void) const
411 std::unique_ptr<ArrayListT<int> > pList(new (std::nothrow) ArrayListT<int>());
412 SysTryReturn(NID_SCL, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
414 result r = pList->Construct(*__pRemovedMembers);
415 SysTryReturn(NID_SCL, !IsFailed(r), null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
417 return pList.release();
421 _CategoryImpl::LoadMemberList(void)
423 SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
426 result r = E_SUCCESS;
427 std::unique_ptr< ArrayListT<RecordId> > pMembers;
429 contacts_record_get_int(__recordHandle, _contacts_group.id, &intValue);
432 __hasMemberList = true;
437 contacts_record_h currentRecord = null;
439 __Filter<__ContactsContactGroupRel> filter;
441 filter.AddInt(_contacts_contact_grouprel.group_id, CONTACTS_MATCH_EQUAL, intValue);
443 __Query<__ContactsContactGroupRel> query;
445 query.SetFilter(filter);
447 std::unique_ptr<__SearchResult<__ContactsContactGroupRel> > pSearchResult(_AddressbookUtil::ExecuteQuery(query));
448 SysTryReturn(NID_SCL, pSearchResult != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
450 while (pSearchResult->MoveNext() == E_SUCCESS)
452 currentRecord = pSearchResult->GetCurrentRecord();
453 SysTryReturn(NID_SCL, currentRecord != null, GetLastResult(), GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
455 contacts_record_get_int(currentRecord, _contacts_contact_grouprel.contact_id, &intValue);
459 r = __pMembers->Add(intValue);
460 SysTryReturn(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
464 __memberCount = __pMembers->GetCount();
465 __hasMemberList = true;
471 _CategoryImpl::ClearAddedMemberList(void)
473 __pAddedMembers->RemoveAll();
477 _CategoryImpl::ClearRemovedMemberList(void)
479 __pRemovedMembers->RemoveAll();
483 _CategoryImpl::GetAddressbookId(void) const
487 contacts_record_get_int(__recordHandle, _contacts_group.address_book_id, &intValue);
493 _CategoryImpl::SetThumbnail(const String& filePath)
495 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));
497 if (!filePath.IsEmpty())
499 std::unique_ptr<char[]> pCharArray( _StringConverter::CopyToCharArrayN(filePath));
500 SysTryReturnResult(NID_SCL, pCharArray != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
502 contacts_record_set_str(__recordHandle, _contacts_group.image_path, pCharArray.get());
506 contacts_record_set_str(__recordHandle, _contacts_group.image_path, null);
513 _CategoryImpl::GetThumbnailPath(void) const
515 char* pCharValue = null;
517 contacts_record_get_str_p(__recordHandle, _contacts_group.image_path, &pCharValue);
519 return String(pCharValue);
523 _CategoryImpl::Invalidate(void)
525 contacts_record_h recordHandle = null;
526 int ret = CONTACTS_ERROR_NONE;
527 char* pCharValue = null;
529 ret = contacts_record_create(_contacts_group._uri, &recordHandle);
530 SysTryReturnResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
532 contacts_record_get_str_p(__recordHandle, _contacts_group.name, &pCharValue);
533 contacts_record_set_str(recordHandle, _contacts_group.name, pCharValue);
535 contacts_record_get_str_p(__recordHandle, _contacts_group.ringtone_path, &pCharValue);
536 contacts_record_set_str(recordHandle, _contacts_group.ringtone_path, pCharValue);
538 contacts_record_get_str_p(__recordHandle, _contacts_group.image_path, &pCharValue);
539 contacts_record_set_str(recordHandle, _contacts_group.image_path, pCharValue);
541 contacts_record_get_str_p(__recordHandle, _contacts_group.vibration, &pCharValue);
542 contacts_record_set_str(recordHandle, _contacts_group.vibration, pCharValue);
544 contacts_record_destroy(__recordHandle, true);
545 __recordHandle = recordHandle;
551 _CategoryImpl::GetInstance(const Category& category)
553 return category.__pCategoryImpl;
557 _CategoryImpl::GetInstance(Category& category)
559 return category.__pCategoryImpl;