[recipes/unit_convert_test: 5/10] all unit conversion and some human readability written and working



commit 2033f692c658d9b89e84754948a70855eb4278f3
Author: Paxana Amanda Xander <VeganBikePunk Gmail com>
Date:   Mon Jul 3 13:35:05 2017 -0700

    all unit conversion and some human readability written and working

 data/org.gnome.Recipes.gschema.xml |   17 ++++-
 src/gr-convert-units.c             |  144 +++++++++++++++++++++++++++++++++---
 src/gr-convert-units.h             |    3 +
 src/gr-ingredients-viewer.c        |    9 ++-
 4 files changed, 160 insertions(+), 13 deletions(-)
---
diff --git a/data/org.gnome.Recipes.gschema.xml b/data/org.gnome.Recipes.gschema.xml
index 40c2391..872e849 100644
--- a/data/org.gnome.Recipes.gschema.xml
+++ b/data/org.gnome.Recipes.gschema.xml
@@ -13,6 +13,12 @@
     <value nick="locale" value="2"/>
   </enum>
 
+      <enum id="org.gnome.recipes.WeightUnit">
+    <value nick="metric" value="0"/>
+    <value nick="imperial" value="1"/>
+    <value nick="locale" value="2"/>
+  </enum>
+
   <schema path="/org/gnome/recipes/" id="org.gnome.Recipes" gettext-domain="gnome-recipes">
 
     <key type="s" name="user">
@@ -85,13 +91,22 @@
     </key>
 
     <key name="volume-unit" enum="org.gnome.recipes.VolumeUnit">
-      <default>'locale'</default>
+      <default>'metric'</default>
       <summary>The setting for which unit temperatures should be displayed in. </summary>
       <description>
         The setting for which unit temperatures should be displayed in. Default is 'locale',
         which means to use the LC_MEASUREMENT category of the current locale to decide.
       </description>
     </key>
+    <key name="weight-unit" enum="org.gnome.recipes.WeightUnit">
+      <default>'metric'</default>
+      <summary>The setting for which unit temperatures should be displayed in. </summary>
+      <description>
+        The setting for which unit temperatures should be displayed in. Default is 'locale',
+        which means to use the LC_MEASUREMENT category of the current locale to decide.
+      </description>
+    </key>
+
 
   </schema>
 
diff --git a/src/gr-convert-units.c b/src/gr-convert-units.c
index a759186..b7b1a6b 100644
--- a/src/gr-convert-units.c
+++ b/src/gr-convert-units.c
@@ -75,6 +75,29 @@ get_volume_unit (void)
         return unit;
 }
 
+static int
+get_weight_unit (void)
+{
+        int unit;
+        GSettings *settings = gr_settings_get ();
+
+        unit = g_settings_get_enum (settings, "weight-unit");
+
+        if (unit == GR_VOLUME_UNIT_LOCALE) {
+#ifdef LC_MEASUREMENT
+                const char *fmt;
+
+                fmt = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
+                if (fmt && *fmt == 2)
+                        unit = GR_VOLUME_UNIT_IMPERIAL;
+                else
+#endif
+                        unit = GR_VOLUME_UNIT_METRIC;
+        }
+
+        return unit;
+}
+
 
 void
 convert_temp (int *num, int *unit, int user_unit)
@@ -106,20 +129,15 @@ convert_temp (int *num, int *unit, int user_unit)
 
 }
 
