Added convenience utility functions for Contacts API records. 81/63881/2
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Mon, 28 Mar 2016 08:31:28 +0000 (11:31 +0300)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Mon, 28 Mar 2016 12:44:04 +0000 (15:44 +0300)
Change-Id: Ic3e1da633e390e6380ec2cf5a1f11baa06083f75
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
lib-common/inc/Contacts/Utils.h
lib-contact/inc/Contacts/Model/ContactBoolField.h
lib-contact/inc/Contacts/Model/ContactEnumField.h
lib-contact/inc/Contacts/Model/ContactTextField.h
lib-contact/src/Contacts/List/Model/MyProfile.cpp
lib-contact/src/Contacts/Model/ContactBoolField.cpp
lib-contact/src/Contacts/Model/ContactEnumField.cpp
lib-contact/src/Contacts/Model/ContactTextField.cpp
lib-logs/src/Logs/Model/Log.cpp

index 06df1ad..a4a549e 100644 (file)
@@ -19,6 +19,7 @@
 #define CONTACTS_UTILS_H
 
 #include <contacts.h>
+#include "Utils/String.h"
 
 #define CONTACTS_LIST_FOREACH(list, record) \
        bool success = (contacts_list_get_current_record_p(list, &record) == CONTACTS_ERROR_NONE); \
 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.
index dcd22bd..9713fe9 100644 (file)
@@ -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;
index 53a17b8..0d91209 100644 (file)
@@ -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;
index aa066c6..677c3e2 100644 (file)
@@ -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;
index 909a7d4..01cb529 100644 (file)
@@ -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;
 }
-
index 44c491d..eae4e14 100644 (file)
@@ -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());
 }
index 03dbd79..d870d31 100644 (file)
@@ -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());
 }
index df6ae3b..67dffb4 100644 (file)
@@ -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;
        }
index d7dd9a6..39d7f09 100644 (file)
@@ -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()