TizenRefApp-6001 Implement search provider 28/64328/4
authorSergei Kobec <s.kobec@samsung.com>
Tue, 5 Apr 2016 13:20:54 +0000 (16:20 +0300)
committerSergei Kobec <s.kobec@samsung.com>
Tue, 5 Apr 2016 13:20:54 +0000 (16:20 +0300)
Change-Id: I6b1f08c8594ae71563b3f1c3b6b15bb172592c7a
Signed-off-by: Sergei Kobec <s.kobec@samsung.com>
lib-common/inc/Contacts/Model/ContactData.h
lib-common/inc/Contacts/Model/ContactDataProvider.h
lib-common/inc/Contacts/Model/ContactRecordProvider.h
lib-common/inc/Contacts/Model/DbChangeObserver.h
lib-common/inc/Contacts/Model/SearchData.h [new file with mode: 0644]
lib-common/inc/Contacts/Model/SearchProvider.h [new file with mode: 0644]
lib-common/src/Contacts/Model/ContactRecordData.cpp
lib-common/src/Contacts/Model/SearchData.cpp [new file with mode: 0644]
lib-common/src/Contacts/Model/SearchProvider.cpp [new file with mode: 0644]

index 9aa2e7918428388cde1a380ed06f22e60a64b877..e6c7b1c94454ba428229a94c3ae0252f114eea9e 100644 (file)
@@ -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 <memory>
 #include <tizen.h>
@@ -172,4 +172,4 @@ namespace Contacts
        }
 }
 
-#endif /* CONTACTS_CONTACT_DATA_H */
+#endif /* CONTACTS_MODEL_CONTACT_DATA_H */
index b58ddb25df0470052cce93cfd2a6a82455edc4e5..ccd554a7d9f7af7afd0e8ed6b627d212a8f5df81 100644 (file)
@@ -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 */
index c07595e0b18329d70afcc3f5d32de7d8d25a70dc..beaa59f3dac5a79c5fddf7f13d9cc2f7f6a42193 100644 (file)
@@ -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 */
index 7f92f7fc82a9569d2d5efe7589a865a6c83331fb..efe5e03f6e1588afbf52d8584917bac09b6240f0 100644 (file)
@@ -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 <contacts.h>
 #include <functional>
@@ -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 (file)
index 0000000..564de1e
--- /dev/null
@@ -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 (file)
index 0000000..154b58c
--- /dev/null
@@ -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 <unordered_map>
+
+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<ContactDataProvider *, ContactDataList::const_iterator> 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 */
index d2b73665054c751c48dd4a5db4ad3a63df19cf77..35070a11e7e9cfd6baa416d075c9ebd154ef7f9c 100644 (file)
@@ -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 (file)
index 0000000..d836565
--- /dev/null
@@ -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 (file)
index 0000000..f63ac8e
--- /dev/null
@@ -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<SearchData *>(*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);
+       }
+}