Init Tizen 2.2.1
[framework/osp/social.git] / src / FScl_AddressbookUtil.cpp
index aba153d..521acbd 100644 (file)
@@ -21,6 +21,7 @@
  */
 
 #include <stdlib.h>
+#include <unique_ptr.h>
 #include <FBaseDateTime.h>
 #include <FBaseColArrayList.h>
 #include <FBaseColArrayListT.h>
@@ -54,6 +55,7 @@
 #include "FScl_AddressbookImpl.h"
 #include "FScl_UserProfileImpl.h"
 
+using namespace std;
 using namespace Tizen::Base;
 using namespace Tizen::Base::Collection;
 
@@ -136,7 +138,6 @@ __ContactsGroupRelation::ConvertResultTo<Category>(__SearchResult<__ContactsGrou
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
        int intValue = 0;
-       contacts_record_h newRecord = null;
 
        contacts_record_h currentRecord = searchResult.GetCurrentRecord();
        SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
@@ -147,12 +148,10 @@ __ContactsGroupRelation::ConvertResultTo<Category>(__SearchResult<__ContactsGrou
        std::unique_ptr<Category> pCategory(new (std::nothrow) Category());
        SysTryReturn(NID_SCL, pCategory != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
-       int ret = contacts_db_get_record(_contacts_group._uri, intValue, &newRecord);
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-       SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
+       unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, intValue));
+       SysTryReturn(NID_SCL, pCategoryRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
-       _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(newRecord);
+       _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(pCategoryRecord.release());
        _RecordImpl::GetInstance(*pCategory)->SetRecordId(intValue);
 
        __Filter<__ContactsGroupRelation> filter;
@@ -184,7 +183,6 @@ __ContactsContactGroupRel::ConvertResultTo<Category>(__SearchResult<__ContactsCo
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
        int intValue = 0;
-       contacts_record_h newRecord = null;
 
        contacts_record_h currentRecord = searchResult.GetCurrentRecord();
        SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
@@ -195,12 +193,10 @@ __ContactsContactGroupRel::ConvertResultTo<Category>(__SearchResult<__ContactsCo
        std::unique_ptr<Category> pCategory(new (std::nothrow) Category());
        SysTryReturn(NID_SCL, pCategory != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       int ret = contacts_db_get_record(_contacts_group._uri, intValue, &newRecord);
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-       SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
+       unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, intValue));
+       SysTryReturn(NID_SCL, pCategoryRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
-       _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(newRecord);
+       _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(pCategoryRecord.release());
        _RecordImpl::GetInstance(*pCategory)->SetRecordId(intValue);
 
        __Filter<__ContactsGroupRelation> filter;
@@ -233,7 +229,6 @@ __ContactsContactGroupRel::ConvertResultTo<Contact>(__SearchResult<__ContactsCon
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
        int intValue = 0;
-       contacts_record_h newRecord = null;
 
        contacts_record_h currentRecord = searchResult.GetCurrentRecord();
        SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
@@ -244,12 +239,10 @@ __ContactsContactGroupRel::ConvertResultTo<Contact>(__SearchResult<__ContactsCon
        std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
        SysTryReturn(NID_SCL, pContact != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       int ret = contacts_db_get_record(_contacts_contact._uri, intValue, &newRecord);
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-       SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
+       unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, intValue));
+       SysTryReturn(NID_SCL, pContactRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
-       _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecord);
+       _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(pContactRecord.release());
        _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
 
        return pContact.release();
@@ -263,7 +256,6 @@ __ContactsContactEmail::ConvertResultTo<Contact>(__SearchResult<__ContactsContac
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
        int intValue = 0;
-       contacts_record_h newRecord = null;
 
        contacts_record_h currentRecord = searchResult.GetCurrentRecord();
        SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
@@ -274,12 +266,10 @@ __ContactsContactEmail::ConvertResultTo<Contact>(__SearchResult<__ContactsContac
        std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
        SysTryReturn(NID_SCL, pContact != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       int ret = contacts_db_get_record(_contacts_contact._uri, intValue, &newRecord);
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-       SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
+       unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, intValue));
+       SysTryReturn(NID_SCL, pContactRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
-       _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecord);
+       _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(pContactRecord.release());
        _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
 
        return pContact.release();
@@ -365,7 +355,6 @@ __ContactsContactNumber::ConvertResultTo<Contact>(__SearchResult<__ContactsConta
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
        int intValue = 0;
-       contacts_record_h newRecord = null;
 
        contacts_record_h currentRecord = searchResult.GetCurrentRecord();
        SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
@@ -376,12 +365,10 @@ __ContactsContactNumber::ConvertResultTo<Contact>(__SearchResult<__ContactsConta
        std::unique_ptr<Contact> pContact(new (std::nothrow) Contact());
        SysTryReturn(NID_SCL, pContact != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
 
-       int ret = contacts_db_get_record(_contacts_contact._uri, intValue, &newRecord);
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The contact is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-       SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
+       unique_ptr<ContactRecord, ContactRecordDeleter> pContactRecord(_AddressbookUtil::GetContactRecordN(_contacts_contact._uri, intValue));
+       SysTryReturn(NID_SCL, pContactRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
-       _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(newRecord);
+       _ContactImpl::GetInstance(*pContact)->SetContactRecordHandle(pContactRecord.release());
        _RecordImpl::GetInstance(*pContact)->SetRecordId(intValue);
 
        return pContact.release();
@@ -539,7 +526,7 @@ __ContactsPerson::ConvertResultTo<Person>(__SearchResult<__ContactsPerson>& quer
 
        contacts_record_get_str_p(currentRecord, _contacts_person.display_name, &pCharValue);
        _PersonImpl::GetInstance(*pPerson)->SetDisplayName(pCharValue);
-       
+
        return pPerson.release();
 
 }
@@ -548,7 +535,7 @@ Person*
 _AddressbookUtil::CreatePersonN(void)
 {
        Person* pPerson = new (std::nothrow) Person();
-       return pPerson;         
+       return pPerson;
 }
 
 template<>
@@ -588,7 +575,7 @@ __ContactsPersonGroupRel::ConvertResultTo<Person>(__SearchResult<__ContactsPerso
 
        contacts_record_get_str_p(currentRecord, _contacts_person_grouprel.display_name, &pCharValue);
        _PersonImpl::GetInstance(*pPerson)->SetDisplayName(pCharValue);
-       
+
        return pPerson.release();
 }
 
@@ -599,7 +586,6 @@ __ContactsPersonGroupRel::ConvertResultTo<Category>(__SearchResult<__ContactsPer
        SysTryReturn(NID_SCL, _ContactDbConnector::EnsureDbConnection() == E_SUCCESS, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
 
        int intValue = 0;
-       contacts_record_h newRecord = null;
 
        contacts_record_h currentRecord = searchResult.GetCurrentRecord();
        SysTryReturn(NID_SCL, currentRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
@@ -610,12 +596,11 @@ __ContactsPersonGroupRel::ConvertResultTo<Category>(__SearchResult<__ContactsPer
        contacts_record_get_int(currentRecord, _contacts_person_grouprel.group_id, &intValue);
        SysTryReturn(NID_SCL, intValue > 0, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
 
-       int ret = contacts_db_get_record(_contacts_group._uri, intValue, &newRecord);
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_NO_DATA, null, E_OBJ_NOT_FOUND, "[%s] The category is not found.", GetErrorMessage(E_OBJ_NOT_FOUND));
-       SysTryReturn(NID_SCL, ret != CONTACTS_ERROR_OUT_OF_MEMORY, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
-       SysTryReturn(NID_SCL, ret == CONTACTS_ERROR_NONE, null, E_SYSTEM, "[%s] A system error has been occurred.", GetErrorMessage(E_SYSTEM));
 
-       _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(newRecord);
+       unique_ptr<ContactRecord, ContactRecordDeleter> pCategoryRecord(_AddressbookUtil::GetContactRecordN(_contacts_group._uri, intValue));
+        SysTryReturn(NID_SCL, pCategoryRecord != null, null, GetLastResult(), "[%s] Propagating.", GetErrorMessage(GetLastResult()));
+
+       _CategoryImpl::GetInstance(*pCategory)->SetRecordHandle(pCategoryRecord.release());
        _RecordImpl::GetInstance(*pCategory)->SetRecordId(intValue);
 
        __Filter<__ContactsGroupRelation> filter;
@@ -674,7 +659,7 @@ __ContactsPerson::ConvertHandleTo<Person>(contacts_record_h recordHandle)
 
        contacts_record_get_str_p(recordHandle, _contacts_person.display_name, &pCharValue);
        _PersonImpl::GetInstance(*pPerson)->SetDisplayName(pCharValue);
-       
+
        return pPerson.release();
 }
 
@@ -712,7 +697,7 @@ __ContactsPersonGroupRel::ConvertHandleTo<Person>(contacts_record_h recordHandle
 
        contacts_record_get_str_p(recordHandle, _contacts_person_grouprel.display_name, &pCharValue);
        _PersonImpl::GetInstance(*pPerson)->SetDisplayName(pCharValue);
-       
+
        return pPerson.release();
 }