[recipes/shopping-list: 3/13] Maintain a 'last changed' timestamp for favorites



commit 5b049f58025e105f0ee027190c62a5d82753e194
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jan 7 00:18:21 2017 -0500

    Maintain a 'last changed' timestamp for favorites
    
    This will be used in the next commit.

 src/gr-recipe-store.c |   36 ++++++++++++++++++++++++++++++++++++
 src/gr-recipe-store.h |    1 +
 2 files changed, 37 insertions(+), 0 deletions(-)
---
diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c
index 2f64f98..61967fb 100644
--- a/src/gr-recipe-store.c
+++ b/src/gr-recipe-store.c
@@ -46,6 +46,8 @@ struct _GrRecipeStore
         char **favorites;
         char **featured_chefs;
         char *user;
+
+        GDateTime *favorite_change;
 };
 
 
@@ -59,6 +61,7 @@ gr_recipe_store_finalize (GObject *object)
         g_clear_pointer (&self->recipes, g_hash_table_unref);
         g_clear_pointer (&self->chefs, g_hash_table_unref);
         g_clear_pointer (&self->cooked, g_hash_table_unref);
+        g_clear_pointer (&self->favorite_change, g_date_time_unref);
         g_strfreev (self->todays);
         g_strfreev (self->picks);
         g_strfreev (self->favorites);
@@ -575,6 +578,7 @@ load_favorites (GrRecipeStore *self,
         g_autofree char *path = NULL;
         g_autoptr(GKeyFile) keyfile = NULL;
         g_autoptr(GError) error = NULL;
+        g_autofree char *tmp = NULL;
 
         keyfile = g_key_file_new ();
 
@@ -597,6 +601,16 @@ load_favorites (GrRecipeStore *self,
                 }
                 g_clear_error (&error);
         }
+
+        tmp = g_key_file_get_string (keyfile, "Content", "LastChange", &error);
+        if (error) {
+                if (!g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) {
+                        g_warning ("Failed to load favorites: %s", error->message);
+                }
+                g_clear_error (&error);
+        }
+
+        self->favorite_change = date_time_from_string (tmp);
 }
 
 static void
@@ -614,6 +628,13 @@ save_favorites (GrRecipeStore *self)
 
         g_key_file_set_string_list (keyfile, "Content", "Favorites", (const char * const *)self->favorites, 
g_strv_length (self->favorites));
 
+        if (self->favorite_change) {
+                g_autofree char *tmp = NULL;
+
+                tmp = date_time_to_string (self->favorite_change);
+                g_key_file_set_string (keyfile, "Content", "LastChange", tmp);
+        }
+
         if (!g_key_file_save_to_file (keyfile, path, &error)) {
                 g_error ("Failed to save recipe database: %s", error->message);
         }
@@ -1287,6 +1308,10 @@ gr_recipe_store_add_favorite (GrRecipeStore *self,
         g_free (self->favorites);
         self->favorites = strv;
 
+        if (self->favorite_change)
+                g_date_time_unref (self->favorite_change);
+        self->favorite_change = g_date_time_new_now_utc ();
+
         save_favorites (self);
 
         g_signal_emit (self, changed_signal, 0, recipe);
@@ -1310,6 +1335,11 @@ gr_recipe_store_remove_favorite (GrRecipeStore *self,
                         break;
                 }
         }
+
+        if (self->favorite_change)
+                g_date_time_unref (self->favorite_change);
+        self->favorite_change = g_date_time_new_now_utc ();
+
         save_favorites (self);
 
         g_signal_emit (self, changed_signal, 0, recipe);
@@ -1329,6 +1359,12 @@ gr_recipe_store_is_favorite (GrRecipeStore *self,
         return g_strv_contains ((const char *const*)self->favorites, id);
 }
 
+GDateTime *
+gr_recipe_store_last_favorite_change (GrRecipeStore *self)
+{
+        return self->favorite_change;
+}
+
 gboolean
 gr_recipe_store_has_diet (GrRecipeStore *self,
                           GrDiets        diet)
diff --git a/src/gr-recipe-store.h b/src/gr-recipe-store.h
index eb9dc24..ea9a3dc 100644
--- a/src/gr-recipe-store.h
+++ b/src/gr-recipe-store.h
@@ -70,6 +70,7 @@ void            gr_recipe_store_remove_favorite     (GrRecipeStore  *self,
                                                      GrRecipe       *recipe);
 gboolean        gr_recipe_store_is_favorite         (GrRecipeStore  *self,
                                                      GrRecipe       *recipe);
+GDateTime      *gr_recipe_store_last_favorite_change (GrRecipeStore *self);
 gboolean        gr_recipe_store_has_diet            (GrRecipeStore  *self,
                                                      GrDiets         diet);
 gboolean        gr_recipe_store_has_chef            (GrRecipeStore  *self,


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