[recipes] Add yield information to GrRecipe
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [recipes] Add yield information to GrRecipe
- Date: Mon, 26 Jun 2017 01:46:52 +0000 (UTC)
commit 956b8507b7a6a572d1660a93db53ab6f2c144161
Author: Matthias Clasen <mclasen redhat com>
Date: Sun Jun 18 18:28:05 2017 -0400
Add yield information to GrRecipe
Add two new properties to GrRecipe, yield (a double)
and yield-unit (a string). We keep the serves property
for now.
The database loading code populates the yield field
from serves and sets a yield-unit of "servings", if
it isn't specified.
src/gr-recipe-store.c | 63 +++++++++++++++++++++++++++++++++++++++++++++----
src/gr-recipe.c | 47 ++++++++++++++++++++++++++++++++++++
src/gr-recipe.h | 3 ++
3 files changed, 108 insertions(+), 5 deletions(-)
---
diff --git a/src/gr-recipe-store.c b/src/gr-recipe-store.c
index 13eb3f7..3f88f05 100644
--- a/src/gr-recipe-store.c
+++ b/src/gr-recipe-store.c
@@ -180,6 +180,32 @@ gr_recipe_store_finalize (GObject *object)
}
static gboolean
+parse_yield (const char *text,
+ double *amount,
+ char **unit)
+{
+ char *tmp;
+ const char *str;
+ g_autofree char *num = NULL;
+
+ g_clear_pointer (unit, g_free);
+
+ tmp = (char *)text;
+ skip_whitespace (&tmp);
+ str = tmp;
+ if (!gr_number_parse (amount, &tmp, NULL)) {
+ *unit = g_strdup (str);
+ return FALSE;
+ }
+
+ skip_whitespace (&tmp);
+ if (tmp)
+ *unit = g_strdup (tmp);
+
+ return TRUE;
+}
+
+static gboolean
load_recipes (GrRecipeStore *self,
const char *dir,
gboolean contributed)
@@ -238,6 +264,9 @@ load_recipes (GrRecipeStore *self,
g_autofree char *ingredients = NULL;
g_autofree char *instructions = NULL;
g_autofree char *notes = NULL;
+ g_autofree char *yield_str = NULL;
+ g_autofree char *yield_unit = NULL;
+ double yield;
g_auto(GStrv) paths = NULL;
int serves;
int spiciness;
@@ -368,6 +397,23 @@ load_recipes (GrRecipeStore *self,
}
g_clear_error (&error);
}
+ yield_str = g_key_file_get_string (keyfile, groups[i], "Yield", &error);
+ if (error) {
+ if (!g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) {
+ g_warning ("Failed to load recipe %s: %s", groups[i], error->message);
+ continue;
+ }
+ g_clear_error (&error);
+ }
+ if (!yield_str) {
+ yield = (double)serves;
+ yield_unit = g_strdup (_("servings"));
+ }
+ else if (!parse_yield (yield_str, &yield, &yield_unit)) {
+ g_warning ("Failed to load recipe %s: bad yield", groups[i]);
+ continue;
+ }
+
spiciness = g_key_file_get_integer (keyfile, groups[i], "Spiciness", &error);
if (error) {
if (!g_error_matches (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) {
@@ -454,11 +500,12 @@ load_recipes (GrRecipeStore *self,
"cook-time", cook_time,
"ingredients", ingredients,
"instructions", instructions,
- "serves", serves,
"spiciness", spiciness,
"diets", diets,
"images", images,
"default-image", default_image,
+ "yield-unit", yield_unit,
+ "yield", yield,
"mtime", mtime,
NULL);
}
@@ -479,11 +526,12 @@ load_recipes (GrRecipeStore *self,
"ingredients", ingredients,
"instructions", instructions,
"notes", notes,
- "serves", serves,
"spiciness", spiciness,
"diets", diets,
"images", images,
"default-image", default_image,
+ "yield-unit", yield_unit,
+ "yield", yield,
"ctime", ctime,
"mtime", mtime,
"contributed", contributed,
@@ -530,8 +578,10 @@ save_recipes (GrRecipeStore *self)
const char *ingredients;
const char *instructions;
const char *notes;
+ const char *yield_unit;
+ double yield;
+ g_autofree char *yield_str = NULL;
GPtrArray *images;
- int serves;
int spiciness;
GrDiets diets;
g_auto(GStrv) paths = NULL;
@@ -547,7 +597,9 @@ save_recipes (GrRecipeStore *self)
name = gr_recipe_get_name (recipe);
author = gr_recipe_get_author (recipe);
description = gr_recipe_get_description (recipe);
- serves = gr_recipe_get_serves (recipe);
+ yield_unit = gr_recipe_get_yield_unit (recipe);
+ yield = gr_recipe_get_yield (recipe);
+ yield_str = g_strdup_printf ("%g %s", yield, yield_unit);
spiciness = gr_recipe_get_spiciness (recipe);
cuisine = gr_recipe_get_cuisine (recipe);
season = gr_recipe_get_season (recipe);
@@ -588,7 +640,8 @@ save_recipes (GrRecipeStore *self)
g_key_file_set_string (keyfile, key, "CookTime", cook_time ? cook_time : "");
g_key_file_set_string (keyfile, key, "Ingredients", ingredients ? ingredients : "");
g_key_file_set_string (keyfile, key, "Instructions", instructions ? instructions : "");
- g_key_file_set_integer (keyfile, key, "Serves", serves);
+ g_key_file_set_integer (keyfile, key, "Serves", (int)yield);
+ g_key_file_set_string (keyfile, key, "Yield", yield_str ? yield_str : "");
g_key_file_set_integer (keyfile, key, "Spiciness", spiciness);
g_key_file_set_integer (keyfile, key, "Diets", diets);
g_key_file_set_integer (keyfile, key, "DefaultImage", default_image);
diff --git a/src/gr-recipe.c b/src/gr-recipe.c
index 58fc316..1050ba2 100644
--- a/src/gr-recipe.c
+++ b/src/gr-recipe.c
@@ -79,6 +79,9 @@ struct _GrRecipe
char *translated_description;
char *translated_instructions;
char *translated_notes;
+
+ double yield;
+ char *yield_unit;
};
G_DEFINE_TYPE (GrRecipe, gr_recipe, G_TYPE_OBJECT)
@@ -97,6 +100,8 @@ enum {
PROP_PREP_TIME,
PROP_COOK_TIME,
PROP_SERVES,
+ PROP_YIELD,
+ PROP_YIELD_UNIT,
PROP_INGREDIENTS,
PROP_INSTRUCTIONS,
PROP_SPICINESS,
@@ -141,6 +146,8 @@ gr_recipe_finalize (GObject *object)
g_free (self->translated_instructions);
g_free (self->translated_notes);
+ g_free (self->yield_unit);
+
G_OBJECT_CLASS (gr_recipe_parent_class)->finalize (object);
}
@@ -237,6 +244,14 @@ gr_recipe_get_property (GObject *object,
g_value_set_boolean (value, self->contributed);
break;
+ case PROP_YIELD:
+ g_value_set_double (value, self->yield);
+ break;
+
+ case PROP_YIELD_UNIT:
+ g_value_set_string (value, self->yield_unit);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -403,6 +418,15 @@ gr_recipe_set_property (GObject *object,
self->contributed = g_value_get_boolean (value);
break;
+ case PROP_YIELD:
+ self->yield = g_value_get_double (value);
+ break;
+
+ case PROP_YIELD_UNIT:
+ g_free (self->yield_unit);
+ self->yield_unit = g_value_dup_string (value);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
@@ -522,6 +546,16 @@ gr_recipe_class_init (GrRecipeClass *klass)
FALSE,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_CONTRIBUTED, pspec);
+
+ pspec = g_param_spec_double ("yield", NULL, NULL,
+ 0.0, G_MAXDOUBLE, 0.0,
+ G_PARAM_READWRITE);
+ g_object_class_install_property (object_class, PROP_YIELD, pspec);
+
+ pspec = g_param_spec_string ("yield-unit", NULL, NULL,
+ NULL,
+ G_PARAM_READWRITE);
+ g_object_class_install_property (object_class, PROP_YIELD_UNIT, pspec);
}
static void
@@ -530,6 +564,7 @@ gr_recipe_init (GrRecipe *self)
self->ctime = g_date_time_new_now_utc ();
self->mtime = g_date_time_new_now_utc ();
self->images = gr_image_array_new ();
+ self->yield = 0.0;
}
GrRecipe *
@@ -707,6 +742,18 @@ gr_recipe_get_images (GrRecipe *recipe)
return recipe->images;
}
+double
+gr_recipe_get_yield (GrRecipe *recipe)
+{
+ return recipe->yield;
+}
+
+const char *
+gr_recipe_get_yield_unit (GrRecipe *recipe)
+{
+ return recipe->yield_unit;
+}
+
/* 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 10408e0..03ac876 100644
--- a/src/gr-recipe.h
+++ b/src/gr-recipe.h
@@ -23,6 +23,7 @@
#include <glib-object.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "gr-diet.h"
+#include "gr-number.h"
G_BEGIN_DECLS
@@ -56,6 +57,8 @@ GDateTime *gr_recipe_get_mtime (GrRecipe *recipe);
gboolean gr_recipe_is_readonly (GrRecipe *recipe);
gboolean gr_recipe_is_contributed (GrRecipe *recipe);
GPtrArray *gr_recipe_get_images (GrRecipe *recipe);
+const char *gr_recipe_get_yield_unit (GrRecipe *recipe);
+double gr_recipe_get_yield (GrRecipe *recipe);
const char *gr_recipe_get_translated_name (GrRecipe *recipe);
const char *gr_recipe_get_translated_description (GrRecipe *recipe);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]