TizenRefApp-6981 Group with same name can be created 97/86097/1
authorEugene Kurzberg <i.kurtsberg@samsung.com>
Tue, 30 Aug 2016 12:45:01 +0000 (15:45 +0300)
committerEugene Kurzberg <i.kurtsberg@samsung.com>
Tue, 30 Aug 2016 12:45:01 +0000 (15:45 +0300)
Change-Id: If3d569ca543838c2e8b5c5331025aed323269089
Signed-off-by: Eugene Kurzberg <i.kurtsberg@samsung.com>
lib-apps-common/inc/Ux/EditfieldPopup.h
lib-apps-common/src/Ux/EditfieldPopup.cpp
lib-contacts/inc/Contacts/Groups/InputView.h
lib-contacts/inc/Contacts/Groups/Model/Group.h
lib-contacts/src/Contacts/Groups/CreateGroupItem.cpp
lib-contacts/src/Contacts/Groups/InputView.cpp
lib-contacts/src/Contacts/Groups/Model/Group.cpp
lib-contacts/src/Contacts/Input/ContactTypedFieldControl.cpp

index 0fd7fb1..ae3872f 100644 (file)
@@ -36,8 +36,9 @@ namespace Ux
                /**
                 * @brief Called when user confirmation button is pressed.
                 * @param[in]   Inputed text
+                * @return Whether to destroy the popup.
                 */
-               typedef std::function<void(const char *)> ResultCallback;
+               typedef std::function<bool(const char *)> ResultCallback;
 
                /**
                 * @brief Translatable strings table for popup elements.
index de3a22a..f632d41 100644 (file)
@@ -51,6 +51,8 @@ void EditfieldPopup::onCreated()
        addButton(m_Strings.buttonCancel);
 
        Evas_Object *button = addButton(m_Strings.buttonDone, std::bind(&EditfieldPopup::onDoneButtonPressed, this));
+       elm_object_focus_allow_set(button, EINA_FALSE);
+
        Evas_Object *entry = m_Editfield->getEntry();
        elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
        elm_entry_input_panel_return_key_disabled_set(entry, EINA_TRUE);
@@ -66,19 +68,21 @@ void EditfieldPopup::onCreated()
 
 bool EditfieldPopup::onDoneButtonPressed()
 {
+       bool shouldClose = true;
        if (m_OnResult) {
                char *text = elm_entry_markup_to_utf8(elm_entry_entry_get(m_Editfield->getEntry()));
-               m_OnResult(text);
+               shouldClose = m_OnResult(text);
                free(text);
        }
 
-       return true;
+       return shouldClose;
 }
 
 void EditfieldPopup::onDoneKeyPressed(Evas_Object *entry, void *eventInfo)
 {
-       onDoneButtonPressed();
-       close();
+       if (onDoneButtonPressed()) {
+               close();
+       }
 }
 
 void EditfieldPopup::onEntryChanged(Evas_Object *button, Evas_Object *entry, void *eventInfo)
index 162ac2f..7c6fa28 100644 (file)
@@ -57,9 +57,7 @@ namespace Contacts
                        void onDonePressed(Evas_Object *button, void *eventInfo);
                        void onNameFilled(bool isFilled);
 
-                       bool isAlreadyExists();
                        void save();
-
                        void getGroupMembers();
 
                        Model::Group m_Group;
index e0ef3e7..0d8d629 100644 (file)
@@ -48,13 +48,13 @@ namespace Contacts
                                 * @brief Create group object
                                 * @param[in]   id  _contacts_group id
                                 */
-                               Group(int id = 0);
+                               explicit Group(int id = 0);
 
                                /**
                                 * @brief Create group object
                                 * @param[in]   record  _contacts_group record
                                 */
-                               Group(contacts_record_h record);
+                               explicit Group(contacts_record_h record);
                                virtual ~Group() override;
 
                                /**
@@ -89,6 +89,17 @@ namespace Contacts
                                contacts_record_h getRecord() const;
 
                                /**
+                                * @return Whether the group has unique name.
+                                */
+                               bool isUnique() const;
+
+                               /**
+                                * @brief Save group to the database.
+                                * @return Group ID on success, Contacts API error code otherwise.
+                                */
+                               int save();
+
+                               /**
                                 * @brief Compares group's values
                                 * @return True if sort value less than in @a that, otherwise false
                                 */
index 5596264..0ea7caa 100644 (file)
  */
 
 #include "Contacts/Groups/CreateGroupItem.h"
+#include "Contacts/Groups/Model/Group.h"
 
 #include "Ui/Genlist.h"
 #include "Ui/Scale.h"
 #include "Ux/EditfieldPopup.h"
 #include "Utils/Callback.h"
 
+#include <app_i18n.h>
 #include <contacts.h>
+#include <notification.h>
 
 #include "AppsCommonButtons.h"
 
@@ -33,6 +36,7 @@
 #define BUTTON_COLOR_PRESSED 61, 76, 135, 255
 
 using namespace Contacts::Groups;
