TizenRefApp-6324 Implement verification of contact name uniqueness 28/71828/3
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Fri, 27 May 2016 08:26:41 +0000 (11:26 +0300)
committerAleksandr Sapozhnik <a.sapozhnik@samsung.com>
Mon, 30 May 2016 12:44:55 +0000 (05:44 -0700)
Change-Id: Icfbac6c56c442522b7ff748f81ff32f2faa82339
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
lib-contacts/inc/Contacts/Input/InputView.h
lib-contacts/inc/Contacts/Model/Contact.h
lib-contacts/src/Contacts/Input/InputView.cpp
lib-contacts/src/Contacts/Model/Contact.cpp

index 9c76abd..693a9e6 100644 (file)
@@ -98,7 +98,9 @@ namespace Contacts
 
                        void onDonePressed(Evas_Object *button, void *eventInfo);
                        void onCancelPressed(Evas_Object *button, void *eventInfo);
+
                        bool onCancel();
+                       void onSave();
 
                        Model::Contact m_Contact;
                        ResultCallback m_OnResult;
index a7a2d0c..d8d984a 100644 (file)
@@ -51,6 +51,11 @@ namespace Contacts
                        bool isNew() const;
 
                        /**
+                        * @return Whether the contact has unique name.
+                        */
+                       bool isUnique() const;
+
+                       /**
                         * @brief Save contact to the database.
                         * @return Contact ID on success, Contacts API error code otherwise.
                         */
index 574634a..db06318 100644 (file)
@@ -299,18 +299,29 @@ void InputView::onContactFilled(bool isFilled)
 
 void InputView::onDonePressed(Evas_Object *button, void *eventInfo)
 {
-       int id = m_Contact.save();
-       if (m_OnResult) {
-               m_OnResult(id);
-       }
-
-       if (m_Contact.isNew()) {
-               using Details::DetailsView;
-               DetailsView *view = new DetailsView(id, DetailsView::Type(m_Contact.getSubType()));
-               getNavigator()->navigateTo(view);
+       if (m_Contact.isUnique()) {
+               onSave();
+               return;
        }
 
-       getPage()->close();
+       /* FIXME: Replace with translatable strings */
+       Ui::Popup *popup = new Ui::Popup();
+       popup->create(getEvasObject());
+       popup->setTitle("Name already in use");
+       popup->setText("A contact with the same name "
+                       "already exists. Tap Save anyway "
+                       "to save it anyway or tap Rename "
+                       "to save this contact with a "
+                       "different name.");
+
+       popup->addButton("Save anyway", [this] {
+               onSave();
+               return true;
+       });
+       popup->addButton("Rename", [this] {
+               m_Items[Model::FieldName]->focus();
+               return true;
+       });
 }
 
 void InputView::onCancelPressed(Evas_Object *button, void *eventInfo)
@@ -339,3 +350,19 @@ bool InputView::onCancel()
 
        return false;
 }
+
+void InputView::onSave()
+{
+       int id = m_Contact.save();
+       if (m_OnResult) {
+               m_OnResult(id);
+       }
+
+       if (m_Contact.isNew()) {
+               using Details::DetailsView;
+               DetailsView *view = new DetailsView(id, DetailsView::Type(m_Contact.getSubType()));
+               getNavigator()->navigateTo(view);
+       }
+
+       getPage()->close();
+}
index 1e54666..79d97f5 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "Contacts/Model/Contact.h"
 #include "Contacts/Model/ContactFieldMetadata.h"
+#include "Contacts/Model/ContactCompoundObject.h"
 #include "Common/Database/RecordUtils.h"
 
 #include "Utils/Callback.h"
@@ -59,6 +60,28 @@ bool Contact::isNew() const
        return m_IsNew;
 }
 
+bool Contact::isUnique() const
+{
+       std::string name = getFieldById<ContactCompoundObject>(FieldName)->getValue();
+
+       contacts_filter_h filter = nullptr;
+       contacts_filter_create(_contacts_contact._uri, &filter);
+       contacts_filter_add_str(filter, _contacts_contact.display_name, CONTACTS_MATCH_EXACTLY, name.c_str());
+
+       contacts_query_h query = nullptr;
+       contacts_query_create(_contacts_contact._uri, &query);
+       contacts_query_set_filter(query, filter);
+
+       int count = 0;
+       int err = contacts_db_get_count_with_query(query, &count);
+       WARN_IF_ERR(err, "contacts_db_get_count_with_query() failed.");
+
+       contacts_query_destroy(query);
+       contacts_filter_destroy(filter);
+
+       return count == 0;
+}
+
 int Contact::save()
 {
        int id = 0;