From: Sergei Kobec Date: Tue, 5 Apr 2016 13:20:54 +0000 (+0300) Subject: TizenRefApp-6001 Implement search provider X-Git-Tag: submit/tizen/20160524.113145~73^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1561621108bb3a4e6ddc147cfb2e6825a6ad6c1f;p=profile%2Fmobile%2Fapps%2Fnative%2Fphone-contacts.git TizenRefApp-6001 Implement search provider Change-Id: I6b1f08c8594ae71563b3f1c3b6b15bb172592c7a Signed-off-by: Sergei Kobec --- diff --git a/lib-common/inc/Contacts/Model/ContactData.h b/lib-common/inc/Contacts/Model/ContactData.h index 9aa2e791..e6c7b1c9 100644 --- a/lib-common/inc/Contacts/Model/ContactData.h +++ b/lib-common/inc/Contacts/Model/ContactData.h @@ -15,8 +15,8 @@ * */ -#ifndef CONTACTS_CONTACT_DATA_H -#define CONTACTS_CONTACT_DATA_H +#ifndef CONTACTS_MODEL_CONTACT_DATA_H +#define CONTACTS_MODEL_CONTACT_DATA_H #include #include @@ -172,4 +172,4 @@ namespace Contacts } } -#endif /* CONTACTS_CONTACT_DATA_H */ +#endif /* CONTACTS_MODEL_CONTACT_DATA_H */ diff --git a/lib-common/inc/Contacts/Model/ContactDataProvider.h b/lib-common/inc/Contacts/Model/ContactDataProvider.h index b58ddb25..ccd554a7 100644 --- a/lib-common/inc/Contacts/Model/ContactDataProvider.h +++ b/lib-common/inc/Contacts/Model/ContactDataProvider.h @@ -15,8 +15,8 @@ * */ -#ifndef CONTACTS_CONTACT_DATA_PROVIDER_H -#define CONTACTS_CONTACT_DATA_PROVIDER_H +#ifndef CONTACTS_MODEL_CONTACT_DATA_PROVIDER_H +#define CONTACTS_MODEL_CONTACT_DATA_PROVIDER_H #include "Contacts/Model/ContactData.h" @@ -67,4 +67,4 @@ namespace Contacts } } -#endif /* CONTACTS_CONTACT_DATA_PROVIDER_H */ +#endif /* CONTACTS_MODEL_CONTACT_DATA_PROVIDER_H */ diff --git a/lib-common/inc/Contacts/Model/ContactRecordProvider.h b/lib-common/inc/Contacts/Model/ContactRecordProvider.h index c07595e0..beaa59f3 100644 --- a/lib-common/inc/Contacts/Model/ContactRecordProvider.h +++ b/lib-common/inc/Contacts/Model/ContactRecordProvider.h @@ -15,8 +15,8 @@ * */ -#ifndef CONTACTS_LIST_MODEL_CONTACT_RECORD_PROVIDER_H -#define CONTACTS_LIST_MODEL_CONTACT_RECORD_PROVIDER_H +#ifndef CONTACTS_MODEL_CONTACT_RECORD_PROVIDER_H +#define CONTACTS_MODEL_CONTACT_RECORD_PROVIDER_H #include "Contacts/Model/ContactDataProvider.h" #include "Contacts/Model/DbChangeObserver.h" @@ -97,4 +97,4 @@ namespace Contacts } } -#endif /* CONTACTS_LIST_MODEL_CONTACT_RECORD_PROVIDER_H */ +#endif /* CONTACTS_MODEL_CONTACT_RECORD_PROVIDER_H */ diff --git a/lib-common/inc/Contacts/Model/DbChangeObserver.h b/lib-common/inc/Contacts/Model/DbChangeObserver.h index 7f92f7fc..efe5e03f 100644 --- a/lib-common/inc/Contacts/Model/DbChangeObserver.h +++ b/lib-common/inc/Contacts/Model/DbChangeObserver.h @@ -15,8 +15,8 @@ * */ -#ifndef CONTACTS_LIST_MODEL_DB_CHANGE_OBSERVER_H -#define CONTACTS_LIST_MODEL_DB_CHANGE_OBSERVER_H +#ifndef CONTACTS_MODEL_DB_CHANGE_OBSERVER_H +#define CONTACTS_MODEL_DB_CHANGE_OBSERVER_H #include #include @@ -129,4 +129,4 @@ namespace Contacts } } -#endif /* CONTACTS_LIST_MODEL_DB_CHANGE_OBSERVER_H */ +#endif /* CONTACTS_MODEL_DB_CHANGE_OBSERVER_H */ diff --git a/lib-common/inc/Contacts/Model/SearchData.h b/lib-common/inc/Contacts/Model/SearchData.h new file mode 100644 index 00000000..564de1e4 --- /dev/null +++ b/lib-common/inc/Contacts/Model/SearchData.h @@ -0,0 +1,65 @@ +/* + * 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_MODEL_SEARCH_DATA_H +#define CONTACTS_MODEL_SEARCH_DATA_H + +#include "Contacts/Model/ContactData.h" + +namespace Contacts +{ + namespace Model + { + class SearchData : public ContactData + { + public: + SearchData(ContactData &contactData); + + /** + * @see ContactData::getId() + */ + virtual int getId() const override; + + /** + * @see ContactData::getName() + */ + virtual const char *getName() const override; + + /** + * @see ContactData::getNumber() + */ + virtual const char *getNumber() const override; + + /** + * @see ContactData::getImagePath + */ + virtual const char *getImagePath() const override; + + /** + * @see ContactData::compare + */ + virtual bool compare(const char *str) override; + + private: + friend class SearchProvider; + + ContactData &m_ContactData; + }; + } +} + +#endif /* CONTACTS_MODEL_SEARCH_DATA_H */ diff --git a/lib-common/inc/Contacts/Model/SearchProvider.h b/lib-common/inc/Contacts/Model/SearchProvider.h new file mode 100644 index 00000000..154b58c9 --- /dev/null +++ b/lib-common/inc/Contacts/Model/SearchProvider.h @@ -0,0 +1,65 @@ +/* + * 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_MODEL_SEARCH_PROVIDER_H +#define CONTACTS_MODEL_SEARCH_PROVIDER_H + +#include "Contacts/Model/ContactDataProvider.h" + +#include + +namespace Contacts +{ + namespace Model + { + class SearchData; + + class SearchProvider : public ContactDataProvider + { + public: + SearchProvider(); + virtual ~SearchProvider() override; + + /** + * @see ContactDataProvider::getContactDataList() + */ + virtual const ContactDataList &getContactDataList() override; + + /** + * @brief Add @a provider to search provider + * @param[in] provider Contact data provider + */ + void addProvider(ContactDataProvider *provider); + + private: + typedef std::unordered_map SubProviders; + using ContactDataProvider::onInserted; + + ContactData &insertContact(ContactDataList::const_iterator position, ContactData &contact, + ContactDataProvider *provider); + + void onInserted(ContactData &contactData, ContactDataProvider *provider); + void onUpdated(SearchData &searchData, int changes); + void onDeleted(ContactDataList::iterator contactIt, ContactDataProvider *provider); + + ContactDataList m_ContactList; + SubProviders m_SubProviders; + }; + } +} + +#endif /* CONTACTS_MODEL_SEARCH_PROVIDER_H */ diff --git a/lib-common/src/Contacts/Model/ContactRecordData.cpp b/lib-common/src/Contacts/Model/ContactRecordData.cpp index d2b73665..35070a11 100644 --- a/lib-common/src/Contacts/Model/ContactRecordData.cpp +++ b/lib-common/src/Contacts/Model/ContactRecordData.cpp @@ -57,6 +57,11 @@ bool ContactRecordData::compare(const char *str) return strstr(getName(), str); //Todo: Compare unicode strings } +const contacts_record_h ContactRecordData::getContactRecord() const +{ + return m_Record; +} + void ContactRecordData::setChangedCallback(DbChangeObserver::Callback callback) { int id = getContactId(m_Record); @@ -71,11 +76,6 @@ void ContactRecordData::unsetChangedCallback() } } -const contacts_record_h ContactRecordData::getContactRecord() const -{ - return m_Record; -} - void ContactRecordData::updateRecord(contacts_record_h record) { contacts_record_destroy(m_Record, true); diff --git a/lib-common/src/Contacts/Model/SearchData.cpp b/lib-common/src/Contacts/Model/SearchData.cpp new file mode 100644 index 00000000..d8365653 --- /dev/null +++ b/lib-common/src/Contacts/Model/SearchData.cpp @@ -0,0 +1,53 @@ +/* + * 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/Model/SearchData.h" + +using namespace Contacts::Model; + +SearchData::SearchData(ContactData &contactData) + : ContactData(contactData.getType()), m_ContactData(contactData) +{ +} + +int SearchData::getId() const +{ + return m_ContactData.getId(); +} + +const char *SearchData::getName() const +{ + return m_ContactData.getName(); +} + +const char *SearchData::getNumber() const +{ + return m_ContactData.getNumber(); +} + +const char *SearchData::getImagePath() const +{ + return m_ContactData.getImagePath(); +} + +bool SearchData::compare(const char *str) +{ + /* Todo: Refactor method logic to return much more informative value, + according to what object type we currently wrap */ + + return false; +} diff --git a/lib-common/src/Contacts/Model/SearchProvider.cpp b/lib-common/src/Contacts/Model/SearchProvider.cpp new file mode 100644 index 00000000..f63ac8e7 --- /dev/null +++ b/lib-common/src/Contacts/Model/SearchProvider.cpp @@ -0,0 +1,98 @@ +/* + * 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/Model/SearchProvider.h" +#include "Contacts/Model/SearchData.h" +#include "Utils/Range.h" + +using namespace Contacts::Model; +using namespace Utils; +using namespace std::placeholders; + +SearchProvider::SearchProvider() +{ +} + +SearchProvider::~SearchProvider() +{ + for (auto &&contact : m_ContactList) { + delete contact; + } +} + +const ContactDataList &SearchProvider::getContactDataList() +{ + return m_ContactList;//Todo: Return filtered list by search engine instead of all contacts +} + +void SearchProvider::addProvider(ContactDataProvider *provider) +{ + const ContactDataList &list = provider->getContactDataList(); + for (auto it = list.begin(); it != list.end(); ++it) { + insertContact(m_ContactList.end(), **it, provider); + } + + m_SubProviders.insert({ provider, --m_ContactList.end() }); + provider->setInsertCallback( + [this, provider](ContactData &contactData) { + onInserted(contactData, provider); + }); +} + +ContactData &SearchProvider::insertContact(ContactDataList::const_iterator position, ContactData &contact, + ContactDataProvider *provider) +{ + auto searchData = new SearchData(contact); + auto newDataIt = m_ContactList.insert(position, searchData); + + ContactDataList::iterator contactIt = --m_ContactList.end(); + + contact.setUpdateCallback(std::bind(&SearchProvider::onUpdated, this, std::ref(*searchData), _1)); + contact.setDeleteCallback(std::bind(&SearchProvider::onDeleted, this, contactIt, provider)); + + if (m_SubProviders.find(provider) != m_SubProviders.end()) { + m_SubProviders[provider] = newDataIt; + } + + return *searchData; +} + +void SearchProvider::onInserted(ContactData &contactData, ContactDataProvider *provider) +{ + auto position = Utils::advance(m_SubProviders[provider], 1); + ContactData &searchData = insertContact(position, contactData, provider); + onInserted(searchData); +} + +void SearchProvider::onUpdated(SearchData &searchData, int changes) +{ + searchData.onUpdated(changes); +} + +void SearchProvider::onDeleted(ContactDataList::iterator contactIt, ContactDataProvider *provider) +{ + SearchData *searchData = static_cast(*contactIt); + searchData->onDeleted(); + + delete searchData; + ContactDataList::const_iterator newBound = m_ContactList.erase(contactIt); + + ContactDataList::const_iterator &oldBound = m_SubProviders[provider]; + if (oldBound == contactIt) { + oldBound = Utils::advance(newBound, -1); + } +}