1 /* Copyright (c) 2007, Nokia Corporation
6 #define _GNU_SOURCE /* So we can use the getline() function, which is a convenient GNU extension. */
9 #include "modest-easysetup-country-combo-box.h"
10 #include <gtk/gtkliststore.h>
11 #include <gtk/gtkcelllayout.h>
12 #include <gtk/gtkcellrenderertext.h>
15 #include <string.h> /* For memcpy() */
17 #include <libintl.h> /* For dgettext(). */
19 /* Include config.h so that _() works: */
24 G_DEFINE_TYPE (EasysetupCountryComboBox, easysetup_country_combo_box, GTK_TYPE_COMBO_BOX);
26 #define COUNTRY_COMBO_BOX_GET_PRIVATE(o) \
27 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_COUNTRY_COMBO_BOX, EasysetupCountryComboBoxPrivate))
29 typedef struct _EasysetupCountryComboBoxPrivate EasysetupCountryComboBoxPrivate;
31 struct _EasysetupCountryComboBoxPrivate
37 easysetup_country_combo_box_get_property (GObject *object, guint property_id,
38 GValue *value, GParamSpec *pspec)
40 switch (property_id) {
42 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
47 easysetup_country_combo_box_set_property (GObject *object, guint property_id,
48 const GValue *value, GParamSpec *pspec)
50 switch (property_id) {
52 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
57 easysetup_country_combo_box_dispose (GObject *object)
59 if (G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose)
60 G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose (object);
64 easysetup_country_combo_box_finalize (GObject *object)
66 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (object);
68 g_object_unref (G_OBJECT (priv->model));
70 G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->finalize (object);
74 easysetup_country_combo_box_class_init (EasysetupCountryComboBoxClass *klass)
76 GObjectClass *object_class = G_OBJECT_CLASS (klass);
78 g_type_class_add_private (klass, sizeof (EasysetupCountryComboBoxPrivate));
80 object_class->get_property = easysetup_country_combo_box_get_property;
81 object_class->set_property = easysetup_country_combo_box_set_property;
82 object_class->dispose = easysetup_country_combo_box_dispose;
83 object_class->finalize = easysetup_country_combo_box_finalize;
91 /** id and country must be freed.
93 static void parse_mcc_mapping_line (const char* line, char** id, char** country)
95 /* Initialize output parameters: */
101 const gboolean is_valid_utf8 = g_utf8_validate (line, -1, NULL);
103 g_warning("UTF8 validation failed.");
107 /* Look at each character, to find the whitespace between the ID and name: */
108 char* result_id = NULL;
109 char* result_country = NULL;
111 const char* p = line;
112 const char* p_start_of_country = NULL;
115 p = g_utf8_next_char(p);
116 gunichar ch = g_utf8_get_char(p);
117 if (g_unichar_isspace(ch)) { /* Note: This checks for any whitespace, not just space. */
119 /* The text before this must be the ID: */
120 const int length = p - line;
121 result_id = g_malloc (length + 1); /* 1 for null-termination. */
122 memcpy(result_id, line, length);
123 result_id[length] = 0; /* Null-termination. */
125 else if(p_start_of_country)
127 /* This whitespace is probably the newline after the country. */
129 /* The text after the whitespace, after the ID, must be the country: */
130 int length = p - p_start_of_country;
131 result_country = g_malloc(length + 1);
132 memcpy(result_country, p_start_of_country, length);
133 result_country[length] = 0; /* Null-termination. */
137 else if(result_id && !p_start_of_country) {
138 p_start_of_country = p;
143 *country = result_country;
146 /** Note that the mcc_mapping file is installed
147 * by the operator-wizard-settings package.
149 static void load_from_file (EasysetupCountryComboBox *self)
151 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
153 /* Load the file one line at a time: */
154 const gchar* filepath = "/usr/share/operator-wizard/mcc_mapping";
155 FILE *file = fopen(filepath, "r");
158 const gchar* filepath_hack = "./src/maemo/easysetup/mcc_mapping";
159 g_warning ("Could not locate the official mcc_mapping countries list file from %s, "
160 "so attempting to load it instead from %s", filepath, filepath_hack);
161 file = fopen(filepath_hack, "r");
165 g_warning("Could not open mcc_mapping file");
169 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
171 /* We use the getline() GNU extension,
172 * because it reads per line, which simplifies our code,
173 * and it doesn't require us to hard-code a buffer length.
177 while (getline (&line, &len, file) > 0) /* getline will realloc line if necessary. */
179 /* printf ("DBEUG: len=%d, line: %s\n", len, line); */
182 char *country = NULL;
183 parse_mcc_mapping_line (line, &id_str, &country);
184 /* printf("DEBUG: parsed: id=%s, country=%s\n", id_str, country); */
186 if(id_str && country) {
187 guint id = (guint)g_ascii_strtod(id_str, NULL); /* Note that this parses locale-independent text. */
189 /* Get the translation for the country name:
190 * Note that the osso_countries_1.0 translation domain files are installed
191 * by the operator-wizard-settings package. */
192 const gchar *name_translated = dgettext ("osso_countries_1.0", country);
194 name_translated = country;
196 /* Add the row to the model: */
198 gtk_list_store_append (liststore, &iter);
199 gtk_list_store_set(liststore, &iter, MODEL_COL_ID, id, MODEL_COL_NAME, name_translated, -1);
213 easysetup_country_combo_box_init (EasysetupCountryComboBox *self)
215 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
217 /* Create a tree model for the combo box,
218 * with a string for the name, and an int for the MCC ID.
219 * This must match our MODEL_COLS enum constants.
221 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_UINT));
223 /* Setup the combo box: */
224 GtkComboBox *combobox = GTK_COMBO_BOX (self);
225 gtk_combo_box_set_model (combobox, priv->model);
228 * The ID model column in not shown in the view. */
229 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
230 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
231 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
232 "text", MODEL_COL_NAME, NULL);
234 /* Fill the model with rows: */
235 load_from_file (self);
238 EasysetupCountryComboBox*
239 easysetup_country_combo_box_new (void)
241 return g_object_new (EASYSETUP_TYPE_COUNTRY_COMBO_BOX, NULL);
245 * Returns the MCC number of the selected country, or 0 if no country was selected.
248 easysetup_country_combo_box_get_active_country_id (EasysetupCountryComboBox *self)
251 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &active);
253 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
256 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
260 return 0; /* Failed. */
264 /* This allows us to pass more than one piece of data to the signal handler,
265 * and get a result: */
268 EasysetupCountryComboBox* self;
274 on_model_foreach_select_id(GtkTreeModel *model,
275 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
277 ForEachData *state = (ForEachData*)(user_data);
279 /* Select the item if it has the matching ID: */
281 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
282 if(id == state->mcc_id) {
283 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
286 return TRUE; /* Stop walking the tree. */
289 return FALSE; /* Keep walking the tree. */
293 * Selects the MCC number of the selected country.
294 * Specify 0 to select no country.
297 easysetup_country_combo_box_set_active_country_id (EasysetupCountryComboBox *self, guint mcc_id)
299 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
301 /* Create a state instance so we can send two items of data to the signal handler: */
302 ForEachData *state = g_new0 (ForEachData, 1);
304 state->mcc_id = mcc_id;
305 state->found = FALSE;
307 /* Look at each item, and select the one with the correct ID: */
308 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
310 const gboolean result = state->found;
312 /* Free the state instance: */