[recipes/save-button] wip: update save button sensitivity



commit cb623ec487f30794e8afffdff51ef11d200e2d24
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Feb 6 08:07:57 2017 +0100

    wip: update save button sensitivity

 src/gr-recipe-store.c |    4 ++++
 src/gr-recipe.c       |   24 ++++++++++++++++++++++++
 src/gr-recipe.h       |    1 +
 src/gr-window.c       |   10 ++++++++++
 src/gr-window.ui      |    2 +-
 5 files changed, 40 insertions(+), 1 deletions(-)
---
diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c
index 66095d1..964312e 100644
--- a/src/gr-recipe-store.c
+++ b/src/gr-recipe-store.c
@@ -375,6 +375,7 @@ load_recipes (GrRecipeStore *self,
                                               "diets", diets,
                                               "images", images,
                                               "default-image", default_image,
+                                              "stored", TRUE,
                                               "readonly", readonly,
                                               NULL);
                 }
@@ -399,6 +400,7 @@ load_recipes (GrRecipeStore *self,
                                                "default-image", default_image,
                                                "ctime", ctime,
                                                "mtime", mtime,
+                                               "stored", TRUE,
                                                "readonly", readonly,
                                                NULL);
                         g_hash_table_insert (self->recipes, g_strdup (id), recipe);
@@ -514,6 +516,8 @@ save_recipes (GrRecipeStore *self)
                         g_autofree char *modified = date_time_to_string (mtime);
                         g_key_file_set_string (keyfile, key, "Modified", modified);
                 }
+
+                g_object_set (recipe, "stored", TRUE, NULL);
         }
 
         if (!g_key_file_save_to_file (keyfile, path, &error)) {
diff --git a/src/gr-recipe.c b/src/gr-recipe.c
index 3567ee6..81f0f50 100644
--- a/src/gr-recipe.c
+++ b/src/gr-recipe.c
@@ -61,6 +61,7 @@ struct _GrRecipe
         int spiciness;
 
         gboolean readonly;
+        gboolean stored;
 
         char *translated_name;
         char *translated_description;
@@ -91,6 +92,7 @@ enum {
         PROP_CTIME,
         PROP_MTIME,
         PROP_READONLY,
+        PROP_STORED,
         N_PROPS
 };
 
@@ -215,6 +217,10 @@ gr_recipe_get_property (GObject    *object,
                 g_value_set_boolean (value, self->readonly);
                 break;
 
+        case PROP_STORED:
+                g_value_set_boolean (value, self->stored);
+                break;
+
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
         }
@@ -225,6 +231,9 @@ update_mtime (GrRecipe *self)
 {
         g_date_time_unref (self->mtime);
         self->mtime = g_date_time_new_now_utc ();
+        self->stored = FALSE;
+        g_object_notify (G_OBJECT (self), "mtime");
+        g_object_notify (G_OBJECT (self), "stored");
 }
 
 static void
@@ -388,6 +397,10 @@ gr_recipe_set_property (GObject      *object,
                 update_mtime (self);
                 break;
 
+        case PROP_STORED:
+                self->stored = g_value_get_boolean (value);
+                break;
+
         default:
                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
         }
@@ -502,6 +515,11 @@ gr_recipe_class_init (GrRecipeClass *klass)
                                       FALSE,
                                       G_PARAM_READWRITE);
         g_object_class_install_property (object_class, PROP_READONLY, pspec);
+
+        pspec = g_param_spec_boolean ("stored", NULL, NULL,
+                                      FALSE,
+                                      G_PARAM_READWRITE);
+        g_object_class_install_property (object_class, PROP_STORED, pspec);
 }
 
 static void
@@ -669,6 +687,12 @@ gr_recipe_is_readonly (GrRecipe *recipe)
         return recipe->readonly;
 }
 
+gboolean
+gr_recipe_is_stored (GrRecipe *recipe)
+{
+        return recipe->stored;
+}
+
 /* terms are assumed to be g_utf8_casefold'ed where appropriate */
 gboolean
 gr_recipe_matches (GrRecipe    *recipe,
diff --git a/src/gr-recipe.h b/src/gr-recipe.h
index daaaf44..7e8a26b 100644
--- a/src/gr-recipe.h
+++ b/src/gr-recipe.h
@@ -54,6 +54,7 @@ int             gr_recipe_get_default_image (GrRecipe   *recipe);
 GDateTime      *gr_recipe_get_ctime        (GrRecipe   *recipe);
 GDateTime      *gr_recipe_get_mtime        (GrRecipe   *recipe);
 gboolean        gr_recipe_is_readonly      (GrRecipe   *recipe);
+gboolean        gr_recipe_is_stored        (GrRecipe   *recipe);
 
 const char     *gr_recipe_get_translated_name         (GrRecipe   *recipe);
 const char     *gr_recipe_get_translated_description  (GrRecipe   *recipe);
diff --git a/src/gr-window.c b/src/gr-window.c
index 2ecc70a..273055f 100644
--- a/src/gr-window.c
+++ b/src/gr-window.c
@@ -64,6 +64,7 @@ struct _GrWindow
         GtkWidget *cooking_page;
         GtkWidget *undo_revealer;
         GtkWidget *undo_label;
+        GtkWidget *save_button;
         GrRecipe  *undo_recipe;
         guint undo_timeout_id;
         GtkWidget *shopping_added_revealer;
@@ -532,6 +533,7 @@ gr_window_class_init (GrWindowClass *klass)
         gtk_widget_class_bind_template_child (widget_class, GrWindow, undo_revealer);
         gtk_widget_class_bind_template_child (widget_class, GrWindow, undo_label);
         gtk_widget_class_bind_template_child (widget_class, GrWindow, shopping_added_revealer);
+        gtk_widget_class_bind_template_child (widget_class, GrWindow, save_button);
 
         gtk_widget_class_bind_template_callback (widget_class, new_recipe);
         gtk_widget_class_bind_template_callback (widget_class, go_back);
@@ -595,6 +597,12 @@ gr_window_show_recipe (GrWindow *window,
         gtk_stack_set_visible_child_name (GTK_STACK (window->main_stack), "details");
 }
 
+static void
+update_save_button (GrRecipe *recipe, GParamSpec *pspec, GrWindow *window)
+{
+        gtk_widget_set_sensitive (window->save_button, !gr_recipe_is_stored (recipe));
+}
+
 void
 gr_window_edit_recipe (GrWindow *window,
                        GrRecipe *recipe)
@@ -608,6 +616,8 @@ gr_window_edit_recipe (GrWindow *window,
         gtk_stack_set_visible_child_name (GTK_STACK (window->header_start_stack), "back");
         gtk_stack_set_visible_child_name (GTK_STACK (window->header_title_stack), "title");
         gtk_stack_set_visible_child_name (GTK_STACK (window->header_end_stack), "edit");
+        g_signal_connect (recipe, "notify::stored", G_CALLBACK (update_save_button), window);
+        update_save_button (recipe, NULL, window);
 
         gtk_stack_set_visible_child_name (GTK_STACK (window->main_stack), "edit");
 }
diff --git a/src/gr-window.ui b/src/gr-window.ui
index e69f7e5..286fbbb 100644
--- a/src/gr-window.ui
+++ b/src/gr-window.ui
@@ -122,7 +122,7 @@
               </packing>
             </child>
             <child>
-              <object class="GtkButton">
+              <object class="GtkButton" id="save_button">
                 <property name="visible">1</property>
                 <property name="use-underline">1</property>
                 <property name="label" translatable="yes">_Save</property>


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