+using namespace Contacts::Groups::Model;
 
 void CreateGroupItem::setResultCallback(ResultCallback callback)
 {
@@ -79,16 +83,18 @@ void CreateGroupItem::onButtonPressed(Evas_Object *button, void *eventInfo)
                "IDS_PB_BUTTON_CANCEL"
        });
        popup->setResultCallback([this](const char *name) {
-               int id = 0;
-               contacts_record_h record = nullptr;
-               contacts_record_create(_contacts_group._uri, &record);
-               contacts_record_set_str(record, _contacts_group.name, name);
-               contacts_db_insert_record(record, &id);
-               contacts_record_destroy(record, true);
+               Group group;
+               contacts_record_set_str(group.getRecord(), _contacts_group.name, name);
+               if (!group.isUnique()) {
+                       notification_status_message_post(_("IDS_PB_TPOP_GROUP_NAME_ALREADY_IN_USE"));
+                       return false;
+               }
 
+               int id = group.save();
                if (m_OnResult) {
                        m_OnResult(id);
                }
+               return true;
        });
        popup->create(getParent()->getEvasObject());
        popup->show();
index 5dadf9f..ef102a4 100644 (file)
@@ -97,12 +97,11 @@ void InputView::onCancelPressed(Evas_Object *button, void *eventInfo)
 
 void InputView::onDonePressed(Evas_Object *button, void *eventInfo)
 {
-       if (isAlreadyExists()) {
+       if (!m_Group.isUnique()) {
                notification_status_message_post(_("IDS_PB_TPOP_GROUP_NAME_ALREADY_IN_USE"));
                return;
        }
        save();
-
        getPage()->close();
 }
 
@@ -111,36 +110,9 @@ void InputView::onNameFilled(bool isFilled)
        elm_object_disabled_set(m_DoneButton, !isFilled);
 }
 
-bool InputView::isAlreadyExists()
-{
-       contacts_filter_h filter = nullptr;
-       contacts_filter_create(_contacts_group._uri, &filter);
-       contacts_filter_add_str(filter, _contacts_group.name,
-                       CONTACTS_MATCH_EXACTLY, m_Group.getName());
-
-       if (m_Group.getId()) {
-               contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
-               contacts_filter_add_int(filter, _contacts_group.id, CONTACTS_MATCH_NOT_EQUAL, m_Group.getId());
-       }
-
-       contacts_query_h query = nullptr;
-       contacts_query_create(_contacts_group._uri, &query);
-       contacts_query_set_filter(query, filter);
-       contacts_filter_destroy(filter);
-
-       int count = 0;
-       contacts_db_get_count_with_query(query, &count);
-       contacts_query_destroy(query);
-
-       return count != 0;
-}
-
 void InputView::save()
 {
-       int groupId = m_Group.getId();
-       if (!groupId) {
-               contacts_db_insert_record(m_Group.getRecord(), &groupId);
-       }
+       int groupId = m_Group.save();
 
        for (auto &&personId : m_AddMembersItem->getMembers()) {
                auto it = std::find_if(m_GroupMembers.begin(), m_GroupMembers.end(), [personId](int id) {
@@ -165,8 +137,6 @@ void InputView::save()
                }
                contacts_list_destroy(contacts, true);
        }
-
-       contacts_db_update_record(m_Group.getRecord());
 }
 
 void InputView::getGroupMembers()
index 724d0ad..d96247c 100644 (file)
  */
 
 #include "Contacts/Groups/Model/Group.h"
-
 #include "Contacts/Groups/Model/Queries.h"
 #include "Common/Database/RecordUtils.h"
+#include "Utils/Logger.h"
 
-using namespace Contacts::Groups::Model;
 using namespace Common::Database;
+using namespace Contacts::Groups::Model;
 
 Group::Group(int id)
 {
@@ -76,6 +76,47 @@ contacts_record_h Group::getRecord() const
        return m_Record;
 }
 
+bool Group::isUnique() const
+{
+       contacts_filter_h filter = nullptr;
+       contacts_filter_create(_contacts_group._uri, &filter);
+       contacts_filter_add_str(filter, _contacts_group.name,
+                       CONTACTS_MATCH_EXACTLY, getName());
+
+       if (int id = getId()) {
+               contacts_filter_add_operator(filter, CONTACTS_FILTER_OPERATOR_AND);
+               contacts_filter_add_int(filter, _contacts_group.id, CONTACTS_MATCH_NOT_EQUAL, id);
+       }
+
+       contacts_query_h query = nullptr;
+       contacts_query_create(_contacts_group._uri, &query);
+       contacts_query_set_filter(query, filter);
+       contacts_filter_destroy(filter);
+
+       int count = 0;
+       contacts_db_get_count_with_query(query, &count);
+       contacts_query_destroy(query);
+
+       return count == 0;
+}
+
+int Group::save()
+{
+       int id = 0;
+       int err = CONTACTS_ERROR_NONE;
+
+       if (getId() == 0) {
+               err = contacts_db_insert_record(m_Record, &id);
+               RETVM_IF_ERR(err, err, "contacts_db_insert_record() failed.");
+       } else {
+               err = contacts_db_update_record(m_Record);
+               RETVM_IF_ERR(err, err, "contacts_db_update_record() failed.");
+               id = getId();
+       }
+
+       return id;
+}
+
 bool Group::operator<(const Group &that) const
 {
        return getSortValue() < that.getSortValue();
index 93a9de9..9ce6cfb 100644 (file)
@@ -68,6 +68,7 @@ bool ContactTypedFieldControl::onSelected(void *data)
                        m_TypeField.setValue(value);
                        m_LabelField.setValue(label);
                        setText(label);
+                       return true;
                });
 
                popup->create(getEvasObject());