Refactored ContactRecordData class and added corresponding provider 36/62536/2
authorSergei Kobec <s.kobec@samsung.com>
Wed, 16 Mar 2016 12:12:57 +0000 (14:12 +0200)
committerSergei Kobec <s.kobec@samsung.com>
Wed, 16 Mar 2016 12:12:57 +0000 (14:12 +0200)
Change-Id: Ibeeca6cb6b5e88739814004e8c4e17a5667cf268
Signed-off-by: Sergei Kobec <s.kobec@samsung.com>
lib-contact/inc/Contacts/List/Model/ContactRecordData.h
lib-contact/inc/Contacts/List/Model/ContactRecordProvider.h [new file with mode: 0644]
lib-contact/inc/Contacts/List/Model/Person.h
lib-contact/src/Contacts/List/Model/ContactRecordData.cpp
lib-contact/src/Contacts/List/Model/ContactRecordProvider.cpp [new file with mode: 0644]
lib-contact/src/Contacts/List/Model/Person.cpp

index 028720f..bda7633 100644 (file)
@@ -19,6 +19,7 @@
 #define CONTACTS_LIST_MODEL_CONTACT_RECORD_DATA_H
 
 #include "Contacts/ContactData.h"
+#include "Contacts/DbChangeObserver.h"
 #include <contacts.h>
 
 namespace Contacts
@@ -74,21 +75,49 @@ namespace Contacts
                                virtual bool compare(const char *str) override;
 
                        protected:
-
                                /**
                                 * @return contact record
                                 */
                                const contacts_record_h getContactRecord() const;
 
                                /**
+                                * @brief Set changed callback
+                                * @param[in]   callback    Change callback
+                                */
+                               void setChangedCallback(DbChangeObserver::Callback callback);
+
+                               /**
+                                * @brief Unset changed callback
+                                */
+                               void unsetChangedCallback();
+
+                               /**
+                                * @param[in]   record  Contact record
+                                * @return Contact ID
+                                */
+                               static int getContactId(contacts_record_h record);
+
+                               /**
                                 * @param[in]   record  Contact record
                                 * @param[in]   field   Contact field
-                                * @return contact value Name/Number/ImagePath
+                                * @return Contact value Name/Number/ImagePath
                                 */
                                static const char *getValue(contacts_record_h record, Field field);
 
+                               /**
+                                * @param[in]   oldContact  Old contact record
+                                * @param[in]   newContact  New contact record
+                                * @return Changes between both contacts
+                                */
+                               static int getChanges(contacts_record_h oldContact, contacts_record_h newContact);
+
                        private:
+                               friend class ContactRecordProvider;
+
+                               void onUpdate(contacts_record_h record);
+
                                contacts_record_h m_Record;
+                               DbChangeObserver::CallbackHandle m_Handle;
                        };
                }
        }
diff --git a/lib-contact/inc/Contacts/List/Model/ContactRecordProvider.h b/lib-contact/inc/Contacts/List/Model/ContactRecordProvider.h
new file mode 100644 (file)
index 0000000..4db3973
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#ifndef CONTACTS_LIST_MODEL_CONTACT_RECORD_PROVIDER_H
+#define CONTACTS_LIST_MODEL_CONTACT_RECORD_PROVIDER_H
+
+#include "Contacts/ContactDataProvider.h"
+#include "Contacts/DbChangeObserver.h"
+
+namespace Contacts
+{
+       namespace List
+       {
+               namespace Model
+               {
+                       class ContactRecordProvider : public ContactDataProvider
+                       {
+                       public:
+                               ContactRecordProvider();
+                               virtual ~ContactRecordProvider() override;
+
+                               /**
+                                * @see ContactDataProvider::getContactDataList()
+                                */
+                               virtual const ContactDataList &getContactDataList() override;
+
+                       private:
+                               void onContactInserted(int id, contacts_changed_e changeType);
+                               void onContactChanged(ContactDataList::iterator contactIt, int id, contacts_changed_e changeType);
+
+                               DbChangeObserver::CallbackHandle m_Handle;
+                               ContactDataList m_ContactList;
+                       };
+               }
+       }
+}
+
+#endif /* CONTACTS_LIST_MODEL_CONTACT_RECORD_PROVIDER_H */
index ce6adb8..11b239e 100644 (file)
@@ -94,8 +94,6 @@ namespace Contacts
                                const Utils::UniString &getSortValue() const;
                                const char *getDbSortValue() const;
 
