[recipes] Stop saving for invalid ingredients as well



commit 49ab6d8ce17a88269e37155d7a9b9e6e8233351d
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon May 15 23:28:08 2017 -0400

    Stop saving for invalid ingredients as well
    
    Add the necessary plumbing to show a notification for
    invalid or incomplete ingredients, and move the focus
    to the right row when the notification is dismissed.

 src/gr-edit-page.c          |   31 ++++++++++++++++++++++++++-----
 src/gr-ingredients-viewer.c |   28 ++++++++++++++++++++++++++++
 src/gr-ingredients-viewer.h |    2 ++
 3 files changed, 56 insertions(+), 5 deletions(-)
---
diff --git a/src/gr-edit-page.c b/src/gr-edit-page.c
index 118159d..952974b 100644
--- a/src/gr-edit-page.c
+++ b/src/gr-edit-page.c
@@ -108,6 +108,8 @@ struct _GrEditPage
         GtkWidget *timer_title;
         GtkWidget *preview_stack;
 
+        GtkWidget *error_field;
+
         GrRecipeSearch *search;
 
         GtkSizeGroup *group;
@@ -149,7 +151,8 @@ static void
 dismiss_error (GrEditPage *page)
 {
         gtk_revealer_set_reveal_child (GTK_REVEALER (page->error_revealer), FALSE);
-        gtk_widget_grab_focus (page->name_entry);
+        if (page->error_field)
+                gtk_widget_grab_focus (page->error_field);
 }
 
 static void add_image_cb (GrEditPage *page);
@@ -455,8 +458,8 @@ populate_season_combo (GrEditPage *page)
         }
 }
 
-static char *
-collect_ingredients (GrEditPage *page)
+static gboolean
+collect_ingredients (GrEditPage *page, GtkWidget **error_field, char **ingredients)
 {
         GString *s;
         GList *children, *l;
@@ -468,6 +471,13 @@ collect_ingredients (GrEditPage *page)
                 GtkWidget *list = l->data;
                 g_autofree char *segment = NULL;
 
+                *error_field = gr_ingredients_viewer_has_error (GR_INGREDIENTS_VIEWER (list));
+                if (*error_field) {
+                        *ingredients = NULL;
+                        g_string_free (s, TRUE);
+                        return FALSE;
+                }
+
                 g_object_get (list, "ingredients", &segment, NULL);
                 if (s->len > 0)
                         g_string_append (s, "\n");
@@ -475,7 +485,10 @@ collect_ingredients (GrEditPage *page)
         }
         g_list_free (children);
 
-        return g_string_free (s, FALSE);
+        *error_field = NULL;
+        *ingredients = g_string_free (s, FALSE);
+
+        return TRUE;
 }
 
 static void
@@ -1494,11 +1507,14 @@ gr_edit_page_save (GrEditPage *page)
         GrDiets diets;
         GPtrArray *images;
 
+        page->error_field = NULL;
+
         store = gr_recipe_store_get ();
 
         name = gtk_entry_get_text (GTK_ENTRY (page->name_entry));
 
         if (name[0] == '\0') {
+                page->error_field = page->name_entry;
                 g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
                              _("You need to provide a name for the recipe"));
                 goto error;
@@ -1511,7 +1527,12 @@ gr_edit_page_save (GrEditPage *page)
         cook_time = get_combo_value (GTK_COMBO_BOX (page->cook_time_combo));
         spiciness = get_spiciness (page);
         serves = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (page->serves_spin));
-        ingredients = collect_ingredients (page);
+        if (!collect_ingredients (page, &page->error_field, &ingredients)) {
+                g_set_error (&error, G_IO_ERROR, G_IO_ERROR_FAILED,
+                             _("Some ingredients need correction"));
+                goto error;
+        }
+
         description = get_text_view_text (GTK_TEXT_VIEW (page->description_field));
         instructions = get_text_view_text (GTK_TEXT_VIEW (page->instructions_field));
         diets = (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (page->gluten_free_check)) ? 
GR_DIET_GLUTEN_FREE : 0) |
diff --git a/src/gr-ingredients-viewer.c b/src/gr-ingredients-viewer.c
index 1486599..3902d4e 100644
--- a/src/gr-ingredients-viewer.c
+++ b/src/gr-ingredients-viewer.c
@@ -160,6 +160,34 @@ collect_ingredients (GrIngredientsViewer *viewer)
         return g_string_free (s, FALSE);
 }
 
+GtkWidget *
+gr_ingredients_viewer_has_error (GrIngredientsViewer *viewer)
+{
+        GList *children, *l;
+
+        set_active_row (viewer, NULL);
+
+        children = gtk_container_get_children (GTK_CONTAINER (viewer->list));
+        for (l = children; l; l = l->next) {
+                GtkWidget *row = l->data;
+                g_autofree char *amount = NULL;
+                g_autofree char *unit = NULL;
+                g_autofree char *ingredient = NULL;
+
+                g_object_get (row,
+                              "amount", &amount,
+                              "unit", &unit,
+                              "ingredient", &ingredient,
+                              NULL);
+
+                if (amount == NULL || amount[0] == '\0' ||
+                    ingredient == NULL || ingredient[0] == '\0')
+                        return row;
+        }
+
+        return NULL;
+}
+
 static void
 gr_ingredients_viewer_get_property (GObject    *object,
                                     guint       prop_id,
diff --git a/src/gr-ingredients-viewer.h b/src/gr-ingredients-viewer.h
index fa15edd..5de7a64 100644
--- a/src/gr-ingredients-viewer.h
+++ b/src/gr-ingredients-viewer.h
@@ -31,5 +31,7 @@ G_DECLARE_FINAL_TYPE (GrIngredientsViewer, gr_ingredients_viewer, GR, INGREDIENT
 void set_active_row (GrIngredientsViewer *viewer,
                      GtkWidget           *row);
 
+GtkWidget *gr_ingredients_viewer_has_error (GrIngredientsViewer *viewer);
+
 G_END_DECLS
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]