2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FScl_CategoryImpl.cpp
19 * @brief This is the implementation for _CategoryImpl class.
21 * This file contains definitions of _CategoryImpl class.
25 #include <FBaseString.h>
26 #include <FBaseColArrayListT.h>
27 #include <FSclAddressbook.h>
28 #include <FSclCategory.h>
29 #include <FBaseSysLog.h>
31 #include <FBase_StringConverter.h>
32 #include <FApp_AppInfo.h>
33 #include "FScl_RecordImpl.h"
34 #include "FScl_AddressbookImpl.h"
35 #include "FScl_AddressbookUtil.h"
36 #include "FScl_CategoryImpl.h"
37 #include "FScl_ContactDbConnector.h"
39 using namespace Tizen::App;
40 using namespace Tizen::Base;
41 using namespace Tizen::Base::Collection;
42 using namespace Tizen::Io;
44 namespace Tizen { namespace Social
46 _CategoryImpl::_CategoryImpl(void)
47 : __hasMemberList(false)
50 , __pAddedMembers(null)
51 , __pRemovedMembers(null)
54 int ret = CONTACTS_ERROR_NONE;
55 contacts_record_h recordHandle = null;
57 SysTryReturnVoidResult(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
59 std::unique_ptr< ArrayListT<int> > pMembers (new (std::nothrow) ArrayListT<int>());
60 SysTryReturnVoidResult(NID_SCL, pMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
62 r = pMembers->Construct();
63 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
65 std::unique_ptr< ArrayListT<int> > pAddedMembers (new (std::nothrow) ArrayListT<int>());
66 SysTryReturnVoidResult(NID_SCL, pAddedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
68 r = pAddedMembers->Construct();
69 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
71 std::unique_ptr< ArrayListT<int> > pRemovedMembers (new (std::nothrow) ArrayListT<int>());
72 SysTryReturnVoidResult(NID_SCL, pRemovedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
74 r = pRemovedMembers->Construct();
75 SysTryReturnVoidResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
77 ret = contacts_record_create(_contacts_group._uri, &recordHandle);
78 SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
80 __pMembers = std::move(pMembers);
81 __pAddedMembers = std::move(pAddedMembers);
82 __pRemovedMembers = std::move(pRemovedMembers);
84 __recordHandle = recordHandle;
89 _CategoryImpl::_CategoryImpl(const _CategoryImpl& rhs)
90 : __hasMemberList(false)
93 , __pAddedMembers(null)
94 , __pRemovedMembers(null)
96 int ret = CONTACTS_ERROR_NONE;
97 contacts_record_h recordHandle = null;
99 SysTryReturnVoidResult(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
101 std::unique_ptr< IListT<int> > pMembers (rhs.GetMembersN());
102 SysTryReturnVoidResult(NID_SCL, pMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
104 std::unique_ptr< IListT<int> > pAddedMembers (rhs.GetAddedMembersN());
105 SysTryReturnVoidResult(NID_SCL, pAddedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
107 std::unique_ptr< IListT<int> > pRemovedMembers (rhs.GetRemovedMembersN());
108 SysTryReturnVoidResult(NID_SCL, pRemovedMembers != null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
110 ret = contacts_record_clone(rhs.__recordHandle, &recordHandle);
111 SysTryReturnVoidResult(NID_SCL, ret == CONTACTS_ERROR_NONE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
113 __hasMemberList = rhs.__hasMemberList;
114 __memberCount = rhs.__memberCount;
116 __pMembers = std::move(pMembers);
117 __pAddedMembers = std::move(pAddedMembers);
118 __pRemovedMembers = std::move(pRemovedMembers);
120 __recordHandle = recordHandle;
125 _CategoryImpl::~_CategoryImpl(void)
127 if (__recordHandle != null)
129 contacts_record_destroy(__recordHandle, true);
134 _CategoryImpl::operator =(const _CategoryImpl& rhs)
141 int ret = CONTACTS_ERROR_NONE;
142 contacts_record_h recordHandle = null;
144 std::unique_ptr< IListT<int> > pMembers (rhs.GetMembersN());
145 SysTryReturn(NID_SCL, pMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
147 std::unique_ptr< IListT<int> > pAddedMembers (rhs.GetAddedMembersN());
148 SysTryReturn(NID_SCL, pAddedMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
150 std::unique_ptr< IListT<int> > pRemovedMembers (rhs.GetRemovedMembersN());
151 SysTryReturn(NID_SCL, pRemovedMembers != null, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
153 ret = contacts_record_clone(rhs.__recordHandle, &recordHandle);
154 SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, *this, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
156 __hasMemberList = rhs.__hasMemberList;
157 __memberCount = rhs.__memberCount;
159 __pMembers = std::move(pMembers);
160 __pAddedMembers = std::move(pAddedMembers);
161 __pRemovedMembers = std::move(pRemovedMembers);
164 contacts_record_destroy(__recordHandle, true);
165 __recordHandle = recordHandle;
171 _CategoryImpl::IsDefault(void) const
173 bool isDefault = false;
175 contacts_record_get_bool(__recordHandle, _contacts_group.is_read_only, &isDefault);
181 _CategoryImpl::SetRecordHandle(contacts_record_h recordHandle)
183 contacts_record_destroy(__recordHandle, true);
185 __recordHandle = recordHandle;
189 _CategoryImpl::GetRecordHandle(void) const
191 return __recordHandle;
195 _CategoryImpl::AddMember(RecordId contactId)
197 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);
199 result r = E_SUCCESS;
200 int tableId = contactId;
202 if (!__hasMemberList)
204 r = LoadMemberList();
205 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
208 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));
210 r = __pMembers->Add(tableId);
211 SysTryReturnResult(NID_SCL, !IsFailed(r), E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
213 if (__pRemovedMembers->Contains(tableId))
215 __pRemovedMembers->Remove(tableId);
219 result r = __pAddedMembers->Add(tableId);
222 __pMembers->Remove(tableId);
224 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
236 _CategoryImpl::GetMemberCount(void) const
238 return __memberCount;
242 _CategoryImpl::SetMemberCount(int memberCount)
244 __memberCount = memberCount;
248 _CategoryImpl::GetName(void) const
250 char* pCharValue = null;
252 contacts_record_get_str_p(__recordHandle, _contacts_group.name, &pCharValue);
254 return String(pCharValue);
258 _CategoryImpl::HasMember(RecordId contactId) const
260 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);
262 result r = E_SUCCESS;
263 int tableId = contactId;
265 if (!__hasMemberList)
267 _CategoryImpl* pThis = const_cast<_CategoryImpl*>(this);
268 r = pThis->LoadMemberList();
269 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
272 return __pMembers->Contains(tableId);
276 _CategoryImpl::RemoveMember(RecordId contactId)
278 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);
280 result r = E_SUCCESS;
281 int tableId = contactId;
283 if (!__hasMemberList)
285 r = LoadMemberList();
286 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
289 if (!_AppInfo::IsOspCompat())
291 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));
294 // check pended delete list
295 if (__pRemovedMembers->Contains(tableId))
300 if (!HasMember(contactId))
305 r = __pMembers->Remove(tableId);
306 SysTryReturn(NID_SCL, !IsFailed(r), r, r, "[%s] Propagating.", GetErrorMessage(r));
308 if (__pAddedMembers->Contains(tableId))
310 __pAddedMembers->Remove(tableId);
314 result r = __pRemovedMembers->Add(tableId);
317 __pMembers->Add(tableId);
318 SysLogException(NID_SCL, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
330 _CategoryImpl::SetName(const String& name)
332 bool isDefault = false;
333 contacts_record_get_bool(__recordHandle, _contacts_group.is_read_only, &isDefault);
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;