Add temperature conversion
[gcalctool.git] / src / unit-manager.c
blobce11819349f9cc9a0d10a966f07aa4f807787163
1 #include <string.h>
2 #include <glib/gi18n.h> // FIXME: Move out of here
4 #include "unit-manager.h"
5 #include "currency-manager.h" // FIXME: Move out of here
7 struct UnitManagerPrivate
9 GList *categories;
12 G_DEFINE_TYPE (UnitManager, unit_manager, G_TYPE_OBJECT);
15 static UnitManager *default_unit_manager = NULL;
18 UnitManager *
19 unit_manager_get_default(void)
21 UnitCategory *category = NULL;
22 const GList *iter;
23 int i;
24 const struct
26 gchar *category;
27 gchar *name;
28 gchar *display_name;
29 gchar *format;
30 gchar *from_function;
31 gchar *to_function;
32 gchar *symbols;
33 } units[] =
35 /* FIXME: Approximations of 1/(units in a circle), therefore, 360 deg != 400 grads */
36 {"angle", "degree", N_("Degrees"), NC_("unit-format", "%s degrees"), "π*x/180", "180x/π", NC_("unit-symbols", "degree,degrees,deg")},
37 {NULL, "radian", N_("Radians"), NC_("unit-format", "%s radians"), "x", "x", NC_("unit-symbols", "radian,radians,rad")},
38 {NULL, "gradian", N_("Gradians"), NC_("unit-format", "%s gradians"), "π*x/200", "200x/π", NC_("unit-symbols", "gradian,gradians,grad")},
39 {"length", "parsec", N_("Parsecs"), NC_("unit-format", "%s pc"), "30857000000000000x", "x/30857000000000000", NC_("unit-symbols", "parsec,parsecs,pc")},
40 {NULL, "lightyear", N_("Light Years"), NC_("unit-format", "%s ly"), "9460730472580800x", "x/9460730472580800", NC_("unit-symbols", "lightyear,lightyears,ly")},
41 {NULL, "astronomical-unit", N_("Austronomical Units"), NC_("unit-format", "%s au"), "149597870691x", "x/149597870691", NC_("unit-symbols", "au")},
42 {NULL, "nautical-mile", N_("Nautical Miles"), NC_("unit-format", "%s nm"), "1852000x", "x/1852000", NC_("unit-symbols", "nm")},
43 {NULL, "mile", N_("Miles"), NC_("unit-format", "%s mi"), "1609.344x", "x/1609.344", NC_("unit-symbols", "mile,miles,mi")},
44 {NULL, "kilometer", N_("Kilometers"), NC_("unit-format", "%s km"), "1000x", "x/1000", NC_("unit-symbols", "kilometer,kilometers,km,kms")},
45 {NULL, "cable", N_("Cables"), NC_("unit-format", "%s cb"), "219.456x", "x/219.456", NC_("unit-symbols", "cable,cables,cb")},
46 {NULL, "fathom", N_("Fathoms"), NC_("unit-format", "%s ftm"), "1.8288x", "x/1.8288", NC_("unit-symbols", "fathom,fathoms,ftm")},
47 {NULL, "meter", N_("Meters"), NC_("unit-format", "%s m"), "x", "x", NC_("unit-symbols", "meter,meters,m")},
48 {NULL, "yard", N_("Yards"), NC_("unit-format", "%s yd"), "0.9144x", "x/0.9144", NC_("unit-symbols", "yard,yards,yd")},
49 {NULL, "foot", N_("Feet"), NC_("unit-format", "%s ft"), "0.3048x", "x/0.3048", NC_("unit-symbols", "foot,feet,ft")},
50 {NULL, "inch", N_("Inches"), NC_("unit-format", "%s in"), "0.0254x", "x/0.0254", NC_("unit-symbols", "inch,inches,in")},
51 {NULL, "centimeter", N_("Centimeters"), NC_("unit-format", "%s cm"), "x/100", "100x", NC_("unit-symbols", "centimeter,centimeters,cm,cms")},
52 {NULL, "millimeter", N_("Millimeters"), NC_("unit-format", "%s mm"), "x/1000", "1000x", NC_("unit-symbols", "millimeter,millimeters,mm")},
53 {NULL, "micrometer", N_("Micrometers"), NC_("unit-format", "%s μm"), "x/1000000", "1000000x", NC_("unit-symbols", "micrometer,micrometers,um")},
54 {NULL, "nanometer", N_("Nanometers"), NC_("unit-format", "%s nm"), "x/1000000000", "1000000000x", NC_("unit-symbols", "nanometer,nanometers")},
55 {"area", "hectare", N_("Hectares"), NC_("unit-format", "%s ha"), "10000x", "x/10000", NC_("unit-symbols", "hectare,hectares,ha")},
56 {NULL, "acre", N_("Acres"), NC_("unit-format", "%s acres"), "4046.8564224x", "x/4046.8564224", NC_("unit-symbols", "acre,acres")},
57 {NULL, "square-meter", N_("Square Meter"), NC_("unit-format", "%s m²"), "x", "x", NC_("unit-symbols", "m²")},
58 {NULL, "square-centimeter", N_("Square Centimeter"), NC_("unit-format", "%s cm²"), "0.001x", "1000x", NC_("unit-symbols", "cm²")},
59 {NULL, "square-millimeter", N_("Square Millimeter"), NC_("unit-format", "%s mm²"), "0.000001x", "1000000x", NC_("unit-symbols", "mm²")},
60 {"volume", "cubic-meter", N_("Cubic Meters"), NC_("unit-format", "%s m³"), "1000x", "x/1000", NC_("unit-symbols", "m³")},
61 {NULL, "gallon", N_("Gallons"), NC_("unit-format", "%s gal"), "3.785412x", "x/3.785412", NC_("unit-symbols", "gallon,gallons,gal")},
62 {NULL, "litre", N_("Litres"), NC_("unit-format", "%s L"), "x", "x", NC_("unit-symbols", "litre,litres,liter,liters,L")},
63 {NULL, "quart", N_("Quarts"), NC_("unit-format", "%s qt"), "0.9463529x", "x/0.9463529", NC_("unit-symbols", "quart,quarts,qt")},
64 {NULL, "pint", N_("Pints"), NC_("unit-format", "%s pt"), "0.4731765x", "x/0.4731765", NC_("unit-symbols", "pint,pints,pt")},
65 {NULL, "millilitre", N_("Millilitres"), NC_("unit-format", "%s mL"), "0.001x", "1000x", NC_("unit-symbols", "millilitre,millilitres,milliliter,milliliters,mL,cm³")},
66 {NULL, "microlitre", N_("Microlitre"), NC_("unit-format", "%s μL"), "0.000001x", "1000000x", NC_("unit-symbols", "mm³,μL,uL")},
67 {"weight", "tonne", N_("Tonnes"), NC_("unit-format", "%s T"), "x/1000", "1000x", NC_("unit-symbols", "tonne,tonnes")},
68 {NULL, "kilograms", N_("Kilograms"), NC_("unit-format", "%s kg"), "x", "x", NC_("unit-symbols", "kilogram,kilograms,kilogramme,kilogrammes,kg,kgs")},
69 {NULL, "pound", N_("Pounds"), NC_("unit-format", "%s lb"), "0.45359237x", "x/0.45359237", NC_("unit-symbols", "pound,pounds,lb")},
70 {NULL, "ounce", N_("Ounces"), NC_("unit-format", "%s oz"), "0.02834952x", "x/0.02834952", NC_("unit-symbols", "ounce,ounces,oz")},
71 {NULL, "gram", N_("Grams"), NC_("unit-format", "%s g"), "0.001x", "1000x", NC_("unit-symbols", "gram,grams,gramme,grammes,g")},
72 {"duration", "year", N_("Years"), NC_("unit-format", "%s years"), "31557600x", "x/31557600", NC_("unit-symbols", "year,years")},
73 {NULL, "day", N_("Days"), NC_("unit-format", "%s days"), "86400x", "x/86400", NC_("unit-symbols", "day,days")},
74 {NULL, "hour", N_("Hours"), NC_("unit-format", "%s hours"), "3600x", "x/3600", NC_("unit-symbols", "hour,hours")},
75 {NULL, "minute", N_("Minutes"), NC_("unit-format", "%s minutes"), "60x", "x/60", NC_("unit-symbols", "minute,minutes")},
76 {NULL, "second", N_("Seconds"), NC_("unit-format", "%s s"), "x", "x", NC_("unit-symbols", "second,seconds,s")},
77 {NULL, "millisecond", N_("Milliseconds"), NC_("unit-format", "%s ms"), "0.001x", "1000x", NC_("unit-symbols", "millisecond,milliseconds,ms")},
78 {NULL, "microsecond", N_("Microseconds"), NC_("unit-format", "%s μs"), "0.000001x", "1000000x", NC_("unit-symbols", "microsecond,microseconds,us")},
79 {"temperature", "degree-celcius", N_("Celcius"), NC_("unit-format", "%s˚C"), "x+273.15", "x-273.15", NC_("unit-symbols", "degC,˚C")},
80 {NULL, "degree-farenheit", N_("Farenheit"), NC_("unit-format", "%s˚F"), "(x+459.67)*5/9", "x*9/5-459.67", NC_("unit-symbols", "degF,˚F")},
81 {NULL, "degree-kelvin", N_("Kelvin"), NC_("unit-format", "%s˚K"), "x", "x", NC_("unit-symbols", "degK,˚K")},
82 {NULL, "degree-rankine", N_("Rankine"), NC_("unit-format", "%s˚R"), "x*5/9", "x*9/5", NC_("unit-symbols", "degR,˚R")},
83 { NULL, NULL, NULL, NULL, NULL, NULL }
86 if (default_unit_manager)
87 return default_unit_manager;
89 default_unit_manager = g_object_new(unit_manager_get_type(), NULL);
91 unit_manager_add_category(default_unit_manager, "angle", _("Angle"));
92 unit_manager_add_category(default_unit_manager, "length", _("Length"));
93 unit_manager_add_category(default_unit_manager, "area", _("Area"));
94 unit_manager_add_category(default_unit_manager, "volume", _("Volume"));
95 unit_manager_add_category(default_unit_manager, "weight", _("Weight"));
96 unit_manager_add_category(default_unit_manager, "duration", _("Duration"));
97 unit_manager_add_category(default_unit_manager, "temperature", _("Temperature"));
99 for (i = 0; units[i].name; i++) {
100 if (units[i].category)
101 category = unit_manager_get_category(default_unit_manager, units[i].category);
102 unit_category_add_unit(category, unit_new(units[i].name,
103 _(units[i].display_name),
104 g_dpgettext2(NULL, "unit-format", units[i].format),
105 units[i].from_function, units[i].to_function,
106 g_dpgettext2(NULL, "unit-symbols", units[i].symbols)));
109 category = unit_manager_add_category(default_unit_manager, "currency", _("Currency"));
110 for (iter = currency_manager_get_currencies(currency_manager_get_default()); iter; iter = iter->next)
112 Currency *currency = iter->data;
113 gchar *format;
114 Unit *unit;
116 format = g_strdup_printf("%s%%s", currency_get_symbol(currency));
117 unit = unit_new(currency_get_name(currency), currency_get_name(currency), format, NULL, NULL, currency_get_name(currency));
118 g_free(format);
120 unit_category_add_unit(category, unit);
123 return default_unit_manager;
127 UnitCategory *
128 unit_manager_add_category(UnitManager *manager, const gchar *name, const gchar *display_name)
130 UnitCategory *category;
132 g_return_val_if_fail(unit_manager_get_category(manager, name) == NULL, NULL);
133 category = unit_category_new(name, display_name);
134 manager->priv->categories = g_list_append(manager->priv->categories, category);
136 return category;
140 const GList *
141 unit_manager_get_categories(UnitManager *manager)
143 return manager->priv->categories;
147 UnitCategory *
148 unit_manager_get_category(UnitManager *manager, const gchar *category)
150 GList *iter;
152 for (iter = manager->priv->categories; iter; iter = iter->next) {
153 UnitCategory *c = iter->data;
154 if (strcmp(unit_category_get_name(c), category) == 0)
155 return c;
158 return NULL;
162 Unit *
163 unit_manager_get_unit(UnitManager *manager, const gchar *unit)
165 GList *iter;
166 Unit *u;
168 for (iter = manager->priv->categories; iter; iter = iter->next) {
169 UnitCategory *c = iter->data;
170 u = unit_category_get_unit (c, unit);
171 if (u)
172 return u;
175 return NULL;
179 gboolean
180 unit_manager_convert(UnitManager *manager, const MPNumber *x, const char *x_units, const char *z_units, MPNumber *z)
182 GList *iter;
184 for (iter = manager->priv->categories; iter; iter = iter->next) {
185 UnitCategory *c = iter->data;
186 if (unit_category_convert(c, x, x_units, z_units, z))
187 return TRUE;
190 return FALSE;
194 static void
195 unit_manager_class_init(UnitManagerClass *klass)
197 g_type_class_add_private(klass, sizeof(UnitManagerPrivate));
201 static void
202 unit_manager_init(UnitManager *manager)
204 manager->priv = G_TYPE_INSTANCE_GET_PRIVATE(manager, unit_manager_get_type(), UnitManagerPrivate);