-                               int getChanges(contacts_record_h record);
-
                                contacts_record_h m_PersonRecord;
                                ContactIds m_ContactIds;
 
index ab0e610..c606693 100644 (file)
@@ -29,6 +29,7 @@ ContactRecordData::ContactRecordData(Type type)
 ContactRecordData::~ContactRecordData()
 {
        contacts_record_destroy(m_Record, true);
+       unsetChangedCallback();
 }
 
 void ContactRecordData::updateContactRecord(contacts_record_h record)
@@ -39,9 +40,7 @@ void ContactRecordData::updateContactRecord(contacts_record_h record)
 
 int ContactRecordData::getId() const
 {
-       int id = 0;
-       contacts_record_get_int(m_Record, _contacts_contact.id, &id);
-       return id;
+       return getContactId(m_Record);
 }
 
 const char *ContactRecordData::getName() const
@@ -69,6 +68,24 @@ const contacts_record_h ContactRecordData::getContactRecord() const
        return m_Record;
 }
 
+void ContactRecordData::setChangedCallback(DbChangeObserver::Callback callback)
+{
+       int id = getContactId(m_Record);
+       m_Handle = DbChangeObserver::getInstance()->addCallback(id, callback);
+}
+
+void ContactRecordData::unsetChangedCallback()
+{
+       DbChangeObserver::getInstance()->removeCallback(getId(), m_Handle);
+}
+
+int ContactRecordData::getContactId(contacts_record_h record)
+{
+       int value = 0;
+       contacts_record_get_int(record, _contacts_contact.id, &value);
+       return value;
+}
+
 const char *ContactRecordData::getValue(contacts_record_h record, Field field)
 {
        static unsigned int properties[] {
@@ -77,7 +94,7 @@ const char *ContactRecordData::getValue(contacts_record_h record, Field field)
                /* FieldImage  = */ _contacts_contact.image_thumbnail_path
        };
 
-       char *strValue = nullptr;
+       char *value = nullptr;
 
        if (field == FieldNumber) {
                auto numbers = Contacts::makeRange(record, properties[field]);
@@ -86,13 +103,44 @@ const char *ContactRecordData::getValue(contacts_record_h record, Field field)
                        contacts_record_get_bool(number, _contacts_number.is_default, &isDefault);
 
                        if (isDefault) {
-                               contacts_record_get_str_p(number, _contacts_number.number, &strValue);
+                               contacts_record_get_str_p(number, _contacts_number.number, &value);
                                break;
                        }
                }
        } else {
-               contacts_record_get_str_p(record, properties[field], &strValue);
+               contacts_record_get_str_p(record, properties[field], &value);
+       }
+
+       return value;
+}
+
+int ContactRecordData::getChanges(contacts_record_h oldContact, contacts_record_h newContact)
+{
+       auto isChanged = [](const char *str1, const char *str2) {
+               return (str1 && str2)
+                               ? (strcmp(str1, str2) != 0)
+                               : (str1 != str2);
+       };
+
+       int changes = ChangedNone;
+
+       for (int i = FieldName; i < FieldMax; ++i) {
+               auto fieldId = static_cast<Field>(i);
+               const char *oldValue = getValue(oldContact, fieldId);
+               const char *newValue = getValue(newContact, fieldId);
+               int changedInfo = 1 << fieldId;
+
+               if (isChanged(oldValue, newValue)) {
+                       changes |= changedInfo;
+               }
        }
 
-       return strValue;
+       return changes;
+}
+
+void ContactRecordData::onUpdate(contacts_record_h record)
+{
+       int changes = getChanges(m_Record, record);
+       updateContactRecord(record);
+       onUpdated(changes);
 }
