*/
enum MatchedField
{
- MatchedNone, /**< Not matched */
- MatchedName, /**< Matched by name */
- MatchedNumber /**< Matched by number */
+ MatchedNone, /**< Not matched */
+ MatchedName, /**< Matched by name */
+ MatchedNickname, /**< Matched by nickname */
+ MatchedOrganization, /**< Matched by organization */
+ MatchedAddress, /**< Matched by contact address */
+ MatchedEmail, /**< Matched by contact email */
+ MatchedNote, /**< Matched by note field */
+ MatchedNumber /**< Matched by number */
};
SearchResult();
/**
* @return Note
*/
- const char *getNotes() const;
+ const char *getNote() const;
/**
* @return Organization record
/**
* @brief Add contact to person
- * @param[in] record Contact record
+ * @param[in] record Contact record
+ * @param[in] isDefault Is default contact record
*/
- void addContact(contacts_record_h record);
+ void addContact(contacts_record_h record, bool isDefault = false);
/**
* @brief Remove contact from person
const Utils::UniString &getSortValue() const;
void update(contacts_record_h personRecord);
int updateName(contacts_record_h record, unsigned sortProperty);
+ void updateDefaultContactRecord();
contacts_record_h m_Record;
contacts_record_h m_DefaultContactRecord;
#define CONTACTS_MODEL_PERSON_SEARCH_DATA_H
#include "Contacts/Model/SearchData.h"
+#include "Contacts/List/Model/Person.h"
+#include <functional>
namespace Contacts
{
{
namespace Model
{
- class Person;
-
class PersonSearchData : public Contacts::Model::SearchData
{
public:
private:
friend class PersonSearchProvider;
+ typedef Contacts::Model::SearchResultPtr (*Comparator)(const Person &, const std::string &);
+
+ static Contacts::Model::SearchResultPtr compareName(const Person &person, const std::string &str);
+ static Contacts::Model::SearchResultPtr compareNickname(const Person &person, const std::string &str);
+ static Contacts::Model::SearchResultPtr compareNote(const Person &person, const std::string &str);
+ static Contacts::Model::SearchResultPtr compareOrganization(const Person &person, const std::string &str);
+ static Contacts::Model::SearchResultPtr compareAddress(const Person &person, const std::string &str);
+ static Contacts::Model::SearchResultPtr compareEmail(const Person &person, const std::string &str);
+ static Contacts::Model::SearchResultPtr compareNumber(const Person &person, const std::string &str);
+
+ static Contacts::Model::SearchResultPtr caseCompare(const char *fieldValue, const std::string &str,
+ Contacts::Model::SearchResult::MatchedField matchedField);
+ static Contacts::Model::SearchResultPtr fieldsCompare(Person::ContactChildRecords records, unsigned propertyId,
+ const std::string &str, Contacts::Model::SearchResult::MatchedField matchedField);
- Contacts::Model::SearchResultPtr compareName(const std::string &str);
- Contacts::Model::SearchResultPtr compareNumber(const std::string &str);
+ static Comparator getCompFunc(size_t i);
};
}
}
return record ? getRecordStr(record, _contacts_nickname.name) : nullptr;
}
-const char *Person::getNotes() const
+const char *Person::getNote() const
{
contacts_record_h record = getContactChildRecord(_contacts_contact.note,
[](contacts_record_h record) {
return getSortValue() < that.getSortValue();
}
-void Person::addContact(contacts_record_h record)
+void Person::addContact(contacts_record_h record, bool isDefault)
{
- if (getContactId() == getRecordInt(record, _contacts_contact.id)) {
+ if (isDefault) {
m_DefaultContactRecord = record;
}
contacts_record_destroy(m_Record, true);
contacts_db_get_record(_contacts_person._uri, id, &m_Record);
- int displayContactId = getRecordInt(m_Record, _contacts_person.display_contact_id);
- auto it = std::find_if(m_ContactRecords.begin(), m_ContactRecords.end(),
- [this, displayContactId](contacts_record_h contactRecord) {
- return displayContactId == getRecordInt(contactRecord, _contacts_contact.id);
- }
- );
- if (it != m_ContactRecords.end()) {
- m_DefaultContactRecord = *it;
- }
+ updateDefaultContactRecord();
}
}
}
contacts_record_destroy(nameRecord, true);
return changes;
}
+
+
+void Person::updateDefaultContactRecord()
+{
+ int displayContactId = getRecordInt(m_Record, _contacts_person.display_contact_id);
+ auto it = std::find_if(m_ContactRecords.begin(), m_ContactRecords.end(),
+ [displayContactId](contacts_record_h contactRecord) {
+ return displayContactId == getRecordInt(contactRecord, _contacts_contact.id);
+ }
+ );
+ if (it != m_ContactRecords.end()) {
+ m_DefaultContactRecord = *it;
+ }
+}
{
Person *person = static_cast<Person *>(*personIt);
if (personRecord) {
- person->update(personRecord);
-
contacts_record_h contactRecord = nullptr;
contacts_db_get_record(_contacts_contact._uri, contactId, &contactRecord);
- person->addContact(contactRecord);
+ int displayContactId = getRecordInt(personRecord, _contacts_person.display_contact_id);
+
+ person->addContact(contactRecord, displayContactId == getRecordInt(contactRecord, _contacts_contact.id));
+ person->update(personRecord);
} else {
deletePerson(personIt);
}
*/
#include "Contacts/List/Model/PersonSearchData.h"
-#include "Contacts/List/Model/Person.h"
#include "Common/Database/RecordUtils.h"
+
#include <cstring>
using namespace Contacts::Model;
using namespace Contacts::List::Model;
using namespace Common::Database;
+using namespace std::placeholders;
+
+namespace
+{
+ enum PersonSubField
+ {
+ PersonName,
+ PersonNickname,
+ PersonOrganization,
+ PersonAddress,
+ PersonEmail,
+ PersonNote,
+ PersonNumber,
+ PersonMax
+ };
+}
PersonSearchData::PersonSearchData(Person &person)
: SearchData(person)
return SearchResultPtr(new SearchResult());
}
- SearchResultPtr nameResult = compareName(str);
- return nameResult ? std::move(nameResult) : std::move(compareNumber(str));
+ for (size_t subField = PersonName; subField < PersonMax; ++subField) {
+ SearchResultPtr result = getCompFunc(subField)(getPerson(), str);
+ if (result) {
+ return result;
+ }
+ }
+
+ return nullptr;
}
-SearchResultPtr PersonSearchData::compareName(const std::string &str)
+SearchResultPtr PersonSearchData::compareName(const Person &person, const std::string &str)
{
- const char *name = getName();
+ const char *name = person.getName();
while (name && *name) {
const char *delim = strchr(name, ' ');
if (strncasecmp(name, str.c_str(), str.size()) == 0) {
- return SearchResultPtr(new SearchResult(SearchResult::MatchedName, getName(), { name, str.size() }));
+ return SearchResultPtr(new SearchResult(SearchResult::MatchedName, person.getName(), { name, str.size() }));
}
if (delim) {
return nullptr;
}
-SearchResultPtr PersonSearchData::compareNumber(const std::string &str)
+SearchResultPtr PersonSearchData::compareNickname(const Person &person, const std::string &str)
+{
+ return caseCompare(person.getNickname(), str, SearchResult::MatchedNickname);
+}
+
+SearchResultPtr PersonSearchData::compareNote(const Person &person, const std::string &str)
+{
+ return caseCompare(person.getNote(), str, SearchResult::MatchedNote);
+}
+
+SearchResultPtr PersonSearchData::compareOrganization(const Person &person, const std::string &str)
+{
+ contacts_record_h record = person.getOrganization();
+ auto nameResult = caseCompare(getRecordStr(record, _contacts_company.name), str, SearchResult::MatchedOrganization);
+
+ return nameResult ? std::move(nameResult) :
+ std::move(caseCompare(getRecordStr(record, _contacts_company.job_title), str, SearchResult::MatchedOrganization));
+}
+
+SearchResultPtr PersonSearchData::compareAddress(const Person &person, const std::string &str)
+{
+ return fieldsCompare(person.getAddresses(), _contacts_address.street, str, SearchResult::MatchedAddress);
+}
+
+SearchResultPtr PersonSearchData::compareEmail(const Person &person, const std::string &str)
+{
+ return fieldsCompare(person.getEmails(), _contacts_email.email, str, SearchResult::MatchedEmail);
+}
+
+SearchResultPtr PersonSearchData::compareNumber(const Person &person, const std::string &str)
{
- auto &person = static_cast<Person &>(getContactData());
for (auto &&numberRange : person.getNumbers()) {
for (auto &&numberRecord : numberRange) {
const char *number = getRecordStr(numberRecord, _contacts_number.number);
return nullptr;
}
+
+SearchResultPtr PersonSearchData::caseCompare(const char *fieldValue, const std::string &str, SearchResult::MatchedField matchedField)
+{
+ if (fieldValue) {
+ if (strncasecmp(fieldValue, str.c_str(), str.size()) == 0) {
+ return SearchResultPtr(new SearchResult(matchedField, fieldValue, { fieldValue, str.size() }));
+ }
+ }
+
+ return nullptr;
+}
+
+SearchResultPtr PersonSearchData::fieldsCompare(Person::ContactChildRecords records,
+ unsigned propertyId, const std::string &str, SearchResult::MatchedField matchedField)
+{
+ for (auto &&range : records) {
+ for (auto &&record : range) {
+ SearchResultPtr result = caseCompare(getRecordStr(record, propertyId), str, matchedField);
+ if (result) {
+ return result;
+ }
+ }
+ }
+
+ return nullptr;
+}
+
+PersonSearchData::Comparator PersonSearchData::getCompFunc(size_t i)
+{
+ static Comparator comparators[] = {
+ /* PersonName = */ &PersonSearchData::compareName,
+ /* PersonNickname = */ &PersonSearchData::compareNickname,
+ /* PersonOrganization = */ &PersonSearchData::compareOrganization,
+ /* PersonAddress = */ &PersonSearchData::compareAddress,
+ /* PersonEmail = */ &PersonSearchData::compareEmail,
+ /* PersonNote = */ &PersonSearchData::compareNote,
+ /* PersonNumber = */ &PersonSearchData::compareNumber,
+ };
+
+ return comparators[i];
+}
if (searchResult) {
SearchResult::MatchedField matchedField = searchResult->getMatchedField();
- if (matchedField == SearchResult::MatchedName) {
- if (strcmp(part, PART_CONTACT_NAME) == 0) {
- return strdup(getHighlightedStr().c_str());
- }
- } else if (matchedField == SearchResult::MatchedNumber) {
- if (strcmp(part, PART_SUBTEXT) == 0) {
- return strdup(getHighlightedStr().c_str());
- }
+ switch (matchedField)
+ {
+ case SearchResult::MatchedName:
+ if (strcmp(part, PART_CONTACT_NAME) == 0) {
+ return strdup(getHighlightedStr().c_str());
+ }
+ break;
+ case SearchResult::MatchedNickname:
+ case SearchResult::MatchedOrganization:
+ case SearchResult::MatchedAddress:
+ case SearchResult::MatchedEmail:
+ case SearchResult::MatchedNote:
+ case SearchResult::MatchedNumber:
+ if (strcmp(part, PART_SUBTEXT) == 0) {
+ return strdup(getHighlightedStr().c_str());
+ }
+ break;
+ default:
+ break;
}
if (matchedField != SearchResult::MatchedNone) {