[recipes/favorite-tile: 2/5] Maintain a 'last changed' timestamp for favorites



commit a761033b341018326bc7a92a8a22324bec416643
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 |   35 +++++++++++++++++++++++++++++++++++
 src/gr-recipe-store.h |    1 +
 2 files changed, 36 insertions(+), 0 deletions(-)
---
diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c
index 2f64f98..7429204 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;
 };
 
 
@@ -575,6 +577,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 +600,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 +627,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 +1307,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 +1334,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 +1358,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]