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 G_DEFINE_TYPE (EasysetupCountryComboBox, easysetup_country_combo_box, GTK_TYPE_COMBO_BOX);
21 #define COUNTRY_COMBO_BOX_GET_PRIVATE(o) \
22 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EASYSETUP_TYPE_COUNTRY_COMBO_BOX, EasysetupCountryComboBoxPrivate))
24 typedef struct _EasysetupCountryComboBoxPrivate EasysetupCountryComboBoxPrivate;
26 struct _EasysetupCountryComboBoxPrivate
32 easysetup_country_combo_box_get_property (GObject *object, guint property_id,
33 GValue *value, GParamSpec *pspec)
35 switch (property_id) {
37 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
42 easysetup_country_combo_box_set_property (GObject *object, guint property_id,
43 const GValue *value, GParamSpec *pspec)
45 switch (property_id) {
47 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
52 easysetup_country_combo_box_dispose (GObject *object)
54 if (G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose)
55 G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->dispose (object);
59 easysetup_country_combo_box_finalize (GObject *object)
61 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (object);
63 g_object_unref (G_OBJECT (priv->model));
65 G_OBJECT_CLASS (easysetup_country_combo_box_parent_class)->finalize (object);
69 easysetup_country_combo_box_class_init (EasysetupCountryComboBoxClass *klass)
71 GObjectClass *object_class = G_OBJECT_CLASS (klass);
73 g_type_class_add_private (klass, sizeof (EasysetupCountryComboBoxPrivate));
75 object_class->get_property = easysetup_country_combo_box_get_property;
76 object_class->set_property = easysetup_country_combo_box_set_property;
77 object_class->dispose = easysetup_country_combo_box_dispose;
78 object_class->finalize = easysetup_country_combo_box_finalize;
86 /** id and country must be freed.
88 static void parse_mcc_mapping_line (const char* line, char** id, char** country)
90 /* Initialize output parameters: */
96 const gboolean is_valid_utf8 = g_utf8_validate (line, -1, NULL);
98 g_warning("UTF8 validation failed.");
102 /* Look at each character, to find the whitespace between the ID and name: */
103 char* result_id = NULL;
104 char* result_country = NULL;
106 const char* p = line;
107 const char* p_start_of_country = NULL;
110 p = g_utf8_next_char(p);
111 gunichar ch = g_utf8_get_char(p);
112 if (g_unichar_isspace(ch)) { /* Note: This checks for any whitespace, not just space. */
114 /* The text before this must be the ID: */
115 const int length = p - line;
116 result_id = g_malloc (length + 1); /* 1 for null-termination. */
117 memcpy(result_id, line, length);
118 result_id[length] = 0; /* Null-termination. */
120 else if(p_start_of_country)
122 /* This whitespace is probably the newline after the country. */
124 /* The text after the whitespace, after the ID, must be the country: */
125 int length = p - p_start_of_country;
126 result_country = g_malloc(length + 1);
127 memcpy(result_country, p_start_of_country, length);
128 result_country[length] = 0; /* Null-termination. */
132 else if(result_id && !p_start_of_country) {
133 p_start_of_country = p;
138 *country = result_country;
141 /** Note that the mcc_mapping file is installed
142 * by the operator-wizard-settings package.
144 static void load_from_file (EasysetupCountryComboBox *self)
146 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
148 /* Load the file one line at a time: */
149 const gchar* filepath = "/usr/share/operator-wizard/mcc_mapping";
150 FILE *file = fopen(filepath, "r");
153 const gchar* filepath_hack = "./src/maemo/easysetup/mcc_mapping";
154 g_warning ("Could not locate the official mcc_mapping countries list file from %s, "
155 "so attempting to load it instead from %s", filepath, filepath_hack);
156 file = fopen(filepath_hack, "r");
160 g_warning("Could not open mcc_mapping file");
164 GtkListStore *liststore = GTK_LIST_STORE (priv->model);
166 /* We use the getline() GNU extension,
167 * because it reads per line, which simplifies our code,
168 * and it doesn't require us to hard-code a buffer length.
172 while (getline (&line, &len, file) > 0) /* getline will realloc line if necessary. */
174 /* printf ("DBEUG: len=%d, line: %s\n", len, line); */
177 char *country = NULL;
178 parse_mcc_mapping_line (line, &id_str, &country);
179 /* printf("DEBUG: parsed: id=%s, country=%s\n", id_str, country); */
181 if(id_str && country) {
182 guint id = (guint)g_ascii_strtod(id_str, NULL); /* Note that this parses locale-independent text. */
184 /* Get the translation for the country name:
185 * Note that the osso_countries_1.0 translation domain files are installed
186 * by the operator-wizard-settings package. */
187 const gchar *name_translated = dgettext ("osso_countries_1.0", country);
189 name_translated = country;
191 /* Add the row to the model: */
193 gtk_list_store_append (liststore, &iter);
194 gtk_list_store_set(liststore, &iter, MODEL_COL_ID, id, MODEL_COL_NAME, name_translated, -1);
208 easysetup_country_combo_box_init (EasysetupCountryComboBox *self)
210 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
212 /* Create a tree model for the combo box,
213 * with a string for the name, and an int for the MCC ID.
214 * This must match our MODEL_COLS enum constants.
216 priv->model = GTK_TREE_MODEL (gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_UINT));
218 /* Setup the combo box: */
219 GtkComboBox *combobox = GTK_COMBO_BOX (self);
220 gtk_combo_box_set_model (combobox, priv->model);
223 * The ID model column in not shown in the view. */
224 GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
225 gtk_cell_layout_pack_start(GTK_CELL_LAYOUT (combobox), renderer, TRUE);
226 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combobox), renderer,
227 "text", MODEL_COL_NAME, NULL);
229 /* Fill the model with rows: */
230 load_from_file (self);
233 EasysetupCountryComboBox*
234 easysetup_country_combo_box_new (void)
236 return g_object_new (EASYSETUP_TYPE_COUNTRY_COMBO_BOX, NULL);
240 * Returns the MCC number of the selected country, or 0 if no country was selected.
243 easysetup_country_combo_box_get_active_country_id (EasysetupCountryComboBox *self)
246 const gboolean found = gtk_combo_box_get_active_iter (GTK_COMBO_BOX (self), &active);
248 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
251 gtk_tree_model_get (priv->model, &active, MODEL_COL_ID, &id, -1);
255 return 0; /* Failed. */
259 /* This allows us to pass more than one piece of data to the signal handler,
260 * and get a result: */
263 EasysetupCountryComboBox* self;
269 on_model_foreach_select_id(GtkTreeModel *model,
270 GtkTreePath *path, GtkTreeIter *iter, gpointer user_data)
272 ForEachData *state = (ForEachData*)(user_data);
274 /* Select the item if it has the matching ID: */
276 gtk_tree_model_get (model, iter, MODEL_COL_ID, &id, -1);
277 if(id == state->mcc_id) {
278 gtk_combo_box_set_active_iter (GTK_COMBO_BOX (state->self), iter);
281 return TRUE; /* Stop walking the tree. */
284 return FALSE; /* Keep walking the tree. */
288 * Selects the MCC number of the selected country.
289 * Specify 0 to select no country.
292 easysetup_country_combo_box_set_active_country_id (EasysetupCountryComboBox *self, guint mcc_id)
294 EasysetupCountryComboBoxPrivate *priv = COUNTRY_COMBO_BOX_GET_PRIVATE (self);
296 /* Create a state instance so we can send two items of data to the signal handler: */
297 ForEachData *state = g_new0 (ForEachData, 1);
299 state->mcc_id = mcc_id;
300 state->found = FALSE;
302 /* Look at each item, and select the one with the correct ID: */
303 gtk_tree_model_foreach (priv->model, &on_model_foreach_select_id, state);
305 const gboolean result = state->found;
307 /* Free the state instance: */