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)
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();
+}
#include "Contacts/Model/Contact.h"
#include "Contacts/Model/ContactFieldMetadata.h"
+#include "Contacts/Model/ContactCompoundObject.h"
#include "Common/Database/RecordUtils.h"
#include "Utils/Callback.h"
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;