diff --git a/lib-contact/src/Contacts/List/Model/ContactRecordProvider.cpp b/lib-contact/src/Contacts/List/Model/ContactRecordProvider.cpp
new file mode 100644 (file)
index 0000000..7a04ac4
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include "Contacts/List/Model/ContactRecordProvider.h"
+#include "Contacts/List/Model/ContactRecordData.h"
+#include "Contacts/Utils.h"
+
+using namespace Contacts;
+using namespace Contacts::List::Model;
+using namespace std::placeholders;
+
+ContactRecordProvider::ContactRecordProvider()
+{
+       m_Handle = DbChangeObserver::getInstance()->addCallback(
+                       std::bind(&ContactRecordProvider::onContactInserted, this, _1, _2));
+}
+
+ContactRecordProvider::~ContactRecordProvider()
+{
+       for (auto &&contact : m_ContactList) {
+               delete contact;
+       }
+}
+
+const ContactDataList &ContactRecordProvider::getContactDataList()
+{
+       if (!m_ContactList.empty()) {
+               return m_ContactList;
+       }
+
+       contacts_list_h list = nullptr;
+       contacts_db_get_all_records(_contacts_contact._uri, 0, 0, &list);
+
+       contacts_record_h record = nullptr;
+       CONTACTS_LIST_FOREACH(list, record) {
+               auto contact = new ContactRecordData(ContactData::TypeContact);
+               contact->updateContactRecord(record);
+               m_ContactList.push_back(contact);
+
+               contact->setChangedCallback(
+                               std::bind(&ContactRecordProvider::onContactChanged, this, --m_ContactList.end(), _1, _2));
+       }
+
+       contacts_list_destroy(list, false);
+
+       return m_ContactList;
+}
+
+void ContactRecordProvider::onContactInserted(int id, contacts_changed_e changeType)
+{
+       if (changeType == CONTACTS_CHANGE_INSERTED) {
+               contacts_record_h record = nullptr;
+               contacts_db_get_record(_contacts_contact._uri, id, &record);
+
+               auto contact = new ContactRecordData(ContactData::TypeContact);
+               contact->updateContactRecord(record);
+               m_ContactList.push_back(contact);
+
+               onInserted(*contact);
+       }
+}
+
+void ContactRecordProvider::onContactChanged(ContactDataList::iterator contactIt, int id, contacts_changed_e changeType)
+{
+       auto contact = static_cast<ContactRecordData *>(*contactIt);
+
+       if (changeType == CONTACTS_CHANGE_UPDATED) {
+               contacts_record_h contactRecord = nullptr;
+               contacts_db_get_record(_contacts_contact._uri, id, &contactRecord);
+
+               contact->onUpdate(contactRecord);
+       } else if (changeType == CONTACTS_CHANGE_DELETED) {
+               contact->onDeleted();
+
+               delete contact;
+               m_ContactList.erase(contactIt);
+       }
+}
index 157cf48..a0d628c 100644 (file)
@@ -122,7 +122,7 @@ void Person::initialize(contacts_record_h personRecord, contacts_record_h contac
 int Person::updatePerson(contacts_record_h personRecord)
 {
        contacts_record_h contactRecord = getDisplayContact(personRecord);
-       auto changes = getChanges(contactRecord);
+       auto changes = getChanges(getContactRecord(), contactRecord);
 
        contacts_record_destroy(m_PersonRecord, true);
        m_SortValue.clear();
@@ -154,28 +154,3 @@ const char *Person::getDbSortValue() const
 
        return sortValue;
 }
-
-int Person::getChanges(contacts_record_h record)
-{
-       auto isChanged = [](const char *str1, const char *str2) {
-               return (str1 && str2)
-                               ? (strcmp(str1, str2) != 0)
-                               : (str1 != str2);
-       };
-
-       int changes = ChangedNone;
-
-       for (int i = FieldFirst; i < FieldMax; ++i) {
-               auto fieldId = static_cast<Field>(i);
-
-               const char *oldValue = getValue(getContactRecord(), fieldId);
-               const char *newValue = getValue(record, fieldId);
-               int changedInfo = 1 << fieldId;
-
-               if (isChanged(oldValue, newValue)) {
-                       changes |= changedInfo;
-               }
-       }
-
-       return changes;
-}