[recipes/temperature-settings: 1/3] Implement temperature conversion for display



commit ea706c373a6960aca021cb085d764a087c692449
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Mar 7 15:13:16 2017 -0500

    Implement temperature conversion for display
    
    If temperatures are marked up using the [temperature:] markup,
    parse it and convert it to the users preferred temperature
    unit.

 data/org.gnome.Recipes.gschema.xml |   10 +++++
 src/gr-recipe-formatter.c          |   69 +++++++++++++++++++++++++++++++----
 2 files changed, 71 insertions(+), 8 deletions(-)
---
diff --git a/data/org.gnome.Recipes.gschema.xml b/data/org.gnome.Recipes.gschema.xml
index 4e8b660..744fead 100644
--- a/data/org.gnome.Recipes.gschema.xml
+++ b/data/org.gnome.Recipes.gschema.xml
@@ -1,6 +1,11 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <schemalist>
 
+  <enum id="org.gnome.recipes.TemperatureUnit">
+    <value value="0" nick="celsius"/>
+    <value value="1" nick="fahrenheit"/>
+  </enum>
+
   <schema path="/org/gnome/recipes/" id="org.gnome.Recipes" gettext-domain="gnome-recipes">
 
     <key type="s" name="user">
@@ -62,6 +67,11 @@
         The list of recipes to be exported. Each entry in the list is the ID of a recipe.
       </description>
     </key>
+    <key name="temperature-unit" enum="org.gnome.recipes.TemperatureUnit">
+      <default>'celsius'</default>
+      <summary>The unit to display temperatures in</summary>
+      <description>The unit to display temperatures in.</description>
+    </key>
 
   </schema>
 
diff --git a/src/gr-recipe-formatter.c b/src/gr-recipe-formatter.c
index e23adbe..86d09b7 100644
--- a/src/gr-recipe-formatter.c
+++ b/src/gr-recipe-formatter.c
@@ -22,6 +22,7 @@
 
 #include <stdlib.h>
 #include <glib/gi18n.h>
+#include <gio/gio.h>
 
 #include "gr-recipe-formatter.h"
 #include "gr-ingredients-list.h"
@@ -122,6 +123,51 @@ recipe_step_free (gpointer data)
         g_free (d);
 }
 
+typedef enum {
+        GR_TEMPERATURE_UNIT_CELSIUS    = 0,
+        GR_TEMPERATURE_UNIT_FAHRENHEIT = 1
+} GrTemperatureUnit;
+
+static double
+convert_temperature (double            num,
+                     GrTemperatureUnit in,
+                     GrTemperatureUnit out)
+{
+        if (in == out)
+                return num;
+        else if (in  == GR_TEMPERATURE_UNIT_CELSIUS &&
+                 out == GR_TEMPERATURE_UNIT_FAHRENHEIT)
+                return num * 9.0 / 5.0 + 32.0;
+        else if (in  == GR_TEMPERATURE_UNIT_FAHRENHEIT &&
+                 out == GR_TEMPERATURE_UNIT_CELSIUS)
+                return (num - 32.0) * 5.0 / 9.0;
+        else {
+                g_message ("Unsupported temperature conversion: %d -> %d", in, out);
+                return num;
+        }
+}
+
+static char *
+format_temperature (double            num,
+                    int               digits,
+                    GrTemperatureUnit unit)
+{
+        char *s = NULL;
+
+        switch (unit) {
+        case GR_TEMPERATURE_UNIT_CELSIUS:
+                s = g_strdup_printf ("%.*f℃", digits, num);
+                break;
+        case GR_TEMPERATURE_UNIT_FAHRENHEIT:
+                s = g_strdup_printf ("%.*f℉", digits, num);
+                break;
+        default:
+                g_assert_not_reached ();
+        }
+
+        return s;
+}
+
 GPtrArray *
 gr_recipe_parse_instructions (const char *instructions,
                               gboolean    format_for_display)
@@ -129,6 +175,12 @@ gr_recipe_parse_instructions (const char *instructions,
         GPtrArray *step_array;
         g_auto(GStrv) steps = NULL;
         int i;
+        int user_unit;
+        int recipe_unit;
+        GSettings *settings;
+
+        settings = g_settings_new ("org.gnome.Recipes");
+        user_unit = g_settings_get_enum (settings, "temperature-unit");
 
         step_array = g_ptr_array_new_with_free_func (recipe_step_free);
 
@@ -147,26 +199,27 @@ gr_recipe_parse_instructions (const char *instructions,
                         p = strstr (step, "[temperature:");
                         while (p) {
                                 g_autofree char *prefix = NULL;
-                                const char *unit;
-                                int num;
+                                double num;
                                 char *tmp;
 
                                 prefix = g_strndup (step, p - step);
 
                                 q = strstr (p, "]");
                                 if (q[-1] == 'C')
-                                        unit = "℃";
+                                        recipe_unit = GR_TEMPERATURE_UNIT_CELSIUS;
                                 else if (q[-1] == 'F')
-                                        unit ="℉";
+                                        recipe_unit = GR_TEMPERATURE_UNIT_FAHRENHEIT;
                                 else {
                                         g_message ("Unsupported temperature unit: %c, using C", q[-1]);
-                                        unit = "℃";
+                                        recipe_unit = GR_TEMPERATURE_UNIT_CELSIUS;
                                 }
-                                num = atoi (p + strlen ("[temperature:"));
 
-                                tmp = g_strdup_printf ("%s%d%s%s", prefix, num, unit, q + 1);
+                                num = atof (p + strlen ("[temperature:"));
+
+                                num = convert_temperature (num, recipe_unit, user_unit);
+                                tmp = format_temperature (num, 0, user_unit);
                                 g_free (step);
-                                step = tmp;
+                                step = g_strconcat (prefix, tmp, q + 1, NULL);
 
                                 p = strstr (step, "[temperature:");
                         }


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