From 885e1c647e4bf06e544fa7afcc0709a9a60f8508 Mon Sep 17 00:00:00 2001 From: Eugene Kurzberg Date: Mon, 28 Mar 2016 11:31:28 +0300 Subject: [PATCH] Added convenience utility functions for Contacts API records. Change-Id: Ic3e1da633e390e6380ec2cf5a1f11baa06083f75 Signed-off-by: Eugene Kurzberg --- lib-common/inc/Contacts/Utils.h | 57 ++++++++++++++++++++++ lib-contact/inc/Contacts/Model/ContactBoolField.h | 5 -- lib-contact/inc/Contacts/Model/ContactEnumField.h | 5 -- lib-contact/inc/Contacts/Model/ContactTextField.h | 5 -- lib-contact/src/Contacts/List/Model/MyProfile.cpp | 16 ++---- .../src/Contacts/Model/ContactBoolField.cpp | 12 ++--- .../src/Contacts/Model/ContactEnumField.cpp | 12 ++--- .../src/Contacts/Model/ContactTextField.cpp | 12 ++--- lib-logs/src/Logs/Model/Log.cpp | 30 ++++-------- 9 files changed, 79 insertions(+), 75 deletions(-) diff --git a/lib-common/inc/Contacts/Utils.h b/lib-common/inc/Contacts/Utils.h index 06df1ad..a4a549e 100644 --- a/lib-common/inc/Contacts/Utils.h +++ b/lib-common/inc/Contacts/Utils.h @@ -19,6 +19,7 @@ #define CONTACTS_UTILS_H #include +#include "Utils/String.h" #define CONTACTS_LIST_FOREACH(list, record) \ bool success = (contacts_list_get_current_record_p(list, &record) == CONTACTS_ERROR_NONE); \ @@ -30,6 +31,62 @@ namespace Contacts { /** + * @brief Convenience wrapper. + * @see contacts_record_get_str_p() + */ + inline const char *getRecordStr(contacts_record_h record, unsigned propertyId) + { + char *str = nullptr; + contacts_record_get_str_p(record, propertyId, &str); + return str; + } + + /** + * @brief Convenience wrapper. + * @see contacts_record_get_int() + */ + inline int getRecordInt(contacts_record_h record, unsigned propertyId) + { + int value = 0; + contacts_record_get_int(record, propertyId, &value); + return value; + } + + /** + * @brief Convenience wrapper. + * @see contacts_record_get_bool() + */ + inline bool getRecordBool(contacts_record_h record, unsigned propertyId) + { + bool value = 0; + contacts_record_get_bool(record, propertyId, &value); + return value; + } + + /** + * @brief Compare two records by the same string property. + * @param[in] record1 First record + * @param[in] records Second record + * @param[in] propertyId Property to compare by + * @return Whether properties are equal. + */ + inline bool compareRecordsStr(contacts_record_h record1, contacts_record_h record2, + unsigned propertyId) + { + return Utils::safeCmp(getRecordStr(record1, propertyId), getRecordStr(record2, propertyId)); + } + + /** + * @brief Compare two records by the same integer property. + * @see compareRecordsStr() + */ + inline bool compareRecordsInt(contacts_record_h record1, contacts_record_h record2, + unsigned propertyId) + { + return getRecordInt(record1, propertyId) == getRecordInt(record2, propertyId); + } + + /** * @brief Get display contact record for a person. * @param[in] personRecord _contacts_person record * @return Display contact's _contacts_contact record. diff --git a/lib-contact/inc/Contacts/Model/ContactBoolField.h b/lib-contact/inc/Contacts/Model/ContactBoolField.h index dcd22bd..9713fe9 100644 --- a/lib-contact/inc/Contacts/Model/ContactBoolField.h +++ b/lib-contact/inc/Contacts/Model/ContactBoolField.h @@ -51,11 +51,6 @@ namespace Contacts protected: /** - * @brief Get field value from the given record. - */ - bool getValue(contacts_record_h record) const; - - /** * @see ContactField::onInitialize() */ virtual void onInitialize(contacts_record_h record) override; diff --git a/lib-contact/inc/Contacts/Model/ContactEnumField.h b/lib-contact/inc/Contacts/Model/ContactEnumField.h index 53a17b8..0d91209 100644 --- a/lib-contact/inc/Contacts/Model/ContactEnumField.h +++ b/lib-contact/inc/Contacts/Model/ContactEnumField.h @@ -76,11 +76,6 @@ namespace Contacts protected: /** - * @brief Get field value from the given record. - */ - int getValue(contacts_record_h record) const; - - /** * @brief Enum type metadata. */ const ContactEnumMetadata &getEnumMetadata() const; diff --git a/lib-contact/inc/Contacts/Model/ContactTextField.h b/lib-contact/inc/Contacts/Model/ContactTextField.h index aa066c6..677c3e2 100644 --- a/lib-contact/inc/Contacts/Model/ContactTextField.h +++ b/lib-contact/inc/Contacts/Model/ContactTextField.h @@ -62,11 +62,6 @@ namespace Contacts protected: /** - * @brief Get field value from the given record. - */ - const char *getValue(contacts_record_h record) const; - - /** * @see ContactField::onInitialize() */ virtual void onInitialize(contacts_record_h record) override; diff --git a/lib-contact/src/Contacts/List/Model/MyProfile.cpp b/lib-contact/src/Contacts/List/Model/MyProfile.cpp index 909a7d4..01cb529 100644 --- a/lib-contact/src/Contacts/List/Model/MyProfile.cpp +++ b/lib-contact/src/Contacts/List/Model/MyProfile.cpp @@ -16,6 +16,7 @@ */ #include "Contacts/List/Model/MyProfile.h" +#include "Contacts/Utils.h" using namespace Contacts::List::Model; @@ -24,9 +25,7 @@ MyProfile::MyProfile() { contacts_list_h list = nullptr; contacts_db_get_all_records(_contacts_my_profile._uri, 0, 1, &list); - contacts_list_get_current_record_p(list, &m_MyProfileRecord); - contacts_list_destroy(list, false); } @@ -37,27 +36,20 @@ MyProfile::~MyProfile() const int MyProfile::getId() const { - int id = 0; - contacts_record_get_int(m_MyProfileRecord, _contacts_my_profile.id, &id); - return id; + return getRecordInt(m_MyProfileRecord, _contacts_my_profile.id); } const char *MyProfile::getName() const { - char *name = nullptr; - contacts_record_get_str_p(m_MyProfileRecord, _contacts_my_profile.display_name, &name); - return name; + return getRecordStr(m_MyProfileRecord, _contacts_my_profile.display_name); } const char *MyProfile::getImagePath() const { - char *path = nullptr; - contacts_record_get_str_p(m_MyProfileRecord, _contacts_my_profile.image_thumbnail_path, &path); - return path; + return getRecordStr(m_MyProfileRecord, _contacts_my_profile.image_thumbnail_path); } const contacts_record_h MyProfile::getRecord() const { return m_MyProfileRecord; } - diff --git a/lib-contact/src/Contacts/Model/ContactBoolField.cpp b/lib-contact/src/Contacts/Model/ContactBoolField.cpp index 44c491d..eae4e14 100644 --- a/lib-contact/src/Contacts/Model/ContactBoolField.cpp +++ b/lib-contact/src/Contacts/Model/ContactBoolField.cpp @@ -16,6 +16,7 @@ */ #include "Contacts/Model/ContactBoolField.h" +#include "Contacts/Utils.h" using namespace Contacts::Model; @@ -26,7 +27,7 @@ bool ContactBoolField::isChanged() const bool ContactBoolField::getValue() const { - return getValue(getRecord()); + return getRecordBool(getRecord(), getPropertyId()); } void ContactBoolField::setValue(bool value) @@ -34,14 +35,7 @@ void ContactBoolField::setValue(bool value) contacts_record_set_bool(getRecord(), getPropertyId(), value); } -bool ContactBoolField::getValue(contacts_record_h record) const -{ - bool value = false; - contacts_record_get_bool(record, getPropertyId(), &value); - return value; -} - void ContactBoolField::onInitialize(contacts_record_h record) { - m_InitialValue = getValue(record); + m_InitialValue = getRecordBool(record, getPropertyId()); } diff --git a/lib-contact/src/Contacts/Model/ContactEnumField.cpp b/lib-contact/src/Contacts/Model/ContactEnumField.cpp index 03dbd79..d870d31 100644 --- a/lib-contact/src/Contacts/Model/ContactEnumField.cpp +++ b/lib-contact/src/Contacts/Model/ContactEnumField.cpp @@ -17,6 +17,7 @@ #include "Contacts/Model/ContactEnumField.h" #include "Contacts/Model/ContactFieldMetadata.h" +#include "Contacts/Utils.h" using namespace Contacts::Model; @@ -42,7 +43,7 @@ int ContactEnumField::getCustomValue() const int ContactEnumField::getValue() const { - return getValue(getRecord()); + return getRecordInt(getRecord(), getPropertyId()); } void ContactEnumField::setValue(int value) @@ -55,13 +56,6 @@ bool ContactEnumField::hasCustomValue() const return getValue() == getEnumMetadata().customValue; } -int ContactEnumField::getValue(contacts_record_h record) const -{ - int value = 0; - contacts_record_get_int(record, getPropertyId(), &value); - return value; -} - const ContactEnumMetadata &ContactEnumField::getEnumMetadata() const { return *(const ContactEnumMetadata *) ContactField::getMetadata().typeMetadata; @@ -69,5 +63,5 @@ const ContactEnumMetadata &ContactEnumField::getEnumMetadata() const void ContactEnumField::onInitialize(contacts_record_h record) { - m_InitialValue = getValue(record); + m_InitialValue = getRecordInt(record, getPropertyId()); } diff --git a/lib-contact/src/Contacts/Model/ContactTextField.cpp b/lib-contact/src/Contacts/Model/ContactTextField.cpp index df6ae3b..67dffb4 100644 --- a/lib-contact/src/Contacts/Model/ContactTextField.cpp +++ b/lib-contact/src/Contacts/Model/ContactTextField.cpp @@ -17,6 +17,7 @@ #include "Contacts/Model/ContactTextField.h" #include "Contacts/Model/ContactFieldMetadata.h" +#include "Contacts/Utils.h" using namespace Contacts::Model; @@ -43,7 +44,7 @@ bool ContactTextField::isChanged() const const char *ContactTextField::getValue() const { - return getValue(getRecord()); + return getRecordStr(getRecord(), getPropertyId()); } void ContactTextField::setValue(const char *value) @@ -57,16 +58,9 @@ void ContactTextField::setValue(const char *value) } } -const char *ContactTextField::getValue(contacts_record_h record) const -{ - char *value = nullptr; - contacts_record_get_str_p(record, getPropertyId(), &value); - return value; -} - void ContactTextField::onInitialize(contacts_record_h record) { - const char *value = getValue(record); + const char *value = getRecordStr(record, getPropertyId()); if (value) { m_InitialValue = value; } diff --git a/lib-logs/src/Logs/Model/Log.cpp b/lib-logs/src/Logs/Model/Log.cpp index d7dd9a6..39d7f09 100644 --- a/lib-logs/src/Logs/Model/Log.cpp +++ b/lib-logs/src/Logs/Model/Log.cpp @@ -17,6 +17,7 @@ #include "Logs/Model/Log.h" #include "Logs/Model/LogGroup.h" +#include "Contacts/Utils.h" #include "Utils/Logger.h" using namespace Logs::Model; @@ -61,9 +62,7 @@ const char *Log::getName() const const char *Log::getNumber() const { - char *number = nullptr; - contacts_record_get_str_p(m_LogRecord, _contacts_phone_log.address, &number); - return number; + return Contacts::getRecordStr(m_LogRecord, _contacts_phone_log.address); } const char *Log::getImagePath() const @@ -77,33 +76,24 @@ const char *Log::getImagePath() const int Log::getType() const { - int type = CONTACTS_PLOG_TYPE_NONE; - contacts_record_get_int(m_LogRecord, _contacts_phone_log.log_type, &type); - - return type; + return Contacts::getRecordInt(m_LogRecord, _contacts_phone_log.log_type); } tm Log::getTime() const { - int time = 0; - contacts_record_get_int(m_LogRecord, _contacts_phone_log.log_time, &time); - - time_t logTime = time; - return *localtime(&logTime); + time_t time = 0; + contacts_record_get_int(m_LogRecord, _contacts_phone_log.log_time, (int *) &time); + return *localtime(&time); } int Log::getId() const { - int id = 0; - contacts_record_get_int(m_LogRecord, _contacts_phone_log.id, &id); - return id; + return Contacts::getRecordInt(m_LogRecord, _contacts_phone_log.id); } int Log::getPersonId() const { - int id = 0; - contacts_record_get_int(m_LogRecord, _contacts_phone_log.person_id, &id); - return id; + return Contacts::getRecordInt(m_LogRecord, _contacts_phone_log.person_id); } int Log::getContactId() const @@ -117,9 +107,7 @@ int Log::getContactId() const time_t Log::getDuration() const { - time_t duration = 0; - contacts_record_get_int(m_LogRecord, _contacts_phone_log.extra_data1, (int *)&duration); - return duration; + return (time_t) Contacts::getRecordInt(m_LogRecord, _contacts_phone_log.extra_data1); } contacts_record_h Log::getContactRecord() -- 2.7.4