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
12 G_DEFINE_TYPE (UnitManager
, unit_manager
, G_TYPE_OBJECT
);
15 static UnitManager
*default_unit_manager
= NULL
;
19 compare_currencies(gconstpointer a
, gconstpointer b
)
21 return strcmp(currency_get_display_name((Currency
*)a
), currency_get_display_name((Currency
*)b
));
26 unit_manager_get_default(void)
28 UnitCategory
*category
= NULL
;
29 GList
*currencies
, *iter
;
42 /* FIXME: Approximations of 1/(units in a circle), therefore, 360 deg != 400 grads */
43 {"angle", "degree", N_("Degrees"), NC_("unit-format", "%s degrees"), "π*x/180", "180x/π", NC_("unit-symbols", "degree,degrees,deg")},
44 {NULL
, "radian", N_("Radians"), NC_("unit-format", "%s radians"), "x", "x", NC_("unit-symbols", "radian,radians,rad")},
45 {NULL
, "gradian", N_("Gradians"), NC_("unit-format", "%s gradians"), "π*x/200", "200x/π", NC_("unit-symbols", "gradian,gradians,grad")},
46 {"length", "parsec", N_("Parsecs"), NC_("unit-format", "%s pc"), "30857000000000000x", "x/30857000000000000", NC_("unit-symbols", "parsec,parsecs,pc")},
47 {NULL
, "lightyear", N_("Light Years"), NC_("unit-format", "%s ly"), "9460730472580800x", "x/9460730472580800", NC_("unit-symbols", "lightyear,lightyears,ly")},
48 {NULL
, "astronomical-unit", N_("Austronomical Units"), NC_("unit-format", "%s au"), "149597870691x", "x/149597870691", NC_("unit-symbols", "au")},
49 {NULL
, "nautical-mile", N_("Nautical Miles"), NC_("unit-format", "%s nm"), "1852000x", "x/1852000", NC_("unit-symbols", "nm")},
50 {NULL
, "mile", N_("Miles"), NC_("unit-format", "%s mi"), "1609.344x", "x/1609.344", NC_("unit-symbols", "mile,miles,mi")},
51 {NULL
, "kilometer", N_("Kilometers"), NC_("unit-format", "%s km"), "1000x", "x/1000", NC_("unit-symbols", "kilometer,kilometers,km,kms")},
52 {NULL
, "cable", N_("Cables"), NC_("unit-format", "%s cb"), "219.456x", "x/219.456", NC_("unit-symbols", "cable,cables,cb")},
53 {NULL
, "fathom", N_("Fathoms"), NC_("unit-format", "%s ftm"), "1.8288x", "x/1.8288", NC_("unit-symbols", "fathom,fathoms,ftm")},
54 {NULL
, "meter", N_("Meters"), NC_("unit-format", "%s m"), "x", "x", NC_("unit-symbols", "meter,meters,m")},
55 {NULL
, "yard", N_("Yards"), NC_("unit-format", "%s yd"), "0.9144x", "x/0.9144", NC_("unit-symbols", "yard,yards,yd")},
56 {NULL
, "foot", N_("Feet"), NC_("unit-format", "%s ft"), "0.3048x", "x/0.3048", NC_("unit-symbols", "foot,feet,ft")},
57 {NULL
, "inch", N_("Inches"), NC_("unit-format", "%s in"), "0.0254x", "x/0.0254", NC_("unit-symbols", "inch,inches,in")},
58 {NULL
, "centimeter", N_("Centimeters"), NC_("unit-format", "%s cm"), "x/100", "100x", NC_("unit-symbols", "centimeter,centimeters,cm,cms")},
59 {NULL
, "millimeter", N_("Millimeters"), NC_("unit-format", "%s mm"), "x/1000", "1000x", NC_("unit-symbols", "millimeter,millimeters,mm")},
60 {NULL
, "micrometer", N_("Micrometers"), NC_("unit-format", "%s μm"), "x/1000000", "1000000x", NC_("unit-symbols", "micrometer,micrometers,um")},
61 {NULL
, "nanometer", N_("Nanometers"), NC_("unit-format", "%s nm"), "x/1000000000", "1000000000x", NC_("unit-symbols", "nanometer,nanometers")},
62 {"area", "hectare", N_("Hectares"), NC_("unit-format", "%s ha"), "10000x", "x/10000", NC_("unit-symbols", "hectare,hectares,ha")},
63 {NULL
, "acre", N_("Acres"), NC_("unit-format", "%s acres"), "4046.8564224x", "x/4046.8564224", NC_("unit-symbols", "acre,acres")},
64 {NULL
, "square-meter", N_("Square Meter"), NC_("unit-format", "%s m²"), "x", "x", NC_("unit-symbols", "m²")},
65 {NULL
, "square-centimeter", N_("Square Centimeter"), NC_("unit-format", "%s cm²"), "0.001x", "1000x", NC_("unit-symbols", "cm²")},
66 {NULL
, "square-millimeter", N_("Square Millimeter"), NC_("unit-format", "%s mm²"), "0.000001x", "1000000x", NC_("unit-symbols", "mm²")},
67 {"volume", "cubic-meter", N_("Cubic Meters"), NC_("unit-format", "%s m³"), "1000x", "x/1000", NC_("unit-symbols", "m³")},
68 {NULL
, "gallon", N_("Gallons"), NC_("unit-format", "%s gal"), "3.785412x", "x/3.785412", NC_("unit-symbols", "gallon,gallons,gal")},
69 {NULL
, "litre", N_("Litres"), NC_("unit-format", "%s L"), "x", "x", NC_("unit-symbols", "litre,litres,liter,liters,L")},
70 {NULL
, "quart", N_("Quarts"), NC_("unit-format", "%s qt"), "0.9463529x", "x/0.9463529", NC_("unit-symbols", "quart,quarts,qt")},
71 {NULL
, "pint", N_("Pints"), NC_("unit-format", "%s pt"), "0.4731765x", "x/0.4731765", NC_("unit-symbols", "pint,pints,pt")},
72 {NULL
, "millilitre", N_("Millilitres"), NC_("unit-format", "%s mL"), "0.001x", "1000x", NC_("unit-symbols", "millilitre,millilitres,milliliter,milliliters,mL,cm³")},
73 {NULL
, "microlitre", N_("Microlitre"), NC_("unit-format", "%s μL"), "0.000001x", "1000000x", NC_("unit-symbols", "mm³,μL,uL")},
74 {"weight", "tonne", N_("Tonnes"), NC_("unit-format", "%s T"), "x/1000", "1000x", NC_("unit-symbols", "tonne,tonnes")},
75 {NULL
, "kilograms", N_("Kilograms"), NC_("unit-format", "%s kg"), "x", "x", NC_("unit-symbols", "kilogram,kilograms,kilogramme,kilogrammes,kg,kgs")},
76 {NULL
, "pound", N_("Pounds"), NC_("unit-format", "%s lb"), "0.45359237x", "x/0.45359237", NC_("unit-symbols", "pound,pounds,lb")},
77 {NULL
, "ounce", N_("Ounces"), NC_("unit-format", "%s oz"), "0.02834952x", "x/0.02834952", NC_("unit-symbols", "ounce,ounces,oz")},
78 {NULL
, "gram", N_("Grams"), NC_("unit-format", "%s g"), "0.001x", "1000x", NC_("unit-symbols", "gram,grams,gramme,grammes,g")},
79 {"duration", "year", N_("Years"), NC_("unit-format", "%s years"), "31557600x", "x/31557600", NC_("unit-symbols", "year,years")},
80 {NULL
, "day", N_("Days"), NC_("unit-format", "%s days"), "86400x", "x/86400", NC_("unit-symbols", "day,days")},
81 {NULL
, "hour", N_("Hours"), NC_("unit-format", "%s hours"), "3600x", "x/3600", NC_("unit-symbols", "hour,hours")},
82 {NULL
, "minute", N_("Minutes"), NC_("unit-format", "%s minutes"), "60x", "x/60", NC_("unit-symbols", "minute,minutes")},
83 {NULL
, "second", N_("Seconds"), NC_("unit-format", "%s s"), "x", "x", NC_("unit-symbols", "second,seconds,s")},
84 {NULL
, "millisecond", N_("Milliseconds"), NC_("unit-format", "%s ms"), "0.001x", "1000x", NC_("unit-symbols", "millisecond,milliseconds,ms")},
85 {NULL
, "microsecond", N_("Microseconds"), NC_("unit-format", "%s μs"), "0.000001x", "1000000x", NC_("unit-symbols", "microsecond,microseconds,us")},
86 {"temperature", "degree-celcius", N_("Celcius"), NC_("unit-format", "%s˚C"), "x+273.15", "x-273.15", NC_("unit-symbols", "degC,˚C")},
87 {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")},
88 {NULL
, "degree-kelvin", N_("Kelvin"), NC_("unit-format", "%s˚K"), "x", "x", NC_("unit-symbols", "degK,˚K")},
89 {NULL
, "degree-rankine", N_("Rankine"), NC_("unit-format", "%s˚R"), "x*5/9", "x*9/5", NC_("unit-symbols", "degR,˚R")},
90 { NULL
, NULL
, NULL
, NULL
, NULL
, NULL
}
93 if (default_unit_manager
)
94 return default_unit_manager
;
96 default_unit_manager
= g_object_new(unit_manager_get_type(), NULL
);
98 unit_manager_add_category(default_unit_manager
, "angle", _("Angle"));
99 unit_manager_add_category(default_unit_manager
, "length", _("Length"));
100 unit_manager_add_category(default_unit_manager
, "area", _("Area"));
101 unit_manager_add_category(default_unit_manager
, "volume", _("Volume"));
102 unit_manager_add_category(default_unit_manager
, "weight", _("Weight"));
103 unit_manager_add_category(default_unit_manager
, "duration", _("Duration"));
104 unit_manager_add_category(default_unit_manager
, "temperature", _("Temperature"));
106 for (i
= 0; units
[i
].name
; i
++) {
107 if (units
[i
].category
)
108 category
= unit_manager_get_category(default_unit_manager
, units
[i
].category
);
109 unit_category_add_unit(category
, unit_new(units
[i
].name
,
110 _(units
[i
].display_name
),
111 g_dpgettext2(NULL
, "unit-format", units
[i
].format
),
112 units
[i
].from_function
, units
[i
].to_function
,
113 g_dpgettext2(NULL
, "unit-symbols", units
[i
].symbols
)));
116 category
= unit_manager_add_category(default_unit_manager
, "currency", _("Currency"));
117 currencies
= g_list_copy(currency_manager_get_currencies(currency_manager_get_default()));
118 currencies
= g_list_sort(currencies
, compare_currencies
);
119 for (iter
= currencies
; iter
; iter
= iter
->next
)
121 Currency
*currency
= iter
->data
;
125 format
= g_strdup_printf("%s%%s", currency_get_symbol(currency
));
126 unit
= unit_new(currency_get_name(currency
), currency_get_display_name(currency
), format
, NULL
, NULL
, currency_get_name(currency
));
129 unit_category_add_unit(category
, unit
);
131 g_list_free(currencies
);
133 return default_unit_manager
;
138 unit_manager_add_category(UnitManager
*manager
, const gchar
*name
, const gchar
*display_name
)
140 UnitCategory
*category
;
142 g_return_val_if_fail(unit_manager_get_category(manager
, name
) == NULL
, NULL
);
143 category
= unit_category_new(name
, display_name
);
144 manager
->priv
->categories
= g_list_append(manager
->priv
->categories
, category
);
151 unit_manager_get_categories(UnitManager
*manager
)
153 return manager
->priv
->categories
;
158 unit_manager_get_category(UnitManager
*manager
, const gchar
*category
)
162 for (iter
= manager
->priv
->categories
; iter
; iter
= iter
->next
) {
163 UnitCategory
*c
= iter
->data
;
164 if (strcmp(unit_category_get_name(c
), category
) == 0)
173 unit_manager_get_unit(UnitManager
*manager
, const gchar
*unit
)
178 for (iter
= manager
->priv
->categories
; iter
; iter
= iter
->next
) {
179 UnitCategory
*c
= iter
->data
;
180 u
= unit_category_get_unit (c
, unit
);
190 unit_manager_convert(UnitManager
*manager
, const MPNumber
*x
, const char *x_units
, const char *z_units
, MPNumber
*z
)
194 for (iter
= manager
->priv
->categories
; iter
; iter
= iter
->next
) {
195 UnitCategory
*c
= iter
->data
;
196 if (unit_category_convert(c
, x
, x_units
, z_units
, z
))
205 unit_manager_class_init(UnitManagerClass
*klass
)
207 g_type_class_add_private(klass
, sizeof(UnitManagerPrivate
));
212 unit_manager_init(UnitManager
*manager
)
214 manager
->priv
= G_TYPE_INSTANCE_GET_PRIVATE(manager
, unit_manager_get_type(), UnitManagerPrivate
);