-
-
 void 
 convert_volume (double *amount, char **unit)
 {
         double amount1 = *amount;        
         char *unit1 = *unit;
 
-        g_message("%f is the amount in convert-unit", amount1);
-        g_message("%s is the unit in convert-unit", unit1);
-
         int user_volume_unit = get_volume_unit();
 
-                if (user_volume_unit == 1) {
+                if (user_volume_unit == GR_VOLUME_UNIT_IMPERIAL) {
                        if (strcmp(unit1, "ml") == 0)
                                 {
                                         amount1 = (amount1 / 4.92892);
@@ -127,16 +145,122 @@ convert_volume (double *amount, char **unit)
                                 }
                         else if (strcmp(unit1, "dl") == 0)
                                 {
-                                        amount1 = (amount1 / 0.422675);
-                                        unit1 = "cup";
+                                        amount1 = (amount1 * 20.2884);
+                                        unit1 = "tsp";
                                 }
                         else if (strcmp(unit1, "l") == 0)
                         {
-                                amount1 = (amount1 * 4.22675);
-                                unit1 = "cup";
+                                amount1 = (amount1 * 202.884);
+                                unit1 = "tsp";
+                        }
+        }
+                if (user_volume_unit == GR_VOLUME_UNIT_METRIC) {
+                       if (strcmp(unit1, "tsp") == 0)
+                                {
+                                        amount1 = (amount1 * 4.92892);
+                                        unit1 = "ml";
+                                }
+                        else if (strcmp(unit1, "tbsp") == 0)
+                                {
+                                        amount1 = (amount1 * 14.79);
+                                        unit1 = "ml";
+                                }
+                        else if (strcmp(unit1, "cup") == 0)
+                        {
+                                amount1 = (amount1 * 236.59);
+                                unit1 = "ml";
+                        }
+                        else if (strcmp(unit1, "pt") == 0)
+                                {
+                                        amount1 = (amount1 * 473.176);
+                                        unit1 = "ml";
+                                }
+                        else if (strcmp(unit1, "qt") == 0)
+                        {
+                                amount1 = (amount1 * 946.353);
+                                unit1 = "ml";
+                        }
+                        else if (strcmp(unit1, "gal") == 0)
+                        {
+                                amount1 = (amount1 * 3785.41);
+                                unit1 = "ml";
+                        }
+                        else if (strcmp(unit1, "fl oz") == 0)
+                        {
+                                amount1 = (amount1 * 29.5735);
+                                unit1 = "ml";
+                        }
+                        else if (strcmp(unit1, "fl. oz.") == 0)
+                        {
+                                amount1 = (amount1 * 29.5735);
+                                unit1 = "ml";
                         }
         }
 
                                 *amount = amount1;
                                 *unit = unit1;
 }
+
+void 
+convert_weight (double *amount, char **unit)
+{
+        double amount1 = *amount;        
+        char *unit1 = *unit;
+
+        int user_weight_unit = get_weight_unit();
+
+        if (user_weight_unit == GR_VOLUME_UNIT_IMPERIAL) {
+
+                       if (strcmp(unit1, "g") == 0)
+                                {
+                                        amount1 = (amount1 * 0.035274);
+                                        unit1 = "oz";
+                                }
+                        else if (strcmp(unit1, "kg") == 0)
+                                {
+                                        amount1 = (amount1 * 35.274);
+                                        unit1 = "oz";
+                                }
+                
+        }
+                if (user_weight_unit == GR_VOLUME_UNIT_METRIC) {
+
+                       if (strcmp(unit1, "oz") == 0)
+                                {
+                                        amount1 = (amount1 * 28.3495);
+                                        unit1 = "g";
+                                }
+                        else if (strcmp(unit1, "lb") == 0)
+                                {
+                                        amount1 = (amount1 * 453.592);
+                                        unit1 = "g";
+                                }
+                        else if (strcmp(unit1, "st") == 0)
+                        {
+                                        amount1 = (amount1 * 6350.29);
+                                        unit1 = "g";
+                        } 
+        }
+                                *amount = amount1;
+                                *unit = unit1;
+}
+
+void 
+human_readable (double *amount, char **unit)
+{
+        double amount1 = *amount;        
+        char *unit1 = *unit;
+
+        if ((strcmp(unit1, "g") == 0) && (amount1 >= 1000) )
+        {
+                amount1 = (amount1 / 1000);
+                unit1 = "kg";
+        }
+        if ((strcmp(unit1, "oz") == 0) && (amount1 >= 16) )
+        {
+                amount1 = (amount1 / 16);
+                unit1 = "lb";
+        } 
+                        *amount = amount1;
+                        *unit = unit1;
+}
diff --git a/src/gr-convert-units.h b/src/gr-convert-units.h
index 72906a4..d9b35ba 100644
--- a/src/gr-convert-units.h
+++ b/src/gr-convert-units.h
@@ -43,6 +43,9 @@ typedef enum {
 int             get_temperature_unit        (void);
 void            convert_temp                (int *num, int *unit, int user_unit); 
 void            convert_volume              (double *amount, char **unit); 
+void            convert_weight              (double *amount, char **unit);
+void            human_readable              (double *amount, char **unit);
+
 
 
 
diff --git a/src/gr-ingredients-viewer.c b/src/gr-ingredients-viewer.c
index 7ed4bd0..fb25a49 100644
--- a/src/gr-ingredients-viewer.c
+++ b/src/gr-ingredients-viewer.c
@@ -336,11 +336,16 @@ gr_ingredients_viewer_set_ingredients (GrIngredientsViewer *viewer,
 
                
                if (measure) {
-               if (strcmp(measure, "volume") == 0) {
+                if (strcmp(measure, "volume") == 0) {
                         g_message ("measure is %s", measure);
                         convert_volume(&amount, &unit);
-                  }
+                        }
+                if (strcmp(measure, "weight") == 0) {
+                        g_message ("measure is %s", measure);
+                        convert_weight(&amount, &unit);
+                        }
                }
+               human_readable(&amount, &unit);
                 g_message("segment is %s", viewer->title);
 
                 g_message("unit is %s", unit);


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