#define CONTACTS_LIST_MODEL_CONTACT_RECORD_DATA_H
#include "Contacts/ContactData.h"
+#include "Contacts/DbChangeObserver.h"
#include <contacts.h>
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;
};
}
}
--- /dev/null
+/*
+ * 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 */
const Utils::UniString &getSortValue() const;
const char *getDbSortValue() const;
- int getChanges(contacts_record_h record);
-
contacts_record_h m_PersonRecord;
ContactIds m_ContactIds;
ContactRecordData::~ContactRecordData()
{
contacts_record_destroy(m_Record, true);
+ unsetChangedCallback();
}
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
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[] {
/* FieldImage = */ _contacts_contact.image_thumbnail_path
};
- char *strValue = nullptr;
+ char *value = nullptr;
if (field == FieldNumber) {
auto numbers = Contacts::makeRange(record, properties[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);
}
--- /dev/null
+/*
+ * 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);
+ }
+}
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();
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;
-}