Updated Latvian translation and added Latvian translation for help by Viesturs Ružāns...
[gcalctool.git] / src / unit-category.c
blob56a90e201bd22111b3dce49b19f557cbd26f7fa1
1 /*
2 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (C) 2008-2011 Robert Ancell.
4 *
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 2 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
9 * license.
12 #include <string.h>
14 #include "unit-category.h"
16 struct UnitCategoryPrivate
18 gchar *name;
19 gchar *display_name;
20 GList *units;
23 G_DEFINE_TYPE (UnitCategory, unit_category, G_TYPE_OBJECT);
26 UnitCategory *
27 unit_category_new(const gchar *name, const gchar *display_name)
29 UnitCategory *category = g_object_new(unit_category_get_type(), NULL);
30 category->priv->name = g_strdup(name);
31 category->priv->display_name = g_strdup(display_name);
32 return category;
36 const gchar *
37 unit_category_get_name(UnitCategory *category)
39 g_return_val_if_fail (category != NULL, NULL);
40 return category->priv->name;
44 const gchar *
45 unit_category_get_display_name(UnitCategory *category)
47 g_return_val_if_fail (category != NULL, NULL);
48 return category->priv->display_name;
52 void
53 unit_category_add_unit(UnitCategory *category, Unit *unit)
55 g_return_if_fail (category != NULL);
56 g_return_if_fail (unit != NULL);
57 category->priv->units = g_list_append(category->priv->units, g_object_ref(unit));
61 Unit *
62 unit_category_get_unit_by_name(UnitCategory *category, const gchar *name)
64 GList *iter;
66 g_return_val_if_fail (category != NULL, NULL);
67 g_return_val_if_fail (name != NULL, NULL);
69 for (iter = category->priv->units; iter; iter = iter->next)
71 Unit *unit = iter->data;
72 if (strcmp(unit_get_name(unit), name) == 0)
73 return unit;
76 return NULL;
80 Unit *
81 unit_category_get_unit_by_symbol(UnitCategory *category, const gchar *symbol)
83 GList *iter;
85 g_return_val_if_fail (category != NULL, NULL);
86 g_return_val_if_fail (symbol != NULL, NULL);
88 for (iter = category->priv->units; iter; iter = iter->next) {
89 Unit *unit = iter->data;
90 if (unit_matches_symbol(unit, symbol))
91 return unit;
94 return NULL;
98 const GList *
99 unit_category_get_units(UnitCategory *category)
101 g_return_val_if_fail (category != NULL, NULL);
102 return category->priv->units;
106 gboolean
107 unit_category_convert(UnitCategory *category, const MPNumber *x, Unit *x_units, Unit *z_units, MPNumber *z)
109 MPNumber t;
111 g_return_val_if_fail (category != NULL, FALSE);
112 g_return_val_if_fail (x_units != NULL, FALSE);
113 g_return_val_if_fail (z_units != NULL, FALSE);
114 g_return_val_if_fail (z != NULL, FALSE);
116 if (!unit_convert_from(x_units, x, &t))
117 return FALSE;
118 if (!unit_convert_to(z_units, &t, z))
119 return FALSE;
121 return TRUE;
125 static void
126 unit_category_class_init(UnitCategoryClass *klass)
128 g_type_class_add_private(klass, sizeof(UnitCategoryPrivate));
132 static void
133 unit_category_init(UnitCategory *category)
135 category->priv = G_TYPE_INSTANCE_GET_PRIVATE(category, unit_category_get_type(), UnitCategoryPrivate);